4 |
4 |
5 Some examples taken from P. Martin-Löf, Intuitionistic type theory |
5 Some examples taken from P. Martin-Löf, Intuitionistic type theory |
6 (Bibliopolis, 1984). |
6 (Bibliopolis, 1984). |
7 *) |
7 *) |
8 |
8 |
9 section "Examples with elimination rules" |
9 section \<open>Examples with elimination rules\<close> |
10 |
10 |
11 theory Elimination |
11 theory Elimination |
12 imports "../CTT" |
12 imports "../CTT" |
13 begin |
13 begin |
14 |
14 |
15 text "This finds the functions fst and snd!" |
15 text \<open>This finds the functions fst and snd!\<close> |
16 |
16 |
17 schematic_goal [folded basic_defs]: "A type \<Longrightarrow> ?a : (A \<times> A) \<longrightarrow> A" |
17 schematic_goal [folded basic_defs]: "A type \<Longrightarrow> ?a : (A \<times> A) \<longrightarrow> A" |
18 apply pc |
18 apply pc |
19 done |
19 done |
20 |
20 |
21 schematic_goal [folded basic_defs]: "A type \<Longrightarrow> ?a : (A \<times> A) \<longrightarrow> A" |
21 schematic_goal [folded basic_defs]: "A type \<Longrightarrow> ?a : (A \<times> A) \<longrightarrow> A" |
22 apply pc |
22 apply pc |
23 back |
23 back |
24 done |
24 done |
25 |
25 |
26 text "Double negation of the Excluded Middle" |
26 text \<open>Double negation of the Excluded Middle\<close> |
27 schematic_goal "A type \<Longrightarrow> ?a : ((A + (A\<longrightarrow>F)) \<longrightarrow> F) \<longrightarrow> F" |
27 schematic_goal "A type \<Longrightarrow> ?a : ((A + (A\<longrightarrow>F)) \<longrightarrow> F) \<longrightarrow> F" |
28 apply intr |
28 apply intr |
29 apply (rule ProdE) |
29 apply (rule ProdE) |
30 apply assumption |
30 apply assumption |
31 apply pc |
31 apply pc |
32 done |
32 done |
|
33 |
|
34 text \<open>Experiment: the proof above in Isar\<close> |
|
35 lemma |
|
36 assumes "A type" shows "(\<^bold>\<lambda>f. f ` inr(\<^bold>\<lambda>y. f ` inl(y))) : ((A + (A\<longrightarrow>F)) \<longrightarrow> F) \<longrightarrow> F" |
|
37 proof intr |
|
38 fix f |
|
39 assume f: "f : A + (A \<longrightarrow> F) \<longrightarrow> F" |
|
40 with assms have "inr(\<^bold>\<lambda>y. f ` inl(y)) : A + (A \<longrightarrow> F)" |
|
41 by pc |
|
42 then show "f ` inr(\<^bold>\<lambda>y. f ` inl(y)) : F" |
|
43 by (rule ProdE [OF f]) |
|
44 qed (rule assms)+ |
33 |
45 |
34 schematic_goal "\<lbrakk>A type; B type\<rbrakk> \<Longrightarrow> ?a : (A \<times> B) \<longrightarrow> (B \<times> A)" |
46 schematic_goal "\<lbrakk>A type; B type\<rbrakk> \<Longrightarrow> ?a : (A \<times> B) \<longrightarrow> (B \<times> A)" |
35 apply pc |
47 apply pc |
36 done |
48 done |
37 (*The sequent version (ITT) could produce an interesting alternative |
49 (*The sequent version (ITT) could produce an interesting alternative |
38 by backtracking. No longer.*) |
50 by backtracking. No longer.*) |
39 |
51 |
40 text "Binary sums and products" |
52 text \<open>Binary sums and products\<close> |
41 schematic_goal "\<lbrakk>A type; B type; C type\<rbrakk> \<Longrightarrow> ?a : (A + B \<longrightarrow> C) \<longrightarrow> (A \<longrightarrow> C) \<times> (B \<longrightarrow> C)" |
53 schematic_goal "\<lbrakk>A type; B type; C type\<rbrakk> \<Longrightarrow> ?a : (A + B \<longrightarrow> C) \<longrightarrow> (A \<longrightarrow> C) \<times> (B \<longrightarrow> C)" |
42 apply pc |
54 apply pc |
43 done |
55 done |
44 |
56 |
45 (*A distributive law*) |
57 (*A distributive law*) |
53 and "\<And>x. x:A \<Longrightarrow> C(x) type" |
65 and "\<And>x. x:A \<Longrightarrow> C(x) type" |
54 shows "?a : (\<Sum>x:A. B(x) + C(x)) \<longrightarrow> (\<Sum>x:A. B(x)) + (\<Sum>x:A. C(x))" |
66 shows "?a : (\<Sum>x:A. B(x) + C(x)) \<longrightarrow> (\<Sum>x:A. B(x)) + (\<Sum>x:A. C(x))" |
55 apply (pc assms) |
67 apply (pc assms) |
56 done |
68 done |
57 |
69 |
58 text "Construction of the currying functional" |
70 text \<open>Construction of the currying functional\<close> |
59 schematic_goal "\<lbrakk>A type; B type; C type\<rbrakk> \<Longrightarrow> ?a : (A \<times> B \<longrightarrow> C) \<longrightarrow> (A \<longrightarrow> (B \<longrightarrow> C))" |
71 schematic_goal "\<lbrakk>A type; B type; C type\<rbrakk> \<Longrightarrow> ?a : (A \<times> B \<longrightarrow> C) \<longrightarrow> (A \<longrightarrow> (B \<longrightarrow> C))" |
60 apply pc |
72 apply pc |
61 done |
73 done |
62 |
74 |
63 (*more general goal with same proof*) |
75 (*more general goal with same proof*) |
68 shows "?a : \<Prod>f: (\<Prod>z : (\<Sum>x:A . B(x)) . C(z)). |
80 shows "?a : \<Prod>f: (\<Prod>z : (\<Sum>x:A . B(x)) . C(z)). |
69 (\<Prod>x:A . \<Prod>y:B(x) . C(<x,y>))" |
81 (\<Prod>x:A . \<Prod>y:B(x) . C(<x,y>))" |
70 apply (pc assms) |
82 apply (pc assms) |
71 done |
83 done |
72 |
84 |
73 text "Martin-Löf (1984), page 48: axiom of sum-elimination (uncurry)" |
85 text \<open>Martin-Löf (1984), page 48: axiom of sum-elimination (uncurry)\<close> |
74 schematic_goal "\<lbrakk>A type; B type; C type\<rbrakk> \<Longrightarrow> ?a : (A \<longrightarrow> (B \<longrightarrow> C)) \<longrightarrow> (A \<times> B \<longrightarrow> C)" |
86 schematic_goal "\<lbrakk>A type; B type; C type\<rbrakk> \<Longrightarrow> ?a : (A \<longrightarrow> (B \<longrightarrow> C)) \<longrightarrow> (A \<times> B \<longrightarrow> C)" |
75 apply pc |
87 apply pc |
76 done |
88 done |
77 |
89 |
78 (*more general goal with same proof*) |
90 (*more general goal with same proof*) |
83 shows "?a : (\<Prod>x:A . \<Prod>y:B(x) . C(<x,y>)) |
95 shows "?a : (\<Prod>x:A . \<Prod>y:B(x) . C(<x,y>)) |
84 \<longrightarrow> (\<Prod>z : (\<Sum>x:A . B(x)) . C(z))" |
96 \<longrightarrow> (\<Prod>z : (\<Sum>x:A . B(x)) . C(z))" |
85 apply (pc assms) |
97 apply (pc assms) |
86 done |
98 done |
87 |
99 |
88 text "Function application" |
100 text \<open>Function application\<close> |
89 schematic_goal "\<lbrakk>A type; B type\<rbrakk> \<Longrightarrow> ?a : ((A \<longrightarrow> B) \<times> A) \<longrightarrow> B" |
101 schematic_goal "\<lbrakk>A type; B type\<rbrakk> \<Longrightarrow> ?a : ((A \<longrightarrow> B) \<times> A) \<longrightarrow> B" |
90 apply pc |
102 apply pc |
91 done |
103 done |
92 |
104 |
93 text "Basic test of quantifier reasoning" |
105 text \<open>Basic test of quantifier reasoning\<close> |
94 schematic_goal |
106 schematic_goal |
95 assumes "A type" |
107 assumes "A type" |
96 and "B type" |
108 and "B type" |
97 and "\<And>x y. \<lbrakk>x:A; y:B\<rbrakk> \<Longrightarrow> C(x,y) type" |
109 and "\<And>x y. \<lbrakk>x:A; y:B\<rbrakk> \<Longrightarrow> C(x,y) type" |
98 shows |
110 shows |
99 "?a : (\<Sum>y:B . \<Prod>x:A . C(x,y)) |
111 "?a : (\<Sum>y:B . \<Prod>x:A . C(x,y)) |
100 \<longrightarrow> (\<Prod>x:A . \<Sum>y:B . C(x,y))" |
112 \<longrightarrow> (\<Prod>x:A . \<Sum>y:B . C(x,y))" |
101 apply (pc assms) |
113 apply (pc assms) |
102 done |
114 done |
103 |
115 |
104 text "Martin-Löf (1984) pages 36-7: the combinator S" |
116 text \<open>Martin-Löf (1984) pages 36-7: the combinator S\<close> |
105 schematic_goal |
117 schematic_goal |
106 assumes "A type" |
118 assumes "A type" |
107 and "\<And>x. x:A \<Longrightarrow> B(x) type" |
119 and "\<And>x. x:A \<Longrightarrow> B(x) type" |
108 and "\<And>x y. \<lbrakk>x:A; y:B(x)\<rbrakk> \<Longrightarrow> C(x,y) type" |
120 and "\<And>x y. \<lbrakk>x:A; y:B(x)\<rbrakk> \<Longrightarrow> C(x,y) type" |
109 shows "?a : (\<Prod>x:A. \<Prod>y:B(x). C(x,y)) |
121 shows "?a : (\<Prod>x:A. \<Prod>y:B(x). C(x,y)) |
110 \<longrightarrow> (\<Prod>f: (\<Prod>x:A. B(x)). \<Prod>x:A. C(x, f`x))" |
122 \<longrightarrow> (\<Prod>f: (\<Prod>x:A. B(x)). \<Prod>x:A. C(x, f`x))" |
111 apply (pc assms) |
123 apply (pc assms) |
112 done |
124 done |
113 |
125 |
114 text "Martin-Löf (1984) page 58: the axiom of disjunction elimination" |
126 text \<open>Martin-Löf (1984) page 58: the axiom of disjunction elimination\<close> |
115 schematic_goal |
127 schematic_goal |
116 assumes "A type" |
128 assumes "A type" |
117 and "B type" |
129 and "B type" |
118 and "\<And>z. z: A+B \<Longrightarrow> C(z) type" |
130 and "\<And>z. z: A+B \<Longrightarrow> C(z) type" |
119 shows "?a : (\<Prod>x:A. C(inl(x))) \<longrightarrow> (\<Prod>y:B. C(inr(y))) |
131 shows "?a : (\<Prod>x:A. C(inl(x))) \<longrightarrow> (\<Prod>y:B. C(inr(y))) |
127 apply pc |
139 apply pc |
128 done |
140 done |
129 |
141 |
130 |
142 |
131 (*Martin-Löf (1984) page 50*) |
143 (*Martin-Löf (1984) page 50*) |
132 text "AXIOM OF CHOICE! Delicate use of elimination rules" |
144 text \<open>AXIOM OF CHOICE! Delicate use of elimination rules\<close> |
133 schematic_goal |
145 schematic_goal |
134 assumes "A type" |
146 assumes "A type" |
135 and "\<And>x. x:A \<Longrightarrow> B(x) type" |
147 and "\<And>x. x:A \<Longrightarrow> B(x) type" |
136 and "\<And>x y. \<lbrakk>x:A; y:B(x)\<rbrakk> \<Longrightarrow> C(x,y) type" |
148 and "\<And>x y. \<lbrakk>x:A; y:B(x)\<rbrakk> \<Longrightarrow> C(x,y) type" |
137 shows "?a : (\<Prod>x:A. \<Sum>y:B(x). C(x,y)) \<longrightarrow> (\<Sum>f: (\<Prod>x:A. B(x)). \<Prod>x:A. C(x, f`x))" |
149 shows "?a : (\<Prod>x:A. \<Sum>y:B(x). C(x,y)) \<longrightarrow> (\<Sum>f: (\<Prod>x:A. B(x)). \<Prod>x:A. C(x, f`x))" |
166 by (rule ProdC [OF \<open>a : A\<close>]) (typechk SumE_fst f) |
178 by (rule ProdC [OF \<open>a : A\<close>]) (typechk SumE_fst f) |
167 ultimately show "snd(f`a) : C(a, (\<^bold>\<lambda>x. fst(f ` x)) ` a)" |
179 ultimately show "snd(f`a) : C(a, (\<^bold>\<lambda>x. fst(f ` x)) ` a)" |
168 by (intro replace_type [OF subst_eqtyparg]) (typechk SumE_fst assms \<open>a : A\<close>) |
180 by (intro replace_type [OF subst_eqtyparg]) (typechk SumE_fst assms \<open>a : A\<close>) |
169 qed |
181 qed |
170 |
182 |
171 text "Axiom of choice. Proof without fst, snd. Harder still!" |
183 text \<open>Axiom of choice. Proof without fst, snd. Harder still!\<close> |
172 schematic_goal [folded basic_defs]: |
184 schematic_goal [folded basic_defs]: |
173 assumes "A type" |
185 assumes "A type" |
174 and "\<And>x. x:A \<Longrightarrow> B(x) type" |
186 and "\<And>x. x:A \<Longrightarrow> B(x) type" |
175 and "\<And>x y. \<lbrakk>x:A; y:B(x)\<rbrakk> \<Longrightarrow> C(x,y) type" |
187 and "\<And>x y. \<lbrakk>x:A; y:B(x)\<rbrakk> \<Longrightarrow> C(x,y) type" |
176 shows "?a : (\<Prod>x:A. \<Sum>y:B(x). C(x,y)) \<longrightarrow> (\<Sum>f: (\<Prod>x:A. B(x)). \<Prod>x:A. C(x, f`x))" |
188 shows "?a : (\<Prod>x:A. \<Sum>y:B(x). C(x,y)) \<longrightarrow> (\<Sum>f: (\<Prod>x:A. B(x)). \<Prod>x:A. C(x, f`x))" |
190 apply (rule comp_rls) |
202 apply (rule comp_rls) |
191 apply (typechk assms) |
203 apply (typechk assms) |
192 apply assumption |
204 apply assumption |
193 done |
205 done |
194 |
206 |
195 text "Example of sequent-style deduction" |
207 text \<open>Example of sequent-style deduction\<close> |
196 (*When splitting z:A \<times> B, the assumption C(z) is affected; ?a becomes |
208 (*When splitting z:A \<times> B, the assumption C(z) is affected; ?a becomes |
197 \<^bold>\<lambda>u. split(u,\<lambda>v w.split(v,\<lambda>x y.\<^bold> \<lambda>z. <x,<y,z>>) ` w) *) |
209 \<^bold>\<lambda>u. split(u,\<lambda>v w.split(v,\<lambda>x y.\<^bold> \<lambda>z. <x,<y,z>>) ` w) *) |
198 schematic_goal |
210 schematic_goal |
199 assumes "A type" |
211 assumes "A type" |
200 and "B type" |
212 and "B type" |