aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/specification/lux/abstract/monad.lux
blob: e95ddf67d2178bac8700c69ee332dd7bcbfb229d (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
(.require
 [library
  [lux (.except)
   ["_" test (.only Test)]
   [math
    ["[0]" random]
    [number
     ["n" nat]]]]]
 [\\library
  ["[0]" / (.only do)]]
 [//
  [functor (.only Injection Comparison)]])

(def (left_identity injection comparison (open "_//[0]"))
  (All (_ f) (-> (Injection f) (Comparison f) (/.Monad f) Test))
  (do [! random.monad]
    [sample random.nat
     morphism (at ! each (function (_ diff)
                           (|>> (n.+ diff) _//in))
                  random.nat)]
    (_.property "Left identity."
      ((comparison n.=)
       (|> (injection sample) (_//each morphism) _//conjoint)
       (morphism sample)))))

(def (right_identity injection comparison (open "_//[0]"))
  (All (_ f) (-> (Injection f) (Comparison f) (/.Monad f) Test))
  (do random.monad
    [sample random.nat]
    (_.property "Right identity."
      ((comparison n.=)
       (|> (injection sample) (_//each _//in) _//conjoint)
       (injection sample)))))

(def (associativity injection comparison (open "_//[0]"))
  (All (_ f) (-> (Injection f) (Comparison f) (/.Monad f) Test))
  (do [! random.monad]
    [sample random.nat
     increase (at ! each (function (_ diff)
                           (|>> (n.+ diff) _//in))
                  random.nat)
     decrease (at ! each (function (_ diff)
                           (|>> (n.- diff) _//in))
                  random.nat)]
    (_.property "Associativity."
      ((comparison n.=)
       (|> (injection sample) (_//each increase) _//conjoint (_//each decrease) _//conjoint)
       (|> (injection sample) (_//each (|>> increase (_//each decrease) _//conjoint)) _//conjoint)))))

(def .public (spec injection comparison monad)
  (All (_ f) (-> (Injection f) (Comparison f) (/.Monad f) Test))
  (<| (_.for [/.Monad])
      (all _.and
           (..left_identity injection comparison monad)
           (..right_identity injection comparison monad)
           (..associativity injection comparison monad)
           )))