-
Notifications
You must be signed in to change notification settings - Fork 19
/
bool.agda
85 lines (67 loc) · 1.69 KB
/
bool.agda
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module bool where
open import level
----------------------------------------------------------------------
-- datatypes
----------------------------------------------------------------------
data 𝔹 : Set where
tt : 𝔹
ff : 𝔹
-- this is an alias for Mac users who cannot see blackboard b.
bool : Set
bool = 𝔹
{-# BUILTIN BOOL 𝔹 #-}
{-# BUILTIN TRUE tt #-}
{-# BUILTIN FALSE ff #-}
----------------------------------------------------------------------
-- syntax
----------------------------------------------------------------------
infix 7 ~_
infix 6 _xor_ _nand_
infixr 6 _&&_
infixr 5 _||_
infix 4 if_then_else_ if*_then_else_
infixr 4 _imp_
infix 4 _iff_
----------------------------------------------------------------------
-- operations
----------------------------------------------------------------------
-- not
~_ : 𝔹 → 𝔹
~ tt = ff
~ ff = tt
_iff_ : 𝔹 → 𝔹 → 𝔹
tt iff tt = tt
tt iff ff = ff
ff iff tt = ff
ff iff ff = tt
-- and
_&&_ : 𝔹 → 𝔹 → 𝔹
tt && b = b
ff && b = ff
-- or
_||_ : 𝔹 → 𝔹 → 𝔹
tt || b = tt
ff || b = b
if_then_else_ : ∀ {ℓ} {A : Set ℓ} → 𝔹 → A → A → A
if tt then y else z = y
if ff then y else z = z
if*_then_else_ : ∀ {ℓ} {A B : Set ℓ} → (b : 𝔹) → A → B → if b then A else B
if* tt then a else b = a
if* ff then a else b = b
_xor_ : 𝔹 → 𝔹 → 𝔹
tt xor ff = tt
ff xor tt = tt
tt xor tt = ff
ff xor ff = ff
-- implication
_imp_ : 𝔹 → 𝔹 → 𝔹
tt imp b2 = b2
ff imp b2 = tt
-- also called the Sheffer stroke
_nand_ : 𝔹 → 𝔹 → 𝔹
tt nand tt = ff
tt nand ff = tt
ff nand tt = tt
ff nand ff = tt
_nor_ : 𝔹 → 𝔹 → 𝔹
x nor y = ~ (x || y)