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