blob: ed56d57874f9dbd3e64d1c9d1c56a1e9993c3405 (
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
|
(.module:
[lux #*
[abstract
[monad (#+ do)]]
[data
[number
["n" nat]]]
[math
["." random]]
["_" test (#+ Test)]]
{1
["." / (#+ CoMonad)]}
[//
[functor (#+ Injection Comparison)]])
(def: (left-identity injection (^open "_//."))
(All [f] (-> (Injection f) (CoMonad f) Test))
(do {! random.monad}
[sample random.nat
morphism (\ ! map (function (_ diff)
(|>> _//unwrap (n.+ diff)))
random.nat)
#let [start (injection sample)]]
(_.test "Left identity."
(n.= (morphism start)
(|> start _//split (_//map morphism) _//unwrap)))))
(def: (right-identity injection comparison (^open "_//."))
(All [f] (-> (Injection f) (Comparison f) (CoMonad f) Test))
(do random.monad
[sample random.nat
#let [start (injection sample)
== (comparison n.=)]]
(_.test "Right identity."
(== start
(|> start _//split (_//map _//unwrap))))))
(def: (associativity injection comparison (^open "_//."))
(All [f] (-> (Injection f) (Comparison f) (CoMonad f) Test))
(do {! random.monad}
[sample random.nat
increase (\ ! map (function (_ diff)
(|>> _//unwrap (n.+ diff)))
random.nat)
decrease (\ ! map (function (_ diff)
(|>> _//unwrap(n.- diff)))
random.nat)
#let [start (injection sample)
== (comparison n.=)]]
(_.test "Associativity."
(== (|> start _//split (_//map (|>> _//split (_//map increase) decrease)))
(|> start _//split (_//map increase) _//split (_//map decrease))))))
(def: #export (spec injection comparison subject)
(All [f] (-> (Injection f) (Comparison f) (CoMonad f) Test))
(<| (_.with-cover [/.CoMonad])
($_ _.and
(..left-identity injection subject)
(..right-identity injection comparison subject)
(..associativity injection comparison subject)
)))
|