Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions plutus-tx-plugin/test/BuiltinList/Budget/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ tests =
, goldenBundle "concat" concat (concat `unsafeApplyCode` l4)
, goldenBundle "concatMap" concatMap (concatMap `unsafeApplyCode` l1)
, goldenBundle "zipWith" zipWith (zipWith `unsafeApplyCode` l1)
, -- , goldenBundle "sort" sort (sort `unsafeApplyCode` l1)
-- , goldenBundle "sortBy" sortBy (sortBy `unsafeApplyCode` l1)
-- , goldenBundle "splitAt" splitAt (splitAt `unsafeApplyCode` l1)
-- , goldenBundle "partition" partition (partition `unsafeApplyCode` l1)
goldenBundle "zip" zip (zip `unsafeApplyCode` l1)
-- , goldenBundle "unzip" unzip (unzip `unsafeApplyCode` l3)
]

map :: CompiledCode (L.BuiltinList Integer -> L.BuiltinList Integer)
Expand Down Expand Up @@ -224,6 +230,30 @@ nub = $$(compile [||\xs -> L.nub (L.append xs xs)||])
nubBy :: CompiledCode (L.BuiltinList Integer -> L.BuiltinList Integer)
nubBy = $$(compile [||\xs -> L.nubBy (<=) xs||])

concat :: CompiledCode (L.BuiltinList (L.BuiltinList Integer) -> L.BuiltinList Integer)
concat = $$(compile [||\xss -> L.concat xss||])

concatMap :: CompiledCode (L.BuiltinList Integer -> L.BuiltinList Integer)
concatMap = $$(compile [||\xss -> L.concatMap (L.replicate 2) xss||])

-- splitAt :: CompiledCode (L.BuiltinList Integer -> BuiltinPair (L.BuiltinList Integer) (L.BuiltinList Integer))
-- splitAt = $$(compile [||\xs -> L.splitAt 5 xs||])

-- partition :: CompiledCode (L.BuiltinList Integer -> BuiltinPair (L.BuiltinList Integer) (L.BuiltinList Integer))
-- partition = $$(compile [||\xs -> L.partition even xs||])

zip :: CompiledCode (L.BuiltinList Integer -> L.BuiltinList (BuiltinPair Integer Integer))
zip = $$(compile [||\xs -> L.zip xs xs||])

-- unzip :: CompiledCode (L.BuiltinList (BuiltinPair Integer Integer) -> BuiltinPair (L.BuiltinList Integer) (L.BuiltinList Integer))
-- unzip = $$(compile [||\xs -> L.unzip xs||])

-- sort :: CompiledCode (L.BuiltinList Integer -> L.BuiltinList Integer)
-- sort = $$(compile [||\xs -> L.sort xs||])

-- sortBy :: CompiledCode (L.BuiltinList Integer -> L.BuiltinList Integer)
-- sortBy = $$(compile [||\xs -> L.sortBy compare xs||])

l1 :: CompiledCode (L.BuiltinList Integer)
l1 = liftCodeDef $ toBuiltin ([1 .. 10] :: [Integer])

Expand All @@ -235,9 +265,3 @@ l3 = liftCodeDef $ toBuiltin ([(1, 2), (3, 4), (5, 6)] :: [(Integer, Integer)])

l4 :: CompiledCode (L.BuiltinList (L.BuiltinList Integer))
l4 = liftCodeDef $ toBuiltin ([[1, 2], [3, 4]] :: [[Integer]])

concat :: CompiledCode (L.BuiltinList (L.BuiltinList Integer) -> L.BuiltinList Integer)
concat = $$(compile [||\xss -> L.concat xss||])

concatMap :: CompiledCode (L.BuiltinList Integer -> L.BuiltinList Integer)
concatMap = $$(compile [||\xss -> L.concatMap (L.replicate 2) xss||])
46 changes: 26 additions & 20 deletions plutus-tx/src/PlutusTx/BuiltinList.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ module PlutusTx.BuiltinList
, nub
, nubBy
, zipWith
, partition
, sort
, sortBy
, splitAt
, zip
, unzip
) where

