author | wenzelm |
Mon, 16 Mar 2009 18:24:30 +0100 | |
changeset 30549 | d2d7874648bd |
parent 30364 | 577edc39b501 |
permissions | -rw-r--r-- |
5177 | 1 |
(* Title: HOL/Tools/datatype_aux.ML |
11539 | 2 |
Author: Stefan Berghofer, TU Muenchen |
5177 | 3 |
|
11539 | 4 |
Auxiliary functions for defining datatypes. |
5177 | 5 |
*) |
6 |
||
7 |
signature DATATYPE_AUX = |
|
8 |
sig |
|
5661 | 9 |
val quiet_mode : bool ref |
10 |
val message : string -> unit |
|
11 |
||
12 |
val add_path : bool -> string -> theory -> theory |
|
13 |
val parent_path : bool -> theory -> theory |
|
14 |
||
18728 | 15 |
val store_thmss_atts : string -> string list -> attribute list list -> thm list list |
18349 | 16 |
-> theory -> thm list list * theory |
18314 | 17 |
val store_thmss : string -> string list -> thm list list -> theory -> thm list list * theory |
18728 | 18 |
val store_thms_atts : string -> string list -> attribute list list -> thm list |
18314 | 19 |
-> theory -> thm list * theory |
20 |
val store_thms : string -> string list -> thm list -> theory -> thm list * theory |
|
5177 | 21 |
|
22 |
val split_conj_thm : thm -> thm list |
|
23 |
val mk_conj : term list -> term |
|
24 |
val mk_disj : term list -> term |
|
25 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
26 |
val app_bnds : term -> int -> term |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
27 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
28 |
val cong_tac : int -> tactic |
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
29 |
val indtac : thm -> string list -> int -> tactic |
5177 | 30 |
val exh_tac : (string -> thm) -> int -> tactic |
31 |
||
26532
3fc9730403c1
Removed QuickAndDirty constructor from simproc_dist datatype.
berghofe
parents:
25888
diff
changeset
|
32 |
datatype simproc_dist = FewConstrs of thm list |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
33 |
| ManyConstrs of thm * simpset; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
34 |
|
5177 | 35 |
datatype dtyp = |
36 |
DtTFree of string |
|
37 |
| DtType of string * (dtyp list) |
|
38 |
| DtRec of int; |
|
8404 | 39 |
type descr |
5177 | 40 |
type datatype_info |
41 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
42 |
exception Datatype |
14887 | 43 |
exception Datatype_Empty of string |
9740 | 44 |
val name_of_typ : typ -> string |
5177 | 45 |
val dtyp_of_typ : (string * string list) list -> typ -> dtyp |
46 |
val mk_Free : string -> typ -> int -> term |
|
47 |
val is_rec_type : dtyp -> bool |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
48 |
val typ_of_dtyp : descr -> (string * sort) list -> dtyp -> typ |
5177 | 49 |
val dest_DtTFree : dtyp -> string |
50 |
val dest_DtRec : dtyp -> int |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
51 |
val strip_dtyp : dtyp -> dtyp list * dtyp |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
52 |
val body_index : dtyp -> int |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
53 |
val mk_fun_dtyp : dtyp list -> dtyp -> dtyp |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
54 |
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
|
55 |
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
|
56 |
val get_arities : descr -> int list |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
57 |
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
|
58 |
val check_nonempty : descr list -> unit |
5177 | 59 |
val unfold_datatypes : |
19250 | 60 |
theory -> descr -> (string * sort) list -> datatype_info Symtab.table -> |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
61 |
descr -> int -> descr list * int |
5177 | 62 |
end; |
63 |
||
64 |
structure DatatypeAux : DATATYPE_AUX = |
|
65 |
struct |
|
66 |
||
5661 | 67 |
val quiet_mode = ref false; |
68 |
fun message s = if !quiet_mode then () else writeln s; |
|
69 |
||
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24699
diff
changeset
|
70 |
fun add_path flat_names s = if flat_names then I else Sign.add_path s; |
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24699
diff
changeset
|
71 |
fun parent_path flat_names = if flat_names then I else Sign.parent_path; |
5661 | 72 |
|
8435 | 73 |
|
5177 | 74 |
(* store theorems in theory *) |
75 |
||
18377 | 76 |
fun store_thmss_atts label tnames attss thmss = |
77 |
fold_map (fn ((tname, atts), thms) => |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24699
diff
changeset
|
78 |
Sign.add_path tname |
29579 | 79 |
#> PureThy.add_thmss [((Binding.name label, thms), atts)] |
28361
232fcbba2e4e
explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents:
26939
diff
changeset
|
80 |
#-> (fn thm::_ => Sign.parent_path #> pair thm)) (tnames ~~ attss ~~ thmss) |
232fcbba2e4e
explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents:
26939
diff
changeset
|
81 |
##> Theory.checkpoint; |
5177 | 82 |
|
18101 | 83 |
fun store_thmss label tnames = store_thmss_atts label tnames (replicate (length tnames) []); |
84 |
||
18377 | 85 |
fun store_thms_atts label tnames attss thmss = |
86 |
fold_map (fn ((tname, atts), thms) => |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24699
diff
changeset
|
87 |
Sign.add_path tname |
29579 | 88 |
#> PureThy.add_thms [((Binding.name label, thms), atts)] |
28361
232fcbba2e4e
explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents:
26939
diff
changeset
|
89 |
#-> (fn thm::_ => Sign.parent_path #> pair thm)) (tnames ~~ attss ~~ thmss) |
232fcbba2e4e
explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents:
26939
diff
changeset
|
90 |
##> Theory.checkpoint; |
8435 | 91 |
|
92 |
fun store_thms label tnames = store_thms_atts label tnames (replicate (length tnames) []); |
|
93 |
||
5177 | 94 |
|
95 |
(* split theorem thm_1 & ... & thm_n into n theorems *) |
|
96 |
||
97 |
fun split_conj_thm th = |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
98 |
((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle THM _ => [th]; |
5177 | 99 |
|
100 |
val mk_conj = foldr1 (HOLogic.mk_binop "op &"); |
|
101 |
val mk_disj = foldr1 (HOLogic.mk_binop "op |"); |
|
102 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
103 |
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
|
104 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
105 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
106 |
fun cong_tac i st = (case Logic.strip_assums_concl |
15570 | 107 |
(List.nth (prems_of st, i - 1)) of |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
108 |
_ $ (_ $ (f $ x) $ (g $ y)) => |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
109 |
let |
18145 | 110 |
val cong' = Thm.lift_rule (Thm.cprem_of st i) cong; |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
111 |
val _ $ (_ $ (f' $ x') $ (g' $ y')) = |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
112 |
Logic.strip_assums_concl (prop_of cong'); |
22596 | 113 |
val insts = map (pairself (cterm_of (Thm.theory_of_thm st)) o |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
handle THM _ => no_tac st |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
118 |
end |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
119 |
| _ => no_tac st); |
5177 | 120 |
|
121 |
(* instantiate induction rule *) |
|
122 |
||
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
123 |
fun indtac indrule indnames i st = |
5177 | 124 |
let |
8305 | 125 |
val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule)); |
126 |
val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop |
|
15570 | 127 |
(Logic.strip_imp_concl (List.nth (prems_of st, i - 1)))); |
5177 | 128 |
val getP = if can HOLogic.dest_imp (hd ts) then |
15531 | 129 |
(apfst SOME) o HOLogic.dest_imp else pair NONE; |
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
130 |
val flt = if null indnames then I else |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
131 |
filter (fn Free (s, _) => s mem indnames | _ => false); |
5177 | 132 |
fun abstr (t1, t2) = (case t1 of |
29265
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
wenzelm
parents:
28361
diff
changeset
|
133 |
NONE => (case flt (OldTerm.term_frees t2) of |
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
134 |
[Free (s, T)] => SOME (absfree (s, T, t2)) |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
135 |
| _ => NONE) |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
136 |
| SOME (_ $ t') => SOME (Abs ("x", fastype_of t', abstract_over (t', t2)))) |
22578 | 137 |
val cert = cterm_of (Thm.theory_of_thm st); |
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
138 |
val insts = List.mapPartial (fn (t, u) => case abstr (getP u) of |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
139 |
NONE => NONE |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
140 |
| SOME u' => SOME (t |> getP |> snd |> head_of |> cert, cert u')) (ts ~~ ts'); |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
141 |
val indrule' = cterm_instantiate insts indrule |
5177 | 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 |
|
22578 | 150 |
val thy = Thm.theory_of_thm state; |
21420 | 151 |
val prem = nth (prems_of state) (i - 1); |
5177 | 152 |
val params = Logic.strip_params prem; |
153 |
val (_, Type (tname, _)) = hd (rev params); |
|
18145 | 154 |
val exhaustion = Thm.lift_rule (Thm.cprem_of state i) (exh_thm_of tname); |
5177 | 155 |
val prem' = hd (prems_of exhaustion); |
156 |
val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem')); |
|
21420 | 157 |
val exhaustion' = cterm_instantiate [(cterm_of thy (head_of lhs), |
30190 | 158 |
cterm_of thy (List.foldr (fn ((_, T), t) => Abs ("z", T, t)) |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
159 |
(Bound 0) params))] exhaustion |
5177 | 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 |
|
26532
3fc9730403c1
Removed QuickAndDirty constructor from simproc_dist datatype.
berghofe
parents:
25888
diff
changeset
|
165 |
datatype simproc_dist = FewConstrs of thm list |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
166 |
| ManyConstrs of thm * simpset; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
167 |
|
5177 | 168 |
(********************** Internal description of datatypes *********************) |
169 |
||
170 |
datatype dtyp = |
|
171 |
DtTFree of string |
|
172 |
| DtType of string * (dtyp list) |
|
173 |
| DtRec of int; |
|
174 |
||
175 |
(* information about datatypes *) |
|
176 |
||
16901 | 177 |
(* index, datatype name, type arguments, constructor name, types of constructor's arguments *) |
8404 | 178 |
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list; |
179 |
||
5177 | 180 |
type datatype_info = |
181 |
{index : int, |
|
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
182 |
alt_names : string list option, |
8404 | 183 |
descr : descr, |
18319
c52b139ebde0
Added new component "sorts" to record datatype_info.
berghofe
parents:
18314
diff
changeset
|
184 |
sorts : (string * sort) list, |
5177 | 185 |
rec_names : string list, |
186 |
rec_rewrites : thm list, |
|
187 |
case_name : string, |
|
188 |
case_rewrites : thm list, |
|
189 |
induction : thm, |
|
190 |
exhaustion : thm, |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
191 |
distinct : simproc_dist, |
5177 | 192 |
inject : thm list, |
193 |
nchotomy : thm, |
|
10120 | 194 |
case_cong : thm, |
195 |
weak_case_cong : thm}; |
|
5177 | 196 |
|
197 |
fun mk_Free s T i = Free (s ^ (string_of_int i), T); |
|
198 |
||
199 |
fun subst_DtTFree _ substs (T as (DtTFree name)) = |
|
17485 | 200 |
AList.lookup (op =) substs name |> the_default T |
5177 | 201 |
| subst_DtTFree i substs (DtType (name, ts)) = |
202 |
DtType (name, map (subst_DtTFree i substs) ts) |
|
203 |
| subst_DtTFree i _ (DtRec j) = DtRec (i + j); |
|
204 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
205 |
exception Datatype; |
14887 | 206 |
exception Datatype_Empty of string; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
207 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
208 |
fun dest_DtTFree (DtTFree a) = a |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
209 |
| dest_DtTFree _ = raise Datatype; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
210 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
211 |
fun dest_DtRec (DtRec i) = i |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
212 |
| dest_DtRec _ = raise Datatype; |
5177 | 213 |
|
214 |
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts |
|
215 |
| is_rec_type (DtRec _) = true |
|
216 |
| is_rec_type _ = false; |
|
217 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
218 |
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
|
219 |
| strip_dtyp T = ([], T); |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
220 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
221 |
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
|
222 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
223 |
fun mk_fun_dtyp [] U = U |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
224 |
| 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
|
225 |
|
13707
55670a70a5f9
name_of_type now replaces non-identifiers by dummy names.
berghofe
parents:
13641
diff
changeset
|
226 |
fun name_of_typ (Type (s, Ts)) = |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
227 |
let val s' = Long_Name.base_name s |
15570 | 228 |
in space_implode "_" (List.filter (not o equal "") (map name_of_typ Ts) @ |
14673 | 229 |
[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
|
230 |
end |
9740 | 231 |
| name_of_typ _ = ""; |
232 |
||
5177 | 233 |
fun dtyp_of_typ _ (TFree (n, _)) = DtTFree n |
234 |
| dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)" |
|
235 |
| dtyp_of_typ new_dts (Type (tname, Ts)) = |
|
17485 | 236 |
(case AList.lookup (op =) new_dts tname of |
15531 | 237 |
NONE => DtType (tname, map (dtyp_of_typ new_dts) Ts) |
25888
48cc198b9ac5
Eliminated DatatypeAux.dest_TFree to avoid clashes
berghofe
parents:
25676
diff
changeset
|
238 |
| SOME vs => if map (try (fst o dest_TFree)) Ts = map SOME vs then |
5177 | 239 |
DtRec (find_index (curry op = tname o fst) new_dts) |
18069 | 240 |
else error ("Illegal occurrence of recursive type " ^ tname)); |
5177 | 241 |
|
17485 | 242 |
fun typ_of_dtyp descr sorts (DtTFree a) = TFree (a, (the o AList.lookup (op =) sorts) a) |
5177 | 243 |
| typ_of_dtyp descr sorts (DtRec i) = |
17485 | 244 |
let val (s, ds, _) = (the o AList.lookup (op =) descr) i |
5177 | 245 |
in Type (s, map (typ_of_dtyp descr sorts) ds) end |
246 |
| typ_of_dtyp descr sorts (DtType (s, ds)) = |
|
247 |
Type (s, map (typ_of_dtyp descr sorts) ds); |
|
248 |
||
249 |
(* find all non-recursive types in datatype description *) |
|
250 |
||
251 |
fun get_nonrec_types descr sorts = |
|
15570 | 252 |
map (typ_of_dtyp descr sorts) (Library.foldl (fn (Ts, (_, (_, _, constrs))) => |
253 |
Library.foldl (fn (Ts', (_, cargs)) => |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
254 |
filter_out is_rec_type cargs union Ts') (Ts, constrs)) ([], descr)); |
5177 | 255 |
|
256 |
(* get all recursive types in datatype description *) |
|
257 |
||
258 |
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) => |
|
259 |
Type (s, map (typ_of_dtyp descr sorts) ds)) descr; |
|
260 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
261 |
(* get all branching types *) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
262 |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
263 |
fun get_branching_types descr sorts = |
26823
f426b9c2a90d
Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents:
26532
diff
changeset
|
264 |
map (typ_of_dtyp descr sorts) (fold (fn (_, (_, _, constrs)) => |
f426b9c2a90d
Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents:
26532
diff
changeset
|
265 |
fold (fn (_, cargs) => fold (strip_dtyp #> fst #> fold (insert op =)) cargs) |
f426b9c2a90d
Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents:
26532
diff
changeset
|
266 |
constrs) descr []); |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
267 |
|
26823
f426b9c2a90d
Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents:
26532
diff
changeset
|
268 |
fun get_arities descr = fold (fn (_, (_, _, constrs)) => |
f426b9c2a90d
Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents:
26532
diff
changeset
|
269 |
fold (fn (_, cargs) => fold (insert op =) (map (length o fst o strip_dtyp) |
f426b9c2a90d
Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents:
26532
diff
changeset
|
270 |
(List.filter is_rec_type cargs))) constrs) descr []; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
271 |
|
5177 | 272 |
(* nonemptiness check for datatypes *) |
273 |
||
274 |
fun check_nonempty descr = |
|
275 |
let |
|
15570 | 276 |
val descr' = List.concat descr; |
5177 | 277 |
fun is_nonempty_dt is i = |
278 |
let |
|
17485 | 279 |
val (_, _, constrs) = (the o AList.lookup (op =) descr') i; |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
280 |
fun arg_nonempty (_, DtRec i) = if i mem is then false |
5177 | 281 |
else is_nonempty_dt (i::is) i |
282 |
| arg_nonempty _ = true; |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
283 |
in exists ((forall (arg_nonempty o strip_dtyp)) o snd) constrs |
5177 | 284 |
end |
285 |
in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr) |
|
14887 | 286 |
(fn (_, (s, _, _)) => raise Datatype_Empty s) |
5177 | 287 |
end; |
288 |
||
289 |
(* unfold a list of mutually recursive datatype specifications *) |
|
290 |
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *) |
|
291 |
(* need to be unfolded *) |
|
292 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
293 |
fun unfold_datatypes sign orig_descr sorts (dt_info : datatype_info Symtab.table) descr i = |
5177 | 294 |
let |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
295 |
fun typ_error T msg = error ("Non-admissible type expression\n" ^ |
26939
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents:
26823
diff
changeset
|
296 |
Syntax.string_of_typ_global sign (typ_of_dtyp (orig_descr @ descr) sorts T) ^ "\n" ^ msg); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
297 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
298 |
fun get_dt_descr T i tname dts = |
17412 | 299 |
(case Symtab.lookup dt_info tname of |
15531 | 300 |
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
|
301 |
\ nested recursion") |
15531 | 302 |
| (SOME {index, descr, ...}) => |
17485 | 303 |
let val (_, vars, _) = (the o AList.lookup (op =) descr) index; |
19841 | 304 |
val subst = ((map dest_DtTFree vars) ~~ dts) handle Library.UnequalLengths => |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
305 |
typ_error T ("Type constructor " ^ tname ^ " used with wrong\ |
5177 | 306 |
\ number of arguments") |
307 |
in (i + index, map (fn (j, (tn, args, cs)) => (i + j, |
|
308 |
(tn, map (subst_DtTFree i subst) args, |
|
309 |
map (apsnd (map (subst_DtTFree i subst))) cs))) descr) |
|
310 |
end); |
|
311 |
||
312 |
(* unfold a single constructor argument *) |
|
313 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
314 |
fun unfold_arg ((i, Ts, descrs), T) = |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
315 |
if is_rec_type T then |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
316 |
let val (Us, U) = strip_dtyp T |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
317 |
in if exists is_rec_type Us then |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
318 |
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
|
319 |
else (case U of |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
320 |
DtType (tname, dts) => |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
321 |
let |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
322 |
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
|
323 |
val (descr', i') = unfold_datatypes sign orig_descr sorts |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
324 |
dt_info descr (i + length descr) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
325 |
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
|
326 |
| _ => (i, Ts @ [T], descrs)) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
327 |
end |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
328 |
else (i, Ts @ [T], descrs); |
5177 | 329 |
|
330 |
(* unfold a constructor *) |
|
331 |
||
332 |
fun unfold_constr ((i, constrs, descrs), (cname, cargs)) = |
|
15570 | 333 |
let val (i', cargs', descrs') = Library.foldl unfold_arg ((i, [], descrs), cargs) |
5177 | 334 |
in (i', constrs @ [(cname, cargs')], descrs') end; |
335 |
||
336 |
(* unfold a single datatype *) |
|
337 |
||
338 |
fun unfold_datatype ((i, dtypes, descrs), (j, (tname, tvars, constrs))) = |
|
339 |
let val (i', constrs', descrs') = |
|
15570 | 340 |
Library.foldl unfold_constr ((i, [], descrs), constrs) |
5177 | 341 |
in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs') |
342 |
end; |
|
343 |
||
15570 | 344 |
val (i', descr', descrs) = Library.foldl unfold_datatype ((i, [],[]), descr); |
5177 | 345 |
|
346 |
in (descr' :: descrs, i') end; |
|
347 |
||
348 |
end; |