@@ -12,18 +12,30 @@ inductive Day : Type where
12
12
13
13
def next_working_day (day : Day) : Day :=
14
14
match day with
15
- | Day.monday => Day.tuesday
16
- | Day.tuesday => Day.wednesday
15
+ | Day.monday => Day.tuesday
16
+ | Day.tuesday => Day.wednesday
17
17
| Day.wednesday => Day.thursday
18
- | Day.thursday => Day.friday
19
- | Day.friday => Day.monday
20
- | Day.saturday => Day.monday
21
- | Day.sunday => Day.monday
18
+ | Day.thursday => Day.friday
19
+ | Day.friday => Day.monday
20
+ | Day.saturday => Day.monday
21
+ | Day.sunday => Day.monday
22
+
23
+ -- NOTE: You can skip the name of the type before the constructors
24
+ -- when lean can infer it.
25
+ def next_working_day' (day : Day) : Day :=
26
+ match day with
27
+ | .monday => .tuesday
28
+ | .tuesday => .wednesday
29
+ | .wednesday => .thursday
30
+ | .thursday => .friday
31
+ | .friday => .monday
32
+ | .saturday => .monday
33
+ | .sunday => .monday
22
34
23
- #eval next_working_day Day .friday -- Day.monday
24
- #eval next_working_day (next_working_day Day .saturday) -- Day.tuesday
35
+ #eval next_working_day .friday -- Day.monday
36
+ #eval next_working_day (next_working_day .saturday) -- Day.tuesday
25
37
26
- example : next_working_day (next_working_day Day .saturday) = Day .tuesday := rfl
38
+ example : next_working_day (next_working_day .saturday) = .tuesday := rfl
27
39
28
40
-- ## Booleans
29
41
-- NOTE: Unlike in Coq, Lean4 does have a built-in `Bool` type. So we will
@@ -34,52 +46,52 @@ inductive MyBool : Type where
34
46
35
47
def negb (b : MyBool) : MyBool :=
36
48
match b with
37
- | MyBool .true => MyBool .false
38
- | MyBool .false => MyBool .true
49
+ | .true => .false
50
+ | .false => .true
39
51
40
52
def andb (b1 : MyBool) (b2 : MyBool) : MyBool :=
41
53
match b1 with
42
- | MyBool .true => b2
43
- | MyBool .false => MyBool .false
54
+ | .true => b2
55
+ | .false => .false
44
56
45
57
def orb (b1 : MyBool) (b2 : MyBool) : MyBool :=
46
58
match b1 with
47
- | MyBool .true => MyBool .true
48
- | MyBool .false => b2
59
+ | .true => .true
60
+ | .false => b2
49
61
50
- example : orb MyBool .true MyBool .false = MyBool .true := rfl
51
- example : orb MyBool .false MyBool .false = MyBool .false := rfl
52
- example : orb MyBool .false MyBool .true = MyBool .true := rfl
53
- example : orb MyBool .true MyBool .true = MyBool .true := rfl
62
+ example : orb .true .false = .true := rfl
63
+ example : orb .false .false = .false := rfl
64
+ example : orb .false .true = .true := rfl
65
+ example : orb .true .true = .true := rfl
54
66
55
67
infixl :60 " my&& " => andb
56
68
infixl :55 " my|| " => orb
57
69
58
- example : MyBool .false my|| MyBool .false my|| MyBool .true = MyBool .true := rfl
70
+ example : .false my|| .false my|| .true = .true := rfl
59
71
60
72
-- Unlike in Coq, Lean4 does not treat first clause constructors as a truthy
61
73
-- value. So we need to define our own coercion from `MyBool` to `Bool` to
62
74
-- allow using `if` statements with `MyBool` values.
63
75
@[coe]
64
76
def MyBool.toBool (b : MyBool) : Bool :=
65
77
match b with
66
- | MyBool .true => True
67
- | MyBool .false => False
78
+ | .true => True
79
+ | .false => False
68
80
-- Typeclass for coercion from `MyBool` to `Bool`. If you don't know what a type
69
81
-- class is, just skip it for now.
70
82
instance : Coe MyBool Bool where coe := MyBool.toBool
71
83
72
84
def negb' (b : MyBool) : MyBool :=
73
85
if b
74
- then MyBool .false
75
- else MyBool .true
86
+ then .false
87
+ else .true
76
88
77
89
def andb' (b1 : MyBool) (b2 : MyBool) : MyBool :=
78
90
if b1 then b2
79
- else MyBool .false
91
+ else .false
80
92
81
93
def orb' (b1 : MyBool) (b2 : MyBool) : MyBool :=
82
- if b1 then MyBool .true
94
+ if b1 then .true
83
95
else b2
84
96
85
97
inductive BW : Type where
@@ -90,35 +102,35 @@ inductive BW : Type where
90
102
-- let's not abuse the `if` statement as a binary pattern matching.
91
103
def invert (x: BW) : BW :=
92
104
match x with
93
- | BW .black => BW .white
94
- | BW .white => BW .black
105
+ | .black => .white
106
+ | .white => .black
95
107
96
- #eval invert BW .black -- BW.white
97
- #eval invert BW .white -- BW.black
108
+ #eval invert .black -- BW.white
109
+ #eval invert .white -- BW.black
98
110
99
111
-- ### Exercise: 1 star, standard (nandb)
100
112
-- TODO: Replace `sorry` with your definition.
101
113
def nandb (b1 b2 : MyBool) : MyBool :=
102
114
/- REPLACE THIS LINE WITH YOUR DEFINITION -/ sorry
103
- example : (nandb MyBool .true MyBool .false) = true :=
115
+ example : (nandb .true .false) = true :=
104
116
/- FILL IN HERE -/ sorry
105
- example : (nandb MyBool .false MyBool .false) = true :=
117
+ example : (nandb .false .false) = true :=
106
118
/- FILL IN HERE -/ sorry
107
- example : (nandb MyBool .false MyBool .true) = true :=
119
+ example : (nandb .false .true) = true :=
108
120
/- FILL IN HERE -/ sorry
109
- example : (nandb MyBool .true MyBool .true) = false :=
121
+ example : (nandb .true .true) = false :=
110
122
/- FILL IN HERE -/ sorry
111
123
112
124
-- ### Exercise: 1 star, standard (andb3)
113
125
def andb3 (b1 b2 b3 : MyBool) : MyBool :=
114
126
/- REPLACE THIS LINE WITH YOUR DEFINITION -/ sorry
115
- example : andb3 MyBool .true MyBool .true MyBool .true = MyBool .true :=
127
+ example : andb3 .true .true .true = .true :=
116
128
/- FILL IN HERE -/ sorry
117
- example : andb3 MyBool .false MyBool .true MyBool .true = MyBool .false :=
129
+ example : andb3 .false .true .true = .false :=
118
130
/- FILL IN HERE -/ sorry
119
- example : andb3 MyBool .true MyBool .false MyBool .true = MyBool .false :=
131
+ example : andb3 .true .false .true = .false :=
120
132
/- FILL IN HERE -/ sorry
121
- example : andb3 MyBool .true MyBool .true MyBool .false = MyBool .false :=
133
+ example : andb3 .true .true .false = .false :=
122
134
/- FILL IN HERE -/ sorry
123
135
124
136
-- NOTE: We'll use lean's built-in `Bool` instead of `MyBool`
@@ -143,21 +155,21 @@ inductive Color : Type where
143
155
144
156
def monochrome (c : Color) : Bool :=
145
157
match c with
146
- | Color .black => true
147
- | Color .white => true
148
- | Color .primary _ => false
158
+ | .black => true
159
+ | .white => true
160
+ | .primary _ => false
149
161
150
162
def isred (c : Color) : Bool :=
151
163
match c with
152
- | Color .black => false
153
- | Color .white => false
154
- | Color .primary RGB .red => true
155
- | Color .primary _ => false
164
+ | .black => false
165
+ | .white => false
166
+ | .primary .red => true
167
+ | .primary _ => false
156
168
157
169
-- ## Modules
158
170
-- In Lean, we use `namespace` to achieve a similar effect to Coq's `Module`.
159
171
namespace Playground
160
- def foo : RGB := RGB .blue
172
+ def foo : RGB := .blue
161
173
end Playground
162
174
163
175
def foo : Bool := true
@@ -173,15 +185,15 @@ namespace TuplePlayground
173
185
inductive Nybble : Type where
174
186
| bits (b0 b1 b2 b3 : Bit)
175
187
176
- #check (Nybble.bits Bit .b1 Bit .b0 Bit .b1 Bit .b0 : Nybble)
188
+ #check (Nybble.bits .b1 .b0 .b1 .b0 : Nybble)
177
189
178
190
def all_zero (nb : Nybble) : Bool :=
179
191
match nb with
180
- | Nybble .bits Bit .b0 Bit .b0 Bit .b0 Bit .b0 => true
181
- | Nybble .bits _ _ _ _ => false
192
+ | .bits .b0 .b0 .b0 .b0 => true
193
+ | .bits _ _ _ _ => false
182
194
183
- #eval all_zero (Nybble.bits Bit .b1 Bit .b0 Bit .b1 Bit .b0) -- false
184
- #eval all_zero (Nybble.bits Bit .b0 Bit .b0 Bit .b0 Bit .b0) -- true
195
+ #eval all_zero (Nybble.bits .b1 .b0 .b1 .b0) -- false
196
+ #eval all_zero (Nybble.bits .b0 .b0 .b0 .b0) -- true
185
197
end TuplePlayground
186
198
187
199
-- ## Numbers
@@ -198,19 +210,19 @@ namespace NatPlayground
198
210
199
211
def pred (n : Nat) : Nat :=
200
212
match n with
201
- | Nat .zero => Nat .zero
202
- | Nat .succ n' => n'
213
+ | .zero => .zero
214
+ | .succ n' => n'
203
215
end NatPlayground
204
216
205
- #check Nat.succ (Nat .succ (Nat .succ (Nat .succ Nat .zero))) -- Nat.zero.succ.succ.succ.succ : Nat
217
+ #check Nat.succ (.succ (.succ (.succ .zero))) -- Nat.zero.succ.succ.succ.succ : Nat
206
218
-- NOTE: Lean treats `n.succ` as a `Nat.succ n`
207
219
#check Nat.zero.succ.succ.succ.succ -- Nat.zero.succ.succ.succ.succ : Nat
208
220
209
221
def minustwo (n : Nat) : Nat :=
210
222
match n with
211
- | Nat .zero => Nat .zero
212
- | Nat .succ Nat .zero => Nat .zero
213
- | Nat .succ (Nat .succ n') => n'
223
+ | .zero => .zero
224
+ | .succ .zero => .zero
225
+ | .succ (.succ n') => n'
214
226
215
227
#eval minustwo 4 -- 2
216
228
@@ -220,9 +232,9 @@ def minustwo (n : Nat) : Nat :=
220
232
221
233
def even (n : Nat) : Bool :=
222
234
match n with
223
- | Nat .zero => true
224
- | Nat .succ Nat .zero => false
225
- | Nat .succ (Nat .succ n') => even n'
235
+ | .zero => true
236
+ | .succ .zero => false
237
+ | .succ (.succ n') => even n'
226
238
227
239
def odd (n : Nat) : Bool :=
228
240
not (even n)
@@ -233,29 +245,29 @@ example : odd 4 = false := rfl
233
245
namespace NatPlayground2
234
246
def plus (n : Nat) (m : Nat) : Nat :=
235
247
match n with
236
- | Nat .zero => m
237
- | Nat .succ n' => Nat .succ (plus n' m)
248
+ | .zero => m
249
+ | .succ n' => .succ (plus n' m)
238
250
239
251
#eval plus 3 2 -- 5
240
252
241
253
def mult (n m : Nat) : Nat :=
242
254
match n with
243
- | Nat .zero => Nat .zero
244
- | Nat .succ n' => plus m (mult n' m)
255
+ | .zero => .zero
256
+ | .succ n' => plus m (mult n' m)
245
257
246
258
example : mult 3 3 = 9 := rfl
247
259
248
260
def minus (n m : Nat) : Nat :=
249
261
match n, m with
250
- | Nat .zero, _ => Nat .zero
251
- | Nat .succ _, Nat .zero => n
252
- | Nat .succ n', Nat .succ m' => minus n' m'
262
+ | .zero, _ => .zero
263
+ | .succ _, .zero => n
264
+ | .succ n', .succ m' => minus n' m'
253
265
end NatPlayground2
254
266
255
267
def exp (base power : Nat) : Nat :=
256
268
match power with
257
- | Nat .zero => Nat .succ Nat .zero
258
- | Nat .succ p => Nat.mul base (exp base p)
269
+ | .zero => .succ .zero
270
+ | .succ p => Nat.mul base (exp base p)
259
271
260
272
-- ### Exercise: 1 star, standard (factorial)
261
273
def factorial (n : Nat) : Nat :=
@@ -273,22 +285,22 @@ infixl:70 " my* " => Nat.mul
273
285
274
286
def eqb (n m : Nat) : Bool :=
275
287
match n with
276
- | Nat .zero =>
288
+ | .zero =>
277
289
match m with
278
- | Nat .zero => true
279
- | Nat .succ _ => false
280
- | Nat .succ n' =>
290
+ | .zero => true
291
+ | .succ _ => false
292
+ | .succ n' =>
281
293
match m with
282
- | Nat .zero => false
283
- | Nat .succ m' => eqb n' m'
294
+ | .zero => false
295
+ | .succ m' => eqb n' m'
284
296
285
297
def leb (n m : Nat) : Bool :=
286
298
match n with
287
- | Nat .zero => true
288
- | Nat .succ n' =>
299
+ | .zero => true
300
+ | .succ n' =>
289
301
match m with
290
- | Nat .zero => false
291
- | Nat .succ m' => leb n' m'
302
+ | .zero => false
303
+ | .succ m' => leb n' m'
292
304
293
305
example : leb 2 2 = true := rfl
294
306
example : leb 2 4 = true := rfl
0 commit comments