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

## [Structures]
(structure: #export _ (Equivalence Bit)
  (def: (= x y)
    (if x
      y
      (not y))))

(structure: #export _ (Hash Bit)
  (def: eq Equivalence<Bit>)
  (def: (hash value)
    (case value
      true +1
      false +0)))

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

  [ Or@Monoid<Bit> false or]
  [And@Monoid<Bit> true and]
  )

(structure: #export _ (Codec Text Bit)
  (def: (encode x)
    (if x
      "true"
      "false"))

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

## [Values]
(def: #export complement
  {#.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)))
  (compose not))