-
Notifications
You must be signed in to change notification settings - Fork 4
/
PrimeriCas.lean
184 lines (158 loc) · 4.96 KB
/
PrimeriCas.lean
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
import Std.Tactic.SimpTrace
import Mathlib.Tactic.LibrarySearch
#check Or.elim
#check Or.inl
#check And.left
#print Or
theorem and_dist_or {p q r : Prop} :
p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) :=
have ltr : p ∧ (q ∨ r) → (p ∧ q) ∨ (p ∧ r) :=
λ h : p ∧ (q ∨ r) =>
have hp : p := And.left h
have hqr : q ∨ r := And.right h
Or.elim hqr
(fun hq : q => Or.inl (And.intro hp hq))
(fun hr : r => Or.inr (And.intro hp hr))
have rtl : (p ∧ q) ∨ (p ∧ r) → p ∧ (q ∨ r) :=
λ h : (p ∧ q) ∨ (p ∧ r) =>
Or.elim h
(λ hpq : p ∧ q =>
And.intro
(And.left hpq)
(Or.inl (And.right hpq)))
(λ hpr : p ∧ r =>
And.intro
(And.left hpr)
(Or.inr (And.right hpr)))
Iff.intro ltr rtl
theorem and_dist_or_2 {p q r : Prop} :
p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) :=
Iff.intro
(λ h =>
Or.elim h.right
(fun hq : q => Or.inl ⟨h.left, hq⟩)
(fun hr : r => Or.inr ⟨h.left, hr⟩))
(λ h =>
Or.elim h
(λ hpq => ⟨hpq.left, (Or.inl hpq.right)⟩)
(λ hpr => ⟨hpr.left, (Or.inr hpr.right)⟩))
theorem and_dist_or_3 {p q r : Prop} :
p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := by
apply Iff.intro
. intro h
have hp := h.left
cases h.right with
| inl hq => exact Or.inl (And.intro hp hq)
| inr hr => exact Or.inr (And.intro hp hr)
. intro h
cases h with
| inl hpq =>
have hp : p := hpq.left
have hq : q := hpq.right
have hqorr : q ∨ r := Or.inl hq
exact And.intro hp hqorr
| inr hpr =>
have hp : p := hpr.left
have hr : r := hpr.right
have hqorr : q ∨ r := Or.inr hr
exact And.intro hp hqorr
-- Jednakost
#print Nat
#print Eq
example : 2 + 2 = 4 := by
rfl
#check Nat.add_assoc
#check Nat.add_mul
#check Nat.add_comm
#check Nat.zero_add
#check Nat.add_zero
#check Nat.add_succ
#check Nat.succ_add
example {a b : Nat} : a + b = b + a := by
induction a with
| zero => rw [Nat.zero_add, Nat.add_zero]
| succ k ih => rw [Nat.add_succ, ←ih, Nat.succ_add]
--------------------------------------------------------------------------------
def sum_first_n : Nat → Nat
| 0 => 0
| Nat.succ k => (Nat.succ k) + sum_first_n k
theorem gauss {n : Nat} : sum_first_n n = n * (n + 1) / 2 := by
induction n with
| zero => rfl
| succ k ih =>
unfold sum_first_n
rw [ih]
-- conv =>
-- lhs
-- lhs
-- rw [@mul_dvd (Nat.succ k)]
-- rw [@mul_dvd k]
sorry
#check Nat.mul_add
#check Nat.right_distrib
theorem gauss2 {n : Nat} : 2 * sum_first_n n = n * (n + 1) := by
induction n with
| zero => rfl
| succ k ih =>
unfold sum_first_n
rw [Nat.mul_add]
rw [ih]
rw [←Nat.right_distrib]
rw [Nat.mul_comm]
rw [Nat.add_comm]
theorem gauss2_5 {n : Nat} : 2 * sum_first_n n = n * (n + 1) := by
induction n with
| zero => rfl
| succ k ih =>
calc
2 * sum_first_n (Nat.succ k) = 2 * ((Nat.succ k) + sum_first_n k) := by rw [sum_first_n]
_ = 2 * (Nat.succ k) + 2 * (sum_first_n k) := by rw [Nat.mul_add]
_ = 2 * (Nat.succ k) + k * (k + 1) := by rw [ih]
_ = (2 + k) * (k + 1) := by rw [Nat.right_distrib]
_ = (k + 1) * (2 + k) := by rw [Nat.mul_comm]
_ = (k + 1) * (k + 2) := by simp only [Nat.add_comm]
example : p ∨ q ↔ q ∨ p :=
have ltr : p ∨ q → q ∨ p :=
λ hpq : p ∨ q => by
cases hpq with
| inl hp => exact Or.inr hp
| inr hq => exact Or.inl hq
have rtl : q ∨ p → p ∨ q :=
λ hqp : q ∨ p => by
cases hqp with
| inl hq => exact Or.inr hq
| inr hp => exact Or.inl hp
Iff.intro ltr rtl
#check absurd
example {hp : p} {hnp : ¬p} : Nat :=
absurd hp hnp
example : ¬(p ∨ q) ↔ ¬p ∧ ¬q :=
have ltr : ¬(p ∨ q) → ¬p ∧ ¬q :=
fun h : ¬(p ∨ q) =>
have hnp : ¬p := (fun hp : p => h (Or.inl hp))
have hnq : ¬q := (fun hq : q => h (Or.inr hq))
And.intro hnp hnq
have rtl : ¬p ∧ ¬q → ¬(p ∨ q) :=
fun h : ¬p ∧ ¬q =>
fun h₁ : p ∨ q =>
Or.elim h₁
(fun hp : p => h.left hp)
(fun hq : q => h.right hq)
Iff.intro ltr rtl
-- excluded middle
#check Classical.em
example : ¬(p ∧ q) ↔ ¬p ∨ ¬q :=
have ltr : ¬(p ∧ q) → ¬p ∨ ¬q :=
fun h : ¬(p ∧ q) =>
Or.elim (Classical.em p)
(fun hp : p =>
have hnq : ¬q := fun hq : q => h (And.intro hp hq)
Or.inr hnq)
(fun hnp : ¬p => Or.inl hnp)
have rtl : ¬p ∨ ¬q → ¬(p ∧ q) :=
fun h : ¬p ∨ ¬q =>
fun h₁ : p ∧ q =>
Or.elim h
(fun hnp : ¬p => hnp h₁.left)
(fun hnq : ¬q => hnq h₁.right)
Iff.intro ltr rtl