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

(type: #export (Injection f)
  (All [a] (-> a (f a))))

(type: #export (Comparison f)
  (All [a]
    (-> (Equivalence a)
        (Equivalence (f a)))))

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

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

(def: (composition injection comparison (^open "/@."))
  (All [f] (-> (Injection f) (Comparison f) (Functor f) Test))
  (do r.monad
    [sample (:: @ map injection r.nat)
     increase (:: @ map n/+ r.nat)
     decrease (:: @ map n/- r.nat)]
    (_.test "Composition."
            ((comparison n/=)
             (|> sample (/@map increase) (/@map decrease))
             (|> sample (/@map (|>> increase decrease)))))))

(def: #export (spec injection comparison functor)
  (All [f] (-> (Injection f) (Comparison f) (Functor f) Test))
  (_.context (%name (name-of /.Functor))
             ($_ _.and
                 (..identity injection comparison functor)
                 (..homomorphism injection comparison functor)
                 (..composition injection comparison functor)
                 )))