Skip to content

Commit a94abe8

Browse files
committed
Basics.lean: Simplify constructors
1 parent 182e492 commit a94abe8

File tree

1 file changed

+91
-79
lines changed

1 file changed

+91
-79
lines changed

SoftwareFoundationsVol1/Basics.lean

Lines changed: 91 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,30 @@ inductive Day : Type where
1212

1313
def next_working_day (day : Day) : Day :=
1414
match day with
15-
| Day.monday => Day.tuesday
16-
| Day.tuesday => Day.wednesday
15+
| Day.monday => Day.tuesday
16+
| Day.tuesday => Day.wednesday
1717
| 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
2234

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
2537

26-
example : next_working_day (next_working_day Day.saturday) = Day.tuesday := rfl
38+
example : next_working_day (next_working_day .saturday) = .tuesday := rfl
2739

2840
-- ## Booleans
2941
-- NOTE: Unlike in Coq, Lean4 does have a built-in `Bool` type. So we will
@@ -34,52 +46,52 @@ inductive MyBool : Type where
3446

3547
def negb (b : MyBool) : MyBool :=
3648
match b with
37-
| MyBool.true => MyBool.false
38-
| MyBool.false => MyBool.true
49+
| .true => .false
50+
| .false => .true
3951

4052
def andb (b1 : MyBool) (b2 : MyBool) : MyBool :=
4153
match b1 with
42-
| MyBool.true => b2
43-
| MyBool.false => MyBool.false
54+
| .true => b2
55+
| .false => .false
4456

4557
def orb (b1 : MyBool) (b2 : MyBool) : MyBool :=
4658
match b1 with
47-
| MyBool.true => MyBool.true
48-
| MyBool.false => b2
59+
| .true => .true
60+
| .false => b2
4961

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
5466

5567
infixl:60 " my&& " => andb
5668
infixl:55 " my|| " => orb
5769

58-
example : MyBool.false my|| MyBool.false my|| MyBool.true = MyBool.true := rfl
70+
example : .false my|| .false my|| .true = .true := rfl
5971

6072
-- Unlike in Coq, Lean4 does not treat first clause constructors as a truthy
6173
-- value. So we need to define our own coercion from `MyBool` to `Bool` to
6274
-- allow using `if` statements with `MyBool` values.
6375
@[coe]
6476
def MyBool.toBool (b : MyBool) : Bool :=
6577
match b with
66-
| MyBool.true => True
67-
| MyBool.false => False
78+
| .true => True
79+
| .false => False
6880
-- Typeclass for coercion from `MyBool` to `Bool`. If you don't know what a type
6981
-- class is, just skip it for now.
7082
instance : Coe MyBool Bool where coe := MyBool.toBool
7183

7284
def negb' (b : MyBool) : MyBool :=
7385
if b
74-
then MyBool.false
75-
else MyBool.true
86+
then .false
87+
else .true
7688

7789
def andb' (b1 : MyBool) (b2 : MyBool) : MyBool :=
7890
if b1 then b2
79-
else MyBool.false
91+
else .false
8092

8193
def orb' (b1 : MyBool) (b2 : MyBool) : MyBool :=
82-
if b1 then MyBool.true
94+
if b1 then .true
8395
else b2
8496

8597
inductive BW : Type where
@@ -90,35 +102,35 @@ inductive BW : Type where
90102
-- let's not abuse the `if` statement as a binary pattern matching.
91103
def invert (x: BW) : BW :=
92104
match x with
93-
| BW.black => BW.white
94-
| BW.white => BW.black
105+
| .black => .white
106+
| .white => .black
95107

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
98110

99111
-- ### Exercise: 1 star, standard (nandb)
100112
-- TODO: Replace `sorry` with your definition.
101113
def nandb (b1 b2 : MyBool) : MyBool :=
102114
/- REPLACE THIS LINE WITH YOUR DEFINITION -/ sorry
103-
example : (nandb MyBool.true MyBool.false) = true :=
115+
example : (nandb .true .false) = true :=
104116
/- FILL IN HERE -/ sorry
105-
example : (nandb MyBool.false MyBool.false) = true :=
117+
example : (nandb .false .false) = true :=
106118
/- FILL IN HERE -/ sorry
107-
example : (nandb MyBool.false MyBool.true) = true :=
119+
example : (nandb .false .true) = true :=
108120
/- FILL IN HERE -/ sorry
109-
example : (nandb MyBool.true MyBool.true) = false :=
121+
example : (nandb .true .true) = false :=
110122
/- FILL IN HERE -/ sorry
111123

