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