author | wenzelm |
Thu, 15 Mar 2012 19:02:34 +0100 | |
changeset 46947 | b8c7eb0c2f89 |
parent 46909 | 3c73a121a387 |
child 46949 | 94aa7b81bcf6 |
permissions | -rw-r--r-- |
45680 | 1 |
(* Title: HOL/Tools/Quotient/quotient_type.ML |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
2 |
Author: Cezary Kaliszyk and Christian Urban |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
3 |
|
35806
a814cccce0b8
rollback of local typedef until problem with type-variables can be sorted out; fixed header
Christian Urban <urbanc@in.tum.de>
parents:
35790
diff
changeset
|
4 |
Definition of a quotient type. |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
5 |
*) |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
6 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
7 |
signature QUOTIENT_TYPE = |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
8 |
sig |
45676
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
9 |
val add_quotient_type: ((string list * binding * mixfix) * (typ * term * bool) * |
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
10 |
((binding * binding) option)) * thm -> local_theory -> Quotient_Info.quotients * local_theory |
35415
1810b1ade437
export add_quotient_type.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
35351
diff
changeset
|
11 |
|
45676
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
12 |
val quotient_type: ((string list * binding * mixfix) * (typ * term * bool) * |
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
13 |
((binding * binding) option)) list -> Proof.context -> Proof.state |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
14 |
|
45676
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
15 |
val quotient_type_cmd: (((((string list * binding) * mixfix) * string) * (bool * string)) * |
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
16 |
(binding * binding) option) list -> Proof.context -> Proof.state |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
17 |
end; |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
18 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
19 |
structure Quotient_Type: QUOTIENT_TYPE = |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
20 |
struct |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
21 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
22 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
23 |
(*** definition of quotient types ***) |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
24 |
|
44204
3cdc4176638c
Quotient Package: make quotient_type work with separate set type
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
43547
diff
changeset
|
25 |
val mem_def1 = @{lemma "y : Collect S ==> S y" by simp} |
3cdc4176638c
Quotient Package: make quotient_type work with separate set type
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
43547
diff
changeset
|
26 |
val mem_def2 = @{lemma "S y ==> y : Collect S" by simp} |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
27 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
28 |
(* constructs the term lambda (c::rty => bool). EX (x::rty). c = rel x *) |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
29 |
fun typedef_term rel rty lthy = |
41444 | 30 |
let |
31 |
val [x, c] = |
|
32 |
[("x", rty), ("c", HOLogic.mk_setT rty)] |
|
33 |
|> Variable.variant_frees lthy [rel] |
|
34 |
|> map Free |
|
35 |
in |
|
45312 | 36 |
HOLogic.Collect_const (HOLogic.mk_setT rty) $ (lambda c (HOLogic.exists_const rty $ |
44204
3cdc4176638c
Quotient Package: make quotient_type work with separate set type
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
43547
diff
changeset
|
37 |
lambda x (HOLogic.mk_conj (rel $ x $ x, |
45312 | 38 |
HOLogic.mk_eq (c, HOLogic.Collect_const rty $ (rel $ x)))))) |
41444 | 39 |
end |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
40 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
41 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
42 |
(* makes the new type definitions and proves non-emptyness *) |
37493
2377d246a631
Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
36960
diff
changeset
|
43 |
fun typedef_make (vs, qty_name, mx, rel, rty) equiv_thm lthy = |
41444 | 44 |
let |
45 |
val typedef_tac = |
|
46 |
EVERY1 (map rtac [@{thm part_equivp_typedef}, equiv_thm]) |
|
47 |
in |
|
46727
0162a0d284ac
Finish localizing the quotient package.
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
45835
diff
changeset
|
48 |
Typedef.add_typedef false NONE (qty_name, map (rpair dummyS) vs, mx) |
41444 | 49 |
(typedef_term rel rty lthy) NONE typedef_tac lthy |
50 |
end |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
51 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
52 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
53 |
(* tactic to prove the quot_type theorem for the new type *) |
35994
9cc3df9a606e
Typedef.info: separate global and local part, only the latter is transformed by morphisms;
wenzelm
parents:
35842
diff
changeset
|
54 |
fun typedef_quot_type_tac equiv_thm ((_, typedef_info): Typedef.info) = |
41444 | 55 |
let |
56 |
val rep_thm = #Rep typedef_info RS mem_def1 |
|
57 |
val rep_inv = #Rep_inverse typedef_info |
|
58 |
val abs_inv = #Abs_inverse typedef_info |
|
59 |
val rep_inj = #Rep_inject typedef_info |
|
60 |
in |
|
61 |
(rtac @{thm quot_type.intro} THEN' RANGE [ |
|
62 |
rtac equiv_thm, |
|
63 |
rtac rep_thm, |
|
64 |
rtac rep_inv, |
|
65 |
rtac abs_inv THEN' rtac mem_def2 THEN' atac, |
|
66 |
rtac rep_inj]) 1 |
|
67 |
end |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
68 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
69 |
(* proves the quot_type theorem for the new type *) |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
70 |
fun typedef_quot_type_thm (rel, abs, rep, equiv_thm, typedef_info) lthy = |
41444 | 71 |
let |
45317
bf8b9ac6000c
more robust, declarative and unsurprising computation of types in the quotient type definition
bulwahn
parents:
45314
diff
changeset
|
72 |
val quot_type_const = Const (@{const_name "quot_type"}, |
bf8b9ac6000c
more robust, declarative and unsurprising computation of types in the quotient type definition
bulwahn
parents:
45314
diff
changeset
|
73 |
fastype_of rel --> fastype_of abs --> fastype_of rep --> @{typ bool}) |
bf8b9ac6000c
more robust, declarative and unsurprising computation of types in the quotient type definition
bulwahn
parents:
45314
diff
changeset
|
74 |
val goal = HOLogic.mk_Trueprop (quot_type_const $ rel $ abs $ rep) |
41444 | 75 |
in |
76 |
Goal.prove lthy [] [] goal |
|
77 |
(K (typedef_quot_type_tac equiv_thm typedef_info)) |
|
78 |
end |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
79 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
80 |
(* main function for constructing a quotient type *) |
45676
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
81 |
fun add_quotient_type (((vs, qty_name, mx), (rty, rel, partial), opt_morphs), equiv_thm) lthy = |
41444 | 82 |
let |
83 |
val part_equiv = |
|
84 |
if partial |
|
85 |
then equiv_thm |
|
86 |
else equiv_thm RS @{thm equivp_implies_part_equivp} |
|
37493
2377d246a631
Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
36960
diff
changeset
|
87 |
|
41444 | 88 |
(* generates the typedef *) |
89 |
val ((qty_full_name, typedef_info), lthy1) = |
|
90 |
typedef_make (vs, qty_name, mx, rel, rty) part_equiv lthy |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
91 |
|
41444 | 92 |
(* abs and rep functions from the typedef *) |
93 |
val Abs_ty = #abs_type (#1 typedef_info) |
|
94 |
val Rep_ty = #rep_type (#1 typedef_info) |
|
95 |
val Abs_name = #Abs_name (#1 typedef_info) |
|
96 |
val Rep_name = #Rep_name (#1 typedef_info) |
|
97 |
val Abs_const = Const (Abs_name, Rep_ty --> Abs_ty) |
|
98 |
val Rep_const = Const (Rep_name, Abs_ty --> Rep_ty) |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
99 |
|
41444 | 100 |
(* more useful abs and rep definitions *) |
45317
bf8b9ac6000c
more robust, declarative and unsurprising computation of types in the quotient type definition
bulwahn
parents:
45314
diff
changeset
|
101 |
val abs_const = Const (@{const_name quot_type.abs}, |
bf8b9ac6000c
more robust, declarative and unsurprising computation of types in the quotient type definition
bulwahn
parents:
45314
diff
changeset
|
102 |
(rty --> rty --> @{typ bool}) --> (Rep_ty --> Abs_ty) --> rty --> Abs_ty) |
bf8b9ac6000c
more robust, declarative and unsurprising computation of types in the quotient type definition
bulwahn
parents:
45314
diff
changeset
|
103 |
val rep_const = Const (@{const_name quot_type.rep}, (Abs_ty --> Rep_ty) --> Abs_ty --> rty) |
bf8b9ac6000c
more robust, declarative and unsurprising computation of types in the quotient type definition
bulwahn
parents:
45314
diff
changeset
|
104 |
val abs_trm = abs_const $ rel $ Abs_const |
bf8b9ac6000c
more robust, declarative and unsurprising computation of types in the quotient type definition
bulwahn
parents:
45314
diff
changeset
|
105 |
val rep_trm = rep_const $ Rep_const |
45676
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
106 |
val (rep_name, abs_name) = |
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
107 |
(case opt_morphs of |
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
108 |
NONE => (Binding.prefix_name "rep_" qty_name, Binding.prefix_name "abs_" qty_name) |
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
109 |
| SOME morphs => morphs) |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
110 |
|
45534
4ab21521b393
adding database of abs and rep terms to the quotient package; registering abs and rep terms in quotient_type and using them in quotient_definition
bulwahn
parents:
45340
diff
changeset
|
111 |
val ((abs_t, (_, abs_def)), lthy2) = lthy1 |
46909 | 112 |
|> Local_Theory.define ((abs_name, NoSyn), ((Thm.def_binding abs_name, []), abs_trm)) |
45534
4ab21521b393
adding database of abs and rep terms to the quotient package; registering abs and rep terms in quotient_type and using them in quotient_definition
bulwahn
parents:
45340
diff
changeset
|
113 |
val ((rep_t, (_, rep_def)), lthy3) = lthy2 |
46909 | 114 |
|> Local_Theory.define ((rep_name, NoSyn), ((Thm.def_binding rep_name, []), rep_trm)) |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
115 |
|
41444 | 116 |
(* quot_type theorem *) |
117 |
val quot_thm = typedef_quot_type_thm (rel, Abs_const, Rep_const, part_equiv, typedef_info) lthy3 |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
118 |
|
41444 | 119 |
(* quotient theorem *) |
120 |
val quotient_thm_name = Binding.prefix_name "Quotient_" qty_name |
|
121 |
val quotient_thm = |
|
122 |
(quot_thm RS @{thm quot_type.Quotient}) |
|
123 |
|> fold_rule [abs_def, rep_def] |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
124 |
|
41444 | 125 |
(* name equivalence theorem *) |
126 |
val equiv_thm_name = Binding.suffix_name "_equivp" qty_name |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
127 |
|
45279 | 128 |
(* storing the quotients *) |
129 |
val quotients = {qtyp = Abs_ty, rtyp = rty, equiv_rel = rel, equiv_thm = equiv_thm} |
|
37530
70d03844b2f9
export of proper information in the ML-interface of the quotient package
Christian Urban <urbanc@in.tum.de>
parents:
37493
diff
changeset
|
130 |
|
45279 | 131 |
fun qinfo phi = Quotient_Info.transform_quotients phi quotients |
45534
4ab21521b393
adding database of abs and rep terms to the quotient package; registering abs and rep terms in quotient_type and using them in quotient_definition
bulwahn
parents:
45340
diff
changeset
|
132 |
fun abs_rep phi = Quotient_Info.transform_abs_rep phi {abs = abs_t, rep = rep_t} |
37530
70d03844b2f9
export of proper information in the ML-interface of the quotient package
Christian Urban <urbanc@in.tum.de>
parents:
37493
diff
changeset
|
133 |
|
41444 | 134 |
val lthy4 = lthy3 |
45291
57cd50f98fdc
uniform Local_Theory.declaration with explicit params;
wenzelm
parents:
45283
diff
changeset
|
135 |
|> Local_Theory.declaration {syntax = false, pervasive = true} |
45534
4ab21521b393
adding database of abs and rep terms to the quotient package; registering abs and rep terms in quotient_type and using them in quotient_definition
bulwahn
parents:
45340
diff
changeset
|
136 |
(fn phi => Quotient_Info.update_quotients qty_full_name (qinfo phi) |
4ab21521b393
adding database of abs and rep terms to the quotient package; registering abs and rep terms in quotient_type and using them in quotient_definition
bulwahn
parents:
45340
diff
changeset
|
137 |
#> Quotient_Info.update_abs_rep qty_full_name (abs_rep phi)) |
45282 | 138 |
|> (snd oo Local_Theory.note) |
139 |
((equiv_thm_name, |
|
140 |
if partial then [] else [Attrib.internal (K Quotient_Info.equiv_rules_add)]), |
|
141 |
[equiv_thm]) |
|
142 |
|> (snd oo Local_Theory.note) |
|
143 |
((quotient_thm_name, [Attrib.internal (K Quotient_Info.quotient_rules_add)]), |
|
144 |
[quotient_thm]) |
|
41444 | 145 |
in |
45279 | 146 |
(quotients, lthy4) |
41444 | 147 |
end |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
148 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
149 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
150 |
(* sanity checks for the quotient type specifications *) |
45676
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
151 |
fun sanity_check ((vs, qty_name, _), (rty, rel, _), _) = |
41444 | 152 |
let |
153 |
val rty_tfreesT = map fst (Term.add_tfreesT rty []) |
|
154 |
val rel_tfrees = map fst (Term.add_tfrees rel []) |
|
155 |
val rel_frees = map fst (Term.add_frees rel []) |
|
156 |
val rel_vars = Term.add_vars rel [] |
|
157 |
val rel_tvars = Term.add_tvars rel [] |
|
43547
f3a8476285c6
clarified Binding.pretty/print: no quotes, only markup -- Binding.str_of is rendered obsolete;
wenzelm
parents:
42361
diff
changeset
|
158 |
val qty_str = Binding.print qty_name ^ ": " |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
159 |
|
41444 | 160 |
val illegal_rel_vars = |
161 |
if null rel_vars andalso null rel_tvars then [] |
|
162 |
else [qty_str ^ "illegal schematic variable(s) in the relation."] |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
163 |
|
41444 | 164 |
val dup_vs = |
165 |
(case duplicates (op =) vs of |
|
166 |
[] => [] |
|
167 |
| dups => [qty_str ^ "duplicate type variable(s) on the lhs: " ^ commas_quote dups]) |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
168 |
|
41444 | 169 |
val extra_rty_tfrees = |
170 |
(case subtract (op =) vs rty_tfreesT of |
|
171 |
[] => [] |
|
172 |
| extras => [qty_str ^ "extra type variable(s) on the lhs: " ^ commas_quote extras]) |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
173 |
|
41444 | 174 |
val extra_rel_tfrees = |
175 |
(case subtract (op =) vs rel_tfrees of |
|
176 |
[] => [] |
|
177 |
| extras => [qty_str ^ "extra type variable(s) in the relation: " ^ commas_quote extras]) |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
178 |
|
41444 | 179 |
val illegal_rel_frees = |
180 |
(case rel_frees of |
|
181 |
[] => [] |
|
182 |
| xs => [qty_str ^ "illegal variable(s) in the relation: " ^ commas_quote xs]) |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
183 |
|
41444 | 184 |
val errs = illegal_rel_vars @ dup_vs @ extra_rty_tfrees @ extra_rel_tfrees @ illegal_rel_frees |
185 |
in |
|
186 |
if null errs then () else error (cat_lines errs) |
|
187 |
end |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
188 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
189 |
(* check for existence of map functions *) |
45795
2d8949268303
maps are taken from enriched type infrastracture, rewritten lifting of constants, now we can lift even contravariant and co/contravariant types
kuncar
parents:
45698
diff
changeset
|
190 |
fun map_check ctxt (_, (rty, _, _), _) = |
41444 | 191 |
let |
192 |
fun map_check_aux rty warns = |
|
45280 | 193 |
(case rty of |
41444 | 194 |
Type (_, []) => warns |
45340
98ec8b51af9c
prefer global Quotient_Info lookup to accomodate Quotient_Term, which is not quite localized yet (cf. 9fd6fce8a230);
wenzelm
parents:
45317
diff
changeset
|
195 |
| Type (s, _) => |
45795
2d8949268303
maps are taken from enriched type infrastracture, rewritten lifting of constants, now we can lift even contravariant and co/contravariant types
kuncar
parents:
45698
diff
changeset
|
196 |
if Symtab.defined (Enriched_Type.entries ctxt) s then warns else s :: warns |
45280 | 197 |
| _ => warns) |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
198 |
|
41444 | 199 |
val warns = map_check_aux rty [] |
200 |
in |
|
201 |
if null warns then () |
|
202 |
else warning ("No map function defined for " ^ commas warns ^ |
|
203 |
". This will cause problems later on.") |
|
204 |
end |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
205 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
206 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
207 |
(*** interface and syntax setup ***) |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
208 |
|
45698
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
209 |
(* the ML-interface takes a list of tuples consisting of: |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
210 |
|
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
211 |
- the name of the quotient type |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
212 |
- its free type variables (first argument) |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
213 |
- its mixfix annotation |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
214 |
- the type to be quotient |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
215 |
- the partial flag (a boolean) |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
216 |
- the relation according to which the type is quotient |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
217 |
- optional names of morphisms (rep/abs) |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
218 |
|
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
219 |
it opens a proof-state in which one has to show that the |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
220 |
relations are equivalence relations |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
221 |
*) |
fd8e140ae879
removed outdated comment moved back and updated (at the direct request of Christian Urban)
kuncar
parents:
45690
diff
changeset
|
222 |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
223 |
fun quotient_type quot_list lthy = |
41444 | 224 |
let |
225 |
(* sanity check *) |
|
226 |
val _ = List.app sanity_check quot_list |
|
45795
2d8949268303
maps are taken from enriched type infrastracture, rewritten lifting of constants, now we can lift even contravariant and co/contravariant types
kuncar
parents:
45698
diff
changeset
|
227 |
val _ = List.app (map_check lthy) quot_list |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
228 |
|
41444 | 229 |
fun mk_goal (rty, rel, partial) = |
230 |
let |
|
231 |
val equivp_ty = ([rty, rty] ---> @{typ bool}) --> @{typ bool} |
|
232 |
val const = |
|
233 |
if partial then @{const_name part_equivp} else @{const_name equivp} |
|
234 |
in |
|
235 |
HOLogic.mk_Trueprop (Const (const, equivp_ty) $ rel) |
|
236 |
end |
|
237 |
||
45676
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
238 |
val goals = map (mk_goal o #2) quot_list |
41444 | 239 |
|
45282 | 240 |
fun after_qed [thms] = fold (snd oo add_quotient_type) (quot_list ~~ thms) |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
241 |
in |
45282 | 242 |
Proof.theorem NONE after_qed [map (rpair []) goals] lthy |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
243 |
end |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
244 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
245 |
fun quotient_type_cmd specs lthy = |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
246 |
let |
45676
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
247 |
fun parse_spec (((((vs, qty_name), mx), rty_str), (partial, rel_str)), opt_morphs) lthy = |
41444 | 248 |
let |
249 |
val rty = Syntax.read_typ lthy rty_str |
|
46727
0162a0d284ac
Finish localizing the quotient package.
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
45835
diff
changeset
|
250 |
val tmp_lthy1 = Variable.declare_typ rty lthy |
41444 | 251 |
val rel = |
46727
0162a0d284ac
Finish localizing the quotient package.
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
45835
diff
changeset
|
252 |
Syntax.parse_term tmp_lthy1 rel_str |
41444 | 253 |
|> Type.constraint (rty --> rty --> @{typ bool}) |
46727
0162a0d284ac
Finish localizing the quotient package.
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
45835
diff
changeset
|
254 |
|> Syntax.check_term tmp_lthy1 |
0162a0d284ac
Finish localizing the quotient package.
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
45835
diff
changeset
|
255 |
val tmp_lthy2 = Variable.declare_term rel tmp_lthy1 |
41444 | 256 |
in |
46727
0162a0d284ac
Finish localizing the quotient package.
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
45835
diff
changeset
|
257 |
(((vs, qty_name, mx), (rty, rel, partial), opt_morphs), tmp_lthy2) |
41444 | 258 |
end |
259 |
||
46727
0162a0d284ac
Finish localizing the quotient package.
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
45835
diff
changeset
|
260 |
val (spec', _) = fold_map parse_spec specs lthy |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
261 |
in |
46727
0162a0d284ac
Finish localizing the quotient package.
Cezary Kaliszyk <cezarykaliszyk@gmail.com>
parents:
45835
diff
changeset
|
262 |
quotient_type spec' lthy |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
263 |
end |
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
264 |
|
37493
2377d246a631
Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
36960
diff
changeset
|
265 |
val partial = Scan.optional (Parse.reserved "partial" -- Parse.$$$ ":" >> K true) false |
2377d246a631
Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
36960
diff
changeset
|
266 |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
267 |
val quotspec_parser = |
37493
2377d246a631
Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
36960
diff
changeset
|
268 |
Parse.and_list1 |
2377d246a631
Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
36960
diff
changeset
|
269 |
((Parse.type_args -- Parse.binding) -- |
45835 | 270 |
(* FIXME Parse.type_args_constrained and standard treatment of sort constraints *) |
37493
2377d246a631
Quotient package now uses Partial Equivalence instead place of equivalence
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
36960
diff
changeset
|
271 |
Parse.opt_mixfix -- (Parse.$$$ "=" |-- Parse.typ) -- |
45676
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
272 |
(Parse.$$$ "/" |-- (partial -- Parse.term)) -- |
fa46fef06590
alternative names of morphisms in the definition of a quotient type can be specified
kuncar
parents:
45534
diff
changeset
|
273 |
Scan.option (Parse.$$$ "morphisms" |-- Parse.!!! (Parse.binding -- Parse.binding))) |
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
274 |
|
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
275 |
val _ = |
41444 | 276 |
Outer_Syntax.local_theory_to_proof "quotient_type" |
277 |
"quotient type definitions (require equivalence proofs)" |
|
278 |
Keyword.thy_goal (quotspec_parser >> quotient_type_cmd) |
|
35222
4f1fba00f66d
Initial version of HOL quotient package.
Cezary Kaliszyk <kaliszyk@in.tum.de>
parents:
diff
changeset
|
279 |
|
45280 | 280 |
end; |