import PlutusTx.Builtins qualified as B
Expand Down Expand Up @@ -282,36 +288,36 @@ reverse xs = revAppend xs empty
{-# INLINEABLE reverse #-}

-- | Plutus Tx version of 'Data.List.zip' for 'BuiltinList'.
_zip
zip
:: forall a b
. (MkNil a, MkNil b)
=> BuiltinList a
-> BuiltinList b
-> BuiltinList (BuiltinPair a b)
_zip = zipWith (curry BI.BuiltinPair)
{-# INLINEABLE _zip #-}
zip = zipWith (curry BI.BuiltinPair)
{-# INLINEABLE zip #-}

-- | Plutus Tx version of 'Data.List.unzip' for 'BuiltinList'.
_unzip
unzip
:: forall a b
. (MkNil a, MkNil b)
=> BuiltinList (BuiltinPair a b)
-> BuiltinPair (BuiltinList a) (BuiltinList b)
_unzip =
unzip =
caseList'
emptyPair
( \p l -> do
let x = BI.fst p
let y = BI.snd p
let l' = _unzip l
let l' = unzip l
let xs' = BI.fst l'
let ys' = BI.snd l'
BI.BuiltinPair (x <| xs', y <| ys')
)
where
emptyPair :: BuiltinPair (BuiltinList a) (BuiltinList b)
emptyPair = BI.BuiltinPair (empty, empty)
{-# INLINEABLE _unzip #-}
{-# INLINEABLE unzip #-}

-- | Plutus Tx version of 'Data.List.head' for 'BuiltinList'.
head :: forall a. BuiltinList a -> a
Expand Down Expand Up @@ -357,20 +363,20 @@ drop n l
{-# INLINEABLE drop #-}

-- | Plutus Tx version of 'Data.List.splitAt' for 'BuiltinList'.
_splitAt
splitAt
:: forall a
. MkNil a
=> Integer
-> BuiltinList a
-> BuiltinPair (BuiltinList a) (BuiltinList a)
_splitAt n xs
splitAt n xs
| n `B.lessThanEqualsInteger` 0 = BI.BuiltinPair (empty, xs)
| B.null xs = BI.BuiltinPair (empty, empty)
| otherwise = do
let (x, xs') = B.unsafeUncons xs
let BI.BuiltinPair (xs'', xs''') = _splitAt (n `B.subtractInteger` 1) xs'
let BI.BuiltinPair (xs'', xs''') = splitAt (n `B.subtractInteger` 1) xs'
BI.BuiltinPair (x <| xs'', xs''')
{-# INLINEABLE _splitAt #-}
{-# INLINEABLE splitAt #-}

-- | Plutus Tx version of 'Data.List.nub' for 'BuiltinList'.
nub :: forall a. (Eq a, MkNil a) => BuiltinList a -> BuiltinList a
Expand Down Expand Up @@ -443,26 +449,26 @@ replicate n0 x = go n0
{-# INLINEABLE replicate #-}

-- | Plutus Tx version of 'Data.List.partition' for 'BuiltinList'.
_partition
partition
:: forall a
. MkNil a
=> (a -> Bool)
-> BuiltinList a
-> BuiltinPair (BuiltinList a) (BuiltinList a)
_partition p = BI.BuiltinPair . foldr select (empty, empty)
partition p = BI.BuiltinPair . foldr select (empty, empty)
where
select :: a -> (BuiltinList a, BuiltinList a) -> (BuiltinList a, BuiltinList a)
select x ~(ts, fs) = if p x then (x <| ts, fs) else (ts, x <| fs)
{-# INLINEABLE _partition #-}
{-# INLINEABLE partition #-}

-- | Plutus Tx version of 'Data.List.sort' for 'BuiltinList'.
_sort :: (MkNil a, Ord a) => BuiltinList a -> BuiltinList a
_sort = _sortBy compare
{-# INLINEABLE _sort #-}
sort :: (MkNil a, Ord a) => BuiltinList a -> BuiltinList a
sort = sortBy compare
{-# INLINEABLE sort #-}

-- | Plutus Tx version of 'Data.List.sortBy' for 'BuiltinList'.
_sortBy :: MkNil a => (a -> a -> Ordering) -> BuiltinList a -> BuiltinList a
_sortBy cmp = mergeAll . sequences
sortBy :: MkNil a => (a -> a -> Ordering) -> BuiltinList a -> BuiltinList a
sortBy cmp = mergeAll . sequences
where
sequences = caseList'' empty (singleton . singleton) f
where
Expand Down Expand Up @@ -511,4 +517,4 @@ _sortBy cmp = mergeAll . sequences

caseList'' :: forall a r. r -> (a -> r) -> (a -> a -> BuiltinList a -> r) -> BuiltinList a -> r
caseList'' f0 f1 f2 = caseList' f0 (\x xs -> caseList' (f1 x) (\y ys -> f2 x y ys) xs)
{-# INLINEABLE _sortBy #-}
{-# INLINEABLE sortBy #-}