aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/bit.lux
blob: d806061379567f0116289d0c69f654cc7a0d3280 (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
(.module:
  [lux #*
   [abstract
    [monoid (#+ Monoid)]
    [equivalence (#+ Equivalence)]
    hash
    [codec (#+ Codec)]]
   [control
    ["." function]]])

(structure: #export equivalence
  (Equivalence Bit)
  
  (def: (= x y)
    (if x
      y
      (not y))))

(structure: #export hash
  (Hash Bit)
  
  (def: &equivalence ..equivalence)
  
  (def: (hash value)
    (case value
      #1 1
      #0 0)))

(template [<name> <identity> <op>]
  [(structure: #export <name>
     (Monoid Bit)
     
     (def: identity <identity>)
     (def: (compose x y) (<op> x y)))]

  [disjunction #0 or]
  [conjunction #1 and]
  )

(structure: #export codec (Codec Text Bit)
  (def: (encode x)
    (if x
      "#1"
      "#0"))

  (def: (decode input)
    (case input
      "#1" (#.Right #1)
      "#0" (#.Right #0)
      _    (#.Left "Wrong syntax for Bit."))))

(def: #export complement
  {#.doc (doc "Generates the complement of a predicate."
              "That is a predicate that returns the oposite of the original predicate.")}
  (All [a] (-> (-> a Bit) (-> a Bit)))
  (function.compose not))