Skip to content

Commit

Permalink
[Builtins] Add the 'dropList' builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
effectfully committed Sep 11, 2024
1 parent 1d2ad5a commit 017e9d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
25 changes: 25 additions & 0 deletions plutus-core/plutus-core/src/PlutusCore/Default/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2144,6 +2166,8 @@ instance Flat DefaultFun where

ExpModInteger -> 87

DropList -> 88

decode = go =<< decodeBuiltin
where go 0 = pure AddInteger
go 1 = pure SubtractInteger
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
all a. integer -> list a -> a

0 comments on commit 017e9d4

Please sign in to comment.