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