forked from mirani404/Assignment1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment1.lean
More file actions
364 lines (282 loc) · 8.89 KB
/
Copy pathAssignment1.lean
File metadata and controls
364 lines (282 loc) · 8.89 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import Batteries
import AutograderLib
/-!
# Homework 1
This homework practices the material from:
* Functions and Implication
* Products and Conjunction
* Coproducts and Disjunction
* Unit and Empty Types, Truth and Falsehood
Replace every `sorry` with a term or proof of the required type. Follow any
proof-style instruction given above an exercise.
Some exercises come in pairs that state the same result twice. Prove the first
of the pair with a direct term and the second in tactic mode.
When an exercise is in tactic mode, build the proof with tactics. Passing
`exact` a single term that does the whole job (a `fun`, a `Sum.elim` or
`Or.elim`, a nested `⟨...⟩`) skips the practice the exercise is for.
-/
namespace Homework1
/-! ## Functions and Implication -/
-- Applying functions. Complete these with direct terms, without tactic mode.
@[autogradedDef 1]
def exercise01 (A B C D : Type)
(f : A → B → C → D)
(a : A) (b : B) (c : C) : D :=
f a b c
@[autogradedDef 1]
def exercise02 (A B C : Type)
(f : A → B) (g : A → B → C) (a : A) : C :=
g a (f a)
-- Constructing functions and implications.
@[autogradedDef 1]
def exercise03 (A B C : Type) : A → B → C → B := by
intro a
intro b
intro c
exact b
@[autogradedProof 1]
theorem exercise04 (P Q : Prop) : P → (P → Q) → Q := by
intro p g
exact g p
@[autogradedProof 1]
theorem exercise05 (P Q R : Prop) (h : P → Q → R) (hP : P) :
Q → R := by
apply h
exact hP
-- Composition and backward use. Reason backward with `apply` at least once in
-- each exercise. In the first, determine which assumption is unnecessary.
@[autogradedProof 2]
theorem exercise06 (P Q R : Prop) (hPQ : P → Q) (hPR : P → R) :
P → Q := by
exact hPQ
@[autogradedProof 2]
theorem exercise07 (P Q R : Prop) (hQR : Q → R) :
P → Q → R := by
intro p
apply hQR
-- Transitivity of implication. Exercises 08 and 09 state the same
-- implication. Reason backward with `apply` at least once in the tactic proof.
@[autogradedProof 1]
theorem exercise08 (P Q R : Prop) :
(P → Q) → (Q → R) → (P → R) :=
λf ↦ λg ↦ (λx ↦ g (f x))
@[autogradedProof 2]
theorem exercise09 (P Q R : Prop) :
(P → Q) → (Q → R) → (P → R) := by
intro f
intro g
exact fun x ↦ g (f x)
/-! ## Products and Conjunction -/
-- Constructing and projecting pairs. Complete these with direct terms.
@[autogradedDef 1]
def exercise10 (A B : Type) (a : A) (b : B) : B × A :=
⟨b,a⟩
@[autogradedDef 1]
def exercise11 (A B C : Type) (p : A × B × C) : B :=
p.snd.fst
@[autogradedDef 1]
def exercise12 (A B C : Type) : A × B × C → C × A :=
λp ↦ (p.snd.snd, p.fst)
-- Conjunctions and compound goals. Give a direct term for the first.
@[autogradedProof 1]
theorem exercise13 (P Q : Prop) (hP : P) (hQ : Q) : Q ∧ P :=
⟨hQ, hP⟩
@[autogradedProof 2]
theorem exercise14 (P Q R : Prop) : P ∧ Q ∧ R → R ∧ P := by
intro a
exact ⟨a.2.2, a.1⟩
@[autogradedProof 2]
theorem exercise15 (P Q R : Prop) :
P → Q → R → (P ∧ Q) ∧ R := by
intro p q r
exact ⟨⟨p, q⟩, r⟩
-- Regrouping.
@[autogradedDef 2]
def exercise16 (A B C : Type) :
A × (B × C) → (A × B) × C := by
intro p
exact ⟨⟨p.fst, p.snd.fst⟩, p.snd.snd⟩
-- Functions with products and conjunctions. Give direct terms for the
-- Type-level exercises.
@[autogradedDef 1]
def exercise17 (X A B : Type) :
(X → A × B) → (X → A) × (X → B) :=
λf ↦ ⟨λx ↦ (f x).fst, λx ↦ (f x).snd⟩
@[autogradedProof 3]
theorem exercise18 (P Q R : Prop) :
(P → Q ∧ R) → (P → Q) ∧ (P → R) := by
intro f
constructor
· intro x
exact (f x).1
· intro x
exact (f x).2
@[autogradedDef 1]
def exercise19 (X A B : Type) :
(X → A) × (X → B) → (X → A × B) :=
λf ↦ (λx ↦ ⟨f.fst x, f.snd x⟩)
-- Composition. Reason backward with `apply` at least once.
@[autogradedProof 2]
theorem exercise20 (P Q R : Prop) :
(P → Q) ∧ (Q → R) → P → R := by
intro f p
exact f.2 (f.1 p)
-- Currying and uncurrying. Exercises 21 and 22 state the same function.
@[autogradedDef 1]
def exercise21 (A B C : Type) :
(A × B → C) → (A → B → C) :=
λf ↦ (λa ↦ λb ↦ f ⟨a,b⟩)
@[autogradedDef 1]
def exercise22 (A B C : Type) :
(A × B → C) → (A → B → C) := by
intro p a b
exact p ⟨a, b⟩
-- The corresponding uncurrying at the level of propositions.
@[autogradedProof 1]
theorem exercise23 (P Q R : Prop) :
(P → Q → R) → (P ∧ Q → R) := by
intro hPQR hPQ
exact (hPQR hPQ.1) hPQ.2
/-! ## Coproducts and Disjunction -/
-- Constructing alternatives. Complete these with direct terms.
@[autogradedDef 1]
def exercise24 (A B : Type) (a : A) : B ⊕ A :=
Sum.inr a
@[autogradedDef 1]
def exercise25 (A B C : Type) (b : B) :
(A ⊕ B) ⊕ C :=
Sum.inl (Sum.inr b)
@[autogradedProof 1]
theorem exercise26 (P Q R : Prop) (hR : R) :
P ∨ Q ∨ R :=
Or.inr (Or.inr hR)
-- Case analysis. Use `cases ... with` in the second.
@[autogradedProof 2]
theorem exercise27 (P Q : Prop) : P ∧ Q → P ∨ Q := by
intro hPQ
exact Or.inl hPQ.1
@[autogradedDef 2]
def exercise28 (A : Type) : A ⊕ A → A := by
intro hAA
cases hAA with
| inl a => exact a
| inr a => exact a
-- Swapping alternatives. Exercises 29 and 30 state the same function. Use
-- `Sum.elim` in the term proof; make the tactic proof split cases with
-- `rcases` or `cases`.
@[autogradedDef 1]
def exercise29 (A B : Type) : A ⊕ B → B ⊕ A :=
fun x ↦ Sum.elim Sum.inr Sum.inl x
@[autogradedDef 2]
def exercise30 (A B : Type) : A ⊕ B → B ⊕ A := by
intro x
rcases x with hA | hB
· exact Sum.inr hA
· exact Sum.inl hB
-- Regrouping.
@[autogradedDef 3]
def exercise31 (A B C : Type) :
A ⊕ (B ⊕ C) → (A ⊕ B) ⊕ C := by
intro hABC
rcases hABC with hA | hBC
· left
left
exact hA
· rcases hBC with hB | hC
· left
right
exact hB
· right
exact hC
-- Three alternatives at once. Use a single `rcases` pattern naming all three
-- cases.
@[autogradedProof 3]
theorem exercise32 (P Q R : Prop) :
P ∨ Q ∨ R → R ∨ Q ∨ P := by
intro hPQR
rcases hPQR with hP | hQ | hR
· right; right; exact hP
· right; left; exact hQ
· left; exact hR
-- Functions and proofs by cases. Give direct terms for the Type-level
-- exercises. Use `Sum.elim` in Exercise 35.
@[autogradedDef 1]
def exercise33 (A B C : Type) :
(A ⊕ B → C) → (A → C) × (B → C) :=
λf ↦ ⟨λa ↦ f (Sum.inl a), λb ↦ f (Sum.inr b)⟩
@[autogradedProof 3]
theorem exercise34 (P Q R : Prop) :
(P ∨ Q → R) → (P → R) ∧ (Q → R) := by
intro hPQR
constructor
· exact fun x ↦ hPQR (Or.inl x)
· exact fun x ↦ hPQR (Or.inr x)
@[autogradedDef 1]
def exercise35 (A B C : Type) :
(A → C) × (B → C) → (A ⊕ B → C) :=
λf ↦ λs ↦ (Sum.elim f.1 f.2 s)
@[autogradedProof 2]
theorem exercise36 (P Q R : Prop) :
(P → R) ∧ (Q → R) → (P ∨ Q → R) := by
intro f hPQ
rcases hPQ with hP | hQ
· exact f.1 hP
· exact f.2 hQ
-- Combining alternatives with products and conjunctions. Give a direct term
-- for the first, using `Sum.elim`.
@[autogradedDef 1]
def exercise37 (A B C : Type) :
(A × B) ⊕ (A × C) → A × (B ⊕ C) :=
λf ↦ Sum.elim (λx ↦ ⟨Prod.fst x, Sum.inl (Prod.snd x)⟩)
(λx ↦ ⟨Prod.fst x, Sum.inr (Prod.snd x)⟩) f
@[autogradedProof 2]
theorem exercise38 (P Q R : Prop) :
(P ∧ Q) ∨ (P ∧ R) → P ∧ (Q ∨ R) := by
intro h
rcases h with hPQ | hPR
· constructor
· exact hPQ.1
· exact Or.inl hPQ.2
· constructor
· exact hPR.1
· exact Or.inr hPR.2
/-! ## Unit and Empty Types, Truth and Falsehood -/
-- Direct construction and elimination. Complete each with a direct term,
-- without tactic mode.
@[autogradedDef 1]
def exercise39 (A : Type) : A → Unit :=
fun _ ↦ ()
@[autogradedProof 1]
theorem exercise40 (P : Prop) : P → True :=
fun _ ↦ True.intro
@[autogradedDef 1]
def exercise41 (A : Type) (e : Empty) : A :=
Empty.elim e
@[autogradedProof 1]
theorem exercise42 (P : Prop) (hFalse : False) : P :=
False.elim hFalse
-- Combining boundary cases with earlier constructions. Give a direct term for
-- the first.
@[autogradedDef 1]
def exercise43 (A : Type) : (Unit → A) → A :=
λf ↦ f ()
@[autogradedProof 1]
theorem exercise44 (P Q : Prop) : P ∧ False → Q := by
intro x
cases x.2
@[autogradedDef 2]
def exercise45 (A : Type) : A ⊕ Empty → A := by
intro x
rcases x with a | b
· exact a
· exact Empty.elim b
-- Zero-branch elimination. Exercises 46 and 47 state the same function. Use
-- `Empty.elim` in the term proof.
@[autogradedDef 1]
def exercise46 (A B : Type) (f : A → Empty) : A → B :=
λa ↦ Empty.elim (f a)
@[autogradedDef 1]
def exercise47 (A B : Type) (f : A → Empty) : A → B := by
intro a
exact Empty.elim (f a)
end Homework1