author | wenzelm |
Sat, 07 Apr 2012 16:41:59 +0200 | |
changeset 47389 | e8552cba702d |
parent 45901 | cea7cd0c7e99 |
child 50239 | fb579401dc26 |
permissions | -rw-r--r-- |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
1 |
(* Title: HOL/Tools/Datatype/datatype_aux.ML |
11539 | 2 |
Author: Stefan Berghofer, TU Muenchen |
5177 | 3 |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
4 |
Datatype package: auxiliary data structures and functions. |
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 = |
|
45822 | 12 |
DtTFree of string * sort |
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 |
descr : descr, |
|
19 |
inject : thm list, |
|
20 |
distinct : thm list, |
|
21 |
induct : thm, |
|
22 |
inducts : thm list, |
|
23 |
exhaust : thm, |
|
24 |
nchotomy : thm, |
|
25 |
rec_names : string list, |
|
26 |
rec_rewrites : thm list, |
|
27 |
case_name : string, |
|
28 |
case_rewrites : thm list, |
|
29 |
case_cong : thm, |
|
30 |
weak_case_cong : thm, |
|
31 |
split : thm, |
|
32 |
split_asm: thm} |
|
31737 | 33 |
end |
34 |
||
5177 | 35 |
signature DATATYPE_AUX = |
36 |
sig |
|
31737 | 37 |
include DATATYPE_COMMON |
38 |
||
39 |
val message : config -> string -> unit |
|
45700 | 40 |
|
18728 | 41 |
val store_thmss_atts : string -> string list -> attribute list list -> thm list list |
18349 | 42 |
-> theory -> thm list list * theory |
18314 | 43 |
val store_thmss : string -> string list -> thm list list -> theory -> thm list list * theory |
18728 | 44 |
val store_thms_atts : string -> string list -> attribute list list -> thm list |
18314 | 45 |
-> theory -> thm list * theory |
46 |
val store_thms : string -> string list -> thm list -> theory -> thm list * theory |
|
5177 | 47 |
|
48 |
val split_conj_thm : thm -> thm list |
|
49 |
val mk_conj : term list -> term |
|
50 |
val mk_disj : term list -> term |
|
51 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
52 |
val app_bnds : term -> int -> term |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
53 |
|
45735 | 54 |
val ind_tac : thm -> string list -> int -> tactic |
5177 | 55 |
val exh_tac : (string -> thm) -> int -> tactic |
56 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
57 |
exception Datatype |
14887 | 58 |
exception Datatype_Empty of string |
9740 | 59 |
val name_of_typ : typ -> string |
45839
43a5b86bc102
'datatype' specifications allow explicit sort constraints;
wenzelm
parents:
45822
diff
changeset
|
60 |
val dtyp_of_typ : (string * (string * sort) list) list -> typ -> dtyp |
5177 | 61 |
val mk_Free : string -> typ -> int -> term |
62 |
val is_rec_type : dtyp -> bool |
|
45822 | 63 |
val typ_of_dtyp : descr -> dtyp -> typ |
64 |
val dest_DtTFree : dtyp -> string * sort |
|
5177 | 65 |
val dest_DtRec : dtyp -> int |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
66 |
val strip_dtyp : dtyp -> dtyp list * dtyp |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
67 |
val body_index : dtyp -> int |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
68 |
val mk_fun_dtyp : dtyp list -> dtyp -> dtyp |
45822 | 69 |
val get_nonrec_types : descr -> typ list |
70 |
val get_branching_types : descr -> typ list |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
71 |
val get_arities : descr -> int list |
45822 | 72 |
val get_rec_types : descr -> typ list |
73 |
val interpret_construction : descr -> (string * sort) list -> |
|
74 |
{atyp: typ -> 'a, dtyp: typ list -> int * bool -> string * typ list -> 'a} -> |
|
75 |
((string * typ list) * (string * 'a list) list) list |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
76 |
val check_nonempty : descr list -> unit |
45822 | 77 |
val unfold_datatypes : Proof.context -> descr -> info Symtab.table -> |
78 |
descr -> int -> descr list * int |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
79 |
val find_shortest_path : descr -> int -> (string * int) option |
5177 | 80 |
end; |
81 |
||
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
82 |
structure Datatype_Aux : DATATYPE_AUX = |
5177 | 83 |
struct |
84 |
||
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31605
diff
changeset
|
85 |
(* datatype option flags *) |
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31605
diff
changeset
|
86 |
|
32964 | 87 |
type config = {strict : bool, quiet : bool}; |
88 |
val default_config : config = {strict = true, quiet = false}; |
|
89 |
||
90 |
fun message ({quiet = true, ...} : config) s = writeln s |
|
91 |
| message _ _ = (); |
|
5661 | 92 |
|
8435 | 93 |
|
5177 | 94 |
(* store theorems in theory *) |
95 |
||
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
96 |
fun store_thmss_atts name tnames attss thmss = |
18377 | 97 |
fold_map (fn ((tname, atts), thms) => |
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
98 |
Global_Theory.note_thmss "" |
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
99 |
[((Binding.qualify true tname (Binding.name name), atts), [(thms, [])])] |
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
100 |
#-> (fn [(_, res)] => pair res)) (tnames ~~ attss ~~ thmss) |
28361
232fcbba2e4e
explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents:
26939
diff
changeset
|
101 |
##> Theory.checkpoint; |
5177 | 102 |
|
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
103 |
fun store_thmss name tnames = store_thmss_atts name tnames (replicate (length tnames) []); |
18101 | 104 |
|
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
105 |
fun store_thms_atts name tnames attss thms = |
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
106 |
fold_map (fn ((tname, atts), thm) => |
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
107 |
Global_Theory.note_thmss "" |
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
108 |
[((Binding.qualify true tname (Binding.name name), atts), [([thm], [])])] |
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
109 |
#-> (fn [(_, [res])] => pair res)) (tnames ~~ attss ~~ thms) |
28361
232fcbba2e4e
explicit checkpoint for low-level (global) theory operations, admits concurrent proofs;
wenzelm
parents:
26939
diff
changeset
|
110 |
##> Theory.checkpoint; |
8435 | 111 |
|
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
112 |
fun store_thms name tnames = store_thms_atts name tnames (replicate (length tnames) []); |
8435 | 113 |
|
5177 | 114 |
|
115 |
(* split theorem thm_1 & ... & thm_n into n theorems *) |
|
116 |
||
117 |
fun split_conj_thm th = |
|
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
118 |
((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th]; |
5177 | 119 |
|
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
36692
diff
changeset
|
120 |
val mk_conj = foldr1 (HOLogic.mk_binop @{const_name HOL.conj}); |
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
36692
diff
changeset
|
121 |
val mk_disj = foldr1 (HOLogic.mk_binop @{const_name HOL.disj}); |
5177 | 122 |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
123 |
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
|
124 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
125 |
|
5177 | 126 |
(* instantiate induction rule *) |
127 |
||
45735 | 128 |
fun ind_tac indrule indnames = CSUBGOAL (fn (cgoal, i) => |
5177 | 129 |
let |
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
130 |
val cert = cterm_of (Thm.theory_of_cterm cgoal); |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
131 |
val goal = term_of cgoal; |
8305 | 132 |
val ts = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of indrule)); |
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
133 |
val ts' = HOLogic.dest_conj (HOLogic.dest_Trueprop (Logic.strip_imp_concl goal)); |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
134 |
val getP = |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
135 |
if can HOLogic.dest_imp (hd ts) |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
136 |
then apfst SOME o HOLogic.dest_imp |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
137 |
else pair NONE; |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
138 |
val flt = |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
139 |
if null indnames then I |
45738 | 140 |
else filter (member (op =) indnames o fst); |
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
141 |
fun abstr (t1, t2) = |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
142 |
(case t1 of |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
143 |
NONE => |
45738 | 144 |
(case flt (Term.add_frees t2 []) of |
145 |
[(s, T)] => SOME (absfree (s, T) t2) |
|
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
146 |
| _ => NONE) |
45700 | 147 |
| SOME (_ $ t') => SOME (Abs ("x", fastype_of t', abstract_over (t', t2)))); |
148 |
val insts = |
|
149 |
map_filter (fn (t, u) => |
|
150 |
(case abstr (getP u) of |
|
151 |
NONE => NONE |
|
152 |
| SOME u' => SOME (t |> getP |> snd |> head_of |> cert, cert u'))) (ts ~~ ts'); |
|
153 |
val indrule' = cterm_instantiate insts indrule; |
|
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
154 |
in rtac indrule' i end); |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
155 |
|
5177 | 156 |
|
157 |
(* perform exhaustive case analysis on last parameter of subgoal i *) |
|
158 |
||
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
159 |
fun exh_tac exh_thm_of = CSUBGOAL (fn (cgoal, i) => |
5177 | 160 |
let |
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
161 |
val thy = Thm.theory_of_cterm cgoal; |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
162 |
val goal = term_of cgoal; |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
163 |
val params = Logic.strip_params goal; |
5177 | 164 |
val (_, Type (tname, _)) = hd (rev params); |
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
165 |
val exhaustion = Thm.lift_rule cgoal (exh_thm_of tname); |
5177 | 166 |
val prem' = hd (prems_of exhaustion); |
167 |
val _ $ (_ $ lhs $ _) = hd (rev (Logic.strip_assums_hyp prem')); |
|
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
168 |
val exhaustion' = |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
169 |
cterm_instantiate [(cterm_of thy (head_of lhs), |
45700 | 170 |
cterm_of thy (fold_rev (fn (_, T) => fn t => Abs ("z", T, t)) params (Bound 0)))] exhaustion; |
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
171 |
in compose_tac (false, exhaustion', nprems_of exhaustion) i end); |
5177 | 172 |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
173 |
|
5177 | 174 |
(********************** Internal description of datatypes *********************) |
175 |
||
176 |
datatype dtyp = |
|
45822 | 177 |
DtTFree of string * sort |
45821 | 178 |
| DtType of string * dtyp list |
5177 | 179 |
| DtRec of int; |
180 |
||
181 |
(* information about datatypes *) |
|
182 |
||
16901 | 183 |
(* index, datatype name, type arguments, constructor name, types of constructor's arguments *) |
8404 | 184 |
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list; |
185 |
||
31737 | 186 |
type info = |
5177 | 187 |
{index : int, |
8404 | 188 |
descr : descr, |
32718 | 189 |
inject : thm list, |
32900 | 190 |
distinct : thm list, |
32727 | 191 |
induct : thm, |
192 |
inducts : thm list, |
|
32718 | 193 |
exhaust : thm, |
194 |
nchotomy : thm, |
|
5177 | 195 |
rec_names : string list, |
196 |
rec_rewrites : thm list, |
|
197 |
case_name : string, |
|
198 |
case_rewrites : thm list, |
|
10120 | 199 |
case_cong : thm, |
32718 | 200 |
weak_case_cong : thm, |
32727 | 201 |
split : thm, |
202 |
split_asm: thm}; |
|
5177 | 203 |
|
45700 | 204 |
fun mk_Free s T i = Free (s ^ string_of_int i, T); |
5177 | 205 |
|
45822 | 206 |
fun subst_DtTFree _ substs (T as DtTFree a) = the_default T (AList.lookup (op =) substs a) |
45700 | 207 |
| subst_DtTFree i substs (DtType (name, ts)) = DtType (name, map (subst_DtTFree i substs) ts) |
5177 | 208 |
| subst_DtTFree i _ (DtRec j) = DtRec (i + j); |
209 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
210 |
exception Datatype; |
14887 | 211 |
exception Datatype_Empty of string; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
212 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
213 |
fun dest_DtTFree (DtTFree a) = a |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
214 |
| dest_DtTFree _ = raise Datatype; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
215 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
216 |
fun dest_DtRec (DtRec i) = i |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
217 |
| dest_DtRec _ = raise Datatype; |
5177 | 218 |
|
219 |
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts |
|
220 |
| is_rec_type (DtRec _) = true |
|
221 |
| is_rec_type _ = false; |
|
222 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
223 |
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
|
224 |
| strip_dtyp T = ([], T); |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
225 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
226 |
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
|
227 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
228 |
fun mk_fun_dtyp [] U = U |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
229 |
| 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
|
230 |
|
13707
55670a70a5f9
name_of_type now replaces non-identifiers by dummy names.
berghofe
parents:
13641
diff
changeset
|
231 |
fun name_of_typ (Type (s, Ts)) = |
45700 | 232 |
let val s' = Long_Name.base_name s in |
233 |
space_implode "_" |
|
234 |
(filter_out (equal "") (map name_of_typ Ts) @ |
|
235 |
[if Lexicon.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 |
||
45822 | 239 |
fun dtyp_of_typ _ (TFree a) = DtTFree a |
5177 | 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 |
45700 | 243 |
NONE => DtType (tname, map (dtyp_of_typ new_dts) Ts) |
244 |
| SOME vs => |
|
45839
43a5b86bc102
'datatype' specifications allow explicit sort constraints;
wenzelm
parents:
45822
diff
changeset
|
245 |
if map (try dest_TFree) Ts = map SOME vs then |
45700 | 246 |
DtRec (find_index (curry op = tname o fst) new_dts) |
45822 | 247 |
else error ("Illegal occurrence of recursive type " ^ quote tname)); |
5177 | 248 |
|
45822 | 249 |
fun typ_of_dtyp descr (DtTFree a) = TFree a |
250 |
| typ_of_dtyp descr (DtRec i) = |
|
45700 | 251 |
let val (s, ds, _) = the (AList.lookup (op =) descr i) |
45822 | 252 |
in Type (s, map (typ_of_dtyp descr) ds) end |
253 |
| typ_of_dtyp descr (DtType (s, ds)) = Type (s, map (typ_of_dtyp descr) ds); |
|
5177 | 254 |
|
255 |
(* find all non-recursive types in datatype description *) |
|
256 |
||
45822 | 257 |
fun get_nonrec_types descr = |
258 |
map (typ_of_dtyp descr) (fold (fn (_, (_, _, constrs)) => |
|
33244 | 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 |
||
45822 | 263 |
fun get_rec_types descr = map (fn (_ , (s, ds, _)) => |
264 |
Type (s, map (typ_of_dtyp descr) ds)) descr; |
|
5177 | 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 |
|
45822 | 268 |
fun get_branching_types descr = |
269 |
map (typ_of_dtyp descr) |
|
45700 | 270 |
(fold |
271 |
(fn (_, (_, _, constrs)) => |
|
272 |
fold (fn (_, cargs) => fold (strip_dtyp #> fst #> fold (insert op =)) cargs) constrs) |
|
273 |
descr []); |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
274 |
|
45700 | 275 |
fun get_arities descr = |
276 |
fold |
|
277 |
(fn (_, (_, _, constrs)) => |
|
278 |
fold (fn (_, cargs) => |
|
279 |
fold (insert op =) (map (length o fst o strip_dtyp) (filter is_rec_type cargs))) constrs) |
|
280 |
descr []; |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
281 |
|
31605 | 282 |
(* interpret construction of datatype *) |
283 |
||
45700 | 284 |
fun interpret_construction descr vs {atyp, dtyp} = |
31605 | 285 |
let |
45822 | 286 |
val typ_of = |
287 |
typ_of_dtyp descr #> |
|
288 |
map_atyps (fn TFree (a, _) => TFree (a, the (AList.lookup (op =) vs a)) | T => T); |
|
45700 | 289 |
fun interpT dT = |
290 |
(case strip_dtyp dT of |
|
291 |
(dTs, DtRec l) => |
|
31605 | 292 |
let |
45700 | 293 |
val (tyco, dTs', _) = the (AList.lookup (op =) descr l); |
45822 | 294 |
val Ts = map typ_of dTs; |
295 |
val Ts' = map typ_of dTs'; |
|
31605 | 296 |
val is_proper = forall (can dest_TFree) Ts'; |
297 |
in dtyp Ts (l, is_proper) (tyco, Ts') end |
|
45822 | 298 |
| _ => atyp (typ_of dT)); |
31605 | 299 |
fun interpC (c, dTs) = (c, map interpT dTs); |
45822 | 300 |
fun interpD (_, (tyco, dTs, cs)) = ((tyco, map typ_of dTs), map interpC cs); |
31605 | 301 |
in map interpD descr end; |
302 |
||
5177 | 303 |
(* nonemptiness check for datatypes *) |
304 |
||
305 |
fun check_nonempty descr = |
|
306 |
let |
|
32952 | 307 |
val descr' = flat descr; |
5177 | 308 |
fun is_nonempty_dt is i = |
309 |
let |
|
45700 | 310 |
val (_, _, constrs) = the (AList.lookup (op =) descr' i); |
311 |
fun arg_nonempty (_, DtRec i) = |
|
312 |
if member (op =) is i then false |
|
313 |
else is_nonempty_dt (i :: is) i |
|
5177 | 314 |
| arg_nonempty _ = true; |
45700 | 315 |
in exists (forall (arg_nonempty o strip_dtyp) o snd) constrs end |
316 |
in |
|
317 |
assert_all (fn (i, _) => is_nonempty_dt [i] i) (hd descr) |
|
318 |
(fn (_, (s, _, _)) => raise Datatype_Empty s) |
|
5177 | 319 |
end; |
320 |
||
321 |
(* unfold a list of mutually recursive datatype specifications *) |
|
322 |
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *) |
|
323 |
(* need to be unfolded *) |
|
324 |
||
45822 | 325 |
fun unfold_datatypes ctxt orig_descr (dt_info : info Symtab.table) descr i = |
5177 | 326 |
let |
45700 | 327 |
fun typ_error T msg = |
328 |
error ("Non-admissible type expression\n" ^ |
|
45822 | 329 |
Syntax.string_of_typ ctxt (typ_of_dtyp (orig_descr @ descr) T) ^ "\n" ^ msg); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
330 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
331 |
fun get_dt_descr T i tname dts = |
17412 | 332 |
(case Symtab.lookup dt_info tname of |
45700 | 333 |
NONE => |
45822 | 334 |
typ_error T (quote tname ^ " is not a datatype - can't use it in nested recursion") |
45700 | 335 |
| SOME {index, descr, ...} => |
336 |
let |
|
337 |
val (_, vars, _) = the (AList.lookup (op =) descr index); |
|
45743 | 338 |
val subst = map dest_DtTFree vars ~~ dts |
45700 | 339 |
handle ListPair.UnequalLengths => |
45822 | 340 |
typ_error T ("Type constructor " ^ quote tname ^ |
341 |
" used with wrong number of arguments"); |
|
45700 | 342 |
in |
343 |
(i + index, |
|
344 |
map (fn (j, (tn, args, cs)) => |
|
345 |
(i + j, (tn, map (subst_DtTFree i subst) args, |
|
346 |
map (apsnd (map (subst_DtTFree i subst))) cs))) descr) |
|
347 |
end); |
|
5177 | 348 |
|
349 |
(* unfold a single constructor argument *) |
|
350 |
||
33244 | 351 |
fun unfold_arg T (i, Ts, descrs) = |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
352 |
if is_rec_type T then |
45700 | 353 |
let val (Us, U) = strip_dtyp T in |
354 |
if exists is_rec_type Us then |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
355 |
typ_error T "Non-strictly positive recursive occurrence of type" |
45700 | 356 |
else |
357 |
(case U of |
|
358 |
DtType (tname, dts) => |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
359 |
let |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
360 |
val (index, descr) = get_dt_descr T i tname dts; |
45700 | 361 |
val (descr', i') = |
45822 | 362 |
unfold_datatypes ctxt orig_descr dt_info descr (i + length descr); |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
363 |
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
|
364 |
| _ => (i, Ts @ [T], descrs)) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
365 |
end |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
366 |
else (i, Ts @ [T], descrs); |
5177 | 367 |
|
368 |
(* unfold a constructor *) |
|
369 |
||
33244 | 370 |
fun unfold_constr (cname, cargs) (i, constrs, descrs) = |
371 |
let val (i', cargs', descrs') = fold unfold_arg cargs (i, [], descrs) |
|
5177 | 372 |
in (i', constrs @ [(cname, cargs')], descrs') end; |
373 |
||
374 |
(* unfold a single datatype *) |
|
375 |
||
33244 | 376 |
fun unfold_datatype (j, (tname, tvars, constrs)) (i, dtypes, descrs) = |
377 |
let val (i', constrs', descrs') = fold unfold_constr constrs (i, [], descrs) |
|
378 |
in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs') end; |
|
5177 | 379 |
|
33244 | 380 |
val (i', descr', descrs) = fold unfold_datatype descr (i, [], []); |
5177 | 381 |
|
382 |
in (descr' :: descrs, i') end; |
|
383 |
||
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
384 |
(* find shortest path to constructor with no recursive arguments *) |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
385 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
386 |
fun find_nonempty descr is i = |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
387 |
let |
45700 | 388 |
fun arg_nonempty (_, DtRec i) = |
389 |
if member (op =) is i |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
390 |
then NONE |
45700 | 391 |
else Option.map (Integer.add 1 o snd) (find_nonempty descr (i :: is) i) |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
392 |
| arg_nonempty _ = SOME 0; |
33971 | 393 |
fun max_inf (SOME i) (SOME j) = SOME (Integer.max i j) |
33970 | 394 |
| max_inf _ _ = NONE; |
395 |
fun max xs = fold max_inf xs (SOME 0); |
|
396 |
val (_, _, constrs) = the (AList.lookup (op =) descr i); |
|
45700 | 397 |
val xs = |
398 |
sort (int_ord o pairself snd) |
|
399 |
(map_filter (fn (s, dts) => Option.map (pair s) |
|
400 |
(max (map (arg_nonempty o strip_dtyp) dts))) constrs) |
|
33970 | 401 |
in if null xs then NONE else SOME (hd xs) end; |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
402 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
403 |
fun find_shortest_path descr i = find_nonempty descr [i] i; |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
404 |
|
5177 | 405 |
end; |