aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/abstract/monad.lux
blob: cc4642e131c2c478bc418826b97b66815bc3f204 (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
(.module:
  [library
   [lux #*
    ["_" test (#+ Test)]
    [data
     ["." identity (#+ Identity)]
     [collection
      ["." list ("#\." functor fold)]]]
    [math
     ["." random]
     [number
      ["n" nat]]]]]
  [\\library
   ["." / (#+ Monad do)]])

(def: #export test
  Test
  (do random.monad
    [mono random.nat
     poly (random.list 10 random.nat)]
    (<| (_.covering /._)
        ($_ _.and
            (_.cover [/.do]
                     (n.= (inc mono)
                          (: (Identity Nat)
                             (/.do identity.monad
                               [sample (wrap mono)]
                               (wrap (inc sample))))))
            (_.cover [/.bind]
                     (n.= (inc mono)
                          (: (Identity Nat)
                             (/.bind identity.monad
                                     (|>> inc (\ identity.monad wrap))
                                     (\ identity.monad wrap mono)))))
            (_.cover [/.seq]
                     (\ (list.equivalence n.equivalence) =
                        (list\map inc poly)
                        (|> poly
                            (list\map (|>> inc (\ identity.monad wrap)))
                            (: (List (Identity Nat)))
                            (/.seq identity.monad)
                            (: (Identity (List Nat))))))
            (_.cover [/.map]
                     (\ (list.equivalence n.equivalence) =
                        (list\map inc poly)
                        (|> poly
                            (/.map identity.monad (|>> inc (\ identity.monad wrap)))
                            (: (Identity (List Nat))))))
            (_.cover [/.filter]
                     (\ (list.equivalence n.equivalence) =
                        (list.filter n.even? poly)
                        (|> poly
                            (/.filter identity.monad (|>> n.even? (\ identity.monad wrap)))
                            (: (Identity (List Nat))))))
            (_.cover [/.fold]
                     (n.= (list\fold n.+ 0 poly)
                          (|> poly
                              (/.fold identity.monad
                                      (function (_ part whole)
                                        (\ identity.monad wrap
                                           (n.+ part whole)))
                                      0)
                              (: (Identity Nat)))))
            ))))