112124
-- ### Exercise: 1 star, standard (andb3)
113125
def andb3 (b1 b2 b3 : MyBool) : MyBool :=
114126
/- 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 :=
116128
/- FILL IN HERE -/ sorry
117-
example : andb3 MyBool.false MyBool.true MyBool.true = MyBool.false :=
129+
example : andb3 .false .true .true = .false :=
118130
/- FILL IN HERE -/ sorry
119-
example : andb3 MyBool.true MyBool.false MyBool.true = MyBool.false :=
131+
example : andb3 .true .false .true = .false :=
120132
/- FILL IN HERE -/ sorry
121-
example : andb3 MyBool.true MyBool.true MyBool.false = MyBool.false :=
133+
example : andb3 .true .true .false = .false :=
122134
/- FILL IN HERE -/ sorry
123135

124136
-- NOTE: We'll use lean's built-in `Bool` instead of `MyBool`
@@ -143,21 +155,21 @@ inductive Color : Type where
143155

144156
def monochrome (c : Color) : Bool :=
145157
match c with
146-
| Color.black => true
147-
| Color.white => true
148-
| Color.primary _ => false
158+
| .black => true
159+
| .white => true
160+
| .primary _ => false
149161

150162
def isred (c : Color) : Bool :=
151163
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
156168

157169
-- ## Modules
158170
-- In Lean, we use `namespace` to achieve a similar effect to Coq's `Module`.
159171
namespace Playground
160-
def foo : RGB := RGB.blue
172+
def foo : RGB := .blue
161173
end Playground
162174

163175
def foo : Bool := true
@@ -173,15 +185,15 @@ namespace TuplePlayground
173185
inductive Nybble : Type where
174186
| bits (b0 b1 b2 b3 : Bit)
175187

176-
#check (Nybble.bits Bit.b1 Bit.b0 Bit.b1 Bit.b0 : Nybble)
188+
#check (Nybble.bits .b1 .b0 .b1 .b0 : Nybble)
177189

178190
def all_zero (nb : Nybble) : Bool :=
179191
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
182194

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
185197
end TuplePlayground
186198

187199
-- ## Numbers
@@ -198,19 +210,19 @@ namespace NatPlayground
198210

199211
def pred (n : Nat) : Nat :=
200212
match n with
201-
| Nat.zero => Nat.zero
202-
| Nat.succ n' => n'
213+
| .zero => .zero
214+
| .succ n' => n'
203215
end NatPlayground
204216

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
206218
-- NOTE: Lean treats `n.succ` as a `Nat.succ n`
207219
#check Nat.zero.succ.succ.succ.succ -- Nat.zero.succ.succ.succ.succ : Nat
208220

209221
def minustwo (n : Nat) : Nat :=
210222
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'
214226

215227
#eval minustwo 4 -- 2
216228

@@ -220,9 +232,9 @@ def minustwo (n : Nat) : Nat :=
220232

221233
def even (n : Nat) : Bool :=
222234
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'
226238

227239
def odd (n : Nat) : Bool :=
228240
not (even n)
@@ -233,29 +245,29 @@ example : odd 4 = false := rfl
233245
namespace NatPlayground2
234246
def plus (n : Nat) (m : Nat) : Nat :=
235247
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)
238250

239251
#eval plus 3 2 -- 5
240252

241253
def mult (n m : Nat) : Nat :=
242254
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)
245257

246258
example : mult 3 3 = 9 := rfl
247259

248260
def minus (n m : Nat) : Nat :=
249261
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'
253265
end NatPlayground2
254266

255267
def exp (base power : Nat) : Nat :=
256268
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)
259271

260272
-- ### Exercise: 1 star, standard (factorial)
261273
def factorial (n : Nat) : Nat :=
@@ -273,22 +285,22 @@ infixl:70 " my* " => Nat.mul
273285

274286
def eqb (n m : Nat) : Bool :=
275287
match n with
276-
| Nat.zero =>
288+
| .zero =>
277289
match m with
278-
| Nat.zero => true
279-
| Nat.succ _ => false
280-
| Nat.succ n' =>
290+
| .zero => true
291+
| .succ _ => false
292+
| .succ n' =>
281293
match m with
282-
| Nat.zero => false
283-
| Nat.succ m' => eqb n' m'
294+
| .zero => false
295+
| .succ m' => eqb n' m'
284296

285297
def leb (n m : Nat) : Bool :=
286298
match n with
287-
| Nat.zero => true
288-
| Nat.succ n' =>
299+
| .zero => true
300+
| .succ n' =>
289301
match m with
290-
| Nat.zero => false
291-
| Nat.succ m' => leb n' m'
302+
| .zero => false
303+
| .succ m' => leb n' m'
292304

293305
example : leb 2 2 = true := rfl
294306
example : leb 2 4 = true := rfl

0 commit comments

Comments
 (0)