aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/abstract/functor.lux
blob: fb3dab4c8346c4707c3d40367745de1c24c18eee (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
(.using
  [library
   [lux {"-" Or And}]])

(type: .public (Functor f)
  (Interface
   (: (All (_ a b)
        (-> (-> a b)
            (-> (f a) (f b))))
      each)))

(type: .public (Or f g)
  (All (_ a) (.Or (f a) (g a))))

(def: .public (sum (^open "f#[0]") (^open "g#[0]"))
  (All (_ F G) (-> (Functor F) (Functor G) (Functor (..Or F G))))
  (implementation
   (def: (each f fa|ga)
     (case fa|ga
       {.#Left fa}
       {.#Left (f#each f fa)}
       
       {.#Right ga}
       {.#Right (g#each f ga)}))))

(type: .public (And f g)
  (All (_ a) (.And (f a) (g a))))

(def: .public (product (^open "f#[0]") (^open "g#[0]"))
  (All (_ F G) (-> (Functor F) (Functor G) (Functor (..And F G))))
  (implementation
   (def: (each f [fa ga])
     [(f#each f fa)
      (g#each f ga)])))

(type: .public (Then f g)
  (All (_ a) (f (g a))))

(def: .public (composite (^open "f#[0]") (^open "g#[0]"))
  (All (_ F G) (-> (Functor F) (Functor G) (Functor (..Then F G))))
  (implementation
   (def: (each f fga)
     (f#each (g#each f) fga))))