author | haftmann |
Fri, 27 Nov 2009 08:41:10 +0100 | |
changeset 33963 | 977b94b64905 |
parent 33959 | 2afc55e8ed27 |
permissions | -rw-r--r-- |
5177 | 1 |
(* Title: HOL/Tools/datatype_rep_proofs.ML |
11539 | 2 |
Author: Stefan Berghofer, TU Muenchen |
5177 | 3 |
|
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
4 |
Definitional introduction of datatypes with proof of characteristic theorems: |
5177 | 5 |
|
6 |
- injectivity of constructors |
|
7228 | 7 |
- distinctness of constructors |
5177 | 8 |
- induction theorem |
9 |
*) |
|
10 |
||
11 |
signature DATATYPE_REP_PROOFS = |
|
12 |
sig |
|
33963
977b94b64905
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33959
diff
changeset
|
13 |
val add_datatype : DatatypeAux.config -> string list -> (string list * binding * mixfix * |
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
14 |
(binding * typ list * mixfix) list) list -> theory -> string list * theory |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
15 |
val datatype_cmd : string list -> (string list * binding * mixfix * |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
16 |
(binding * string list * mixfix) list) list -> theory -> theory |
5177 | 17 |
end; |
18 |
||
19 |
structure DatatypeRepProofs : DATATYPE_REP_PROOFS = |
|
20 |
struct |
|
21 |
||
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
22 |
(** auxiliary **) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
23 |
|
5177 | 24 |
open DatatypeAux; |
25 |
||
26 |
val (_ $ (_ $ (_ $ (distinct_f $ _) $ _))) = hd (prems_of distinct_lemma); |
|
27 |
||
21021 | 28 |
val collect_simp = rewrite_rule [mk_meta_eq mem_Collect_eq]; |
29 |
||
31737 | 30 |
fun exh_thm_of (dt_info : info Symtab.table) tname = |
32712
ec5976f4d3d8
registering split rules and projected induction rules; ML identifiers more close to Isar theorem names
haftmann
parents:
32124
diff
changeset
|
31 |
#exhaust (the (Symtab.lookup dt_info tname)); |
5177 | 32 |
|
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
33 |
val node_name = @{type_name "Datatype.node"}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
34 |
val In0_name = @{const_name "Datatype.In0"}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
35 |
val In1_name = @{const_name "Datatype.In1"}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
36 |
val Scons_name = @{const_name "Datatype.Scons"}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
37 |
val Leaf_name = @{const_name "Datatype.Leaf"}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
38 |
val Numb_name = @{const_name "Datatype.Numb"}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
39 |
val Lim_name = @{const_name "Datatype.Lim"}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
40 |
val Suml_name = @{const_name "Sum_Type.Suml"}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
41 |
val Sumr_name = @{const_name "Sum_Type.Sumr"}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
42 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
43 |
val In0_inject = @{thm In0_inject}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
44 |
val In1_inject = @{thm In1_inject}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
45 |
val Scons_inject = @{thm Scons_inject}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
46 |
val Leaf_inject = @{thm Leaf_inject}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
47 |
val In0_eq = @{thm In0_eq}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
48 |
val In1_eq = @{thm In1_eq}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
49 |
val In0_not_In1 = @{thm In0_not_In1}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
50 |
val In1_not_In0 = @{thm In1_not_In0}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
51 |
val Lim_inject = @{thm Lim_inject}; |
33963
977b94b64905
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33959
diff
changeset
|
52 |
val Inl_inject = @{thm Inl_inject}; |
977b94b64905
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33959
diff
changeset
|
53 |
val Inr_inject = @{thm Inr_inject}; |
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
54 |
val Suml_inject = @{thm Suml_inject}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
55 |
val Sumr_inject = @{thm Sumr_inject}; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
56 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
57 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
58 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
59 |
(** proof of characteristic theorems **) |
5177 | 60 |
|
31737 | 61 |
fun representation_proofs (config : config) (dt_info : info Symtab.table) |
8436 | 62 |
new_type_names descr sorts types_syntax constr_syntax case_names_induct thy = |
5177 | 63 |
let |
28362
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
64 |
val descr' = flat descr; |
5661 | 65 |
val big_name = space_implode "_" new_type_names; |
32124 | 66 |
val thy1 = Sign.add_path big_name thy; |
5661 | 67 |
val big_rec_name = big_name ^ "_rep_set"; |
21021 | 68 |
val rep_set_names' = |
5177 | 69 |
(if length descr' = 1 then [big_rec_name] else |
70 |
(map ((curry (op ^) (big_rec_name ^ "_")) o string_of_int) |
|
71 |
(1 upto (length descr')))); |
|
28965 | 72 |
val rep_set_names = map (Sign.full_bname thy1) rep_set_names'; |
5177 | 73 |
|
5661 | 74 |
val tyvars = map (fn (_, (_, Ts, _)) => map dest_DtTFree Ts) (hd descr); |
75 |
val leafTs' = get_nonrec_types descr' sorts; |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
76 |
val branchTs = get_branching_types descr' sorts; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
77 |
val branchT = if null branchTs then HOLogic.unitT |
32765 | 78 |
else Balanced_Tree.make (fn (T, U) => Type ("+", [T, U])) branchTs; |
33040 | 79 |
val arities = remove (op =) 0 (get_arities descr'); |
33338 | 80 |
val unneeded_vars = |
81 |
subtract (op =) (List.foldr OldTerm.add_typ_tfree_names [] (leafTs' @ branchTs)) (hd tyvars); |
|
82 |
val leafTs = leafTs' @ map (fn n => TFree (n, (the o AList.lookup (op =) sorts) n)) unneeded_vars; |
|
5177 | 83 |
val recTs = get_rec_types descr' sorts; |
33957 | 84 |
val (newTs, oldTs) = chop (length (hd descr)) recTs; |
5177 | 85 |
val sumT = if null leafTs then HOLogic.unitT |
32765 | 86 |
else Balanced_Tree.make (fn (T, U) => Type ("+", [T, U])) leafTs; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
87 |
val Univ_elT = HOLogic.mk_setT (Type (node_name, [sumT, branchT])); |
5177 | 88 |
val UnivT = HOLogic.mk_setT Univ_elT; |
21021 | 89 |
val UnivT' = Univ_elT --> HOLogic.boolT; |
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
90 |
val Collect = Const (@{const_name Collect}, UnivT' --> UnivT); |
5177 | 91 |
|
92 |
val In0 = Const (In0_name, Univ_elT --> Univ_elT); |
|
93 |
val In1 = Const (In1_name, Univ_elT --> Univ_elT); |
|
94 |
val Leaf = Const (Leaf_name, sumT --> Univ_elT); |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
95 |
val Lim = Const (Lim_name, (branchT --> Univ_elT) --> Univ_elT); |
5177 | 96 |
|
97 |
(* make injections needed for embedding types in leaves *) |
|
98 |
||
99 |
fun mk_inj T' x = |
|
100 |
let |
|
101 |
fun mk_inj' T n i = |
|
102 |
if n = 1 then x else |
|
103 |
let val n2 = n div 2; |
|
104 |
val Type (_, [T1, T2]) = T |
|
105 |
in |
|
106 |
if i <= n2 then |
|
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
107 |
Const (@{const_name "Sum_Type.Inl"}, T1 --> T) $ (mk_inj' T1 n2 i) |
5177 | 108 |
else |
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
109 |
Const (@{const_name "Sum_Type.Inr"}, T2 --> T) $ (mk_inj' T2 (n - n2) (i - n2)) |
5177 | 110 |
end |
31986 | 111 |
in mk_inj' sumT (length leafTs) (1 + find_index (fn T'' => T'' = T') leafTs) |
5177 | 112 |
end; |
113 |
||
114 |
(* make injections for constructors *) |
|
115 |
||
32765 | 116 |
fun mk_univ_inj ts = Balanced_Tree.access |
23419 | 117 |
{left = fn t => In0 $ t, |
118 |
right = fn t => In1 $ t, |
|
119 |
init = |
|
28524 | 120 |
if ts = [] then Const (@{const_name undefined}, Univ_elT) |
23419 | 121 |
else foldr1 (HOLogic.mk_binop Scons_name) ts}; |
5177 | 122 |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
123 |
(* function spaces *) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
124 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
125 |
fun mk_fun_inj T' x = |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
126 |
let |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
127 |
fun mk_inj T n i = |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
128 |
if n = 1 then x else |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
129 |
let |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
130 |
val n2 = n div 2; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
131 |
val Type (_, [T1, T2]) = T; |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
132 |
fun mkT U = (U --> Univ_elT) --> T --> Univ_elT |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
133 |
in |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
134 |
if i <= n2 then Const (Suml_name, mkT T1) $ mk_inj T1 n2 i |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
135 |
else Const (Sumr_name, mkT T2) $ mk_inj T2 (n - n2) (i - n2) |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
136 |
end |
31986 | 137 |
in mk_inj branchT (length branchTs) (1 + find_index (fn T'' => T'' = T') branchTs) |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
138 |
end; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
139 |
|
33338 | 140 |
fun mk_lim t Ts = fold_rev (fn T => fn t => Lim $ mk_fun_inj T (Abs ("x", T, t))) Ts t; |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
141 |
|
5177 | 142 |
(************** generate introduction rules for representing set **********) |
143 |
||
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31643
diff
changeset
|
144 |
val _ = message config "Constructing representing sets ..."; |
5177 | 145 |
|
146 |
(* make introduction rule for a single constructor *) |
|
147 |
||
148 |
fun make_intr s n (i, (_, cargs)) = |
|
149 |
let |
|
33338 | 150 |
fun mk_prem dt (j, prems, ts) = |
151 |
(case strip_dtyp dt of |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
152 |
(dts, DtRec k) => |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
153 |
let |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
154 |
val Ts = map (typ_of_dtyp descr' sorts) dts; |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
155 |
val free_t = |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
156 |
app_bnds (mk_Free "x" (Ts ---> Univ_elT) j) (length Ts) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
157 |
in (j + 1, list_all (map (pair "x") Ts, |
21021 | 158 |
HOLogic.mk_Trueprop |
31949 | 159 |
(Free (nth rep_set_names' k, UnivT') $ free_t)) :: prems, |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
160 |
mk_lim free_t Ts :: ts) |
5177 | 161 |
end |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
162 |
| _ => |
5177 | 163 |
let val T = typ_of_dtyp descr' sorts dt |
164 |
in (j + 1, prems, (Leaf $ mk_inj T (mk_Free "x" T j))::ts) |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
165 |
end); |
5177 | 166 |
|
33338 | 167 |
val (_, prems, ts) = fold_rev mk_prem cargs (1, [], []); |
21021 | 168 |
val concl = HOLogic.mk_Trueprop |
169 |
(Free (s, UnivT') $ mk_univ_inj ts n i) |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
170 |
in Logic.list_implies (prems, concl) |
5177 | 171 |
end; |
172 |
||
28362
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
173 |
val intr_ts = maps (fn ((_, (_, _, constrs)), rep_set_name) => |
5177 | 174 |
map (make_intr rep_set_name (length constrs)) |
28362
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
175 |
((1 upto (length constrs)) ~~ constrs)) (descr' ~~ rep_set_names'); |
5177 | 176 |
|
21365
4ee8e2702241
InductivePackage.add_inductive_i: canonical argument order;
wenzelm
parents:
21291
diff
changeset
|
177 |
val ({raw_induct = rep_induct, intrs = rep_intrs, ...}, thy2) = |
33278 | 178 |
thy1 |
179 |
|> Sign.map_naming Name_Space.conceal |
|
33726
0878aecbf119
eliminated slightly odd name space grouping -- now managed by Isar toplevel;
wenzelm
parents:
33669
diff
changeset
|
180 |
|> Inductive.add_inductive_global |
33669 | 181 |
{quiet_mode = #quiet config, verbose = false, alt_name = Binding.name big_rec_name, |
182 |
coind = false, no_elim = true, no_ind = false, skip_mono = true, fork_mono = false} |
|
28965 | 183 |
(map (fn s => ((Binding.name s, UnivT'), NoSyn)) rep_set_names') [] |
33278 | 184 |
(map (fn x => (Attrib.empty_binding, x)) intr_ts) [] |
185 |
||> Sign.restore_naming thy1 |
|
186 |
||> Theory.checkpoint; |
|
5177 | 187 |
|
188 |
(********************************* typedef ********************************) |
|
189 |
||
21021 | 190 |
val (typedefs, thy3) = thy2 |> |
32124 | 191 |
Sign.parent_path |> |
21021 | 192 |
fold_map (fn ((((name, mx), tvs), c), name') => |
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31668
diff
changeset
|
193 |
Typedef.add_typedef false (SOME (Binding.name name')) (name, tvs, mx) |
21021 | 194 |
(Collect $ Const (c, UnivT')) NONE |
195 |
(rtac exI 1 THEN rtac CollectI 1 THEN |
|
196 |
QUIET_BREADTH_FIRST (has_fewer_prems 1) |
|
26475
3cc1e48d0ce1
eliminated quiet_mode ref of some packages (avoid CRITICAL setmp!);
wenzelm
parents:
26359
diff
changeset
|
197 |
(resolve_tac rep_intrs 1))) |
21021 | 198 |
(types_syntax ~~ tyvars ~~ |
33957 | 199 |
(take (length newTs) rep_set_names) ~~ new_type_names) ||> |
32124 | 200 |
Sign.add_path big_name; |
5177 | 201 |
|
202 |
(*********************** definition of constructors ***********************) |
|
203 |
||
204 |
val big_rep_name = (space_implode "_" new_type_names) ^ "_Rep_"; |
|
205 |
val rep_names = map (curry op ^ "Rep_") new_type_names; |
|
206 |
val rep_names' = map (fn i => big_rep_name ^ (string_of_int i)) |
|
28362
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
207 |
(1 upto (length (flat (tl descr)))); |
22578 | 208 |
val all_rep_names = map (Sign.intern_const thy3) rep_names @ |
28965 | 209 |
map (Sign.full_bname thy3) rep_names'; |
5177 | 210 |
|
211 |
(* isomorphism declarations *) |
|
212 |
||
30345 | 213 |
val iso_decls = map (fn (T, s) => (Binding.name s, T --> Univ_elT, NoSyn)) |
5177 | 214 |
(oldTs ~~ rep_names'); |
215 |
||
216 |
(* constructor definitions *) |
|
217 |
||
33244 | 218 |
fun make_constr_def tname T n ((cname, cargs), (cname', mx)) (thy, defs, eqns, i) = |
5177 | 219 |
let |
33338 | 220 |
fun constr_arg dt (j, l_args, r_args) = |
5177 | 221 |
let val T = typ_of_dtyp descr' sorts dt; |
222 |
val free_t = mk_Free "x" T j |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
223 |
in (case (strip_dtyp dt, strip_type T) of |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
224 |
((_, DtRec m), (Us, U)) => (j + 1, free_t :: l_args, mk_lim |
31949 | 225 |
(Const (nth all_rep_names m, U --> Univ_elT) $ |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
226 |
app_bnds free_t (length Us)) Us :: r_args) |
5177 | 227 |
| _ => (j + 1, free_t::l_args, (Leaf $ mk_inj T free_t)::r_args)) |
228 |
end; |
|
229 |
||
33338 | 230 |
val (_, l_args, r_args) = fold_rev constr_arg cargs (1, [], []); |
5177 | 231 |
val constrT = (map (typ_of_dtyp descr' sorts) cargs) ---> T; |
22578 | 232 |
val abs_name = Sign.intern_const thy ("Abs_" ^ tname); |
233 |
val rep_name = Sign.intern_const thy ("Rep_" ^ tname); |
|
5177 | 234 |
val lhs = list_comb (Const (cname, constrT), l_args); |
235 |
val rhs = mk_univ_inj r_args n i; |
|
27330 | 236 |
val def = Logic.mk_equals (lhs, Const (abs_name, Univ_elT --> T) $ rhs); |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30345
diff
changeset
|
237 |
val def_name = Long_Name.base_name cname ^ "_def"; |
5177 | 238 |
val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq |
239 |
(Const (rep_name, T --> Univ_elT) $ lhs, rhs)); |
|
18377 | 240 |
val ([def_thm], thy') = |
241 |
thy |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24112
diff
changeset
|
242 |
|> Sign.add_consts_i [(cname', constrT, mx)] |
29579 | 243 |
|> (PureThy.add_defs false o map Thm.no_attributes) [(Binding.name def_name, def)]; |
5177 | 244 |
|
8436 | 245 |
in (thy', defs @ [def_thm], eqns @ [eqn], i + 1) end; |
5177 | 246 |
|
247 |
(* constructor definitions for datatype *) |
|
248 |
||
33244 | 249 |
fun dt_constr_defs ((((_, (_, _, constrs)), tname), T), constr_syntax) |
250 |
(thy, defs, eqns, rep_congs, dist_lemmas) = |
|
5177 | 251 |
let |
252 |
val _ $ (_ $ (cong_f $ _) $ _) = concl_of arg_cong; |
|
22578 | 253 |
val rep_const = cterm_of thy |
254 |
(Const (Sign.intern_const thy ("Rep_" ^ tname), T --> Univ_elT)); |
|
32957 | 255 |
val cong' = |
256 |
Drule.standard (cterm_instantiate [(cterm_of thy cong_f, rep_const)] arg_cong); |
|
257 |
val dist = |
|
258 |
Drule.standard (cterm_instantiate [(cterm_of thy distinct_f, rep_const)] distinct_lemma); |
|
33244 | 259 |
val (thy', defs', eqns', _) = fold ((make_constr_def tname T) (length constrs)) |
260 |
(constrs ~~ constr_syntax) (Sign.add_path tname thy, defs, [], 1); |
|
5177 | 261 |
in |
32124 | 262 |
(Sign.parent_path thy', defs', eqns @ [eqns'], |
5177 | 263 |
rep_congs @ [cong'], dist_lemmas @ [dist]) |
264 |
end; |
|
265 |
||
33244 | 266 |
val (thy4, constr_defs, constr_rep_eqns, rep_congs, dist_lemmas) = |
267 |
fold dt_constr_defs |
|
268 |
(hd descr ~~ new_type_names ~~ newTs ~~ constr_syntax) |
|
269 |
(thy3 |> Sign.add_consts_i iso_decls |> Sign.parent_path, [], [], [], []); |
|
270 |
||
5177 | 271 |
|
272 |
(*********** isomorphisms for new types (introduced by typedef) ***********) |
|
273 |
||
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31643
diff
changeset
|
274 |
val _ = message config "Proving isomorphism properties ..."; |
5177 | 275 |
|
21021 | 276 |
val newT_iso_axms = map (fn (_, td) => |
277 |
(collect_simp (#Abs_inverse td), #Rep_inverse td, |
|
278 |
collect_simp (#Rep td))) typedefs; |
|
5177 | 279 |
|
21021 | 280 |
val newT_iso_inj_thms = map (fn (_, td) => |
281 |
(collect_simp (#Abs_inject td) RS iffD1, #Rep_inject td RS iffD1)) typedefs; |
|
5177 | 282 |
|
283 |
(********* isomorphisms between existing types and "unfolded" types *******) |
|
284 |
||
285 |
(*---------------------------------------------------------------------*) |
|
286 |
(* isomorphisms are defined using primrec-combinators: *) |
|
287 |
(* generate appropriate functions for instantiating primrec-combinator *) |
|
288 |
(* *) |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
289 |
(* e.g. dt_Rep_i = list_rec ... (%h t y. In1 (Scons (Leaf h) y)) *) |
5177 | 290 |
(* *) |
291 |
(* also generate characteristic equations for isomorphisms *) |
|
292 |
(* *) |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
293 |
(* e.g. dt_Rep_i (cons h t) = In1 (Scons (dt_Rep_j h) (dt_Rep_i t)) *) |
5177 | 294 |
(*---------------------------------------------------------------------*) |
295 |
||
33244 | 296 |
fun make_iso_def k ks n (cname, cargs) (fs, eqns, i) = |
5177 | 297 |
let |
298 |
val argTs = map (typ_of_dtyp descr' sorts) cargs; |
|
31949 | 299 |
val T = nth recTs k; |
300 |
val rep_name = nth all_rep_names k; |
|
5177 | 301 |
val rep_const = Const (rep_name, T --> Univ_elT); |
302 |
val constr = Const (cname, argTs ---> T); |
|
303 |
||
33244 | 304 |
fun process_arg ks' dt (i2, i2', ts, Ts) = |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
305 |
let |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
306 |
val T' = typ_of_dtyp descr' sorts dt; |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
307 |
val (Us, U) = strip_type T' |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
308 |
in (case strip_dtyp dt of |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
309 |
(_, DtRec j) => if j mem ks' then |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
310 |
(i2 + 1, i2' + 1, ts @ [mk_lim (app_bnds |
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
311 |
(mk_Free "y" (Us ---> Univ_elT) i2') (length Us)) Us], |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
312 |
Ts @ [Us ---> Univ_elT]) |
5177 | 313 |
else |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
314 |
(i2 + 1, i2', ts @ [mk_lim |
31949 | 315 |
(Const (nth all_rep_names j, U --> Univ_elT) $ |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
316 |
app_bnds (mk_Free "x" T' i2) (length Us)) Us], Ts) |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
317 |
| _ => (i2 + 1, i2', ts @ [Leaf $ mk_inj T' (mk_Free "x" T' i2)], Ts)) |
5177 | 318 |
end; |
319 |
||
33244 | 320 |
val (i2, i2', ts, Ts) = fold (process_arg ks) cargs (1, 1, [], []); |
5177 | 321 |
val xs = map (uncurry (mk_Free "x")) (argTs ~~ (1 upto (i2 - 1))); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
322 |
val ys = map (uncurry (mk_Free "y")) (Ts ~~ (1 upto (i2' - 1))); |
5177 | 323 |
val f = list_abs_free (map dest_Free (xs @ ys), mk_univ_inj ts n i); |
324 |
||
33244 | 325 |
val (_, _, ts', _) = fold (process_arg []) cargs (1, 1, [], []); |
5177 | 326 |
val eqn = HOLogic.mk_Trueprop (HOLogic.mk_eq |
327 |
(rep_const $ list_comb (constr, xs), mk_univ_inj ts' n i)) |
|
328 |
||
329 |
in (fs @ [f], eqns @ [eqn], i + 1) end; |
|
330 |
||
331 |
(* define isomorphisms for all mutually recursive datatypes in list ds *) |
|
332 |
||
33244 | 333 |
fun make_iso_defs ds (thy, char_thms) = |
5177 | 334 |
let |
335 |
val ks = map fst ds; |
|
336 |
val (_, (tname, _, _)) = hd ds; |
|
17412 | 337 |
val {rec_rewrites, rec_names, ...} = the (Symtab.lookup dt_info tname); |
5177 | 338 |
|
33244 | 339 |
fun process_dt (k, (tname, _, constrs)) (fs, eqns, isos) = |
5177 | 340 |
let |
33244 | 341 |
val (fs', eqns', _) = |
342 |
fold (make_iso_def k ks (length constrs)) constrs (fs, eqns, 1); |
|
31949 | 343 |
val iso = (nth recTs k, nth all_rep_names k) |
5177 | 344 |
in (fs', eqns', isos @ [iso]) end; |
345 |
||
33244 | 346 |
val (fs, eqns, isos) = fold process_dt ds ([], [], []); |
5177 | 347 |
val fTs = map fastype_of fs; |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30345
diff
changeset
|
348 |
val defs = map (fn (rec_name, (T, iso_name)) => (Binding.name (Long_Name.base_name iso_name ^ "_def"), |
27330 | 349 |
Logic.mk_equals (Const (iso_name, T --> Univ_elT), |
350 |
list_comb (Const (rec_name, fTs @ [T] ---> Univ_elT), fs)))) (rec_names ~~ isos); |
|
28362
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
351 |
val (def_thms, thy') = |
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
352 |
apsnd Theory.checkpoint ((PureThy.add_defs false o map Thm.no_attributes) defs thy); |
5177 | 353 |
|
354 |
(* prove characteristic equations *) |
|
355 |
||
5553 | 356 |
val rewrites = def_thms @ (map mk_meta_eq rec_rewrites); |
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
357 |
val char_thms' = map (fn eqn => Skip_Proof.prove_global thy' [] [] eqn |
20046 | 358 |
(fn _ => EVERY [rewrite_goals_tac rewrites, rtac refl 1])) eqns; |
5177 | 359 |
|
360 |
in (thy', char_thms' @ char_thms) end; |
|
361 |
||
33244 | 362 |
val (thy5, iso_char_thms) = apfst Theory.checkpoint (fold_rev make_iso_defs |
363 |
(tl descr) (Sign.add_path big_name thy4, [])); |
|
5177 | 364 |
|
365 |
(* prove isomorphism properties *) |
|
366 |
||
28362
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
367 |
fun mk_funs_inv thy thm = |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
368 |
let |
26626
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents:
26531
diff
changeset
|
369 |
val prop = Thm.prop_of thm; |
21021 | 370 |
val _ $ (_ $ ((S as Const (_, Type (_, [U, _]))) $ _ )) $ |
33832 | 371 |
(_ $ (_ $ (r $ (a $ _)) $ _)) = Type.legacy_freeze prop; |
29270
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
28965
diff
changeset
|
372 |
val used = OldTerm.add_term_tfree_names (a, []); |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
373 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
374 |
fun mk_thm i = |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
375 |
let |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
376 |
val Ts = map (TFree o rpair HOLogic.typeS) |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
20046
diff
changeset
|
377 |
(Name.variant_list used (replicate i "'t")); |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
378 |
val f = Free ("f", Ts ---> U) |
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
379 |
in Skip_Proof.prove_global thy [] [] (Logic.mk_implies |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
380 |
(HOLogic.mk_Trueprop (HOLogic.list_all |
21021 | 381 |
(map (pair "x") Ts, S $ app_bnds f i)), |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
382 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (list_abs (map (pair "x") Ts, |
17985 | 383 |
r $ (a $ app_bnds f i)), f)))) |
26806 | 384 |
(fn _ => EVERY [REPEAT_DETERM_N i (rtac ext 1), |
385 |
REPEAT (etac allE 1), rtac thm 1, atac 1]) |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
386 |
end |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
387 |
in map (fn r => r RS subst) (thm :: map mk_thm arities) end; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
388 |
|
5177 | 389 |
(* prove inj dt_Rep_i and dt_Rep_i x : dt_rep_set_i *) |
390 |
||
26806 | 391 |
val fun_congs = map (fn T => make_elim (Drule.instantiate' |
392 |
[SOME (ctyp_of thy5 T)] [] fun_cong)) branchTs; |
|
393 |
||
33338 | 394 |
fun prove_iso_thms ds (inj_thms, elem_thms) = |
5177 | 395 |
let |
396 |
val (_, (tname, _, _)) = hd ds; |
|
32727 | 397 |
val induct = (#induct o the o Symtab.lookup dt_info) tname; |
5177 | 398 |
|
399 |
fun mk_ind_concl (i, _) = |
|
400 |
let |
|
31949 | 401 |
val T = nth recTs i; |
402 |
val Rep_t = Const (nth all_rep_names i, T --> Univ_elT); |
|
403 |
val rep_set_name = nth rep_set_names i |
|
5177 | 404 |
in (HOLogic.all_const T $ Abs ("y", T, HOLogic.imp $ |
405 |
HOLogic.mk_eq (Rep_t $ mk_Free "x" T i, Rep_t $ Bound 0) $ |
|
406 |
HOLogic.mk_eq (mk_Free "x" T i, Bound 0)), |
|
21021 | 407 |
Const (rep_set_name, UnivT') $ (Rep_t $ mk_Free "x" T i)) |
5177 | 408 |
end; |
409 |
||
410 |
val (ind_concl1, ind_concl2) = ListPair.unzip (map mk_ind_concl ds); |
|
411 |
||
5553 | 412 |
val rewrites = map mk_meta_eq iso_char_thms; |
21021 | 413 |
val inj_thms' = map snd newT_iso_inj_thms @ |
26359 | 414 |
map (fn r => r RS @{thm injD}) inj_thms; |
5177 | 415 |
|
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
416 |
val inj_thm = Skip_Proof.prove_global thy5 [] [] |
17985 | 417 |
(HOLogic.mk_Trueprop (mk_conj ind_concl1)) (fn _ => EVERY |
32712
ec5976f4d3d8
registering split rules and projected induction rules; ML identifiers more close to Isar theorem names
haftmann
parents:
32124
diff
changeset
|
418 |
[(indtac induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1, |
5177 | 419 |
REPEAT (EVERY |
420 |
[rtac allI 1, rtac impI 1, |
|
421 |
exh_tac (exh_thm_of dt_info) 1, |
|
422 |
REPEAT (EVERY |
|
423 |
[hyp_subst_tac 1, |
|
424 |
rewrite_goals_tac rewrites, |
|
425 |
REPEAT (dresolve_tac [In0_inject, In1_inject] 1), |
|
426 |
(eresolve_tac [In0_not_In1 RS notE, In1_not_In0 RS notE] 1) |
|
427 |
ORELSE (EVERY |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
428 |
[REPEAT (eresolve_tac (Scons_inject :: |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
429 |
map make_elim [Leaf_inject, Inl_inject, Inr_inject]) 1), |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
430 |
REPEAT (cong_tac 1), rtac refl 1, |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
431 |
REPEAT (atac 1 ORELSE (EVERY |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
432 |
[REPEAT (rtac ext 1), |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
433 |
REPEAT (eresolve_tac (mp :: allE :: |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
434 |
map make_elim (Suml_inject :: Sumr_inject :: |
26806 | 435 |
Lim_inject :: inj_thms') @ fun_congs) 1), |
20046 | 436 |
atac 1]))])])])]); |
5177 | 437 |
|
26359 | 438 |
val inj_thms'' = map (fn r => r RS @{thm datatype_injI}) |
6171 | 439 |
(split_conj_thm inj_thm); |
5177 | 440 |
|
6171 | 441 |
val elem_thm = |
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
442 |
Skip_Proof.prove_global thy5 [] [] (HOLogic.mk_Trueprop (mk_conj ind_concl2)) |
20046 | 443 |
(fn _ => |
32712
ec5976f4d3d8
registering split rules and projected induction rules; ML identifiers more close to Isar theorem names
haftmann
parents:
32124
diff
changeset
|
444 |
EVERY [(indtac induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1, |
20046 | 445 |
rewrite_goals_tac rewrites, |
446 |
REPEAT ((resolve_tac rep_intrs THEN_ALL_NEW |
|
447 |
((REPEAT o etac allE) THEN' ares_tac elem_thms)) 1)]); |
|
5177 | 448 |
|
11471
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
449 |
in (inj_thms'' @ inj_thms, elem_thms @ (split_conj_thm elem_thm)) |
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
450 |
end; |
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
451 |
|
33338 | 452 |
val (iso_inj_thms_unfolded, iso_elem_thms) = |
453 |
fold_rev prove_iso_thms (tl descr) ([], map #3 newT_iso_axms); |
|
21021 | 454 |
val iso_inj_thms = map snd newT_iso_inj_thms @ |
26359 | 455 |
map (fn r => r RS @{thm injD}) iso_inj_thms_unfolded; |
11471
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
456 |
|
21021 | 457 |
(* prove dt_rep_set_i x --> x : range dt_Rep_i *) |
11471
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
458 |
|
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
459 |
fun mk_iso_t (((set_name, iso_name), i), T) = |
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
460 |
let val isoT = T --> Univ_elT |
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
461 |
in HOLogic.imp $ |
21021 | 462 |
(Const (set_name, UnivT') $ mk_Free "x" Univ_elT i) $ |
31643 | 463 |
(if i < length newTs then HOLogic.true_const |
11471
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
464 |
else HOLogic.mk_mem (mk_Free "x" Univ_elT i, |
31643 | 465 |
Const (@{const_name image}, isoT --> HOLogic.mk_setT T --> UnivT) $ |
30304
d8e4cd2ac2a1
set operations Int, Un, INTER, UNION, Inter, Union, empty, UNIV are now proper qualified constants with authentic syntax
haftmann
parents:
29579
diff
changeset
|
466 |
Const (iso_name, isoT) $ Const (@{const_name UNIV}, HOLogic.mk_setT T))) |
5177 | 467 |
end; |
468 |
||
11471
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
469 |
val iso_t = HOLogic.mk_Trueprop (mk_conj (map mk_iso_t |
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
470 |
(rep_set_names ~~ all_rep_names ~~ (0 upto (length descr' - 1)) ~~ recTs))); |
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
471 |
|
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
472 |
(* all the theorems are proved by one single simultaneous induction *) |
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
473 |
|
26359 | 474 |
val range_eqs = map (fn r => mk_meta_eq (r RS @{thm range_ex1_eq})) |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
475 |
iso_inj_thms_unfolded; |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
476 |
|
11471
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
477 |
val iso_thms = if length descr = 1 then [] else |
33957 | 478 |
drop (length newTs) (split_conj_thm |
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
479 |
(Skip_Proof.prove_global thy5 [] [] iso_t (fn _ => EVERY |
25678 | 480 |
[(indtac rep_induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1, |
11471
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
481 |
REPEAT (rtac TrueI 1), |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
482 |
rewrite_goals_tac (mk_meta_eq choice_eq :: |
26359 | 483 |
symmetric (mk_meta_eq @{thm expand_fun_eq}) :: range_eqs), |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
484 |
rewrite_goals_tac (map symmetric range_eqs), |
11471
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
485 |
REPEAT (EVERY |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
486 |
[REPEAT (eresolve_tac ([rangeE, ex1_implies_ex RS exE] @ |
28362
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
487 |
maps (mk_funs_inv thy5 o #1) newT_iso_axms) 1), |
11471
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
488 |
TRY (hyp_subst_tac 1), |
ba2c252b55ad
- Fixed bug in isomorphism proofs (caused by migration from SOME to THE)
berghofe
parents:
11435
diff
changeset
|
489 |
rtac (sym RS range_eqI) 1, |
20046 | 490 |
resolve_tac iso_char_thms 1])]))); |
11435 | 491 |
|
492 |
val Abs_inverse_thms' = |
|
493 |
map #1 newT_iso_axms @ |
|
33057 | 494 |
map2 (fn r_inj => fn r => @{thm f_the_inv_into_f} OF [r_inj, r RS mp]) |
18330 | 495 |
iso_inj_thms_unfolded iso_thms; |
11435 | 496 |
|
28362
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
497 |
val Abs_inverse_thms = maps (mk_funs_inv thy5) Abs_inverse_thms'; |
5177 | 498 |
|
499 |
(******************* freeness theorems for constructors *******************) |
|
500 |
||
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31643
diff
changeset
|
501 |
val _ = message config "Proving freeness of constructors ..."; |
5177 | 502 |
|
503 |
(* prove theorem Rep_i (Constr_j ...) = Inj_j ... *) |
|
504 |
||
505 |
fun prove_constr_rep_thm eqn = |
|
506 |
let |
|
21021 | 507 |
val inj_thms = map fst newT_iso_inj_thms; |
26359 | 508 |
val rewrites = @{thm o_def} :: constr_defs @ (map (mk_meta_eq o #2) newT_iso_axms) |
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
509 |
in Skip_Proof.prove_global thy5 [] [] eqn (fn _ => EVERY |
5177 | 510 |
[resolve_tac inj_thms 1, |
511 |
rewrite_goals_tac rewrites, |
|
21021 | 512 |
rtac refl 3, |
5177 | 513 |
resolve_tac rep_intrs 2, |
20046 | 514 |
REPEAT (resolve_tac iso_elem_thms 1)]) |
5177 | 515 |
end; |
516 |
||
517 |
(*--------------------------------------------------------------*) |
|
518 |
(* constr_rep_thms and rep_congs are used to prove distinctness *) |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
519 |
(* of constructors. *) |
5177 | 520 |
(*--------------------------------------------------------------*) |
521 |
||
522 |
val constr_rep_thms = map (map prove_constr_rep_thm) constr_rep_eqns; |
|
523 |
||
524 |
val dist_rewrites = map (fn (rep_thms, dist_lemma) => |
|
525 |
dist_lemma::(rep_thms @ [In0_eq, In1_eq, In0_not_In1, In1_not_In0])) |
|
526 |
(constr_rep_thms ~~ dist_lemmas); |
|
527 |
||
32900 | 528 |
fun prove_distinct_thms dist_rewrites' (k, ts) = |
529 |
let |
|
530 |
fun prove [] = [] |
|
531 |
| prove (t :: ts) = |
|
532 |
let |
|
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
533 |
val dist_thm = Skip_Proof.prove_global thy5 [] [] t (fn _ => |
32900 | 534 |
EVERY [simp_tac (HOL_ss addsimps dist_rewrites') 1]) |
32957 | 535 |
in dist_thm :: Drule.standard (dist_thm RS not_sym) :: prove ts end; |
32900 | 536 |
in prove ts end; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
537 |
|
32900 | 538 |
val distinct_thms = map2 (prove_distinct_thms) |
539 |
dist_rewrites (DatatypeProp.make_distincts descr sorts); |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6522
diff
changeset
|
540 |
|
5177 | 541 |
(* prove injectivity of constructors *) |
542 |
||
543 |
fun prove_constr_inj_thm rep_thms t = |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
544 |
let val inj_thms = Scons_inject :: (map make_elim |
21021 | 545 |
(iso_inj_thms @ |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
546 |
[In0_inject, In1_inject, Leaf_inject, Inl_inject, Inr_inject, |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
547 |
Lim_inject, Suml_inject, Sumr_inject])) |
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
548 |
in Skip_Proof.prove_global thy5 [] [] t (fn _ => EVERY |
5177 | 549 |
[rtac iffI 1, |
550 |
REPEAT (etac conjE 2), hyp_subst_tac 2, rtac refl 2, |
|
551 |
dresolve_tac rep_congs 1, dtac box_equals 1, |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
552 |
REPEAT (resolve_tac rep_thms 1), |
5177 | 553 |
REPEAT (eresolve_tac inj_thms 1), |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
554 |
REPEAT (ares_tac [conjI] 1 ORELSE (EVERY [REPEAT (rtac ext 1), |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
555 |
REPEAT (eresolve_tac (make_elim fun_cong :: inj_thms) 1), |
20046 | 556 |
atac 1]))]) |
5177 | 557 |
end; |
558 |
||
559 |
val constr_inject = map (fn (ts, thms) => map (prove_constr_inj_thm thms) ts) |
|
560 |
((DatatypeProp.make_injs descr sorts) ~~ constr_rep_thms); |
|
561 |
||
18314 | 562 |
val ((constr_inject', distinct_thms'), thy6) = |
563 |
thy5 |
|
32124 | 564 |
|> Sign.parent_path |
18314 | 565 |
|> store_thmss "inject" new_type_names constr_inject |
566 |
||>> store_thmss "distinct" new_type_names distinct_thms; |
|
5177 | 567 |
|
568 |
(*************************** induction theorem ****************************) |
|
569 |
||
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31643
diff
changeset
|
570 |
val _ = message config "Proving induction rule for datatypes ..."; |
5177 | 571 |
|
572 |
val Rep_inverse_thms = (map (fn (_, iso, _) => iso RS subst) newT_iso_axms) @ |
|
32999 | 573 |
(map (fn r => r RS @{thm the_inv_f_f} RS subst) iso_inj_thms_unfolded); |
574 |
val Rep_inverse_thms' = map (fn r => r RS @{thm the_inv_f_f}) iso_inj_thms_unfolded; |
|
5177 | 575 |
|
33244 | 576 |
fun mk_indrule_lemma ((i, _), T) (prems, concls) = |
5177 | 577 |
let |
31949 | 578 |
val Rep_t = Const (nth all_rep_names i, T --> Univ_elT) $ |
5177 | 579 |
mk_Free "x" T i; |
580 |
||
581 |
val Abs_t = if i < length newTs then |
|
22578 | 582 |
Const (Sign.intern_const thy6 |
31949 | 583 |
("Abs_" ^ (nth new_type_names i)), Univ_elT --> T) |
33057 | 584 |
else Const (@{const_name the_inv_into}, |
32999 | 585 |
[HOLogic.mk_setT T, T --> Univ_elT, Univ_elT] ---> T) $ |
586 |
HOLogic.mk_UNIV T $ Const (nth all_rep_names i, T --> Univ_elT) |
|
5177 | 587 |
|
21021 | 588 |
in (prems @ [HOLogic.imp $ |
31949 | 589 |
(Const (nth rep_set_names i, UnivT') $ Rep_t) $ |
5177 | 590 |
(mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ (Abs_t $ Rep_t))], |
591 |
concls @ [mk_Free "P" (T --> HOLogic.boolT) (i + 1) $ mk_Free "x" T i]) |
|
592 |
end; |
|
593 |
||
594 |
val (indrule_lemma_prems, indrule_lemma_concls) = |
|
33244 | 595 |
fold mk_indrule_lemma (descr' ~~ recTs) ([], []); |
5177 | 596 |
|
22578 | 597 |
val cert = cterm_of thy6; |
5177 | 598 |
|
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
599 |
val indrule_lemma = Skip_Proof.prove_global thy6 [] [] |
5177 | 600 |
(Logic.mk_implies |
601 |
(HOLogic.mk_Trueprop (mk_conj indrule_lemma_prems), |
|
17985 | 602 |
HOLogic.mk_Trueprop (mk_conj indrule_lemma_concls))) (fn _ => EVERY |
603 |
[REPEAT (etac conjE 1), |
|
5177 | 604 |
REPEAT (EVERY |
605 |
[TRY (rtac conjI 1), resolve_tac Rep_inverse_thms 1, |
|
20046 | 606 |
etac mp 1, resolve_tac iso_elem_thms 1])]); |
5177 | 607 |
|
8305 | 608 |
val Ps = map head_of (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule_lemma))); |
5177 | 609 |
val frees = if length Ps = 1 then [Free ("P", snd (dest_Var (hd Ps)))] else |
610 |
map (Free o apfst fst o dest_Var) Ps; |
|
611 |
val indrule_lemma' = cterm_instantiate (map cert Ps ~~ map cert frees) indrule_lemma; |
|
612 |
||
17985 | 613 |
val dt_induct_prop = DatatypeProp.make_ind descr sorts; |
32970
fbd2bb2489a8
operations of structure Skip_Proof (formerly SkipProof) no longer require quick_and_dirty mode;
wenzelm
parents:
32957
diff
changeset
|
614 |
val dt_induct = Skip_Proof.prove_global thy6 [] |
17985 | 615 |
(Logic.strip_imp_prems dt_induct_prop) (Logic.strip_imp_concl dt_induct_prop) |
26711 | 616 |
(fn {prems, ...} => EVERY |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
617 |
[rtac indrule_lemma' 1, |
25678 | 618 |
(indtac rep_induct [] THEN_ALL_NEW ObjectLogic.atomize_prems_tac) 1, |
5177 | 619 |
EVERY (map (fn (prem, r) => (EVERY |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
620 |
[REPEAT (eresolve_tac Abs_inverse_thms 1), |
5177 | 621 |
simp_tac (HOL_basic_ss addsimps ((symmetric r)::Rep_inverse_thms')) 1, |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
622 |
DEPTH_SOLVE_1 (ares_tac [prem] 1 ORELSE etac allE 1)])) |
20046 | 623 |
(prems ~~ (constr_defs @ (map mk_meta_eq iso_char_thms))))]); |
5177 | 624 |
|
18377 | 625 |
val ([dt_induct'], thy7) = |
626 |
thy6 |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24112
diff
changeset
|
627 |
|> Sign.add_path big_name |
29579 | 628 |
|> PureThy.add_thms [((Binding.name "induct", dt_induct), [case_names_induct])] |
28362
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
629 |
||> Sign.parent_path |
b0ebac91c8d5
explicit checkpoint for low-level (global) theory operations admits concurrent SkipProof.prove;
wenzelm
parents:
28084
diff
changeset
|
630 |
||> Theory.checkpoint; |
5177 | 631 |
|
18314 | 632 |
in |
32907 | 633 |
((constr_inject', distinct_thms', dt_induct'), thy7) |
5177 | 634 |
end; |
635 |
||
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
636 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
637 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
638 |
(** definitional introduction of datatypes **) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
639 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
640 |
fun gen_add_datatype prep_typ config new_type_names dts thy = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
641 |
let |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
642 |
val _ = Theory.requires thy "Datatype" "datatype definitions"; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
643 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
644 |
(* this theory is used just for parsing *) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
645 |
val tmp_thy = thy |> |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
646 |
Theory.copy |> |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
647 |
Sign.add_types (map (fn (tvs, tname, mx, _) => |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
648 |
(tname, length tvs, mx)) dts); |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
649 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
650 |
val (tyvars, _, _, _)::_ = dts; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
651 |
val (new_dts, types_syntax) = ListPair.unzip (map (fn (tvs, tname, mx, _) => |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
652 |
let val full_tname = Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
653 |
in |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
654 |
(case duplicates (op =) tvs of |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
655 |
[] => |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
656 |
if eq_set (op =) (tyvars, tvs) then ((full_tname, tvs), (tname, mx)) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
657 |
else error ("Mutually recursive datatypes must have same type parameters") |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
658 |
| dups => error ("Duplicate parameter(s) for datatype " ^ quote (Binding.str_of tname) ^ |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
659 |
" : " ^ commas dups)) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
660 |
end) dts); |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
661 |
val dt_names = map fst new_dts; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
662 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
663 |
val _ = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
664 |
(case duplicates (op =) (map fst new_dts) @ duplicates (op =) new_type_names of |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
665 |
[] => () |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
666 |
| dups => error ("Duplicate datatypes: " ^ commas dups)); |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
667 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
668 |
fun prep_dt_spec (tvs, tname, mx, constrs) tname' (dts', constr_syntax, sorts, i) = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
669 |
let |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
670 |
fun prep_constr (cname, cargs, mx') (constrs, constr_syntax', sorts') = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
671 |
let |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
672 |
val (cargs', sorts'') = fold_map (prep_typ tmp_thy) cargs sorts'; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
673 |
val _ = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
674 |
(case subtract (op =) tvs (fold (curry OldTerm.add_typ_tfree_names) cargs' []) of |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
675 |
[] => () |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
676 |
| vs => error ("Extra type variables on rhs: " ^ commas vs)) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
677 |
in (constrs @ [(Sign.full_name_path tmp_thy tname' |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
678 |
(Binding.map_name (Syntax.const_name mx') cname), |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
679 |
map (dtyp_of_typ new_dts) cargs')], |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
680 |
constr_syntax' @ [(cname, mx')], sorts'') |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
681 |
end handle ERROR msg => cat_error msg |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
682 |
("The error above occured in constructor " ^ quote (Binding.str_of cname) ^ |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
683 |
" of datatype " ^ quote (Binding.str_of tname)); |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
684 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
685 |
val (constrs', constr_syntax', sorts') = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
686 |
fold prep_constr constrs ([], [], sorts) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
687 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
688 |
in |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
689 |
case duplicates (op =) (map fst constrs') of |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
690 |
[] => |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
691 |
(dts' @ [(i, (Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname), |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
692 |
map DtTFree tvs, constrs'))], |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
693 |
constr_syntax @ [constr_syntax'], sorts', i + 1) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
694 |
| dups => error ("Duplicate constructors " ^ commas dups ^ |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
695 |
" in datatype " ^ quote (Binding.str_of tname)) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
696 |
end; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
697 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
698 |
val (dts', constr_syntax, sorts', i) = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
699 |
fold2 prep_dt_spec dts new_type_names ([], [], [], 0); |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
700 |
val sorts = sorts' @ map (rpair (Sign.defaultS tmp_thy)) (subtract (op =) (map fst sorts') tyvars); |
33963
977b94b64905
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33959
diff
changeset
|
701 |
val dt_info = Datatype_Data.get_all thy; |
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
702 |
val (descr, _) = unfold_datatypes tmp_thy dts' sorts dt_info dts' i; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
703 |
val _ = check_nonempty descr handle (exn as Datatype_Empty s) => |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
704 |
if #strict config then error ("Nonemptiness check failed for datatype " ^ s) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
705 |
else raise exn; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
706 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
707 |
val _ = message config ("Constructing datatype(s) " ^ commas_quote new_type_names); |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
708 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
709 |
in |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
710 |
thy |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
711 |
|> representation_proofs config dt_info new_type_names descr sorts |
33963
977b94b64905
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33959
diff
changeset
|
712 |
types_syntax constr_syntax (Datatype_Data.mk_case_names_induct (flat descr)) |
977b94b64905
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33959
diff
changeset
|
713 |
|-> (fn (inject, distinct, induct) => Datatype_Data.derive_datatype_props |
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
714 |
config dt_names (SOME new_type_names) descr sorts |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
715 |
induct inject distinct) |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
716 |
end; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
717 |
|
33963
977b94b64905
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33959
diff
changeset
|
718 |
val add_datatype = gen_add_datatype Datatype_Data.cert_typ; |
977b94b64905
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33959
diff
changeset
|
719 |
val datatype_cmd = snd ooo gen_add_datatype Datatype_Data.read_typ default_config; |
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
720 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
721 |
local |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
722 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
723 |
structure P = OuterParse and K = OuterKeyword |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
724 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
725 |
fun prep_datatype_decls args = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
726 |
let |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
727 |
val names = map |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
728 |
(fn ((((NONE, _), t), _), _) => Binding.name_of t | ((((SOME t, _), _), _), _) => t) args; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
729 |
val specs = map (fn ((((_, vs), t), mx), cons) => |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
730 |
(vs, t, mx, map (fn ((x, y), z) => (x, y, z)) cons)) args; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
731 |
in (names, specs) end; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
732 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
733 |
val parse_datatype_decl = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
734 |
(Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.type_args -- P.binding -- P.opt_infix -- |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
735 |
(P.$$$ "=" |-- P.enum1 "|" (P.binding -- Scan.repeat P.typ -- P.opt_mixfix))); |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
736 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
737 |
val parse_datatype_decls = P.and_list1 parse_datatype_decl >> prep_datatype_decls; |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
738 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
739 |
in |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
740 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
741 |
val _ = |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
742 |
OuterSyntax.command "datatype" "define inductive datatypes" K.thy_decl |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
743 |
(parse_datatype_decls >> (fn (names, specs) => Toplevel.theory (datatype_cmd names specs))); |
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
744 |
|
5177 | 745 |
end; |
33959
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
746 |
|
2afc55e8ed27
bootstrap datatype_rep_proofs in Datatype.thy (avoids unchecked dynamic name references)
haftmann
parents:
33957
diff
changeset
|
747 |
end; |