author | wenzelm |
Tue, 27 Oct 2009 22:55:27 +0100 | |
changeset 33244 | db230399f890 |
parent 33243 | 17014b1b9353 |
child 33317 | b4534348b8fd |
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 |
||
31737 | 7 |
signature DATATYPE_COMMON = |
8 |
sig |
|
32964 | 9 |
type config = {strict : bool, quiet : bool} |
31737 | 10 |
val default_config : config |
11 |
datatype dtyp = |
|
12 |
DtTFree of string |
|
32964 | 13 |
| DtType of string * dtyp list |
14 |
| DtRec of int |
|
15 |
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list |
|
16 |
type info = |
|
17 |
{index : int, |
|
18 |
alt_names : string list option, |
|
19 |
descr : descr, |
|
20 |
sorts : (string * sort) list, |
|
21 |
inject : thm list, |
|
22 |
distinct : thm list, |
|
23 |
induct : thm, |
|
24 |
inducts : thm list, |
|
25 |
exhaust : thm, |
|
26 |
nchotomy : thm, |
|
27 |
rec_names : string list, |
|
28 |
rec_rewrites : thm list, |
|
29 |
case_name : string, |
|
30 |
case_rewrites : thm list, |
|
31 |
case_cong : thm, |
|
32 |
weak_case_cong : thm, |
|
33 |
split : thm, |
|
34 |
split_asm: thm} |
|
31737 | 35 |
end |
36 |
||
5177 | 37 |
signature DATATYPE_AUX = |
38 |
sig |
|
31737 | 39 |
include DATATYPE_COMMON |
40 |
||
41 |
val message : config -> string -> unit |
|
5661 | 42 |
|
18728 | 43 |
val store_thmss_atts : string -> string list -> attribute list list -> thm list list |
18349 | 44 |
-> theory -> thm list list * theory |
18314 | 45 |
val store_thmss : string -> string list -> thm list list -> theory -> thm list list * theory |
18728 | 46 |
val store_thms_atts : string -> string list -> attribute list list -> thm list |
18314 | 47 |
-> theory -> thm list * theory |
48 |
val store_thms : string -> string list -> thm list -> theory -> thm list * theory |
|
5177 | 49 |
|
50 |
val split_conj_thm : thm -> thm list |
|
51 |
val mk_conj : term list -> term |
|
52 |
val mk_disj : term list -> term |
|
53 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
54 |
val app_bnds : term -> int -> term |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
55 |
|
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
56 |
val indtac : thm -> string list -> int -> tactic |
5177 | 57 |
val exh_tac : (string -> thm) -> int -> tactic |
58 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
59 |
exception Datatype |
14887 | 60 |
exception Datatype_Empty of string |
9740 | 61 |
val name_of_typ : typ -> string |
5177 | 62 |
val dtyp_of_typ : (string * string list) list -> typ -> dtyp |
63 |
val mk_Free : string -> typ -> int -> term |
|
64 |
val is_rec_type : dtyp -> bool |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
65 |
val typ_of_dtyp : descr -> (string * sort) list -> dtyp -> typ |
5177 | 66 |
val dest_DtTFree : dtyp -> string |
67 |
val dest_DtRec : dtyp -> int |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
68 |
val strip_dtyp : dtyp -> dtyp list * dtyp |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
69 |
val body_index : dtyp -> int |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
70 |
val mk_fun_dtyp : dtyp list -> dtyp -> dtyp |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
val get_arities : descr -> int list |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
74 |
val get_rec_types : descr -> (string * sort) list -> typ list |
31605 | 75 |
val interpret_construction : descr -> (string * sort) list |
76 |
-> { atyp: typ -> 'a, dtyp: typ list -> int * bool -> string * typ list -> 'a } |
|
33243 | 77 |
-> ((string * typ list) * (string * 'a list) list) list |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
78 |
val check_nonempty : descr list -> unit |
5177 | 79 |
val unfold_datatypes : |
31737 | 80 |
theory -> descr -> (string * sort) list -> info Symtab.table -> |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
81 |
descr -> int -> descr list * int |
5177 | 82 |
end; |
83 |
||
84 |
structure DatatypeAux : DATATYPE_AUX = |
|
85 |
struct |
|
86 |
||
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31605
diff
changeset
|
87 |
(* datatype option flags *) |
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31605
diff
changeset
|
88 |
|
32964 | 89 |
type config = {strict : bool, quiet : bool}; |
90 |
val default_config : config = {strict = true, quiet = false}; |
|
91 |
||
92 |
fun message ({quiet = true, ...} : config) s = writeln s |
|
93 |
| message _ _ = (); |
|
5661 | 94 |
|
8435 | 95 |
|
5177 | 96 |
(* store theorems in theory *) |
97 |
||
18377 | 98 |
fun store_thmss_atts label tnames attss thmss = |
99 |
fold_map (fn ((tname, atts), thms) => |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24699
diff
changeset
|
100 |
Sign.add_path tname |
29579 | 101 |
#> 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
|
102 |
#-> (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
|
103 |
##> Theory.checkpoint; |
5177 | 104 |
|
18101 | 105 |
fun store_thmss label tnames = store_thmss_atts label tnames (replicate (length tnames) []); |
106 |
||
18377 | 107 |
fun store_thms_atts label tnames attss thmss = |
108 |
fold_map (fn ((tname, atts), thms) => |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24699
diff
changeset
|
109 |
Sign.add_path tname |
29579 | 110 |
#> 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
|
111 |
#-> (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
|
112 |
##> Theory.checkpoint; |
8435 | 113 |
|
114 |
fun store_thms label tnames = store_thms_atts label tnames (replicate (length tnames) []); |
|
115 |
||
5177 | 116 |
|
117 |
(* split theorem thm_1 & ... & thm_n into n theorems *) |
|
118 |
||
119 |
fun split_conj_thm th = |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
120 |
((th RS conjunct1)::(split_conj_thm (th RS conjunct2))) handle THM _ => [th]; |
5177 | 121 |
|
122 |
val mk_conj = foldr1 (HOLogic.mk_binop "op &"); |
|
123 |
val mk_disj = foldr1 (HOLogic.mk_binop "op |"); |
|
124 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
125 |
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
|
126 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
127 |
|
5177 | 128 |
(* instantiate induction rule *) |
129 |
||
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
130 |
fun indtac indrule indnames i st = |
5177 | 131 |
let |
8305 | 132 |
val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule)); |
133 |
val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop |
|
15570 | 134 |
(Logic.strip_imp_concl (List.nth (prems_of st, i - 1)))); |
5177 | 135 |
val getP = if can HOLogic.dest_imp (hd ts) then |
15531 | 136 |
(apfst SOME) o HOLogic.dest_imp else pair NONE; |
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
137 |
val flt = if null indnames then I else |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
138 |
filter (fn Free (s, _) => s mem indnames | _ => false); |
5177 | 139 |
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
|
140 |
NONE => (case flt (OldTerm.term_frees t2) of |
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
141 |
[Free (s, T)] => SOME (absfree (s, T, t2)) |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
142 |
| _ => NONE) |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
143 |
| SOME (_ $ t') => SOME (Abs ("x", fastype_of t', abstract_over (t', t2)))) |
22578 | 144 |
val cert = cterm_of (Thm.theory_of_thm st); |
32952 | 145 |
val insts = map_filter (fn (t, u) => case abstr (getP u) of |
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
146 |
NONE => NONE |
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
147 |
| 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
|
148 |
val indrule' = cterm_instantiate insts indrule |
5177 | 149 |
in |
150 |
rtac indrule' i st |
|
151 |
end; |
|
152 |
||
153 |
(* perform exhaustive case analysis on last parameter of subgoal i *) |
|
154 |
||
155 |
fun exh_tac exh_thm_of i state = |
|
156 |
let |
|
22578 | 157 |
val thy = Thm.theory_of_thm state; |
21420 | 158 |
val prem = nth (prems_of state) (i - 1); |
5177 | 159 |
val params = Logic.strip_params prem; |
160 |
val (_, Type (tname, _)) = hd (rev params); |
|
18145 | 161 |
val exhaustion = Thm.lift_rule (Thm.cprem_of state i) (exh_thm_of tname); |
5177 | 162 |
val prem' = hd (prems_of exhaustion); |
163 |
val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem')); |
|
21420 | 164 |
val exhaustion' = cterm_instantiate [(cterm_of thy (head_of lhs), |
30190 | 165 |
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
|
166 |
(Bound 0) params))] exhaustion |
5177 | 167 |
in compose_tac (false, exhaustion', nprems_of exhaustion) i state |
168 |
end; |
|
169 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
170 |
|
5177 | 171 |
(********************** Internal description of datatypes *********************) |
172 |
||
173 |
datatype dtyp = |
|
174 |
DtTFree of string |
|
175 |
| DtType of string * (dtyp list) |
|
176 |
| DtRec of int; |
|
177 |
||
178 |
(* information about datatypes *) |
|
179 |
||
16901 | 180 |
(* index, datatype name, type arguments, constructor name, types of constructor's arguments *) |
8404 | 181 |
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list; |
182 |
||
31737 | 183 |
type info = |
5177 | 184 |
{index : int, |
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
185 |
alt_names : string list option, |
8404 | 186 |
descr : descr, |
18319
c52b139ebde0
Added new component "sorts" to record datatype_info.
berghofe
parents:
18314
diff
changeset
|
187 |
sorts : (string * sort) list, |
32718 | 188 |
inject : thm list, |
32900 | 189 |
distinct : thm list, |
32727 | 190 |
induct : thm, |
191 |
inducts : thm list, |
|
32718 | 192 |
exhaust : thm, |
193 |
nchotomy : thm, |
|
5177 | 194 |
rec_names : string list, |
195 |
rec_rewrites : thm list, |
|
196 |
case_name : string, |
|
197 |
case_rewrites : thm list, |
|
10120 | 198 |
case_cong : thm, |
32718 | 199 |
weak_case_cong : thm, |
32727 | 200 |
split : thm, |
201 |
split_asm: thm}; |
|
5177 | 202 |
|
203 |
fun mk_Free s T i = Free (s ^ (string_of_int i), T); |
|
204 |
||
205 |
fun subst_DtTFree _ substs (T as (DtTFree name)) = |
|
17485 | 206 |
AList.lookup (op =) substs name |> the_default T |
5177 | 207 |
| subst_DtTFree i substs (DtType (name, ts)) = |
208 |
DtType (name, map (subst_DtTFree i substs) ts) |
|
209 |
| subst_DtTFree i _ (DtRec j) = DtRec (i + j); |
|
210 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
211 |
exception Datatype; |
14887 | 212 |
exception Datatype_Empty of string; |
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 dest_DtTFree (DtTFree a) = a |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
215 |
| dest_DtTFree _ = raise Datatype; |
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 dest_DtRec (DtRec i) = i |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
218 |
| dest_DtRec _ = raise Datatype; |
5177 | 219 |
|
220 |
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts |
|
221 |
| is_rec_type (DtRec _) = true |
|
222 |
| is_rec_type _ = false; |
|
223 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
224 |
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
|
225 |
| strip_dtyp T = ([], T); |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
226 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
227 |
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
|
228 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
229 |
fun mk_fun_dtyp [] U = U |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
230 |
| 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
|
231 |
|
13707
55670a70a5f9
name_of_type now replaces non-identifiers by dummy names.
berghofe
parents:
13641
diff
changeset
|
232 |
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
|
233 |
let val s' = Long_Name.base_name s |
15570 | 234 |
in space_implode "_" (List.filter (not o equal "") (map name_of_typ Ts) @ |
14673 | 235 |
[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
|
236 |
end |
9740 | 237 |
| name_of_typ _ = ""; |
238 |
||
5177 | 239 |
fun dtyp_of_typ _ (TFree (n, _)) = DtTFree n |
240 |
| dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)" |
|
241 |
| dtyp_of_typ new_dts (Type (tname, Ts)) = |
|
17485 | 242 |
(case AList.lookup (op =) new_dts tname of |
15531 | 243 |
NONE => DtType (tname, map (dtyp_of_typ new_dts) Ts) |
25888
48cc198b9ac5
Eliminated DatatypeAux.dest_TFree to avoid clashes
berghofe
parents:
25676
diff
changeset
|
244 |
| SOME vs => if map (try (fst o dest_TFree)) Ts = map SOME vs then |
5177 | 245 |
DtRec (find_index (curry op = tname o fst) new_dts) |
18069 | 246 |
else error ("Illegal occurrence of recursive type " ^ tname)); |
5177 | 247 |
|
17485 | 248 |
fun typ_of_dtyp descr sorts (DtTFree a) = TFree (a, (the o AList.lookup (op =) sorts) a) |
5177 | 249 |
| typ_of_dtyp descr sorts (DtRec i) = |
17485 | 250 |
let val (s, ds, _) = (the o AList.lookup (op =) descr) i |
5177 | 251 |
in Type (s, map (typ_of_dtyp descr sorts) ds) end |
252 |
| typ_of_dtyp descr sorts (DtType (s, ds)) = |
|
253 |
Type (s, map (typ_of_dtyp descr sorts) ds); |
|
254 |
||
255 |
(* find all non-recursive types in datatype description *) |
|
256 |
||
257 |
fun get_nonrec_types descr sorts = |
|
33244 | 258 |
map (typ_of_dtyp descr sorts) (fold (fn (_, (_, _, constrs)) => |
259 |
fold (fn (_, cargs) => union (op =) (filter_out is_rec_type cargs)) constrs) descr []); |
|
5177 | 260 |
|
261 |
(* get all recursive types in datatype description *) |
|
262 |
||
263 |
fun get_rec_types descr sorts = map (fn (_ , (s, ds, _)) => |
|
264 |
Type (s, map (typ_of_dtyp descr sorts) ds)) descr; |
|
265 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
266 |
(* get all branching types *) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
267 |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
268 |
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
|
269 |
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
|
270 |
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
|
271 |
constrs) descr []); |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
272 |
|
26823
f426b9c2a90d
Functions get_branching_types and get_arities now use fold instead of foldl/r.
berghofe
parents:
26532
diff
changeset
|
273 |
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
|
274 |
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
|
275 |
(List.filter is_rec_type cargs))) constrs) descr []; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
276 |
|
31605 | 277 |
(* interpret construction of datatype *) |
278 |
||
279 |
fun interpret_construction descr vs { atyp, dtyp } = |
|
280 |
let |
|
281 |
val typ_of_dtyp = typ_of_dtyp descr vs; |
|
282 |
fun interpT dT = case strip_dtyp dT |
|
283 |
of (dTs, DtRec l) => |
|
284 |
let |
|
285 |
val (tyco, dTs', _) = (the o AList.lookup (op =) descr) l; |
|
286 |
val Ts = map typ_of_dtyp dTs; |
|
287 |
val Ts' = map typ_of_dtyp dTs'; |
|
288 |
val is_proper = forall (can dest_TFree) Ts'; |
|
289 |
in dtyp Ts (l, is_proper) (tyco, Ts') end |
|
290 |
| _ => atyp (typ_of_dtyp dT); |
|
291 |
fun interpC (c, dTs) = (c, map interpT dTs); |
|
292 |
fun interpD (_, (tyco, dTs, cs)) = ((tyco, map typ_of_dtyp dTs), map interpC cs); |
|
293 |
in map interpD descr end; |
|
294 |
||
5177 | 295 |
(* nonemptiness check for datatypes *) |
296 |
||
297 |
fun check_nonempty descr = |
|
298 |
let |
|
32952 | 299 |
val descr' = flat descr; |
5177 | 300 |
fun is_nonempty_dt is i = |
301 |
let |
|
17485 | 302 |
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
|
303 |
fun arg_nonempty (_, DtRec i) = if i mem is then false |
5177 | 304 |
else is_nonempty_dt (i::is) i |
305 |
| arg_nonempty _ = true; |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
306 |
in exists ((forall (arg_nonempty o strip_dtyp)) o snd) constrs |
5177 | 307 |
end |
308 |
in assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr) |
|
14887 | 309 |
(fn (_, (s, _, _)) => raise Datatype_Empty s) |
5177 | 310 |
end; |
311 |
||
312 |
(* unfold a list of mutually recursive datatype specifications *) |
|
313 |
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *) |
|
314 |
(* need to be unfolded *) |
|
315 |
||
31737 | 316 |
fun unfold_datatypes sign orig_descr sorts (dt_info : info Symtab.table) descr i = |
5177 | 317 |
let |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
318 |
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
|
319 |
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
|
320 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
321 |
fun get_dt_descr T i tname dts = |
17412 | 322 |
(case Symtab.lookup dt_info tname of |
15531 | 323 |
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
|
324 |
\ nested recursion") |
15531 | 325 |
| (SOME {index, descr, ...}) => |
17485 | 326 |
let val (_, vars, _) = (the o AList.lookup (op =) descr) index; |
19841 | 327 |
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
|
328 |
typ_error T ("Type constructor " ^ tname ^ " used with wrong\ |
5177 | 329 |
\ number of arguments") |
330 |
in (i + index, map (fn (j, (tn, args, cs)) => (i + j, |
|
331 |
(tn, map (subst_DtTFree i subst) args, |
|
332 |
map (apsnd (map (subst_DtTFree i subst))) cs))) descr) |
|
333 |
end); |
|
334 |
||
335 |
(* unfold a single constructor argument *) |
|
336 |
||
33244 | 337 |
fun unfold_arg T (i, Ts, descrs) = |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
338 |
if is_rec_type T then |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
339 |
let val (Us, U) = strip_dtyp T |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
340 |
in if exists is_rec_type Us then |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
341 |
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
|
342 |
else (case U of |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
343 |
DtType (tname, dts) => |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
344 |
let |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
345 |
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
|
346 |
val (descr', i') = unfold_datatypes sign orig_descr sorts |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
347 |
dt_info descr (i + length descr) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
348 |
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
|
349 |
| _ => (i, Ts @ [T], descrs)) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
350 |
end |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
351 |
else (i, Ts @ [T], descrs); |
5177 | 352 |
|
353 |
(* unfold a constructor *) |
|
354 |
||
33244 | 355 |
fun unfold_constr (cname, cargs) (i, constrs, descrs) = |
356 |
let val (i', cargs', descrs') = fold unfold_arg cargs (i, [], descrs) |
|
5177 | 357 |
in (i', constrs @ [(cname, cargs')], descrs') end; |
358 |
||
359 |
(* unfold a single datatype *) |
|
360 |
||
33244 | 361 |
fun unfold_datatype (j, (tname, tvars, constrs)) (i, dtypes, descrs) = |
362 |
let val (i', constrs', descrs') = fold unfold_constr constrs (i, [], descrs) |
|
363 |
in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs') end; |
|
5177 | 364 |
|
33244 | 365 |
val (i', descr', descrs) = fold unfold_datatype descr (i, [], []); |
5177 | 366 |
|
367 |
in (descr' :: descrs, i') end; |
|
368 |
||
369 |
end; |