author | skalberg |
Sun, 13 Feb 2005 17:15:14 +0100 | |
changeset 15531 | 08c8dad8e399 |
parent 15379 | 830239e6eb73 |
child 15570 | 8d8c70b41bab |
permissions | -rw-r--r-- |
5177 | 1 |
(* Title: HOL/Tools/datatype_aux.ML |
2 |
ID: $Id$ |
|
11539 | 3 |
Author: Stefan Berghofer, TU Muenchen |
5177 | 4 |
|
11539 | 5 |
Auxiliary functions for defining datatypes. |
5177 | 6 |
*) |
7 |
||
8 |
signature DATATYPE_AUX = |
|
9 |
sig |
|
5661 | 10 |
val quiet_mode : bool ref |
11 |
val message : string -> unit |
|
12 |
||
5177 | 13 |
val foldl1 : ('a * 'a -> 'a) -> 'a list -> 'a |
14 |
||
5661 | 15 |
val add_path : bool -> string -> theory -> theory |
16 |
val parent_path : bool -> theory -> theory |
|
17 |
||
8435 | 18 |
val store_thmss : string -> string list -> thm list list -> theory -> theory * thm list list |
19 |
val store_thms_atts : string -> string list -> theory attribute list list -> thm list |
|
20 |
-> theory -> theory * thm list |
|
21 |
val store_thms : string -> string list -> thm list -> theory -> theory * thm list |
|
5177 | 22 |
|
23 |
val split_conj_thm : thm -> thm list |
|
24 |
val mk_conj : term list -> term |
|
25 |
val mk_disj : term list -> term |
|
26 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
27 |
val app_bnds : term -> int -> term |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
28 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
29 |
val cong_tac : int -> tactic |
5177 | 30 |
val indtac : thm -> int -> tactic |
31 |
val exh_tac : (string -> thm) -> int -> tactic |
|
32 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
33 |
datatype simproc_dist = QuickAndDirty |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
34 |
| FewConstrs of thm list |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
35 |
| ManyConstrs of thm * simpset; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
36 |
|
5177 | 37 |
datatype dtyp = |
38 |
DtTFree of string |
|
39 |
| DtType of string * (dtyp list) |
|
40 |
| DtRec of int; |
|
8404 | 41 |
type descr |
5177 | 42 |
type datatype_info |
43 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
44 |
exception Datatype |
14887 | 45 |
exception Datatype_Empty of string |
9740 | 46 |
val name_of_typ : typ -> string |
5177 | 47 |
val dtyp_of_typ : (string * string list) list -> typ -> dtyp |
48 |
val mk_Free : string -> typ -> int -> term |
|
49 |
val is_rec_type : dtyp -> bool |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
50 |
val typ_of_dtyp : descr -> (string * sort) list -> dtyp -> typ |
5177 | 51 |
val dest_DtTFree : dtyp -> string |
52 |
val dest_DtRec : dtyp -> int |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
53 |
val strip_dtyp : dtyp -> dtyp list * dtyp |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
54 |
val body_index : dtyp -> int |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
55 |
val mk_fun_dtyp : dtyp list -> dtyp -> dtyp |
5177 | 56 |
val dest_TFree : typ -> string |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
57 |
val get_nonrec_types : descr -> (string * sort) list -> typ list |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
58 |
val get_branching_types : descr -> (string * sort) list -> typ list |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
59 |
val get_arities : descr -> int list |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
60 |
val get_rec_types : descr -> (string * sort) list -> typ list |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
61 |
val check_nonempty : descr list -> unit |
5177 | 62 |
val unfold_datatypes : |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
63 |
Sign.sg -> descr -> (string * sort) list -> datatype_info Symtab.table -> |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
64 |
descr -> int -> descr list * int |
5177 | 65 |
end; |
66 |
||
67 |
structure DatatypeAux : DATATYPE_AUX = |
|
68 |
struct |
|
69 |
||
5661 | 70 |
val quiet_mode = ref false; |
71 |
fun message s = if !quiet_mode then () else writeln s; |
|
72 |
||
5177 | 73 |
(* FIXME: move to library ? *) |
74 |
fun foldl1 f (x::xs) = foldl f (x, xs); |
|
75 |
||
5661 | 76 |
fun add_path flat_names s = if flat_names then I else Theory.add_path s; |
77 |
fun parent_path flat_names = if flat_names then I else Theory.parent_path; |
|
78 |
||
8435 | 79 |
|
5177 | 80 |
(* store theorems in theory *) |
81 |
||
82 |
fun store_thmss label tnames thmss thy = |
|
8435 | 83 |
(thy, tnames ~~ thmss) |> |
84 |
foldl_map (fn (thy', (tname, thms)) => thy' |> |
|
5661 | 85 |
Theory.add_path tname |> |
8435 | 86 |
(apsnd hd o PureThy.add_thmss [((label, thms), [])]) |>> |
87 |
Theory.parent_path); |
|
5177 | 88 |
|
8435 | 89 |
fun store_thms_atts label tnames attss thms thy = |
90 |
(thy, tnames ~~ attss ~~ thms) |> |
|
91 |
foldl_map (fn (thy', ((tname, atts), thm)) => thy' |> |
|
5661 | 92 |
Theory.add_path tname |> |
8435 | 93 |
(apsnd hd o PureThy.add_thms [((label, thm), atts)]) |>> |
94 |
Theory.parent_path); |
|
95 |
||
96 |
fun store_thms label tnames = store_thms_atts label tnames (replicate (length tnames) []); |
|
97 |
||
5177 | 98 |
|
99 |
(* split theorem thm_1 & ... & thm_n into n theorems *) |
|
100 |
||
101 |
fun split_conj_thm th = |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
102 |
((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle THM _ => [th]; |
5177 | 103 |
|
104 |
val mk_conj = foldr1 (HOLogic.mk_binop "op &"); |
|
105 |
val mk_disj = foldr1 (HOLogic.mk_binop "op |"); |
|
106 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
107 |
fun app_bnds t i = list_comb (t, map Bound (i - 1 downto 0)); |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
108 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
109 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
110 |
fun cong_tac i st = (case Logic.strip_assums_concl |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
111 |
(nth_elem (i - 1, prems_of st)) of |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
112 |
_ $ (_ $ (f $ x) $ (g $ y)) => |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
113 |
let |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
114 |
val cong' = lift_rule (st, i) cong; |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
115 |
val _ $ (_ $ (f' $ x') $ (g' $ y')) = |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
116 |
Logic.strip_assums_concl (prop_of cong'); |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
117 |
val insts = map (pairself (cterm_of (#sign (rep_thm st))) o |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
118 |
apsnd (curry list_abs (Logic.strip_params (concl_of cong'))) o |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
119 |
apfst head_of) [(f', f), (g', g), (x', x), (y', y)] |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
120 |
in compose_tac (false, cterm_instantiate insts cong', 2) i st |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
121 |
handle THM _ => no_tac st |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
122 |
end |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
123 |
| _ => no_tac st); |
5177 | 124 |
|
125 |
(* instantiate induction rule *) |
|
126 |
||
127 |
fun indtac indrule i st = |
|
128 |
let |
|
8305 | 129 |
val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule)); |
130 |
val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop |
|
5177 | 131 |
(Logic.strip_imp_concl (nth_elem (i - 1, prems_of st)))); |
132 |
val getP = if can HOLogic.dest_imp (hd ts) then |
|
15531 | 133 |
(apfst SOME) o HOLogic.dest_imp else pair NONE; |
5177 | 134 |
fun abstr (t1, t2) = (case t1 of |
15531 | 135 |
NONE => let val [Free (s, T)] = add_term_frees (t2, []) |
5177 | 136 |
in absfree (s, T, t2) end |
15531 | 137 |
| SOME (_ $ t' $ _) => Abs ("x", fastype_of t', abstract_over (t', t2))) |
6394 | 138 |
val cert = cterm_of (Thm.sign_of_thm st); |
5177 | 139 |
val Ps = map (cert o head_of o snd o getP) ts; |
140 |
val indrule' = cterm_instantiate (Ps ~~ |
|
141 |
(map (cert o abstr o getP) ts')) indrule |
|
142 |
in |
|
143 |
rtac indrule' i st |
|
144 |
end; |
|
145 |
||
146 |
(* perform exhaustive case analysis on last parameter of subgoal i *) |
|
147 |
||
148 |
fun exh_tac exh_thm_of i state = |
|
149 |
let |
|
6394 | 150 |
val sg = Thm.sign_of_thm state; |
5177 | 151 |
val prem = nth_elem (i - 1, prems_of state); |
152 |
val params = Logic.strip_params prem; |
|
153 |
val (_, Type (tname, _)) = hd (rev params); |
|
154 |
val exhaustion = lift_rule (state, i) (exh_thm_of tname); |
|
155 |
val prem' = hd (prems_of exhaustion); |
|
156 |
val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem')); |
|
157 |
val exhaustion' = cterm_instantiate [(cterm_of sg (head_of lhs), |
|
158 |
cterm_of sg (foldr (fn ((_, T), t) => Abs ("z", T, t)) |
|
159 |
(params, Bound 0)))] exhaustion |
|
160 |
in compose_tac (false, exhaustion', nprems_of exhaustion) i state |
|
161 |
end; |
|
162 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
163 |
(* handling of distinctness theorems *) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
164 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
165 |
datatype simproc_dist = QuickAndDirty |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
166 |
| FewConstrs of thm list |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
167 |
| ManyConstrs of thm * simpset; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
168 |
|
5177 | 169 |
(********************** Internal description of datatypes *********************) |
170 |
||
171 |
datatype dtyp = |
|
172 |
DtTFree of string |
|
173 |
| DtType of string * (dtyp list) |
|
174 |
| DtRec of int; |
|
175 |
||
176 |
(* information about datatypes *) |
|
177 |
||
15379 | 178 |
(* index, datatype name, type arguments (DtTFree's), constructor name, types of constructor's arguments *) |
8404 | 179 |
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list; |
180 |
||
5177 | 181 |
type datatype_info = |
182 |
{index : int, |
|
8404 | 183 |
descr : descr, |
5177 | 184 |
rec_names : string list, |
185 |
rec_rewrites : thm list, |
|
186 |
case_name : string, |
|
187 |
case_rewrites : thm list, |
|
188 |
induction : thm, |
|
189 |
exhaustion : thm, |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
190 |
distinct : simproc_dist, |
5177 | 191 |
inject : thm list, |
192 |
nchotomy : thm, |
|
10120 | 193 |
case_cong : thm, |
194 |
weak_case_cong : thm}; |
|
5177 | 195 |
|
196 |
fun mk_Free s T i = Free (s ^ (string_of_int i), T); |
|
197 |
||
198 |
fun subst_DtTFree _ substs (T as (DtTFree name)) = |
|
199 |
(case assoc (substs, name) of |
|
15531 | 200 |
NONE => T |
201 |
| SOME U => U) |
|
5177 | 202 |
| subst_DtTFree i substs (DtType (name, ts)) = |
203 |
DtType (name, map (subst_DtTFree i substs) ts) |
|
204 |
| subst_DtTFree i _ (DtRec j) = DtRec (i + j); |
|
205 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
206 |
exception Datatype; |
14887 | 207 |
exception Datatype_Empty of string; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
208 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
209 |
fun dest_DtTFree (DtTFree a) = a |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
210 |
| dest_DtTFree _ = raise Datatype; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
211 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
212 |
fun dest_DtRec (DtRec i) = i |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
213 |
| dest_DtRec _ = raise Datatype; |
5177 | 214 |
|
215 |
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts |
|
216 |
| is_rec_type (DtRec _) = true |
|
217 |
| is_rec_type _ = false; |
|
218 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
219 |
fun strip_dtyp (DtType ("fun", [T, U])) = apfst (cons T) (strip_dtyp U) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
220 |
| strip_dtyp T = ([], T); |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
221 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
222 |
val body_index = dest_DtRec o snd o strip_dtyp; |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
223 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
224 |
fun mk_fun_dtyp [] U = U |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
225 |
| mk_fun_dtyp (T :: Ts) U = DtType ("fun", [T, mk_fun_dtyp Ts U]); |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
226 |
|
5177 | 227 |
fun dest_TFree (TFree (n, _)) = n; |
228 |
||
13707
55670a70a5f9
name_of_type now replaces non-identifiers by dummy names.
berghofe
parents:
13641
diff
changeset
|
229 |
fun name_of_typ (Type (s, Ts)) = |
55670a70a5f9
name_of_type now replaces non-identifiers by dummy names.
berghofe
parents:
13641
diff
changeset
|
230 |
let val s' = Sign.base_name s |
55670a70a5f9
name_of_type now replaces non-identifiers by dummy names.
berghofe
parents:
13641
diff
changeset
|
231 |
in space_implode "_" (filter (not o equal "") (map name_of_typ Ts) @ |
14673 | 232 |
[if Syntax.is_identifier s' then s' else "x"]) |
13707
55670a70a5f9
name_of_type now replaces non-identifiers by dummy names.
berghofe
parents:
13641
diff
changeset
|
233 |
end |
9740 | 234 |
| name_of_typ _ = ""; |
235 |
||
5177 | 236 |
fun dtyp_of_typ _ (TFree (n, _)) = DtTFree n |
237 |
| dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)" |
|
238 |
| dtyp_of_typ new_dts (Type (tname, Ts)) = |
|
239 |
(case assoc (new_dts, tname) of |
|
15531 | 240 |
NONE => DtType (tname, map (dtyp_of_typ new_dts) Ts) |
241 |
| SOME vs => if map (try dest_TFree) Ts = map SOME vs then |
|
5177 | 242 |
DtRec (find_index (curry op = tname o fst) new_dts) |
243 |
else error ("Illegal occurence of recursive type " ^ tname)); |
|
244 |
||
245 |
fun typ_of_dtyp descr sorts (DtTFree a) = TFree (a, the (assoc (sorts, a))) |
|
246 |
| typ_of_dtyp descr sorts (DtRec i) = |
|
247 |
let val (s, ds, _) = the (assoc (descr, i)) |
|
248 |
in Type (s, map (typ_of_dtyp descr sorts) ds) end |
|
249 |
| typ_of_dtyp descr sorts (DtType (s, ds)) = |
|
250 |
Type (s, map (typ_of_dtyp descr sorts) ds); |
|
251 |
||
252 |
(* find all non-recursive types in datatype description *) |
|
253 |
||
254 |
fun get_nonrec_types descr sorts = |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
255 |
map (typ_of_dtyp descr sorts) (foldl (fn (Ts, (_, (_, _, constrs))) => |
5177 | 256 |
foldl (fn (Ts', (_, cargs)) => |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
257 |
filter_out is_rec_type cargs union Ts') (Ts, constrs)) ([], descr)); |
5177 | 258 |
|
259 |
(* get all recursive types in datatype description *) |
|
260 |
||
261 |
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) => |
|
262 |
Type (s, map (typ_of_dtyp descr sorts) ds)) descr; |
|
263 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
264 |
(* get all branching types *) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
265 |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
266 |
fun get_branching_types descr sorts = |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
267 |
map (typ_of_dtyp descr sorts) (foldl (fn (Ts, (_, (_, _, constrs))) => |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
268 |
foldl (fn (Ts', (_, cargs)) => foldr op union (map (fst o strip_dtyp) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
269 |
cargs, Ts')) (Ts, constrs)) ([], descr)); |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
270 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
271 |
fun get_arities descr = foldl (fn (is, (_, (_, _, constrs))) => |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
272 |
foldl (fn (is', (_, cargs)) => map (length o fst o strip_dtyp) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
273 |
(filter is_rec_type cargs) union is') (is, constrs)) ([], descr); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
274 |
|
5177 | 275 |
(* nonemptiness check for datatypes *) |
276 |
||
277 |
fun check_nonempty descr = |
|
278 |
let |
|
279 |
val descr' = flat descr; |
|
280 |
fun is_nonempty_dt is i = |
|
281 |
let |
|
282 |
val (_, _, constrs) = the (assoc (descr', i)); |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
283 |
fun arg_nonempty (_, DtRec i) = if i mem is then false |
5177 | 284 |
else is_nonempty_dt (i::is) i |
285 |
| arg_nonempty _ = true; |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
286 |
in exists ((forall (arg_nonempty o strip_dtyp)) o snd) constrs |
5177 | 287 |
end |
288 |
in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr) |
|
14887 | 289 |
(fn (_, (s, _, _)) => raise Datatype_Empty s) |
5177 | 290 |
end; |
291 |
||
292 |
(* unfold a list of mutually recursive datatype specifications *) |
|
293 |
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *) |
|
294 |
(* need to be unfolded *) |
|
295 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
296 |
fun unfold_datatypes sign orig_descr sorts (dt_info : datatype_info Symtab.table) descr i = |
5177 | 297 |
let |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
298 |
fun typ_error T msg = error ("Non-admissible type expression\n" ^ |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
299 |
Sign.string_of_typ sign (typ_of_dtyp (orig_descr @ descr) sorts T) ^ "\n" ^ msg); |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
300 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
301 |
fun get_dt_descr T i tname dts = |
5177 | 302 |
(case Symtab.lookup (dt_info, tname) of |
15531 | 303 |
NONE => typ_error T (tname ^ " is not a datatype - can't use it in\ |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
304 |
\ nested recursion") |
15531 | 305 |
| (SOME {index, descr, ...}) => |
5177 | 306 |
let val (_, vars, _) = the (assoc (descr, index)); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
307 |
val subst = ((map dest_DtTFree vars) ~~ dts) handle LIST _ => |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
308 |
typ_error T ("Type constructor " ^ tname ^ " used with wrong\ |
5177 | 309 |
\ number of arguments") |
310 |
in (i + index, map (fn (j, (tn, args, cs)) => (i + j, |
|
311 |
(tn, map (subst_DtTFree i subst) args, |
|
312 |
map (apsnd (map (subst_DtTFree i subst))) cs))) descr) |
|
313 |
end); |
|
314 |
||
315 |
(* unfold a single constructor argument *) |
|
316 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
317 |
fun unfold_arg ((i, Ts, descrs), T) = |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
318 |
if is_rec_type T then |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
319 |
let val (Us, U) = strip_dtyp T |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
320 |
in if exists is_rec_type Us then |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
321 |
typ_error T "Non-strictly positive recursive occurrence of type" |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
322 |
else (case U of |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
323 |
DtType (tname, dts) => |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
324 |
let |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
325 |
val (index, descr) = get_dt_descr T i tname dts; |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
326 |
val (descr', i') = unfold_datatypes sign orig_descr sorts |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
327 |
dt_info descr (i + length descr) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
328 |
in (i', Ts @ [mk_fun_dtyp Us (DtRec index)], descrs @ descr') end |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
329 |
| _ => (i, Ts @ [T], descrs)) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
330 |
end |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
331 |
else (i, Ts @ [T], descrs); |
5177 | 332 |
|
333 |
(* unfold a constructor *) |
|
334 |
||
335 |
fun unfold_constr ((i, constrs, descrs), (cname, cargs)) = |
|
336 |
let val (i', cargs', descrs') = foldl unfold_arg ((i, [], descrs), cargs) |
|
337 |
in (i', constrs @ [(cname, cargs')], descrs') end; |
|
338 |
||
339 |
(* unfold a single datatype *) |
|
340 |
||
341 |
fun unfold_datatype ((i, dtypes, descrs), (j, (tname, tvars, constrs))) = |
|
342 |
let val (i', constrs', descrs') = |
|
343 |
foldl unfold_constr ((i, [], descrs), constrs) |
|
344 |
in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs') |
|
345 |
end; |
|
346 |
||
347 |
val (i', descr', descrs) = foldl unfold_datatype ((i, [],[]), descr); |
|
348 |
||
349 |
in (descr' :: descrs, i') end; |
|
350 |
||
351 |
end; |