aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/spec/lux/abstract/comonad.lux
blob: 7d68d7a242f69717d3ae83b73809ddfc9fbbffb5 (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
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [monad (#+ do)]]
   [math
    ["." random]
    [number
     ["n" nat]]]]
  [\\
   ["." / (#+ 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))
  (<| (_.for [/.CoMonad])
      ($_ _.and
          (..left-identity injection subject)
          (..right-identity injection comparison subject)
          (..associativity injection comparison subject)
          )))