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