/
List.hs
75 lines (68 loc) · 2.8 KB
/
List.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
{-# LANGUAGE QualifiedDo #-}
module Examples.List (tests) where
import Test.Tasty
import Test.Tasty.HUnit
import Utils
import Plutarch
import Plutarch.Bool (pnot, (#<), (#==))
import Plutarch.Builtin (PBuiltinList (..))
import Plutarch.Integer
import Plutarch.Lift
import Plutarch.List
import qualified Plutarch.Monadic as P
--------------------------------------------------------------------------------
integerList :: [Integer] -> Term s (PList PInteger)
integerList xs = pconvertLists #$ pconstant @(PBuiltinList PInteger) xs
tests :: HasTester => TestTree
tests = do
testGroup "List tests" $
[ testCase "pconcat identities" $ do
let xs :: Term s (PList PInteger)
xs = psingleton # (fromInteger @(Term _ PInteger) 0)
expect $ (pconcat # xs # pnil) #== xs
, testCase "pmap" $ do
let xs :: Term _ (PList PInteger)
xs = integerList [1 .. 10]
expect $
pmap # (plam $ \x -> x + x) # xs
#== (integerList $ fmap (* 2) [1 .. 10])
expect $ pmap @PList # (plam $ \(x :: Term _ PInteger) -> x) # pnil #== pnil
, testCase "pfilter" $ do
let xs :: Term _ (PList PInteger)
xs = integerList [1 .. 10]
expect $
(pfilter # (plam $ \x -> pmod # x # 2 #== 0) # xs)
#== (integerList [2, 4, 6, 8, 10])
expect $
(pfilter # (plam $ \x -> 5 #< x) # xs)
#== (integerList [6 .. 10])
, testCase "phead" $
expect $ (phead # integerList [1 .. 10]) #== 1
, testCase "ptail" $
expect $ (ptail # integerList [1 .. 10]) #== integerList [2 .. 10]
, testCase "pnull" $ do
expect $ pnot #$ pnull # integerList [1 .. 10]
expect $ pnull # integerList []
, testCase "pzipWith" $ do
expect $
(pzipWith' (+) # integerList [1 .. 10] # integerList [1 .. 10])
#== integerList (fmap (* 2) [1 .. 10])
, testCase "pfoldl" $ do
expect $
(pfoldl # plam (-) # 0 # integerList [1 .. 10])
#== pconstant (foldl (-) 0 [1 .. 10])
expect $
(pfoldl' (-) # 0 # integerList [1 .. 10])
#== pconstant (foldl (-) 0 [1 .. 10])
expect $
(pfoldl # plam (-) # 0 # integerList [])
#== pconstant 0
expect $
(pfoldl' (-) # 0 # integerList [])
#== pconstant 0
, testCase "pmatch" $ do
let t = P.do
_ <- pmatch $ integerList [1, 3, 1]
perror
in printTerm t @?= "(program 1.0.0 ((\\i0 -> (\\i0 -> i2 (\\i0 -> i2 i2 i1)) (\\i0 -> i2 (\\i0 -> i2 i2 i1))) (\\i0 -> \\i0 -> force (force (force chooseList) i1 (delay (\\i0 -> \\i0 -> force i1)) (delay ((\\i0 -> \\i0 -> \\i0 -> \\i0 -> i2 i4 i3) (force headList i1) (i2 (force tailList i1)))))) [1,3,1] (\\i0 -> \\i0 -> error) (delay error)))"
]