From 017e9d4ed91e8c891938f5b4a3dec8a7768ac147 Mon Sep 17 00:00:00 2001 From: effectfully Date: Wed, 11 Sep 2024 20:03:11 +0200 Subject: [PATCH] [Builtins] Add the 'dropList' builtin --- .../src/PlutusCore/Default/Builtins.hs | 25 +++++++++++++++++++ .../Golden/DefaultFun/DropList.plc.golden | 1 + 2 files changed, 26 insertions(+) create mode 100644 plutus-core/plutus-core/test/TypeSynthesis/Golden/DefaultFun/DropList.plc.golden diff --git a/plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs b/plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs index ff2a4164a31..44e23803a38 100644 --- a/plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs +++ b/plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs @@ -103,6 +103,7 @@ data DefaultFun | HeadList | TailList | NullList + | DropList -- Data -- See Note [Pattern matching on built-in types]. -- It is convenient to have a "choosing" function for a data type that has more than two @@ -1543,6 +1544,27 @@ instance uni ~ DefaultUni => ToBuiltinMeaning uni DefaultFun where nullListDenotation (runCostingFunOneArgument . paramNullList) + toBuiltinMeaning _semvar DropList = + let dropListDenotation :: Integer -> SomeConstant uni [a] -> BuiltinResult (Opaque val a) + dropListDenotation n0 (SomeConstant (Some (ValueOf uniListA xs0))) = do + -- See Note [Operational vs structural errors within builtins]. + case uniListA of + DefaultUniList _ -> do + let -- We don't want to restrict ourselves to 'Int, hence we can't use + -- 'drop'. And 'genericDrop' is likely less efficient due to it being + -- recursive and therefore not inlinable, so there's probably a lot of + -- dictionary passing going on. And we don't care about fusion. So we + -- just implement the straightforward thing ourselves. + go n xs | n <= 0 = xs + go _ [] = [] + go n (_ : xs) = go (n - 1) xs + pure . fromValueOf uniListA $ go n0 xs0 + _ -> throwing _StructuralUnliftingError "Expected a list but got something else" + {-# INLINE dropListDenotation #-} + in makeBuiltinMeaning + dropListDenotation + (runCostingFunTwoArguments . unimplementedCostingFun) + -- Data toBuiltinMeaning _semvar ChooseData = let chooseDataDenotation :: Data -> a -> a -> a -> a -> a -> a @@ -2144,6 +2166,8 @@ instance Flat DefaultFun where ExpModInteger -> 87 + DropList -> 88 + decode = go =<< decodeBuiltin where go 0 = pure AddInteger go 1 = pure SubtractInteger @@ -2233,6 +2257,7 @@ instance Flat DefaultFun where go 85 = pure FindFirstSetBit go 86 = pure Ripemd_160 go 87 = pure ExpModInteger + go 88 = pure DropList go t = fail $ "Failed to decode builtin tag, got: " ++ show t size _ n = n + builtinTagWidth diff --git a/plutus-core/plutus-core/test/TypeSynthesis/Golden/DefaultFun/DropList.plc.golden b/plutus-core/plutus-core/test/TypeSynthesis/Golden/DefaultFun/DropList.plc.golden new file mode 100644 index 00000000000..b7b380c65b6 --- /dev/null +++ b/plutus-core/plutus-core/test/TypeSynthesis/Golden/DefaultFun/DropList.plc.golden @@ -0,0 +1 @@ +all a. integer -> list a -> a \ No newline at end of file