blob: 6d66265229e746dbd15f10758073e1a48296540e (
plain)
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
|
(.module:
[lux #*
["_" test (#+ Test)]
[abstract
[monad (#+ do)]
[\spec
["$." functor (#+ Injection Comparison)]
["$." apply]
["$." monad]]]
[control
[pipe (#+ let>)]
["." io]]
[data
["." product]]
[math
["." random]
[number
["n" nat]]]]
[\\
["." / (#+ State)]])
(def: (with-conditions [state output] computation)
(-> [Nat Nat] (State Nat Nat) Bit)
(|> computation
(/.run state)
product.right
(n.= output)))
(def: basics
(do random.monad
[state random.nat
value random.nat]
($_ _.and
(_.cover [/.State /.get]
(with-conditions [state state]
/.get))
(_.cover [/.put]
(with-conditions [state value]
(do /.monad
[_ (/.put value)]
/.get)))
(_.cover [/.update]
(with-conditions [state (n.* value state)]
(do /.monad
[_ (/.update (n.* value))]
/.get)))
(_.cover [/.use]
(with-conditions [state (inc state)]
(/.use inc)))
(_.cover [/.local]
(with-conditions [state (n.* value state)]
(/.local (n.* value)
/.get)))
)))
(def: (injection value)
(All [s] (Injection (State s)))
(function (_ state)
[state value]))
(def: (comparison init)
(All [s] (-> s (Comparison (State s))))
(function (_ == left right)
(== (product.right (/.run init left))
(product.right (/.run init right)))))
(def: structures
Test
(do random.monad
[state random.nat
value random.nat]
($_ _.and
(_.for [/.functor]
($functor.spec ..injection (..comparison state) /.functor))
(_.for [/.apply]
($apply.spec ..injection (..comparison state) /.apply))
(_.for [/.monad]
($monad.spec ..injection (..comparison state) /.monad))
)))
(def: loops
Test
(do {! random.monad}
[limit (|> random.nat (\ ! map (n.% 10)))
#let [condition (do /.monad
[state /.get]
(wrap (n.< limit state)))]]
($_ _.and
(_.cover [/.while /.run]
(|> (/.while condition (/.update inc))
(/.run 0)
(let> [state' output']
(n.= limit state'))))
(_.cover [/.do-while]
(|> (/.do-while condition (/.update inc))
(/.run 0)
(let> [state' output']
(or (n.= limit state')
(and (n.= 0 limit)
(n.= 1 state'))))))
)))
(def: monad-transformer
Test
(do random.monad
[state random.nat
left random.nat
right random.nat]
(let [(^open "io\.") io.monad]
(_.cover [/.State' /.with /.lift /.run']
(|> (: (/.State' io.IO Nat Nat)
(do (/.with io.monad)
[a (/.lift io.monad (io\wrap left))
b (wrap right)]
(wrap (n.+ a b))))
(/.run' state)
io.run
(let> [state' output']
(and (n.= state state')
(n.= (n.+ left right) output')))))
)))
(def: #export test
Test
(<| (_.covering /._)
($_ _.and
..basics
..structures
..loops
..monad-transformer)))
|