aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/abstract/predicate.lux
blob: 61a2bb41c6d95f08130abf567863860603246a7b (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
(.using
 [library
  [lux (.except all or and)
   [control
    ["[0]" function]]]]
 [//
  [monoid (.only Monoid)]
  [functor
   ["[0]" contravariant]]])

(type: .public (Predicate a)
  (-> a Bit))

(with_template [<identity_name> <identity_value> <composition_name> <composition>]
  [(def: .public <identity_name>
     Predicate
     (function.constant <identity_value>))

   (def: .public (<composition_name> left right)
     (All (_ a) (-> (Predicate a) (Predicate a) (Predicate a)))
     (function (_ value)
       (<composition> (left value)
                      (right value))))]

  [none #0 or .or]
  [all  #1 and .and]
  )

(with_template [<name> <identity> <composition>]
  [(def: .public <name>
     (All (_ a) (Monoid (Predicate a)))
     (implementation
      (def: identity <identity>)
      (def: composite <composition>)))]

  [union        ..none ..or]
  [intersection ..all  ..and]
  )

(def: .public (complement predicate)
  (All (_ a) (-> (Predicate a) (Predicate a)))
  (|>> predicate not))

(def: .public (difference sub base)
  (All (_ a) (-> (Predicate a) (Predicate a) (Predicate a)))
  (function (_ value)
    (.and (base value)
          (not (sub value)))))

(def: .public (rec predicate)
  (All (_ a)
    (-> (-> (Predicate a) (Predicate a))
        (Predicate a)))
  (function (again input)
    (predicate again input)))

(def: .public functor
  (contravariant.Functor Predicate)
  (implementation
   (def: (each f fb)
     (|>> f fb))))