-
Notifications
You must be signed in to change notification settings - Fork 0
/
coq3.v
316 lines (277 loc) · 7.83 KB
/
coq3.v
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
(* Jacques Garrigue, 2018 年 10 月 17 日 *)
(*** 述語論理と帰納法 ***)
(**
* 1 述語論理
**)
(*
* 前回見た命題論理は,推論の概念を捉えているが,具体的な対象に対して議論することができない.
* 我々が一般的に使う論理はその拡張である述語論理になる.
*)
(**
* 論理式 論理式は以下の結合子から定義される.
* t ::= x 項変数
* | c 項定数
* | f(t1, . . . , tn) 項関数
* P, Q ::= . . . 命題
* | p(t1, . . . , tn) 述語
* | ∀ x.P 全称
* | ∃ x.P 存在
* | t1 = t2 等価性
**)
(**
* 導出規則 命題論理の導出規則に以下の規則を加える.
*
* ∆ ⊢ P x ∉ 2 fv(∆)
* ∀ 導入 -------------------
* ∆ ⊢ ∀ x.P
*
* ∆ ⊢ ∀ x.P
* ∀ 除去 ------------
* ∆ ⊢ [t/x]P
*
* 反射律 ∆ ⊢ t = t
*
* ∆ ⊢ [t/x]P
* ∃ 導入 -----------
* ∆ ⊢ ∃ x.P
*
* ∆ ⊢ ∃ x.P ∆,P ⊢ Q x ∉ fv(∆, Q)
* ∃ 除去 ---------------------------------
* ∆ ⊢ Q
*
* ∆ ⊢ [t1/x]P ∆ ⊢ t1 = t2
* 代入 -------------------------
* ∆ ⊢ [t2/x]P
**)
(*
* ここで自由変数 fv(P) と代入が利用される.
* fv(x) = {x} fv(c) = ∅ fv(f(t1, . . . , tn)) = ⋓ fv(ti)
* fv(True) = fv(False) = fv(X) = ∅ fv(p(t1, . . . , tn)) = ⋓ fv(ti)
* fv(P ⊃ Q) = fv(P ∧ Q) = fv(P ∨ Q) = fv(P) ∪ fv(Q)
* fv(∀ x.P) = fv(∃ x.P) = fv(P) ∖ {x}
*)
(*
* [t/x]x = t [t/x]y = y
* [t/x]c = c [t/x]f(t1, . . . , tn) = f([t/x]t1, . . . , [t/x]tn)
* [t/x]True = True [t/x]False = False [t/x]X = X
* [t/x](P ⊃ Q) = [t/x]P ⊃ [t/x]Q ∧,∨ も同様
* [t/x]p(t1, . . . , tn) = p([t/x]t1, . . . , [t/x]tn)
* [t/x] ∀ y.P = ∀ y.[t/x]P (y ∉ fv(t)) [t/x] ∀ x.P = ∀ x.P
* [t/x] ∃ y.P = ∃ y.[t/x]P (y ∉ fv(t)) [t/x] ∃ x.P = ∃ x.P
*)
(**
* 導出の例
* ∀ x.human(x) ⊃ mortal(x) ⊢ ∀ x.human(x) ⊃ mortal(x)
* ---------------------------------------------------
* ∀ x.human(x) ⊃ mortal(x) ⊢ human(S) ⊃ mortal(S) human(S) ⊢ human(S)
* ------------------------------------------------------------------------
* ∀ x.human(x) ⊃ mortal(x), human(S) ⊢ mortal(S)
**)
(**
* Coq との対応
*
* Coq では項は Set に属する型,命題は Prop にそれぞれ入るが,扱いは同じである.
**)
(**
* 抽象と適用はあらゆる種類のものに対してできる.
*
* 抽象
* Γ,X:S ⊢ M:T Γ ⊢ ∀ X:S,T:Type
* ------------------------------
* Γ ⊢ fun X:S ⇒ M : ∀ X:S,T
* 適用
* Γ ⊢ M:∀ X:S,T Γ ⊢ A:S
* ----------------------
* Γ ⊢ M A:[A/X]T
*
* Γ ⊢ Set:Type
*
* Γ ⊢ A:Set
* ----------
* Γ ⊢ A:Type
*
* Γ ⊢ A:S Γ,X:A ⊢ B:Set
* --------------------- S ∈ {Set, Prop}
* Γ ⊢ ∀ X:A, B:Set
*
* Γ ⊢ Prop:Type
*
* Γ ⊢ A:Prop
* ----------
* Γ ⊢ A:Type
*
* Γ ⊢ A:S Γ,X:A ⊢ B:Prop S ∈ {Set, Prop}
* ----------------------- ∨
* Γ ⊢ ∀ X:A, B:Prop A ∈ {Set, Prop}
*
*)
(*
* この抽象と適用は普通の関数の抽象と適用の代わりに使える.
* そもそも A → B は ∀ X:A,B の略記法でしかない.
*
* 上の導出の例は適用 2 回でできる.
*)
Section Socrates.
Variable A : Set.
Variables human mortal : A -> Prop.
Variable socrates : A.
Hypothesis hm : forall x, human x -> mortal x.
Hypothesis hs : human socrates.
Theorem ms : mortal socrates.
Proof.
apply (hm socrates).
assumption.
Qed.
Print ms.
(*
ms = hm socrates hs
: mortal socrates
*)
End Socrates.
(*
* ∀ と ∃ の間に De Morgan の法則がなりたつ.
* 前回と同様に,∃ を導出しようとしたときに classic を使わなければならない.
*)
Section Laws.
Variables (A:Set) (P Q:A->Prop).
Lemma DeMorgan2 : (~ exists x, P x) -> forall x, ~ P x.
Proof.
intros N x Px.
apply N.
exists x.
apply Px.
Qed.
Theorem exists_or : (exists x, P x \/ Q x) -> (exists x, P x) \/ (exists x, Q x).
Proof.
intros H.
destruct H as [x [p|q]]. (* 中まで破壊 *)
left. exists x. assumption.
right. exists x. assumption.
Qed.
Hypothesis classic : forall P, ~~P -> P.
Lemma DeMorgan2' : (~ forall x, P x) -> exists x, ~ P x.
Proof.
intros np.
apply classic.
intros nen.
apply np; clear np.
intros a; apply classic.
intros np.
apply nen.
exists a; assumption.
Qed.
End Laws.
(**
* 練習問題 1.1 以下の定理を Coq で証明せよ.
*)
Section Coq3.
Variable A : Set.
Variable R : A -> A -> Prop.
Variables P Q : A -> Prop.
Theorem exists_postpone :
(exists x, forall y, R x y) -> (forall y, exists x, R x y).
Proof. Admitted.
Theorem or_exists : (exists x, P x) \/ (exists x, Q x) -> exists x, P x \/ Q x.
Proof. Admitted.
Hypothesis classic : forall P, ~~P -> P.
Theorem remove_c : forall a,
(forall x y, Q x -> Q y) ->
(forall c, ((exists x, P x) -> P c) -> Q c) -> Q a.
Proof. Admitted.
End Coq3.
(**
* 2 帰納法
**)
(*
* Coq でデータ型を定義すると,自動的に帰納法の原理が生成される.
*)
Module MyNat.
Inductive nat : Set := O : nat | S : nat -> nat.
(*
nat is defined
nat_rect is defined
nat_ind is defined
nat_rec is defined
*)
Check nat_ind.
(*
nat_ind
: forall P : nat -> Prop,
P O ->
(forall n : nat, P n -> P (S n)) ->
forall n : nat, P n
*)
(*
* もっと分かりやすく書くと,nat ind の型は
* ∀P, P 0 → (∀n, P n → P (S n)) → (∀n, P n)
* である.
* 即ち P は 0 でなりたち,任意の n について P が n でなりたてば,n + 1 でもなりたつこ
とが証明できれば,任意の n について P がなりたつ.
* ちなみに,nat rec の定義を見ると,
*)
Check nat_rec.
(*
nat_rec
: forall P : nat -> Set,
P O ->
(forall n : nat, P n -> P (S n)) ->
forall n : nat, P n
*)
(*
* P が Prop ではなく Set を返すこと以外,全く同じである.
* 本当の定義を見ると,
*)
Print nat_rect.
(*
nat_rect =
fun (P : nat -> Type) (f : P O) (f0 : forall n : nat, P n -> P (S n)) =>
fix F (n : nat) : P n :=
match n as n0 return (P n0) with
| O => f
| S n0 => f0 n0 (F n0)
end
*)
(*
* 実は普通の再帰関数同様,fix と match を使って定義されている.
*)
End MyNat. (* 普通の nat に戻る *)
Definition plus' : nat -> nat -> nat.
intros m n.
induction m.
exact n. (* n を返す *)
exact (S IHm). (* 帰納法によって得られた IHm の後者を返す *)
Defined. (* 計算を可能にするために Defined で閉じる *)
Print plus'.
(*
fun m n : nat => nat_rec (fun _ : nat => nat) n (fun _ IHm : nat => S IHm) m
*)
Eval compute in plus' 2 3.
(*
= 5
: N
*)
Lemma plus_assoc : forall m n p, m + (n + p) = (m + n) + p.
Proof.
intros m n p.
induction m.
simpl. (* 計算する *)
SearchPattern (?X = ?X). (* 反射律を調べる *)
(* eq_refl: forall (A : Type) (x : A), x = x *)
apply eq_refl.
simpl.
rewrite IHm. (* 代入を行う *)
reflexivity. (* apply eq_refl と同じ *)
Qed.
(**
* 練習問題 2.1 以下の定理を証明せよ.
**)
Theorem plus_0 : forall n, n + 0 = n.
Proof. Admitted.
Theorem plus_m_Sn : forall m n, m + (S n) = S (m + n).
Proof. Admitted.
Theorem plus_comm : forall m n, m + n = n + m.
Proof. Admitted.
Theorem plus_distr : forall m n p, (m + n) * p = m * p + n * p.
Proof. Admitted.
Theorem mult_assoc : forall m n p, m * (n * p) = (m * n) * p.
Proof. Admitted.