127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
1 |
signature QUOTIENT =
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
2 |
sig
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
3 |
val mk_quotient_type: ((binding * mixfix) * (typ * term)) list -> Proof.context -> Proof.state
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
4 |
val mk_quotient_type_cmd: (((bstring * mixfix) * string) * string) list -> Proof.context -> Proof.state
|
128
|
5 |
val define: binding * mixfix * term -> local_theory -> (term * thm) * local_theory
|
|
6 |
val note: binding * thm -> local_theory -> thm * local_theory
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
7 |
end;
|
71
|
8 |
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
9 |
structure Quotient: QUOTIENT =
|
71
|
10 |
struct
|
|
11 |
|
205
|
12 |
(* wrappers for define, note and theorem_i *)
|
135
|
13 |
fun define (name, mx, rhs) lthy =
|
|
14 |
let
|
|
15 |
val ((rhs, (_ , thm)), lthy') =
|
|
16 |
LocalTheory.define Thm.internalK ((name, mx), (Attrib.empty_binding, rhs)) lthy
|
|
17 |
in
|
|
18 |
((rhs, thm), lthy')
|
|
19 |
end
|
|
20 |
|
|
21 |
fun note (name, thm) lthy =
|
|
22 |
let
|
|
23 |
val ((_,[thm']), lthy') = LocalTheory.note Thm.theoremK ((name, []), [thm]) lthy
|
|
24 |
in
|
|
25 |
(thm', lthy')
|
|
26 |
end
|
|
27 |
|
203
|
28 |
fun theorem after_qed goals ctxt =
|
|
29 |
let
|
|
30 |
val goals' = map (rpair []) goals
|
|
31 |
fun after_qed' thms = after_qed (the_single thms)
|
|
32 |
in
|
|
33 |
Proof.theorem_i NONE after_qed' [goals'] ctxt
|
|
34 |
end
|
135
|
35 |
|
130
|
36 |
|
|
37 |
(* definition of the quotient type *)
|
|
38 |
(***********************************)
|
|
39 |
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
40 |
(* constructs the term lambda (c::rty => bool). EX (x::rty). c = rel x *)
|
71
|
41 |
fun typedef_term rel rty lthy =
|
|
42 |
let
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
43 |
val [x, c] = [("x", rty), ("c", HOLogic.mk_setT rty)]
|
71
|
44 |
|> Variable.variant_frees lthy [rel]
|
|
45 |
|> map Free
|
|
46 |
in
|
|
47 |
lambda c
|
|
48 |
(HOLogic.exists_const rty $
|
|
49 |
lambda x (HOLogic.mk_eq (c, (rel $ x))))
|
|
50 |
end
|
|
51 |
|
|
52 |
(* makes the new type definitions and proves non-emptyness*)
|
|
53 |
fun typedef_make (qty_name, mx, rel, rty) lthy =
|
|
54 |
let
|
|
55 |
val typedef_tac =
|
|
56 |
EVERY1 [rewrite_goal_tac @{thms mem_def},
|
|
57 |
rtac @{thm exI},
|
|
58 |
rtac @{thm exI},
|
|
59 |
rtac @{thm refl}]
|
|
60 |
val tfrees = map fst (Term.add_tfreesT rty [])
|
|
61 |
in
|
|
62 |
LocalTheory.theory_result
|
|
63 |
(Typedef.add_typedef false NONE
|
|
64 |
(qty_name, tfrees, mx)
|
|
65 |
(typedef_term rel rty lthy)
|
|
66 |
NONE typedef_tac) lthy
|
|
67 |
end
|
|
68 |
|
|
69 |
(* tactic to prove the QUOT_TYPE theorem for the new type *)
|
|
70 |
fun typedef_quot_type_tac equiv_thm (typedef_info: Typedef.info) =
|
|
71 |
let
|
205
|
72 |
val unfold_mem = MetaSimplifier.rewrite_rule [@{thm mem_def}]
|
71
|
73 |
val rep_thm = #Rep typedef_info |> unfold_mem
|
|
74 |
val rep_inv = #Rep_inverse typedef_info
|
|
75 |
val abs_inv = #Abs_inverse typedef_info |> unfold_mem
|
|
76 |
val rep_inj = #Rep_inject typedef_info
|
|
77 |
in
|
|
78 |
EVERY1 [rtac @{thm QUOT_TYPE.intro},
|
|
79 |
rtac equiv_thm,
|
|
80 |
rtac rep_thm,
|
|
81 |
rtac rep_inv,
|
|
82 |
rtac abs_inv,
|
|
83 |
rtac @{thm exI},
|
|
84 |
rtac @{thm refl},
|
|
85 |
rtac rep_inj]
|
|
86 |
end
|
|
87 |
|
|
88 |
(* proves the QUOT_TYPE theorem *)
|
|
89 |
fun typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy =
|
|
90 |
let
|
|
91 |
val quot_type_const = Const (@{const_name "QUOT_TYPE"}, dummyT)
|
|
92 |
val goal = HOLogic.mk_Trueprop (quot_type_const $ rel $ abs $ rep)
|
|
93 |
|> Syntax.check_term lthy
|
|
94 |
in
|
|
95 |
Goal.prove lthy [] [] goal
|
|
96 |
(K (typedef_quot_type_tac equiv_thm typedef_info))
|
|
97 |
end
|
|
98 |
|
|
99 |
(* proves the quotient theorem *)
|
|
100 |
fun typedef_quotient_thm (rel, abs, rep, abs_def, rep_def, quot_type_thm) lthy =
|
|
101 |
let
|
|
102 |
val quotient_const = Const (@{const_name "QUOTIENT"}, dummyT)
|
|
103 |
val goal = HOLogic.mk_Trueprop (quotient_const $ rel $ abs $ rep)
|
|
104 |
|> Syntax.check_term lthy
|
|
105 |
|
|
106 |
val typedef_quotient_thm_tac =
|
|
107 |
EVERY1 [K (rewrite_goals_tac [abs_def, rep_def]),
|
|
108 |
rtac @{thm QUOT_TYPE.QUOTIENT},
|
|
109 |
rtac quot_type_thm]
|
|
110 |
in
|
|
111 |
Goal.prove lthy [] [] goal
|
|
112 |
(K typedef_quotient_thm_tac)
|
|
113 |
end
|
|
114 |
|
|
115 |
(* main function for constructing the quotient type *)
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
116 |
fun mk_typedef_main (((qty_name, mx), (rty, rel)), equiv_thm) lthy =
|
71
|
117 |
let
|
|
118 |
(* generates typedef *)
|
|
119 |
val ((_, typedef_info), lthy1) = typedef_make (qty_name, mx, rel, rty) lthy
|
|
120 |
|
|
121 |
(* abs and rep functions *)
|
|
122 |
val abs_ty = #abs_type typedef_info
|
|
123 |
val rep_ty = #rep_type typedef_info
|
|
124 |
val abs_name = #Abs_name typedef_info
|
|
125 |
val rep_name = #Rep_name typedef_info
|
|
126 |
val abs = Const (abs_name, rep_ty --> abs_ty)
|
|
127 |
val rep = Const (rep_name, abs_ty --> rep_ty)
|
|
128 |
|
|
129 |
(* ABS and REP definitions *)
|
|
130 |
val ABS_const = Const (@{const_name "QUOT_TYPE.ABS"}, dummyT )
|
|
131 |
val REP_const = Const (@{const_name "QUOT_TYPE.REP"}, dummyT )
|
|
132 |
val ABS_trm = Syntax.check_term lthy1 (ABS_const $ rel $ abs)
|
|
133 |
val REP_trm = Syntax.check_term lthy1 (REP_const $ rep)
|
|
134 |
val ABS_name = Binding.prefix_name "ABS_" qty_name
|
|
135 |
val REP_name = Binding.prefix_name "REP_" qty_name
|
|
136 |
val (((ABS, ABS_def), (REP, REP_def)), lthy2) =
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
137 |
lthy1 |> define (ABS_name, NoSyn, ABS_trm)
|
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
138 |
||>> define (REP_name, NoSyn, REP_trm)
|
71
|
139 |
|
|
140 |
(* quot_type theorem *)
|
|
141 |
val quot_thm = typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy2
|
|
142 |
val quot_thm_name = Binding.prefix_name "QUOT_TYPE_" qty_name
|
|
143 |
|
|
144 |
(* quotient theorem *)
|
|
145 |
val quotient_thm = typedef_quotient_thm (rel, ABS, REP, ABS_def, REP_def, quot_thm) lthy2
|
|
146 |
val quotient_thm_name = Binding.prefix_name "QUOTIENT_" qty_name
|
|
147 |
|
182
|
148 |
(* storing the quot-info *)
|
254
|
149 |
val lthy3 = quotdata_update (Logic.varifyT abs_ty, Logic.varifyT rty, rel, equiv_thm) lthy2
|
182
|
150 |
|
71
|
151 |
(* interpretation *)
|
|
152 |
val bindd = ((Binding.make ("", Position.none)), ([]: Attrib.src list))
|
182
|
153 |
val ((_, [eqn1pre]), lthy4) = Variable.import true [ABS_def] lthy3;
|
71
|
154 |
val eqn1i = Thm.prop_of (symmetric eqn1pre)
|
182
|
155 |
val ((_, [eqn2pre]), lthy5) = Variable.import true [REP_def] lthy4;
|
71
|
156 |
val eqn2i = Thm.prop_of (symmetric eqn2pre)
|
|
157 |
|
182
|
158 |
val exp_morphism = ProofContext.export_morphism lthy5 (ProofContext.init (ProofContext.theory_of lthy5));
|
71
|
159 |
val exp_term = Morphism.term exp_morphism;
|
|
160 |
val exp = Morphism.thm exp_morphism;
|
|
161 |
|
|
162 |
val mthd = Method.SIMPLE_METHOD ((rtac quot_thm 1) THEN
|
|
163 |
ALLGOALS (simp_tac (HOL_basic_ss addsimps [(symmetric (exp ABS_def)), (symmetric (exp REP_def))])))
|
|
164 |
val mthdt = Method.Basic (fn _ => mthd)
|
|
165 |
val bymt = Proof.global_terminal_proof (mthdt, NONE)
|
|
166 |
val exp_i = [(@{const_name QUOT_TYPE}, ((("QUOT_TYPE_I_" ^ (Binding.name_of qty_name)), true),
|
170
|
167 |
Expression.Named [("R", rel), ("Abs", abs), ("Rep", rep) ]))]
|
71
|
168 |
in
|
182
|
169 |
lthy5
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
170 |
|> note (quot_thm_name, quot_thm)
|
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
171 |
||>> note (quotient_thm_name, quotient_thm)
|
71
|
172 |
||> LocalTheory.theory (fn thy =>
|
|
173 |
let
|
|
174 |
val global_eqns = map exp_term [eqn2i, eqn1i];
|
|
175 |
(* Not sure if the following context should not be used *)
|
182
|
176 |
val (global_eqns2, lthy6) = Variable.import_terms true global_eqns lthy5;
|
71
|
177 |
val global_eqns3 = map (fn t => (bindd, t)) global_eqns2;
|
|
178 |
in ProofContext.theory_of (bymt (Expression.interpretation (exp_i, []) global_eqns3 thy)) end)
|
|
179 |
end
|
|
180 |
|
130
|
181 |
|
|
182 |
|
|
183 |
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
184 |
(* interface and syntax setup *)
|
75
|
185 |
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
186 |
(* the ML-interface takes a list of 4-tuples consisting of *)
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
187 |
(* *)
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
188 |
(* - the name of the quotient type *)
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
189 |
(* - its mixfix annotation *)
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
190 |
(* - the type to be quotient *)
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
191 |
(* - the relation according to which the type is quotient *)
|
130
|
192 |
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
193 |
fun mk_quotient_type quot_list lthy =
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
194 |
let
|
185
|
195 |
fun mk_goal (rty, rel) =
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
196 |
let
|
148
8e24e65f1e9b
tuned and attempted to store data about the quotients (does not work yet)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
197 |
val EQUIV_ty = ([rty, rty] ---> @{typ bool}) --> @{typ bool}
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
198 |
in
|
203
|
199 |
HOLogic.mk_Trueprop (Const (@{const_name EQUIV}, EQUIV_ty) $ rel)
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
200 |
end
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
201 |
|
185
|
202 |
val goals = map (mk_goal o snd) quot_list
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
203 |
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
204 |
fun after_qed thms lthy =
|
203
|
205 |
fold_map mk_typedef_main (quot_list ~~ thms) lthy |> snd
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
206 |
in
|
203
|
207 |
theorem after_qed goals lthy
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
208 |
end
|
205
|
209 |
|
|
210 |
fun mk_quotient_type_cmd spec lthy =
|
|
211 |
let
|
|
212 |
fun parse_spec (((qty_str, mx), rty_str), rel_str) =
|
|
213 |
let
|
|
214 |
val qty_name = Binding.name qty_str
|
|
215 |
val rty = Syntax.parse_typ lthy rty_str |> Syntax.check_typ lthy
|
|
216 |
val rel = Syntax.parse_term lthy rel_str |> Syntax.check_term lthy
|
|
217 |
in
|
|
218 |
((qty_name, mx), (rty, rel))
|
|
219 |
end
|
|
220 |
in
|
|
221 |
mk_quotient_type (map parse_spec spec) lthy
|
|
222 |
end
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
223 |
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
224 |
val quotspec_parser =
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
225 |
OuterParse.and_list1
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
226 |
(OuterParse.short_ident -- OuterParse.opt_infix --
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
227 |
(OuterParse.$$$ "=" |-- OuterParse.typ) --
|
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
228 |
(OuterParse.$$$ "/" |-- OuterParse.term))
|
75
|
229 |
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
230 |
val _ = OuterKeyword.keyword "/"
|
75
|
231 |
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
232 |
val _ =
|
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
233 |
OuterSyntax.local_theory_to_proof "quotient"
|
82
|
234 |
"quotient type definitions (requires equivalence proofs)"
|
127
b054cf6bd179
the command "quotient" can now define more than one quotient at the same time; quotients need to be separated by and
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
235 |
OuterKeyword.thy_goal (quotspec_parser >> mk_quotient_type_cmd)
|
79
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
236 |
|
c0c41fefeb06
added quotient command (you need to update isar-keywords-prove.el)
Christian Urban <urbanc@in.tum.de>
diff
changeset
|
237 |
end; (* structure *)
|
71
|
238 |
|
|
239 |
open Quotient |