aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/abstract/apply.lux
blob: eb8fd4e5260baa1795bae61b463ce092d31eb42f (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
(.module:
  [lux #*
   [abstract/monad (#+ do)]
   [data
    ["." maybe]
    [number
     ["n" nat]]
    [collection
     ["." list]]]
   [control
    ["." function]]
   [math
    ["." random]]
   ["_" test (#+ Test)]]
  {1
   ["." / (#+ Apply)]}
  [//
   [functor (#+ Injection Comparison)]])

(def: (identity injection comparison (^open "_@."))
  (All [f] (-> (Injection f) (Comparison f) (Apply f) Test))
  (do {@ random.monad}
    [sample (:: @ map injection random.nat)]
    (_.test "Identity."
            ((comparison n.=)
             (_@apply (injection function.identity) sample)
             sample))))

(def: (homomorphism injection comparison (^open "_@."))
  (All [f] (-> (Injection f) (Comparison f) (Apply f) Test))
  (do {@ random.monad}
    [sample random.nat
     increase (:: @ map n.+ random.nat)]
    (_.test "Homomorphism."
            ((comparison n.=)
             (_@apply (injection increase) (injection sample))
             (injection (increase sample))))))

(def: (interchange injection comparison (^open "_@."))
  (All [f] (-> (Injection f) (Comparison f) (Apply f) Test))
  (do {@ random.monad}
    [sample random.nat
     increase (:: @ map n.+ random.nat)]
    (_.test "Interchange."
            ((comparison n.=)
             (_@apply (injection increase) (injection sample))
             (_@apply (injection (function (_ f) (f sample))) (injection increase))))))

(def: (composition injection comparison (^open "_@."))
  (All [f] (-> (Injection f) (Comparison f) (Apply f) Test))
  (do {@ random.monad}
    [sample random.nat
     increase (:: @ map n.+ random.nat)
     decrease (:: @ map n.- random.nat)]
    (_.test "Composition."
            ((comparison n.=)
             (_$ _@apply
                 (injection function.compose)
                 (injection increase)
                 (injection decrease)
                 (injection sample))
             ($_ _@apply
                 (injection increase)
                 (injection decrease)
                 (injection sample))))))

(def: #export (spec injection comparison apply)
  (All [f] (-> (Injection f) (Comparison f) (Apply f) Test))
  (_.with-cover [/.Apply]
    ($_ _.and
        (..identity injection comparison apply)
        (..homomorphism injection comparison apply)
        (..interchange injection comparison apply)
        (..composition injection comparison apply)
        )))

(def: #export test
  Test
  (do random.monad
    [left random.nat
     right random.nat]
    (<| (_.covering /._)
        ($_ _.and
            (_.cover [/.compose]
                     (let [expected (n.+ left right)]
                       (case (:: (/.compose maybe.monad maybe.apply list.apply) apply
                                 (#.Some (list (n.+ left)))
                                 (#.Some (list right)))
                         (^ (#.Some (list actual)))
                         (n.= expected actual)

                         _
                         false)))
            ))))