author | wenzelm |
Fri, 02 Dec 2011 16:37:35 +0100 | |
changeset 45743 | 857b7fcb0365 |
parent 45739 | b545ea8bc731 |
child 45821 | c2f6c50e3d42 |
permissions | -rw-r--r-- |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
1 |
(* Title: HOL/Tools/Datatype/datatype_prop.ML |
11539 | 2 |
Author: Stefan Berghofer, TU Muenchen |
5177 | 3 |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
4 |
Datatype package: characteristic properties of datatypes. |
5177 | 5 |
*) |
6 |
||
7 |
signature DATATYPE_PROP = |
|
8 |
sig |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
9 |
include DATATYPE_COMMON |
8434 | 10 |
val indexify_names: string list -> string list |
13465 | 11 |
val make_tnames: typ list -> string list |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
12 |
val make_injs : descr list -> (string * sort) list -> term list list |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
13 |
val make_distincts : descr list -> |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
14 |
(string * sort) list -> (int * term list) list (*no symmetric inequalities*) |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
15 |
val make_ind : descr list -> (string * sort) list -> term |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
16 |
val make_casedists : descr list -> (string * sort) list -> term list |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
17 |
val make_primrec_Ts : descr list -> (string * sort) list -> |
15459
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
18 |
string list -> typ list * typ list |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
19 |
val make_primrecs : string list -> descr list -> |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
20 |
(string * sort) list -> theory -> term list |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
21 |
val make_cases : string list -> descr list -> |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
22 |
(string * sort) list -> theory -> term list list |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
23 |
val make_splits : string list -> descr list -> |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
24 |
(string * sort) list -> theory -> (term * term) list |
35324
c9f428269b38
adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents:
33968
diff
changeset
|
25 |
val make_case_combs : string list -> descr list -> |
c9f428269b38
adopting mutabelle and quickcheck to return timing information; exporting make_case_combs in datatype package for predicate compiler; adding Spec_Rules declaration for tail recursive functions; improving the predicate compiler and function flattening
bulwahn
parents:
33968
diff
changeset
|
26 |
(string * sort) list -> theory -> string -> term list |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
27 |
val make_weak_case_congs : string list -> descr list -> |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
28 |
(string * sort) list -> theory -> term list |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
29 |
val make_case_congs : string list -> descr list -> |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
30 |
(string * sort) list -> theory -> term list |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
31 |
val make_nchotomys : descr list -> |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
32 |
(string * sort) list -> term list |
5177 | 33 |
end; |
34 |
||
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
35 |
structure Datatype_Prop : DATATYPE_PROP = |
5177 | 36 |
struct |
37 |
||
8434 | 38 |
fun indexify_names names = |
39 |
let |
|
40 |
fun index (x :: xs) tab = |
|
45739 | 41 |
(case AList.lookup (op =) tab x of |
42 |
NONE => |
|
43 |
if member (op =) xs x |
|
44 |
then (x ^ "1") :: index xs ((x, 2) :: tab) |
|
45 |
else x :: index xs tab |
|
46 |
| SOME i => (x ^ string_of_int i) :: index xs ((x, i + 1) :: tab)) |
|
47 |
| index [] _ = []; |
|
8434 | 48 |
in index names [] end; |
49 |
||
5177 | 50 |
fun make_tnames Ts = |
51 |
let |
|
40720 | 52 |
fun type_name (TFree (name, _)) = unprefix "'" name |
53 |
| type_name (Type (name, _)) = |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
54 |
let val name' = Long_Name.base_name name |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
41423
diff
changeset
|
55 |
in if Lexicon.is_identifier name' then name' else "x" end; |
8434 | 56 |
in indexify_names (map type_name Ts) end; |
5177 | 57 |
|
58 |
||
59 |
(************************* injectivity of constructors ************************) |
|
60 |
||
61 |
fun make_injs descr sorts = |
|
62 |
let |
|
21078 | 63 |
val descr' = flat descr; |
64 |
fun make_inj T (cname, cargs) = |
|
45700 | 65 |
if null cargs then I |
66 |
else |
|
5177 | 67 |
let |
41423 | 68 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs; |
5177 | 69 |
val constr_t = Const (cname, Ts ---> T); |
70 |
val tnames = make_tnames Ts; |
|
71 |
val frees = map Free (tnames ~~ Ts); |
|
45743 | 72 |
val frees' = map Free (map (suffix "'") tnames ~~ Ts); |
73 |
in |
|
74 |
cons (HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
75 |
(HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')), |
|
76 |
foldr1 (HOLogic.mk_binop @{const_name HOL.conj}) |
|
77 |
(map HOLogic.mk_eq (frees ~~ frees'))))) |
|
5177 | 78 |
end; |
21078 | 79 |
in |
80 |
map2 (fn d => fn T => fold_rev (make_inj T) (#3 (snd d)) []) |
|
41423 | 81 |
(hd descr) (take (length (hd descr)) (Datatype_Aux.get_rec_types descr' sorts)) |
5177 | 82 |
end; |
83 |
||
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
84 |
|
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
85 |
(************************* distinctness of constructors ***********************) |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
86 |
|
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
87 |
fun make_distincts descr sorts = |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
88 |
let |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
89 |
val descr' = flat descr; |
41423 | 90 |
val recTs = Datatype_Aux.get_rec_types descr' sorts; |
33957 | 91 |
val newTs = take (length (hd descr)) recTs; |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
92 |
|
45700 | 93 |
fun prep_constr (cname, cargs) = |
94 |
(cname, map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs); |
|
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
95 |
|
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
96 |
fun make_distincts' _ [] = [] |
45700 | 97 |
| make_distincts' T ((cname, cargs) :: constrs) = |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
98 |
let |
45743 | 99 |
val frees = map Free (make_tnames cargs ~~ cargs); |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
100 |
val t = list_comb (Const (cname, cargs ---> T), frees); |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
101 |
|
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
102 |
fun make_distincts'' (cname', cargs') = |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
103 |
let |
45700 | 104 |
val frees' = map Free (map (suffix "'") (make_tnames cargs') ~~ cargs'); |
105 |
val t' = list_comb (Const (cname', cargs' ---> T), frees'); |
|
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
106 |
in |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
107 |
HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t')) |
45700 | 108 |
end; |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
109 |
in map make_distincts'' constrs @ make_distincts' T constrs end; |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
110 |
in |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
111 |
map2 (fn ((_, (_, _, constrs))) => fn T => |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
112 |
(length constrs, make_distincts' T (map prep_constr constrs))) (hd descr) newTs |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
113 |
end; |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
114 |
|
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
115 |
|
5177 | 116 |
(********************************* induction **********************************) |
117 |
||
118 |
fun make_ind descr sorts = |
|
119 |
let |
|
32952 | 120 |
val descr' = flat descr; |
41423 | 121 |
val recTs = Datatype_Aux.get_rec_types descr' sorts; |
122 |
val pnames = |
|
123 |
if length descr' = 1 then ["P"] |
|
5177 | 124 |
else map (fn i => "P" ^ string_of_int i) (1 upto length descr'); |
125 |
||
126 |
fun make_pred i T = |
|
127 |
let val T' = T --> HOLogic.boolT |
|
42364 | 128 |
in Free (nth pnames i, T') end; |
5177 | 129 |
|
130 |
fun make_ind_prem k T (cname, cargs) = |
|
131 |
let |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
132 |
fun mk_prem ((dt, s), T) = |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
133 |
let val (Us, U) = strip_type T |
41423 | 134 |
in |
135 |
list_all (map (pair "x") Us, |
|
136 |
HOLogic.mk_Trueprop |
|
137 |
(make_pred (Datatype_Aux.body_index dt) U $ |
|
138 |
Datatype_Aux.app_bnds (Free (s, T)) (length Us))) |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
139 |
end; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
140 |
|
41423 | 141 |
val recs = filter Datatype_Aux.is_rec_type cargs; |
142 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs; |
|
143 |
val recTs' = map (Datatype_Aux.typ_of_dtyp descr' sorts) recs; |
|
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
144 |
val tnames = Name.variant_list pnames (make_tnames Ts); |
41423 | 145 |
val rec_tnames = map fst (filter (Datatype_Aux.is_rec_type o snd) (tnames ~~ cargs)); |
5177 | 146 |
val frees = tnames ~~ Ts; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
147 |
val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs'); |
45700 | 148 |
in |
149 |
list_all_free (frees, |
|
150 |
Logic.list_implies (prems, |
|
151 |
HOLogic.mk_Trueprop (make_pred k T $ |
|
152 |
list_comb (Const (cname, Ts ---> T), map Free frees)))) |
|
5177 | 153 |
end; |
154 |
||
45700 | 155 |
val prems = |
156 |
maps (fn ((i, (_, _, constrs)), T) => map (make_ind_prem i T) constrs) (descr' ~~ recTs); |
|
5177 | 157 |
val tnames = make_tnames recTs; |
45700 | 158 |
val concl = |
159 |
HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop @{const_name HOL.conj}) |
|
160 |
(map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T)) |
|
161 |
(descr' ~~ recTs ~~ tnames))); |
|
5177 | 162 |
|
163 |
in Logic.list_implies (prems, concl) end; |
|
164 |
||
165 |
(******************************* case distinction *****************************) |
|
166 |
||
167 |
fun make_casedists descr sorts = |
|
168 |
let |
|
32952 | 169 |
val descr' = flat descr; |
5177 | 170 |
|
171 |
fun make_casedist_prem T (cname, cargs) = |
|
172 |
let |
|
41423 | 173 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs; |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
174 |
val frees = Name.variant_list ["P", "y"] (make_tnames Ts) ~~ Ts; |
45700 | 175 |
val free_ts = map Free frees; |
176 |
in |
|
177 |
list_all_free (frees, |
|
178 |
Logic.mk_implies (HOLogic.mk_Trueprop |
|
179 |
(HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))), |
|
180 |
HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))) |
|
5177 | 181 |
end; |
182 |
||
33957 | 183 |
fun make_casedist ((_, (_, _, constrs))) T = |
5177 | 184 |
let val prems = map (make_casedist_prem T) constrs |
45700 | 185 |
in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))) end; |
5177 | 186 |
|
41423 | 187 |
in |
188 |
map2 make_casedist (hd descr) |
|
189 |
(take (length (hd descr)) (Datatype_Aux.get_rec_types descr' sorts)) |
|
190 |
end; |
|
5177 | 191 |
|
192 |
(*************** characteristic equations for primrec combinator **************) |
|
193 |
||
15459
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
194 |
fun make_primrec_Ts descr sorts used = |
5177 | 195 |
let |
32952 | 196 |
val descr' = flat descr; |
5177 | 197 |
|
45700 | 198 |
val rec_result_Ts = |
199 |
map TFree |
|
200 |
(Name.variant_list used (replicate (length descr') "'t") ~~ |
|
201 |
replicate (length descr') HOLogic.typeS); |
|
5177 | 202 |
|
32952 | 203 |
val reccomb_fn_Ts = maps (fn (i, (_, _, constrs)) => |
5177 | 204 |
map (fn (_, cargs) => |
205 |
let |
|
41423 | 206 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs; |
207 |
val recs = filter (Datatype_Aux.is_rec_type o fst) (cargs ~~ Ts); |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
208 |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
209 |
fun mk_argT (dt, T) = |
42364 | 210 |
binder_types T ---> nth rec_result_Ts (Datatype_Aux.body_index dt); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
211 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
212 |
val argTs = Ts @ map mk_argT recs |
42364 | 213 |
in argTs ---> nth rec_result_Ts i end) constrs) descr'; |
5177 | 214 |
|
15459
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
215 |
in (rec_result_Ts, reccomb_fn_Ts) end; |
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
216 |
|
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
217 |
fun make_primrecs new_type_names descr sorts thy = |
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
218 |
let |
32952 | 219 |
val descr' = flat descr; |
41423 | 220 |
val recTs = Datatype_Aux.get_rec_types descr' sorts; |
45738 | 221 |
val used = fold Term.add_tfree_namesT recTs []; |
15459
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
222 |
|
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
223 |
val (rec_result_Ts, reccomb_fn_Ts) = make_primrec_Ts descr sorts used; |
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
224 |
|
45700 | 225 |
val rec_fns = |
226 |
map (uncurry (Datatype_Aux.mk_Free "f")) |
|
227 |
(reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts))); |
|
5177 | 228 |
|
45229 | 229 |
val big_reccomb_name = space_implode "_" new_type_names ^ "_rec"; |
45700 | 230 |
val reccomb_names = |
231 |
map (Sign.intern_const thy) |
|
232 |
(if length descr' = 1 then [big_reccomb_name] |
|
233 |
else (map (prefix (big_reccomb_name ^ "_") o string_of_int) (1 upto (length descr')))); |
|
234 |
val reccombs = |
|
235 |
map (fn ((name, T), T') => list_comb (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns)) |
|
5177 | 236 |
(reccomb_names ~~ recTs ~~ rec_result_Ts); |
237 |
||
45700 | 238 |
fun make_primrec T comb_t (cname, cargs) (ts, f :: fs) = |
5177 | 239 |
let |
41423 | 240 |
val recs = filter Datatype_Aux.is_rec_type cargs; |
241 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs; |
|
242 |
val recTs' = map (Datatype_Aux.typ_of_dtyp descr' sorts) recs; |
|
5177 | 243 |
val tnames = make_tnames Ts; |
41423 | 244 |
val rec_tnames = map fst (filter (Datatype_Aux.is_rec_type o snd) (tnames ~~ cargs)); |
5177 | 245 |
val frees = map Free (tnames ~~ Ts); |
246 |
val frees' = map Free (rec_tnames ~~ recTs'); |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
247 |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
248 |
fun mk_reccomb ((dt, T), t) = |
42364 | 249 |
let val (Us, U) = strip_type T in |
250 |
list_abs (map (pair "x") Us, |
|
251 |
nth reccombs (Datatype_Aux.body_index dt) $ Datatype_Aux.app_bnds t (length Us)) |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
252 |
end; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
253 |
|
45700 | 254 |
val reccombs' = map mk_reccomb (recs ~~ recTs' ~~ frees'); |
5177 | 255 |
|
45700 | 256 |
in |
257 |
(ts @ [HOLogic.mk_Trueprop |
|
258 |
(HOLogic.mk_eq (comb_t $ list_comb (Const (cname, Ts ---> T), frees), |
|
259 |
list_comb (f, frees @ reccombs')))], fs) |
|
33244 | 260 |
end; |
261 |
in |
|
262 |
fold (fn ((dt, T), comb_t) => fold (make_primrec T comb_t) (#3 (snd dt))) |
|
263 |
(descr' ~~ recTs ~~ reccombs) ([], rec_fns) |
|
264 |
|> fst |
|
5177 | 265 |
end; |
266 |
||
267 |
(****************** make terms of form t_case f1 ... fn *********************) |
|
268 |
||
269 |
fun make_case_combs new_type_names descr sorts thy fname = |
|
270 |
let |
|
32952 | 271 |
val descr' = flat descr; |
41423 | 272 |
val recTs = Datatype_Aux.get_rec_types descr' sorts; |
45738 | 273 |
val used = fold Term.add_tfree_namesT recTs []; |
33957 | 274 |
val newTs = take (length (hd descr)) recTs; |
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
42364
diff
changeset
|
275 |
val T' = TFree (singleton (Name.variant_list used) "'t", HOLogic.typeS); |
5177 | 276 |
|
277 |
val case_fn_Ts = map (fn (i, (_, _, constrs)) => |
|
278 |
map (fn (_, cargs) => |
|
41423 | 279 |
let val Ts = map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs |
5177 | 280 |
in Ts ---> T' end) constrs) (hd descr); |
281 |
||
45700 | 282 |
val case_names = map (fn s => Sign.intern_const thy (s ^ "_case")) new_type_names; |
5177 | 283 |
in |
284 |
map (fn ((name, Ts), T) => list_comb |
|
285 |
(Const (name, Ts @ [T] ---> T'), |
|
41423 | 286 |
map (uncurry (Datatype_Aux.mk_Free fname)) (Ts ~~ (1 upto length Ts)))) |
5177 | 287 |
(case_names ~~ case_fn_Ts ~~ newTs) |
288 |
end; |
|
289 |
||
290 |
(**************** characteristic equations for case combinator ****************) |
|
291 |
||
292 |
fun make_cases new_type_names descr sorts thy = |
|
293 |
let |
|
32952 | 294 |
val descr' = flat descr; |
41423 | 295 |
val recTs = Datatype_Aux.get_rec_types descr' sorts; |
33957 | 296 |
val newTs = take (length (hd descr)) recTs; |
5177 | 297 |
|
298 |
fun make_case T comb_t ((cname, cargs), f) = |
|
299 |
let |
|
41423 | 300 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs; |
45700 | 301 |
val frees = map Free ((make_tnames Ts) ~~ Ts); |
302 |
in |
|
303 |
HOLogic.mk_Trueprop |
|
304 |
(HOLogic.mk_eq (comb_t $ list_comb (Const (cname, Ts ---> T), frees), |
|
305 |
list_comb (f, frees))) |
|
306 |
end; |
|
307 |
in |
|
308 |
map (fn (((_, (_, _, constrs)), T), comb_t) => |
|
309 |
map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t)))) |
|
310 |
((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f")) |
|
5177 | 311 |
end; |
312 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
313 |
|
5177 | 314 |
(*************************** the "split" - equations **************************) |
315 |
||
316 |
fun make_splits new_type_names descr sorts thy = |
|
317 |
let |
|
32952 | 318 |
val descr' = flat descr; |
41423 | 319 |
val recTs = Datatype_Aux.get_rec_types descr' sorts; |
45738 | 320 |
val used' = fold Term.add_tfree_namesT recTs []; |
33957 | 321 |
val newTs = take (length (hd descr)) recTs; |
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
42364
diff
changeset
|
322 |
val T' = TFree (singleton (Name.variant_list used') "'t", HOLogic.typeS); |
5177 | 323 |
val P = Free ("P", T' --> HOLogic.boolT); |
324 |
||
325 |
fun make_split (((_, (_, _, constrs)), T), comb_t) = |
|
326 |
let |
|
327 |
val (_, fs) = strip_comb comb_t; |
|
45700 | 328 |
val used = ["P", "x"] @ map (fst o dest_Free) fs; |
5177 | 329 |
|
33338 | 330 |
fun process_constr ((cname, cargs), f) (t1s, t2s) = |
5177 | 331 |
let |
41423 | 332 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs; |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
333 |
val frees = map Free (Name.variant_list used (make_tnames Ts) ~~ Ts); |
45700 | 334 |
val eqn = HOLogic.mk_eq (Free ("x", T), list_comb (Const (cname, Ts ---> T), frees)); |
335 |
val P' = P $ list_comb (f, frees); |
|
336 |
in |
|
337 |
(fold_rev (fn Free (s, T) => fn t => HOLogic.mk_all (s, T, t)) frees |
|
338 |
(HOLogic.imp $ eqn $ P') :: t1s, |
|
339 |
fold_rev (fn Free (s, T) => fn t => HOLogic.mk_exists (s, T, t)) frees |
|
340 |
(HOLogic.conj $ eqn $ (HOLogic.Not $ P')) :: t2s) |
|
5177 | 341 |
end; |
342 |
||
33338 | 343 |
val (t1s, t2s) = fold_rev process_constr (constrs ~~ fs) ([], []); |
45700 | 344 |
val lhs = P $ (comb_t $ Free ("x", T)); |
5177 | 345 |
in |
41423 | 346 |
(HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, Datatype_Aux.mk_conj t1s)), |
347 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ Datatype_Aux.mk_disj t2s))) |
|
5177 | 348 |
end |
349 |
||
45700 | 350 |
in |
45743 | 351 |
map make_split (hd descr ~~ newTs ~~ make_case_combs new_type_names descr sorts thy "f") |
5177 | 352 |
end; |
353 |
||
354 |
(************************* additional rules for TFL ***************************) |
|
355 |
||
8601 | 356 |
fun make_weak_case_congs new_type_names descr sorts thy = |
357 |
let |
|
358 |
val case_combs = make_case_combs new_type_names descr sorts thy "f"; |
|
359 |
||
360 |
fun mk_case_cong comb = |
|
45700 | 361 |
let |
8601 | 362 |
val Type ("fun", [T, _]) = fastype_of comb; |
363 |
val M = Free ("M", T); |
|
364 |
val M' = Free ("M'", T); |
|
365 |
in |
|
366 |
Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')), |
|
367 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M'))) |
|
45700 | 368 |
end; |
8601 | 369 |
in |
370 |
map mk_case_cong case_combs |
|
371 |
end; |
|
45700 | 372 |
|
8601 | 373 |
|
5177 | 374 |
(*--------------------------------------------------------------------------- |
375 |
* Structure of case congruence theorem looks like this: |
|
376 |
* |
|
45700 | 377 |
* (M = M') |
378 |
* ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) |
|
379 |
* ==> ... |
|
380 |
* ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) |
|
5177 | 381 |
* ==> |
382 |
* (ty_case f1..fn M = ty_case g1..gn M') |
|
383 |
*---------------------------------------------------------------------------*) |
|
384 |
||
385 |
fun make_case_congs new_type_names descr sorts thy = |
|
386 |
let |
|
387 |
val case_combs = make_case_combs new_type_names descr sorts thy "f"; |
|
388 |
val case_combs' = make_case_combs new_type_names descr sorts thy "g"; |
|
389 |
||
390 |
fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) = |
|
391 |
let |
|
392 |
val Type ("fun", [T, _]) = fastype_of comb; |
|
393 |
val (_, fs) = strip_comb comb; |
|
394 |
val (_, gs) = strip_comb comb'; |
|
395 |
val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs); |
|
396 |
val M = Free ("M", T); |
|
397 |
val M' = Free ("M'", T); |
|
398 |
||
399 |
fun mk_clause ((f, g), (cname, _)) = |
|
400 |
let |
|
40844 | 401 |
val Ts = binder_types (fastype_of f); |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
402 |
val tnames = Name.variant_list used (make_tnames Ts); |
45743 | 403 |
val frees = map Free (tnames ~~ Ts); |
5177 | 404 |
in |
405 |
list_all_free (tnames ~~ Ts, Logic.mk_implies |
|
406 |
(HOLogic.mk_Trueprop |
|
407 |
(HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))), |
|
408 |
HOLogic.mk_Trueprop |
|
409 |
(HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees))))) |
|
45700 | 410 |
end; |
5177 | 411 |
in |
412 |
Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) :: |
|
413 |
map mk_clause (fs ~~ gs ~~ constrs), |
|
414 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M'))) |
|
45700 | 415 |
end; |
5177 | 416 |
in |
417 |
map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr) |
|
418 |
end; |
|
419 |
||
420 |
(*--------------------------------------------------------------------------- |
|
421 |
* Structure of exhaustion theorem looks like this: |
|
422 |
* |
|
423 |
* !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj) |
|
424 |
*---------------------------------------------------------------------------*) |
|
425 |
||
426 |
fun make_nchotomys descr sorts = |
|
427 |
let |
|
32952 | 428 |
val descr' = flat descr; |
41423 | 429 |
val recTs = Datatype_Aux.get_rec_types descr' sorts; |
33957 | 430 |
val newTs = take (length (hd descr)) recTs; |
5177 | 431 |
|
432 |
fun mk_eqn T (cname, cargs) = |
|
433 |
let |
|
41423 | 434 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr' sorts) cargs; |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
435 |
val tnames = Name.variant_list ["v"] (make_tnames Ts); |
45743 | 436 |
val frees = tnames ~~ Ts; |
5177 | 437 |
in |
33338 | 438 |
fold_rev (fn (s, T') => fn t => HOLogic.mk_exists (s, T', t)) frees |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
439 |
(HOLogic.mk_eq (Free ("v", T), |
33338 | 440 |
list_comb (Const (cname, Ts ---> T), map Free frees))) |
45700 | 441 |
end; |
442 |
in |
|
443 |
map (fn ((_, (_, _, constrs)), T) => |
|
444 |
HOLogic.mk_Trueprop |
|
445 |
(HOLogic.mk_all ("v", T, Datatype_Aux.mk_disj (map (mk_eqn T) constrs)))) |
|
5177 | 446 |
(hd descr ~~ newTs) |
447 |
end; |
|
448 |
||
41423 | 449 |
|
450 |
open Datatype_Aux; |
|
451 |
||
5177 | 452 |
end; |