Skip to content

Commit 19cb362

Browse files
committed
PList cosmetics
1 parent 7ec8992 commit 19cb362

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

runtimes/haskell/lbr-plutarch/src/LambdaBuffers/Runtime/Plutarch.hs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ module LambdaBuffers.Runtime.Plutarch (
1616
PMaybe (..),
1717
pcon,
1818
PList (..),
19-
caseList,
20-
pcons,
21-
pnil,
19+
plistCase,
20+
plistCons,
21+
plistNil,
22+
plistFrom,
2223
) where
2324

2425
import Data.Functor.Const (Const)
@@ -697,13 +698,16 @@ pcon = pdata . Pl.pcon
697698
698699
TODO(bladyjoker): Upstream with PList and plan to remove.
699700
-}
700-
caseList :: (PIsData a) => (Term s a -> Term s (PList a) -> Term s r) -> Term s r -> Term s (PList a) -> Term s r
701-
caseList consCase nilCase ls = pmatch (Pl.pto ls) $ \case
702-
Pl.PCons x xs -> consCase (Pl.pfromData x) (Pl.pcon $ PList xs)
701+
plistCase :: (PIsData a) => Term s (a :--> PList a :--> r) -> Term s r -> Term s (PList a) -> Term s r
702+
plistCase consCase nilCase ls = pmatch (Pl.pto ls) $ \case
703+
Pl.PCons x xs -> consCase # Pl.pfromData x # Pl.pcon (PList xs)
703704
Pl.PNil -> nilCase
704705

705-
pcons :: PIsData a => Term s (a :--> (PList a :--> PList a))
706-
pcons = phoistAcyclic $ plam $ \x xs -> Pl.pcon $ PList (Pl.pcons # Pl.pdata x # Pl.pto xs)
706+
plistCons :: PIsData a => Term s (a :--> (PList a :--> PList a))
707+
plistCons = phoistAcyclic $ plam $ \x xs -> Pl.pcon $ PList (Pl.pcons # Pl.pdata x # Pl.pto xs)
707708

708-
pnil :: Term s (PList a)
709-
pnil = Pl.pcon $ PList $ Pl.pcon Pl.PNil
709+
plistNil :: Term s (PList a)
710+
plistNil = Pl.pcon $ PList $ Pl.pcon Pl.PNil
711+
712+
plistFrom :: PIsData a => [Term s a] -> Term s (PList a)
713+
plistFrom = foldr (\x -> (#) (plistCons # x)) plistNil

runtimes/haskell/lbr-plutarch/test/Test/LambdaBuffers/Runtime/Plutarch.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import Hedgehog.Gen qualified as Gen
66
import Hedgehog.Range qualified as Range
77
import LambdaBuffers.Runtime.Plutarch (PList)
88
import LambdaBuffers.Runtime.Plutarch qualified as Lb
9-
import Plutarch (ClosedTerm, Config (Config), Term, TracingMode (DoTracingAndBinds), compile, pcon, perror, (#))
9+
import Plutarch (ClosedTerm, Config (Config), TracingMode (DoTracingAndBinds), compile, pcon, perror)
1010
import Plutarch.Evaluate (evalScript)
11-
import Plutarch.Prelude (PBool (PTrue), PEq ((#==)), PInteger, PIsData, pconstant, pif)
11+
import Plutarch.Prelude (PBool (PTrue), PEq ((#==)), PInteger, pconstant, pif)
1212
import Test.Tasty (TestTree, adjustOption, testGroup)
1313
import Test.Tasty.HUnit (assertFailure)
1414
import Test.Tasty.Hedgehog (testProperty)
@@ -19,22 +19,27 @@ test =
1919
adjustOption (\_ -> H.HedgehogTestLimit $ Just 1000) $
2020
testGroup
2121
"PList tests"
22-
[ testProperty "forall xs :: [Integer] ys :: [Integer]. (xs == ys) === evalEq (toPlutarch xs) (toPlutarch ys)" $
22+
[ testProperty "forall xs :: [Integer] ys :: [Integer]. (xs == ys) === evalEq (plistFrom xs) (plistFrom ys)" $
2323
H.property $
2424
H.forAll
2525
((,) <$> genInts <*> genInts)
2626
>>= ( \(xs, ys) -> do
27-
b <- liftIO $ evalEq (fromList $ pconstant <$> xs) (fromList $ pconstant <$> ys)
27+
b <- liftIO $ evalEq (Lb.plistFrom $ pconstant <$> xs) (Lb.plistFrom $ pconstant <$> ys)
2828
(xs == ys) H.=== b
2929
)
30+
, testProperty "forall xs :: [Integer]. evalEq (plistCase plistCons plistNil (plistFrom xs)) (plistFrom xs)" $
31+
H.property $
32+
H.forAll
33+
genInts
34+
>>= ( \xs -> do
35+
b <- liftIO $ evalEq (Lb.plistCase Lb.plistCons Lb.plistNil (Lb.plistFrom $ pconstant <$> xs)) (Lb.plistFrom $ pconstant <$> xs)
36+
True H.=== b
37+
)
3038
]
3139
where
3240
genInts :: H.Gen [Integer]
3341
genInts = Gen.list (Range.linear 0 100) (Gen.integral (Range.linear 0 100))
3442

35-
fromList :: PIsData a => [Term s a] -> Term s (PList a)
36-
fromList = foldr (\x -> (#) (Lb.pcons # x)) Lb.pnil
37-
3843
evalEq :: ClosedTerm (PList PInteger) -> ClosedTerm (PList PInteger) -> IO Bool
3944
evalEq l r =
4045
let

0 commit comments

Comments
 (0)