-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathPositive.hs
160 lines (143 loc) · 7.22 KB
/
Positive.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module Nockma.Eval.Positive where
import Base hiding (Path)
import Juvix.Compiler.Core.Language.Base (defaultSymbol)
import Juvix.Compiler.Nockma.Evaluator
import Juvix.Compiler.Nockma.Language
import Juvix.Compiler.Nockma.Pretty
import Juvix.Compiler.Nockma.Translation.FromSource.QQ
import Juvix.Compiler.Nockma.Translation.FromTree
type Check = Sem '[Reader [Term Natural], Reader (Term Natural), EmbedIO]
data Test = Test
{ _testEvalOptions :: EvalOptions,
_testName :: Text,
_testProgramSubject :: Term Natural,
_testProgramFormula :: Term Natural,
_testCheck :: Check ()
}
makeLenses ''Test
allTests :: TestTree
allTests =
testGroup
"Nockma eval positive"
[ testGroup "Unit" (map mkNockmaTest unitTests),
testGroup "Juvix calling convention" (map mkNockmaTest juvixCallingConventionTests),
testGroup "Anoma calling convention" (map mkNockmaTest anomaCallingConventionTests)
]
where
mkNockmaTest :: Test -> TestTree
mkNockmaTest Test {..} = testCase (unpack _testName) $ do
let (traces, evalResult) =
run
. runReader _testEvalOptions
. runOutputList @(Term Natural)
. runError @(ErrNockNatural Natural)
. runError @(NockEvalError Natural)
$ eval _testProgramSubject _testProgramFormula
case evalResult of
Left natErr -> assertFailure ("Evaluation error: " <> show natErr)
Right r -> case r of
Left evalErr -> assertFailure ("Evaluation error: " <> unpack (ppTrace evalErr))
Right res -> runM (runReader res (runReader traces _testCheck))
eqNock :: Term Natural -> Check ()
eqNock expected = do
actual <- ask
unless (nockmaEq expected actual) (err actual)
where
err :: Term Natural -> Check ()
err actual = do
let msg =
"Expected:\n"
<> ppTrace expected
<> "\nBut got:\n"
<> ppTrace actual
assertFailure (unpack msg)
eqTraces :: [Term Natural] -> Check ()
eqTraces expected = do
ts <- ask
unless (nockmaEq ts expected) (err ts)
where
err :: [Term Natural] -> Check ()
err ts = do
let msg =
"Expected traces:\n"
<> ppTrace expected
<> "\nBut got:\n"
<> ppTrace ts
assertFailure (unpack msg)
compilerTest :: Text -> Term Natural -> Check () -> Bool -> Test
compilerTest n mainFun _testCheck _evalInterceptStdlibCalls =
let f =
CompilerFunction
{ _compilerFunctionName = UserFunction (defaultSymbol 0),
_compilerFunctionArity = 0,
_compilerFunction = return mainFun
}
_testName :: Text
| _evalInterceptStdlibCalls = n <> " - intercept stdlib"
| otherwise = n
opts = CompilerOptions {_compilerOptionsEnableTrace = False}
Cell _testProgramSubject _testProgramFormula = runCompilerWithJuvix opts mempty [] f
_testEvalOptions = EvalOptions {..}
in Test {..}
anomaTest :: Text -> Term Natural -> [Term Natural] -> Check () -> Bool -> Test
anomaTest n mainFun args _testCheck _evalInterceptStdlibCalls =
let f =
CompilerFunction
{ _compilerFunctionName = UserFunction (defaultSymbol 0),
_compilerFunctionArity = fromIntegral (length args),
_compilerFunction = return mainFun
}
_testName :: Text
| _evalInterceptStdlibCalls = n <> " - intercept stdlib"
| otherwise = n
opts = CompilerOptions {_compilerOptionsEnableTrace = False}
_testProgramSubject = TermCell (runCompilerWithAnoma opts mempty [] f)
_testProgramFormula = case nonEmpty args of
Just args' -> OpCall # [L] # OpReplace # ([R, L] # foldTerms args') # (OpAddress # emptyPath)
Nothing -> OpCall # [L] # (OpAddress # emptyPath)
_testEvalOptions = EvalOptions {..}
in Test {..}
test :: Text -> Term Natural -> Term Natural -> Check () -> Test
test = Test defaultEvalOptions
anomaCallingConventionTests :: [Test]
anomaCallingConventionTests =
[True, False]
<**> [ anomaTest "stdlib add" (add (nockNatLiteral 1) (nockNatLiteral 2)) [] (eqNock [nock| 3 |]),
anomaTest "stdlib add with arg" (add (nockNatLiteral 1) (nockNatLiteral 2)) [nockNatLiteral 1] (eqNock [nock| 3 |]),
anomaTest "stdlib sub args" (sub (OpAddress # pathToArg 0) (OpAddress # pathToArg 1)) [nockNatLiteral 3, nockNatLiteral 1] (eqNock [nock| 2 |])
]
juvixCallingConventionTests :: [Test]
juvixCallingConventionTests =
[True, False]
<**> [ compilerTest "stdlib add" (add (nockNatLiteral 1) (nockNatLiteral 2)) (eqNock [nock| 3 |]),
compilerTest "stdlib dec" (dec (nockNatLiteral 1)) (eqNock [nock| 0 |]),
compilerTest "stdlib mul" (mul (nockNatLiteral 2) (nockNatLiteral 3)) (eqNock [nock| 6 |]),
compilerTest "stdlib sub" (sub (nockNatLiteral 2) (nockNatLiteral 1)) (eqNock [nock| 1 |]),
compilerTest "stdlib div" (callStdlib StdlibDiv [nockNatLiteral 10, nockNatLiteral 3]) (eqNock [nock| 3 |]),
compilerTest "stdlib mod" (callStdlib StdlibMod [nockNatLiteral 3, nockNatLiteral 2]) (eqNock [nock| 1 |]),
compilerTest "stdlib le" (callStdlib StdlibLe [nockNatLiteral 3, nockNatLiteral 3]) (eqNock [nock| true |]),
compilerTest "stdlib lt" (callStdlib StdlibLt [nockNatLiteral 3, nockNatLiteral 3]) (eqNock [nock| false |]),
compilerTest "stdlib pow2" (pow2 (nockNatLiteral 3)) (eqNock [nock| 8 |]),
compilerTest "stdlib nested" (dec (dec (nockNatLiteral 20))) (eqNock [nock| 18 |]),
compilerTest "append rights - empty" (appendRights emptyPath (nockNatLiteral 3)) (eqNock (toNock [R, R, R])),
compilerTest "append rights" (appendRights [L, L] (nockNatLiteral 3)) (eqNock (toNock [L, L, R, R, R]))
]
unitTests :: [Test]
unitTests =
[ test "address" [nock| [0 1] |] [nock| [[@ R] [@ L]] |] (eqNock [nock| [1 0] |]),
test "address nested" [nock| [0 1 2 3 4 5] |] [nock| [@ RRRRR] |] (eqNock [nock| 5 |]),
test "quote" [nock| [0 1] |] [nock| [quote [1 0]] |] (eqNock [nock| [1 0] |]),
test "apply" [nock| [0 1] |] [nock| [apply [@ S] [quote [@ R]]] |] (eqNock [nock| 1 |]),
test "isCell atom" [nock| [0 1] |] [nock| [isCell [@ L]] |] (eqNock [nock| false |]),
test "isCell cell" [nock| [0 1] |] [nock| [isCell [@ S]] |] (eqNock [nock| true |]),
test "suc" [nock| [0 1] |] [nock| [suc [quote 1]] |] (eqNock [nock| 2 |]),
test "eq" [nock| [0 1] |] [nock| [= [1 0] [1 0]] |] (eqNock [nock| true |]),
test "eq" [nock| [0 1] |] [nock| [= [1 0] [0 1]] |] (eqNock [nock| false |]),
test "if" [nock| [0 1] |] [nock| [if [quote true] [@ L] [@ R]] |] (eqNock [nock| 0 |]),
test "if" [nock| [0 1] |] [nock| [if [quote false] [@ L] [@ R]] |] (eqNock [nock| 1 |]),
test "seq" [nock| [0 1] |] [nock| [seq [[suc [@ L]] [@ R]] [suc [@ L]]] |] (eqNock [nock| 2 |]),
test "push" [nock| [0 1] |] [nock| [push [[suc [@ L]] [@ S]]] |] (eqNock [nock| [1 0 1] |]),
test "call" [nock| [quote 1] |] [nock| [call [S [@ S]]] |] (eqNock [nock| 1 |]),
test "replace" [nock| [0 1] |] [nock| [replace [[L [quote 1]] [@ S]]] |] (eqNock [nock| [1 1] |]),
test "hint" [nock| [0 1] |] [nock| [hint [nil [trace [quote 2] [quote 3]]] [quote 1]] |] (eqTraces [[nock| 2 |]] >> eqNock [nock| 1 |])
]