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