aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/control/function/mixin.lux
blob: 35cd36027af0bdd9dbb9f533449c69b9dd3a2464 (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
131
132
133
134
(.module:
  [lux #*
   ["_" test (#+ Test)]
   [abstract
    [equivalence (#+ Equivalence)]
    [predicate (#+ Predicate)]
    [monad (#+ do)]
    [\spec
     ["$." monoid]]]
   [control
    ["." state (#+ State)]]
   [data
    ["." product]
    [collection
     ["." list ("#\." functor fold)]]]
   [math
    ["." random (#+ Random)]
    [number
     ["n" nat]]]]
  [\\
   ["." /]])

(def: #export test
  Test
  (<| (_.covering /._)
      (do {! random.monad}
        [input (|> random.nat (\ ! map (|>> (n.% 6) (n.+ 20))))
         dummy random.nat
         shift (|> random.nat (random.filter (|>> (n.= dummy) not)))
         #let [equivalence (: (Equivalence (/.Mixin Nat Nat))
                              (implementation
                               (def: (= left right)
                                 (n.= ((/.mixin left) input)
                                      ((/.mixin right) input)))))
               generator (: (Random (/.Mixin Nat Nat))
                            (do !
                              [output random.nat]
                              (wrap (function (_ delegate recur input)
                                      output))))
               expected (|> (list.indices input)
                            (list\map inc)
                            (list\fold n.* 1))]])
      ($_ _.and
          (_.for [/.Mixin]
                 ($_ _.and
                     (_.for [/.monoid]
                            ($monoid.spec equivalence /.monoid generator))
                     
                     (_.cover [/.mixin]
                              (let [factorial (/.mixin
                                               (function (_ delegate recur input)
                                                 (case input
                                                   (^or 0 1) 1
                                                   _ (n.* input (recur (dec input))))))]
                                (n.= expected
                                     (factorial input))))
                     (_.cover [/.inherit]
                              (let [bottom (: (/.Mixin Nat Nat)
                                              (function (_ delegate recur input)
                                                (case input
                                                  (^or 0 1) 1
                                                  _ (delegate input))))
                                    multiplication (: (/.Mixin Nat Nat)
                                                      (function (_ delegate recur input)
                                                        (n.* input (recur (dec input)))))
                                    factorial (/.mixin (/.inherit bottom multiplication))]
                                (n.= expected
                                     (factorial input))))
                     (_.cover [/.nothing]
                              (let [loop (: (/.Mixin Nat Nat)
                                            (function (_ delegate recur input)
                                              (case input
                                                (^or 0 1) 1
                                                _ (n.* input (delegate (dec input))))))
                                    left (/.mixin (/.inherit /.nothing loop))
                                    right (/.mixin (/.inherit loop /.nothing))]
                                (and (n.= expected
                                          (left input))
                                     (n.= expected
                                          (right input)))))
                     (_.cover [/.advice]
                              (let [bottom (: (/.Mixin Nat Nat)
                                              (function (_ delegate recur input)
                                                1))
                                    bottom? (: (Predicate Nat)
                                               (function (_ input)
                                                 (case input
                                                   (^or 0 1) true
                                                   _ false)))
                                    multiplication (: (/.Mixin Nat Nat)
                                                      (function (_ delegate recur input)
                                                        (n.* input (recur (dec input)))))
                                    factorial (/.mixin (/.inherit (/.advice bottom? bottom)
                                                                  multiplication))]
                                (n.= expected
                                     (factorial input))))
                     (_.cover [/.before]
                              (let [implant (: (-> Nat (State Nat []))
                                               (function (_ input)
                                                 (function (_ state)
                                                   [shift []])))
                                    meld (: (/.Mixin Nat (State Nat Nat))
                                            (function (_ delegate recur input)
                                              (function (_ state)
                                                [state (n.+ state input)])))
                                    function (/.mixin (/.inherit (/.before state.monad implant)
                                                                 meld))]
                                (n.= (n.+ shift input)
                                     (|> input function (state.run dummy) product.right))))
                     (_.cover [/.after]
                              (let [implant (: (-> Nat Nat (State Nat []))
                                               (function (_ input output)
                                                 (function (_ state)
                                                   [shift []])))
                                    meld (: (/.Mixin Nat (State Nat Nat))
                                            (function (_ delegate recur input)
                                              (function (_ state)
                                                [state (n.+ state input)])))
                                    function (/.mixin (/.inherit (/.after state.monad implant)
                                                                 meld))]
                                (n.= (n.+ dummy input)
                                     (|> input function (state.run dummy) product.right))))
                     ))
          (_.for [/.Recursive]
                 (_.cover [/.from-recursive]
                          (let [factorial (/.mixin
                                           (/.from-recursive
                                            (function (_ recur input)
                                              (case input
                                                (^or 0 1) 1
                                                _ (n.* input (recur (dec input)))))))]
                            (n.= expected
                                 (factorial input)))))
          )))