aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/abstract/apply.lux
blob: 6b3ea631fbf781c4a21de23563d8ca5cd2a1720b (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
(.module:
  [lux #*
   [abstract/monad (#+ do)]
   [data
    [text
     format]]
   [control
    ["." function]]
   [math
    ["r" random]]
   ["_" test (#+ Test)]]
  {1
   ["." / (#+ Apply)]}
  [//
   [functor (#+ Injection Comparison)]])

(def: (identity injection comparison (^open "_;."))
  (All [f] (-> (Injection f) (Comparison f) (Apply f) Test))
  (do r.monad
    [sample (:: @ map injection r.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 r.monad
    [sample r.nat
     increase (:: @ map n/+ r.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 r.monad
    [sample r.nat
     increase (:: @ map n/+ r.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 r.monad
    [sample r.nat
     increase (:: @ map n/+ r.nat)
     decrease (:: @ map n/- r.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))
  (_.context (%name (name-of /.Apply))
             ($_ _.and
                 (..identity injection comparison apply)
                 (..homomorphism injection comparison apply)
                 (..interchange injection comparison apply)
                 (..composition injection comparison apply)
                 )))