-
Notifications
You must be signed in to change notification settings - Fork 0
/
induction_in_Coq
190 lines (123 loc) · 3.61 KB
/
induction_in_Coq
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
(** Induction on Lists*)
(**Theorem. For all lists lst, P(lst).
Proof. By induction on lst.
Case: lst = nil
Show: P(nil)
Case: lst = h::t
IH: P(t)
Show: P(h::t)
QED.*)
(**Theorem: for all lists lst, lst ++ nil = lst.
Proof: by induction on lst.
P(lst) = lst ++ nil = lst.
Case: lst = nil
Show:
P(nil)
= nil ++ nil = nil
= nil = nil
Case: lst = h::t
IH: P(t) = (t ++ nil = t)
Show
P(h::t)
= (h::t) ++ nil = h::t
= h::(t ++ nil) = h::t // by definition of ++
= h::t = h::t // by IH
QED*)
(***********************************************************************************************************)
Require Import List.
Import ListNotations.
Theorem app_nil : forall (A : Type) (lst : list A), lst ++ nil = lst.
Proof.
intros A lst.
induction lst as [ | h t IH].
simpl. trivial.
simpl. rewrite -> IH.
trivial.
Qed.
(*************************************************************************************************************)
Theorem app_assoc : forall (A : Type) (l1 l2 l3 : list A),
l1 ++ (l2 ++ l3) = (l1 ++ l2) ++ l3.
Proof.
intros A l1 l2 l3.
induction l1 as [ | h t IH].
-trivial.
-simpl. rewrite -> IH.
trivial.
Qed.
(***************************************************************************************************************)
(**Induction on natural numbers*)
(**Theorem. For all natural numbers n, P(n).
Proof. By induction on n.
Case: n = 0
Show: P(0)
Case: n = k+1
IH: P(k)
Show: P(k+1)
QED.*)
Require Import Arith.
Print nat.
Compute S ( S O).
(**Adition of two nat numbers*)
Fixpoint my_add ( a b : nat) : nat :=
match a with
| O => b
| S a' => S (my_add a' b)
end.
Compute my_add (S (S O)) (S (S (S O))).
(**Sum of first n natural numbers*)
Fixpoint sum_to (n : nat) : nat :=
match n with
| O => O
| S n' => n + sum_to n'
end.
Compute sum_to (S (S O)).
(**************************************************************************************************************)
(**The equality operator = returns a proposition (i.e., something we could try to prove),
whereas the if expression expects a Boolean (i.e., true or false) as its guard.
(Actually if is willing to accept any value of an inductive type with exactly two constructors
as its guard, and bool is an example of such a type.) To fix this problem, we need to use an equality operator
that returns a bool, rather than a Prop, when applied to two nats. Such an operator is defined in the
Arith library for us:*)
Locate "=?".
Check Nat.eqb.
(*sum_to n = n * (n+1) / 2. This theorem cannot be proved easily. This involves divison. Hence in order to
ease the proof, we rewrite the Theorem as 2 * sum_to n = n * (n + 1)*)
(**Now we cannot prove this theorem easily, without writing a helper lemma,
that is 2 * sum_to (S n) = 2 * (S n) + 2 * sum_to n. This rewriting help Coq to find a match with the
Induction Hypothesis easily.*)
Lemma sum_helper : forall n : nat, 2 * sum_to (S n) = 2 * (S n) + 2 * sum_to n.
Proof.
intros n.
simpl.
ring.
Qed.
Theorem sum_sq_no_div : forall n : nat, 2 * sum_to n = n * (n+1).
Proof.
intros n.
induction n as [ | k IH].
trivial.
rewrite -> sum_helper.
rewrite -> IH.
ring.
Qed.
Search (_ * _ / _).
Search (_ * _ = _ * _).
Lemma div_helper : forall a b c : nat,
c <> 0 -> c * a = b -> a = b / c.
Proof.
intros a b c neq eq.
rewrite <- eq.
rewrite Nat.mul_comm.
rewrite Nat.div_mul.
trivial.
assumption.
Qed.
Theorem sum_sq : forall n : nat , sum_to n = n * (n+1) /2.
Proof.
intros n.
apply div_helper.
discriminate.
rewrite -> sum_sq_no_div.
trivial.
Qed.
(**************************************************************************************************************)