author | wenzelm |
Mon, 13 Mar 2000 13:21:39 +0100 | |
changeset 8434 | 5e4bba59bfaa |
parent 7704 | 9a6783fdb9a5 |
child 8601 | 8fb3a81b4ccf |
permissions | -rw-r--r-- |
5177 | 1 |
(* Title: HOL/Tools/datatype_prop.ML |
2 |
ID: $Id$ |
|
3 |
Author: Stefan Berghofer |
|
4 |
Copyright 1998 TU Muenchen |
|
5 |
||
6 |
Characteristic properties of datatypes |
|
7 |
*) |
|
8 |
||
9 |
signature DATATYPE_PROP = |
|
10 |
sig |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
11 |
val dtK : int ref |
8434 | 12 |
val indexify_names: string list -> string list |
5177 | 13 |
val make_injs : (int * (string * DatatypeAux.dtyp list * |
14 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
15 |
term list list |
|
16 |
val make_ind : (int * (string * DatatypeAux.dtyp list * |
|
17 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term |
|
18 |
val make_casedists : (int * (string * DatatypeAux.dtyp list * |
|
19 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term list |
|
20 |
val make_primrecs : string list -> (int * (string * DatatypeAux.dtyp list * |
|
21 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
22 |
theory -> term list |
|
23 |
val make_cases : string list -> (int * (string * DatatypeAux.dtyp list * |
|
24 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
25 |
theory -> term list list |
|
26 |
val make_distincts : string list -> (int * (string * DatatypeAux.dtyp list * |
|
27 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
28 |
theory -> term list list |
|
29 |
val make_splits : string list -> (int * (string * DatatypeAux.dtyp list * |
|
30 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
31 |
theory -> (term * term) list |
|
32 |
val make_case_trrules : string list -> (int * (string * DatatypeAux.dtyp list * |
|
33 |
(string * DatatypeAux.dtyp list) list)) list list -> ast Syntax.trrule list |
|
34 |
val make_size : string list -> (int * (string * DatatypeAux.dtyp list * |
|
35 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
36 |
theory -> term list |
|
37 |
val make_case_congs : string list -> (int * (string * DatatypeAux.dtyp list * |
|
38 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> |
|
39 |
theory -> term list |
|
40 |
val make_nchotomys : (int * (string * DatatypeAux.dtyp list * |
|
41 |
(string * DatatypeAux.dtyp list) list)) list list -> (string * sort) list -> term list |
|
42 |
end; |
|
43 |
||
44 |
structure DatatypeProp : DATATYPE_PROP = |
|
45 |
struct |
|
46 |
||
47 |
open DatatypeAux; |
|
48 |
||
49 |
(*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
|
50 |
val dtK = ref 7; |
5177 | 51 |
|
8434 | 52 |
fun indexify_names names = |
53 |
let |
|
54 |
fun index (x :: xs) tab = |
|
55 |
(case assoc (tab, x) of |
|
56 |
None => if x mem xs then (x ^ "1") :: index xs ((x, 2) :: tab) else x :: index xs tab |
|
57 |
| Some i => (x ^ Library.string_of_int i) :: index xs ((x, i + 1) :: tab)) |
|
58 |
| index [] _ = []; |
|
59 |
in index names [] end; |
|
60 |
||
5177 | 61 |
fun make_tnames Ts = |
62 |
let |
|
63 |
fun type_name (TFree (name, _)) = implode (tl (explode name)) |
|
64 |
| type_name (Type (name, _)) = |
|
65 |
let val name' = Sign.base_name name |
|
8434 | 66 |
in if Syntax.is_identifier name' then name' else "x" end; |
67 |
in indexify_names (map type_name Ts) end; |
|
5177 | 68 |
|
69 |
||
70 |
||
71 |
(************************* injectivity of constructors ************************) |
|
72 |
||
73 |
fun make_injs descr sorts = |
|
74 |
let |
|
75 |
val descr' = flat descr; |
|
76 |
||
77 |
fun make_inj T ((cname, cargs), injs) = |
|
78 |
if null cargs then injs else |
|
79 |
let |
|
80 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
81 |
val constr_t = Const (cname, Ts ---> T); |
|
82 |
val tnames = make_tnames Ts; |
|
83 |
val frees = map Free (tnames ~~ Ts); |
|
84 |
val frees' = map Free ((map ((op ^) o (rpair "'")) tnames) ~~ Ts); |
|
85 |
in (HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
86 |
(HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')), |
|
87 |
foldr1 (HOLogic.mk_binop "op &") |
|
88 |
(map HOLogic.mk_eq (frees ~~ frees')))))::injs |
|
89 |
end; |
|
90 |
||
91 |
in map (fn (d, T) => foldr (make_inj T) (#3 (snd d), [])) |
|
92 |
((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts)) |
|
93 |
end; |
|
94 |
||
95 |
(********************************* induction **********************************) |
|
96 |
||
97 |
fun make_ind descr sorts = |
|
98 |
let |
|
99 |
val descr' = flat descr; |
|
100 |
val recTs = get_rec_types descr' sorts; |
|
101 |
val pnames = if length descr' = 1 then ["P"] |
|
102 |
else map (fn i => "P" ^ string_of_int i) (1 upto length descr'); |
|
103 |
||
104 |
fun make_pred i T = |
|
105 |
let val T' = T --> HOLogic.boolT |
|
106 |
in Free (nth_elem (i, pnames), T') end; |
|
107 |
||
108 |
fun make_ind_prem k T (cname, cargs) = |
|
109 |
let |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
110 |
fun mk_prem ((DtRec k, s), T) = HOLogic.mk_Trueprop |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
111 |
(make_pred k T $ Free (s, T)) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
112 |
| mk_prem ((DtType ("fun", [_, DtRec k]), s), T' as Type ("fun", [T, U])) = |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
113 |
HOLogic.mk_Trueprop (HOLogic.all_const T $ |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
114 |
Abs ("x", T, make_pred k U $ (Free (s, T') $ Bound 0))); |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
115 |
|
5177 | 116 |
val recs = filter is_rec_type cargs; |
117 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
118 |
val recTs' = map (typ_of_dtyp descr' sorts) recs; |
|
119 |
val tnames = variantlist (make_tnames Ts, pnames); |
|
120 |
val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs)); |
|
121 |
val frees = tnames ~~ Ts; |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
122 |
val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs'); |
5177 | 123 |
|
124 |
in list_all_free (frees, Logic.list_implies (prems, |
|
125 |
HOLogic.mk_Trueprop (make_pred k T $ |
|
126 |
list_comb (Const (cname, Ts ---> T), map Free frees)))) |
|
127 |
end; |
|
128 |
||
129 |
val prems = flat (map (fn ((i, (_, _, constrs)), T) => |
|
130 |
map (make_ind_prem i T) constrs) (descr' ~~ recTs)); |
|
131 |
val tnames = make_tnames recTs; |
|
132 |
val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &") |
|
133 |
(map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T)) |
|
134 |
(descr' ~~ recTs ~~ tnames))) |
|
135 |
||
136 |
in Logic.list_implies (prems, concl) end; |
|
137 |
||
138 |
(******************************* case distinction *****************************) |
|
139 |
||
140 |
fun make_casedists descr sorts = |
|
141 |
let |
|
142 |
val descr' = flat descr; |
|
143 |
||
144 |
fun make_casedist_prem T (cname, cargs) = |
|
145 |
let |
|
146 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
147 |
val frees = variantlist (make_tnames Ts, ["P", "y"]) ~~ Ts; |
|
148 |
val free_ts = map Free frees |
|
149 |
in list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop |
|
150 |
(HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))), |
|
151 |
HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))) |
|
152 |
end; |
|
153 |
||
154 |
fun make_casedist ((_, (_, _, constrs)), T) = |
|
155 |
let val prems = map (make_casedist_prem T) constrs |
|
156 |
in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))) |
|
157 |
end |
|
158 |
||
159 |
in map make_casedist |
|
160 |
((hd descr) ~~ take (length (hd descr), get_rec_types descr' sorts)) |
|
161 |
end; |
|
162 |
||
163 |
(*************** characteristic equations for primrec combinator **************) |
|
164 |
||
165 |
fun make_primrecs new_type_names descr sorts thy = |
|
166 |
let |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
167 |
val o_name = Sign.intern_const (sign_of Fun.thy) "op o"; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
168 |
|
6394 | 169 |
val sign = Theory.sign_of thy; |
5177 | 170 |
|
171 |
val descr' = flat descr; |
|
172 |
val recTs = get_rec_types descr' sorts; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
173 |
val used = foldr add_typ_tfree_names (recTs, []); |
5177 | 174 |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
175 |
val rec_result_Ts = map TFree (variantlist (replicate (length descr') "'t", used) ~~ |
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
176 |
replicate (length descr') HOLogic.termS); |
5177 | 177 |
|
178 |
val reccomb_fn_Ts = flat (map (fn (i, (_, _, constrs)) => |
|
179 |
map (fn (_, cargs) => |
|
180 |
let |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
181 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
182 |
val recs = filter (is_rec_type o fst) (cargs ~~ Ts); |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
183 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
184 |
fun mk_argT (DtRec k, _) = nth_elem (k, rec_result_Ts) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
185 |
| mk_argT (DtType ("fun", [_, DtRec k]), Type ("fun", [T, _])) = |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
186 |
T --> nth_elem (k, rec_result_Ts); |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
187 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
188 |
val argTs = Ts @ map mk_argT recs |
5177 | 189 |
in argTs ---> nth_elem (i, rec_result_Ts) |
190 |
end) constrs) descr'); |
|
191 |
||
192 |
val rec_fns = map (uncurry (mk_Free "f")) |
|
193 |
(reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts))); |
|
194 |
||
195 |
val big_reccomb_name = (space_implode "_" new_type_names) ^ "_rec"; |
|
196 |
val reccomb_names = map (Sign.intern_const sign) |
|
197 |
(if length descr' = 1 then [big_reccomb_name] else |
|
198 |
(map ((curry (op ^) (big_reccomb_name ^ "_")) o string_of_int) |
|
199 |
(1 upto (length descr')))); |
|
200 |
val reccombs = map (fn ((name, T), T') => list_comb |
|
201 |
(Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns)) |
|
202 |
(reccomb_names ~~ recTs ~~ rec_result_Ts); |
|
203 |
||
204 |
fun make_primrec T comb_t ((ts, f::fs), (cname, cargs)) = |
|
205 |
let |
|
206 |
val recs = filter is_rec_type cargs; |
|
207 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
208 |
val recTs' = map (typ_of_dtyp descr' sorts) recs; |
|
209 |
val tnames = make_tnames Ts; |
|
210 |
val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs)); |
|
211 |
val frees = map Free (tnames ~~ Ts); |
|
212 |
val frees' = map Free (rec_tnames ~~ recTs'); |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
213 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
214 |
fun mk_reccomb (DtRec i, _) = nth_elem (i, reccombs) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
215 |
| mk_reccomb (DtType ("fun", [_, DtRec i]), Type ("fun", [T, U])) = |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
216 |
let val T' = nth_elem (i, rec_result_Ts) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
217 |
in Const (o_name, [U --> T', T --> U, T] ---> T') $ nth_elem (i, reccombs) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
218 |
end; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
219 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
220 |
val reccombs' = map mk_reccomb (recs ~~ recTs') |
5177 | 221 |
|
222 |
in (ts @ [HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
223 |
(comb_t $ list_comb (Const (cname, Ts ---> T), frees), |
|
7704 | 224 |
list_comb (f, frees @ (map (op $) (reccombs' ~~ frees')))))], fs) |
5177 | 225 |
end |
226 |
||
227 |
in fst (foldl (fn (x, ((dt, T), comb_t)) => |
|
228 |
foldl (make_primrec T comb_t) (x, #3 (snd dt))) |
|
229 |
(([], rec_fns), descr' ~~ recTs ~~ reccombs)) |
|
230 |
end; |
|
231 |
||
232 |
(****************** make terms of form t_case f1 ... fn *********************) |
|
233 |
||
234 |
fun make_case_combs new_type_names descr sorts thy fname = |
|
235 |
let |
|
236 |
val descr' = flat descr; |
|
237 |
val recTs = get_rec_types descr' sorts; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
238 |
val used = foldr add_typ_tfree_names (recTs, []); |
5177 | 239 |
val newTs = take (length (hd descr), recTs); |
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
240 |
val T' = TFree (variant used "'t", HOLogic.termS); |
5177 | 241 |
|
242 |
val case_fn_Ts = map (fn (i, (_, _, constrs)) => |
|
243 |
map (fn (_, cargs) => |
|
244 |
let val Ts = map (typ_of_dtyp descr' sorts) cargs |
|
245 |
in Ts ---> T' end) constrs) (hd descr); |
|
246 |
||
247 |
val case_names = map (fn s => |
|
6394 | 248 |
Sign.intern_const (Theory.sign_of thy) (s ^ "_case")) new_type_names |
5177 | 249 |
in |
250 |
map (fn ((name, Ts), T) => list_comb |
|
251 |
(Const (name, Ts @ [T] ---> T'), |
|
252 |
map (uncurry (mk_Free fname)) (Ts ~~ (1 upto length Ts)))) |
|
253 |
(case_names ~~ case_fn_Ts ~~ newTs) |
|
254 |
end; |
|
255 |
||
256 |
(**************** characteristic equations for case combinator ****************) |
|
257 |
||
258 |
fun make_cases new_type_names descr sorts thy = |
|
259 |
let |
|
260 |
val descr' = flat descr; |
|
261 |
val recTs = get_rec_types descr' sorts; |
|
262 |
val newTs = take (length (hd descr), recTs); |
|
263 |
||
264 |
fun make_case T comb_t ((cname, cargs), f) = |
|
265 |
let |
|
266 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
267 |
val frees = map Free ((make_tnames Ts) ~~ Ts) |
|
268 |
in HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
269 |
(comb_t $ list_comb (Const (cname, Ts ---> T), frees), |
|
270 |
list_comb (f, frees))) |
|
271 |
end |
|
272 |
||
273 |
in map (fn (((_, (_, _, constrs)), T), comb_t) => |
|
274 |
map (make_case T comb_t) (constrs ~~ (snd (strip_comb comb_t)))) |
|
275 |
((hd descr) ~~ newTs ~~ (make_case_combs new_type_names descr sorts thy "f")) |
|
276 |
end; |
|
277 |
||
278 |
(************************* distinctness of constructors ***********************) |
|
279 |
||
280 |
fun make_distincts new_type_names descr sorts thy = |
|
281 |
let |
|
282 |
val descr' = flat descr; |
|
283 |
val recTs = get_rec_types descr' sorts; |
|
284 |
val newTs = take (length (hd descr), recTs); |
|
285 |
||
286 |
(**** number of constructors < dtK : C_i ... ~= C_j ... ****) |
|
287 |
||
288 |
fun make_distincts_1 _ [] = [] |
|
289 |
| make_distincts_1 T ((cname, cargs)::constrs) = |
|
290 |
let |
|
291 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
292 |
val frees = map Free ((make_tnames Ts) ~~ Ts); |
|
293 |
val t = list_comb (Const (cname, Ts ---> T), frees); |
|
294 |
||
295 |
fun make_distincts' [] = [] |
|
296 |
| make_distincts' ((cname', cargs')::constrs') = |
|
297 |
let |
|
298 |
val Ts' = map (typ_of_dtyp descr' sorts) cargs'; |
|
299 |
val frees' = map Free ((map ((op ^) o (rpair "'")) |
|
300 |
(make_tnames Ts')) ~~ Ts'); |
|
301 |
val t' = list_comb (Const (cname', Ts' ---> T), frees') |
|
302 |
in |
|
8434 | 303 |
(HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t'))):: |
304 |
(HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t', t))):: |
|
5177 | 305 |
(make_distincts' constrs') |
306 |
end |
|
307 |
||
308 |
in (make_distincts' constrs) @ (make_distincts_1 T constrs) |
|
309 |
end; |
|
310 |
||
311 |
in map (fn (((_, (_, _, constrs)), T), tname) => |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
312 |
if length constrs < !dtK then make_distincts_1 T constrs else []) |
5177 | 313 |
((hd descr) ~~ newTs ~~ new_type_names) |
314 |
end; |
|
315 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
316 |
|
5177 | 317 |
(*************************** the "split" - equations **************************) |
318 |
||
319 |
fun make_splits new_type_names descr sorts thy = |
|
320 |
let |
|
321 |
val descr' = flat descr; |
|
322 |
val recTs = get_rec_types descr' sorts; |
|
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
323 |
val used' = foldr add_typ_tfree_names (recTs, []); |
5177 | 324 |
val newTs = take (length (hd descr), recTs); |
5578
7de426cf179c
Package now chooses type variable names more carefully to
berghofe
parents:
5177
diff
changeset
|
325 |
val T' = TFree (variant used' "'t", HOLogic.termS); |
5177 | 326 |
val P = Free ("P", T' --> HOLogic.boolT); |
327 |
||
328 |
fun make_split (((_, (_, _, constrs)), T), comb_t) = |
|
329 |
let |
|
330 |
val (_, fs) = strip_comb comb_t; |
|
331 |
val used = ["P", "x"] @ (map (fst o dest_Free) fs); |
|
332 |
||
333 |
fun process_constr (((cname, cargs), f), (t1s, t2s)) = |
|
334 |
let |
|
335 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
336 |
val frees = map Free (variantlist (make_tnames Ts, used) ~~ Ts); |
|
337 |
val eqn = HOLogic.mk_eq (Free ("x", T), |
|
338 |
list_comb (Const (cname, Ts ---> T), frees)); |
|
339 |
val P' = P $ list_comb (f, frees) |
|
340 |
in ((foldr (fn (Free (s, T), t) => HOLogic.mk_all (s, T, t)) |
|
341 |
(frees, HOLogic.imp $ eqn $ P'))::t1s, |
|
342 |
(foldr (fn (Free (s, T), t) => HOLogic.mk_exists (s, T, t)) |
|
8434 | 343 |
(frees, HOLogic.conj $ eqn $ (HOLogic.Not $ P')))::t2s) |
5177 | 344 |
end; |
345 |
||
346 |
val (t1s, t2s) = foldr process_constr (constrs ~~ fs, ([], [])); |
|
347 |
val lhs = P $ (comb_t $ Free ("x", T)) |
|
348 |
in |
|
349 |
(HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, mk_conj t1s)), |
|
8434 | 350 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ mk_disj t2s))) |
5177 | 351 |
end |
352 |
||
353 |
in map make_split ((hd descr) ~~ newTs ~~ |
|
354 |
(make_case_combs new_type_names descr sorts thy "f")) |
|
355 |
end; |
|
356 |
||
357 |
(************************ translation rules for case **************************) |
|
358 |
||
359 |
fun make_case_trrules new_type_names descr = |
|
360 |
let |
|
361 |
fun mk_asts i j ((cname, cargs)::constrs) = |
|
362 |
let |
|
363 |
val k = length cargs; |
|
364 |
val xs = map (fn i => Variable ("x" ^ string_of_int i)) (i upto i + k - 1); |
|
365 |
val t = Variable ("t" ^ string_of_int j); |
|
5695 | 366 |
val ast = Syntax.mk_appl (Constant "@case1") |
367 |
[Syntax.mk_appl (Constant (Sign.base_name cname)) xs, t]; |
|
5177 | 368 |
val ast' = foldr (fn (x, y) => |
5695 | 369 |
Syntax.mk_appl (Constant "_abs") [x, y]) (xs, t) |
5177 | 370 |
in |
371 |
(case constrs of |
|
372 |
[] => (ast, [ast']) |
|
373 |
| cs => let val (ast'', asts) = mk_asts (i + k) (j + 1) cs |
|
5695 | 374 |
in (Syntax.mk_appl (Constant "@case2") [ast, ast''], |
5177 | 375 |
ast'::asts) |
376 |
end) |
|
377 |
end; |
|
378 |
||
379 |
fun mk_trrule ((_, (_, _, constrs)), tname) = |
|
380 |
let val (ast, asts) = mk_asts 1 1 constrs |
|
381 |
in Syntax.ParsePrintRule |
|
5695 | 382 |
(Syntax.mk_appl (Constant "@case") [Variable "t", ast], |
383 |
Syntax.mk_appl (Constant (tname ^ "_case")) |
|
5177 | 384 |
(asts @ [Variable "t"])) |
385 |
end |
|
386 |
||
387 |
in |
|
388 |
map mk_trrule (hd descr ~~ new_type_names) |
|
389 |
end; |
|
390 |
||
391 |
(******************************* size functions *******************************) |
|
392 |
||
393 |
fun make_size new_type_names descr sorts thy = |
|
394 |
let |
|
395 |
val descr' = flat descr; |
|
396 |
val recTs = get_rec_types descr' sorts; |
|
397 |
||
398 |
val big_size_name = space_implode "_" new_type_names ^ "_size"; |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
399 |
val size_name = Sign.intern_const (Theory.sign_of (theory "Arith")) "size"; |
5177 | 400 |
val size_names = replicate (length (hd descr)) size_name @ |
6394 | 401 |
map (Sign.intern_const (Theory.sign_of thy)) |
5177 | 402 |
(if length (flat (tl descr)) = 1 then [big_size_name] else |
403 |
map (fn i => big_size_name ^ "_" ^ string_of_int i) |
|
404 |
(1 upto length (flat (tl descr)))); |
|
405 |
val size_consts = map (fn (s, T) => |
|
406 |
Const (s, T --> HOLogic.natT)) (size_names ~~ recTs); |
|
407 |
||
7704 | 408 |
fun plus (t1, t2) = Const ("op +", [HOLogic.natT, HOLogic.natT] ---> HOLogic.natT) $ t1 $ t2; |
5177 | 409 |
|
410 |
fun make_size_eqn size_const T (cname, cargs) = |
|
411 |
let |
|
412 |
val recs = filter is_rec_type cargs; |
|
413 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
414 |
val recTs = map (typ_of_dtyp descr' sorts) recs; |
|
415 |
val tnames = make_tnames Ts; |
|
416 |
val rec_tnames = map fst (filter (is_rec_type o snd) (tnames ~~ cargs)); |
|
417 |
val ts = map (fn ((r, s), T) => nth_elem (dest_DtRec r, size_consts) $ |
|
418 |
Free (s, T)) (recs ~~ rec_tnames ~~ recTs); |
|
419 |
val t = if ts = [] then HOLogic.zero else |
|
7704 | 420 |
foldl1 plus (ts @ [HOLogic.mk_nat 1]) |
5177 | 421 |
in |
422 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (size_const $ |
|
423 |
list_comb (Const (cname, Ts ---> T), map Free (tnames ~~ Ts)), t)) |
|
424 |
end |
|
425 |
||
426 |
in |
|
427 |
flat (map (fn (((_, (_, _, constrs)), size_const), T) => |
|
428 |
map (make_size_eqn size_const T) constrs) (descr' ~~ size_consts ~~ recTs)) |
|
429 |
end; |
|
430 |
||
431 |
(************************* additional rules for TFL ***************************) |
|
432 |
||
433 |
(*--------------------------------------------------------------------------- |
|
434 |
* Structure of case congruence theorem looks like this: |
|
435 |
* |
|
436 |
* (M = M') |
|
437 |
* ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) |
|
438 |
* ==> ... |
|
439 |
* ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) |
|
440 |
* ==> |
|
441 |
* (ty_case f1..fn M = ty_case g1..gn M') |
|
442 |
*---------------------------------------------------------------------------*) |
|
443 |
||
444 |
fun make_case_congs new_type_names descr sorts thy = |
|
445 |
let |
|
446 |
val case_combs = make_case_combs new_type_names descr sorts thy "f"; |
|
447 |
val case_combs' = make_case_combs new_type_names descr sorts thy "g"; |
|
448 |
||
449 |
fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) = |
|
450 |
let |
|
451 |
val Type ("fun", [T, _]) = fastype_of comb; |
|
452 |
val (_, fs) = strip_comb comb; |
|
453 |
val (_, gs) = strip_comb comb'; |
|
454 |
val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs); |
|
455 |
val M = Free ("M", T); |
|
456 |
val M' = Free ("M'", T); |
|
457 |
||
458 |
fun mk_clause ((f, g), (cname, _)) = |
|
459 |
let |
|
460 |
val (Ts, _) = strip_type (fastype_of f); |
|
461 |
val tnames = variantlist (make_tnames Ts, used); |
|
462 |
val frees = map Free (tnames ~~ Ts) |
|
463 |
in |
|
464 |
list_all_free (tnames ~~ Ts, Logic.mk_implies |
|
465 |
(HOLogic.mk_Trueprop |
|
466 |
(HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))), |
|
467 |
HOLogic.mk_Trueprop |
|
468 |
(HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees))))) |
|
469 |
end |
|
470 |
||
471 |
in |
|
472 |
Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) :: |
|
473 |
map mk_clause (fs ~~ gs ~~ constrs), |
|
474 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M'))) |
|
475 |
end |
|
476 |
||
477 |
in |
|
478 |
map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr) |
|
479 |
end; |
|
480 |
||
481 |
(*--------------------------------------------------------------------------- |
|
482 |
* Structure of exhaustion theorem looks like this: |
|
483 |
* |
|
484 |
* !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj) |
|
485 |
*---------------------------------------------------------------------------*) |
|
486 |
||
487 |
fun make_nchotomys descr sorts = |
|
488 |
let |
|
489 |
val descr' = flat descr; |
|
490 |
val recTs = get_rec_types descr' sorts; |
|
491 |
val newTs = take (length (hd descr), recTs); |
|
492 |
||
493 |
fun mk_eqn T (cname, cargs) = |
|
494 |
let |
|
495 |
val Ts = map (typ_of_dtyp descr' sorts) cargs; |
|
496 |
val tnames = variantlist (make_tnames Ts, ["v"]); |
|
497 |
val frees = tnames ~~ Ts |
|
498 |
in |
|
499 |
foldr (fn ((s, T'), t) => HOLogic.mk_exists (s, T', t)) |
|
500 |
(frees, HOLogic.mk_eq (Free ("v", T), |
|
501 |
list_comb (Const (cname, Ts ---> T), map Free frees))) |
|
502 |
end |
|
503 |
||
504 |
in map (fn ((_, (_, _, constrs)), T) => |
|
505 |
HOLogic.mk_Trueprop (HOLogic.mk_all ("v", T, mk_disj (map (mk_eqn T) constrs)))) |
|
506 |
(hd descr ~~ newTs) |
|
507 |
end; |
|
508 |
||
509 |
end; |