aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/bit.lux
blob: dcb0ad6bb4d8795dd8cf2f15be0d1a0e994bcc2e (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
(.require
 [library
  [lux (.except)
   [abstract
    [monoid (.only Monoid)]
    [equivalence (.only Equivalence)]
    [hash (.only Hash)]
    [codec (.only Codec)]]
   [control
    ["[0]" function]]]])

(with_template [<zero> <one>]
  [(def .public <zero> Bit #0)
   (def .public <one> Bit  #1)]

  [no  yes]
  [off on]
  )

(def .public equivalence
  (Equivalence Bit)
  (implementation
   (def (= x y)
     (if x
       y
       (not y)))))

(def .public hash
  (Hash Bit)
  (implementation
   (def equivalence ..equivalence)
   
   (def (hash value)
     (case value
       #0 2
       #1 3))))

(with_template [<name> <identity> <op>]
  [(def .public <name>
     (Monoid Bit)
     (implementation
      (def identity <identity>)
      (def (composite x y) (<op> x y))))]

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

(def .public codec
  (Codec Text Bit)
  (implementation
   (def (encoded x)
     (if x
       "#1"
       "#0"))

   (def (decoded input)
     (case input
       "#1" {.#Right #1}
       "#0" {.#Right #0}
       _    {.#Left "Wrong syntax for Bit."}))))

(def .public complement
  (All (_ a) (-> (-> a Bit) (-> a Bit)))
  (function.composite not))