-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter1-Agda.agda
219 lines (163 loc) · 4.55 KB
/
Chapter1-Agda.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
module Chapter1-Agda where
module foo where
module bar where
module qux where
module zaz where
module ram where
module Example-Imports where
open import Data.Bool
_ : Bool
_ = false
module Example-TypingJudgments where
postulate
Bool : Set
false : Bool
true : Bool
-- illegal : false
file-not-found : Bool
module Booleans where
data Bool : Set where
false : Bool
true : Bool
not : Bool → Bool
not false = true
not true = false
open import Relation.Binary.PropositionalEquality
_ : not (not false) ≡ false
_ = refl
-- _ : not (not false) ≡ true
-- _ = refl
_∨_ : Bool → Bool → Bool
false ∨ b = b
true ∨ b = true
_∧_ : Bool → Bool → Bool
false ∧ b = false
true ∧ b = b
_∨₁_ : Bool → Bool → Bool
false ∨₁ false = false
false ∨₁ true = true
true ∨₁ false = true
true ∨₁ true = true
_∨₂_ : Bool → Bool → Bool
false ∨₂ b = b
true ∨₂ b = true
foo = true ∨₂_
bar = true ∨₁_
postulate always-stuck : Bool
foo₂ = true ∨₂ always-stuck
bar₂ = true ∨₁ always-stuck
module Example-Employees where
open Booleans
open import Data.String using (String)
data Department : Set where
administrative : Department
engineering : Department
finance : Department
marketing : Department
sales : Department
record Employee : Set where
field
name : String
department : Department
is-new-hire : Bool
tillman : Employee
tillman = record
{ name = "Tillman"
; department = engineering
; is-new-hire = false
}
module Sandbox-Tuples where
record _×_ (A : Set) (B : Set) : Set where
field
proj₁ : A
proj₂ : B
open Booleans
my-tuple : Bool × Bool
my-tuple = record { proj₁ = true ∨ true ; proj₂ = not true }
first : Bool × Bool → Bool
first record { proj₁ = x } = x
open _×_
my-tuple-first : Bool
my-tuple-first = my-tuple .proj₁
my-tuple-second : Bool
my-tuple-second = proj₂ my-tuple
my-copattern : Bool × Bool
my-copattern .proj₁ = true ∨ true
my-copattern .proj₂ = not true
_,_ : {A B : Set} → A → B → A × B
a , b = record { proj₁ = a ; proj₂ = b }
my-tuple’ : Bool × Bool
my-tuple’ = (true ∨ true) , not true
module Sandbox-Tuples₂ where
open Booleans
record _×_ (A : Set) (B : Set) : Set where
constructor _,_
field
proj₁ : A
proj₂ : B
open _×_
infixr 4 _,_
infixr 2 _×_
data _⊎_ (A : Set) (B : Set) : Set where
inj₁ : A → A ⊎ B
inj₂ : B → A ⊎ B
infixr 1 _⊎_
module Example-Pets where
open import Data.String using (String)
data Species : Set where
bird cat dog reptile : Species
data Temperament : Set where
anxious : Temperament
chill : Temperament
excitable : Temperament
grumpy : Temperament
record Pet : Set where
constructor makePet
field
species : Species
temperament : Temperament
name : String
makeGrumpyCat : String → Pet
makeGrumpyCat = makePet cat grumpy
or : Bool × Bool → Bool
or (false , b) = b
or (true , b) = true
_ : Bool
_ = or (true , false)
curry : {A B C : Set} → (A × B → C) → A → B → C
curry f a b = f (a , b)
uncurry : {A B C : Set} → (A → B → C) → A × B → C
uncurry f (a , b) = f a b
_ : Bool × Bool → Bool
_ = uncurry _∨_
module Sandbox-Implicits where
open import Data.Bool using (Bool; false; true; not; _∨_)
open import Data.Product
using (_×_; proj₁; proj₂)
renaming ( _,′_ to _,_
; curry′ to curry
; uncurry′ to uncurry
)
mk-tuple : (A : Set) → (B : Set) → A → B → A × B
mk-tuple A B a b = a , b
_ : Bool × Bool
_ = mk-tuple Bool Bool true false
data PrimaryColor : Set where
red green blue : PrimaryColor
-- bad-tuple : Bool × Bool
-- bad-tuple = mk-tuple PrimaryColor Bool {!!} {!!}
color-bool-tuple : PrimaryColor × Bool
color-bool-tuple = mk-tuple _ _ red false
mk-color-bool-tuple : PrimaryColor → Bool → PrimaryColor × Bool
mk-color-bool-tuple = _,_ {A = PrimaryColor} {B = Bool}
-- ambiguous : Bool
-- ambiguous = _
open import Data.Bool
using (Bool; false; true; not; _∨_; _∧_)
public
open import Data.Product
using (_×_; _,_; proj₁; proj₂; curry; uncurry)
public
open import Data.Sum
using (_⊎_; inj₁; inj₂)
public