author | blanchet |
Mon, 18 Aug 2014 17:19:58 +0200 | |
changeset 57983 | 6edc3529bb4e |
parent 57884 | 36b5691b81a5 |
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, |
|
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57884
diff
changeset
|
30 |
case_cong_weak : thm, |
32964 | 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, [])])] |
52788 | 100 |
#-> (fn [(_, res)] => pair res)) (tnames ~~ attss ~~ thmss); |
5177 | 101 |
|
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
102 |
fun store_thmss name tnames = store_thmss_atts name tnames (replicate (length tnames) []); |
18101 | 103 |
|
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
104 |
fun store_thms_atts name tnames attss thms = |
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
105 |
fold_map (fn ((tname, atts), thm) => |
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
106 |
Global_Theory.note_thmss "" |
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
107 |
[((Binding.qualify true tname (Binding.name name), atts), [([thm], [])])] |
52788 | 108 |
#-> (fn [(_, [res])] => pair res)) (tnames ~~ attss ~~ thms); |
8435 | 109 |
|
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
110 |
fun store_thms name tnames = store_thms_atts name tnames (replicate (length tnames) []); |
8435 | 111 |
|
5177 | 112 |
|
113 |
(* split theorem thm_1 & ... & thm_n into n theorems *) |
|
114 |
||
115 |
fun split_conj_thm th = |
|
45901
cea7cd0c7e99
eliminated old-fashioned Global_Theory.add_thms(s);
wenzelm
parents:
45839
diff
changeset
|
116 |
((th RS conjunct1) :: split_conj_thm (th RS conjunct2)) handle THM _ => [th]; |
5177 | 117 |
|
38795
848be46708dc
formerly unnamed infix conjunction and disjunction now named HOL.conj and HOL.disj
haftmann
parents:
36692
diff
changeset
|
118 |
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
|
119 |
val mk_disj = foldr1 (HOLogic.mk_binop @{const_name HOL.disj}); |
5177 | 120 |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
121 |
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
|
122 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
123 |
|
5177 | 124 |
(* instantiate induction rule *) |
125 |
||
45735 | 126 |
fun ind_tac indrule indnames = CSUBGOAL (fn (cgoal, i) => |
5177 | 127 |
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
|
128 |
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
|
129 |
val goal = term_of cgoal; |
8305 | 130 |
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
|
131 |
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
|
132 |
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
|
133 |
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
|
134 |
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
|
135 |
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
|
136 |
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
|
137 |
if null indnames then I |
45738 | 138 |
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
|
139 |
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
|
140 |
(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
|
141 |
NONE => |
45738 | 142 |
(case flt (Term.add_frees t2 []) of |
143 |
[(s, T)] => SOME (absfree (s, T) t2) |
|
25676
f3815084e89e
- Removed redundant head_len field in datatype_info
berghofe
parents:
24712
diff
changeset
|
144 |
| _ => NONE) |
45700 | 145 |
| SOME (_ $ t') => SOME (Abs ("x", fastype_of t', abstract_over (t', t2)))); |
146 |
val insts = |
|
147 |
map_filter (fn (t, u) => |
|
148 |
(case abstr (getP u) of |
|
149 |
NONE => NONE |
|
150 |
| SOME u' => SOME (t |> getP |> snd |> head_of |> cert, cert u'))) (ts ~~ ts'); |
|
151 |
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
|
152 |
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
|
153 |
|
5177 | 154 |
|
155 |
(* perform exhaustive case analysis on last parameter of subgoal i *) |
|
156 |
||
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
157 |
fun exh_tac exh_thm_of = CSUBGOAL (fn (cgoal, i) => |
5177 | 158 |
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
|
159 |
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
|
160 |
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
|
161 |
val params = Logic.strip_params goal; |
5177 | 162 |
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
|
163 |
val exhaustion = Thm.lift_rule cgoal (exh_thm_of tname); |
5177 | 164 |
val prem' = hd (prems_of exhaustion); |
165 |
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
|
166 |
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
|
167 |
cterm_instantiate [(cterm_of thy (head_of lhs), |
45700 | 168 |
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
|
169 |
in compose_tac (false, exhaustion', nprems_of exhaustion) i end); |
5177 | 170 |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
171 |
|
5177 | 172 |
(********************** Internal description of datatypes *********************) |
173 |
||
174 |
datatype dtyp = |
|
45822 | 175 |
DtTFree of string * sort |
45821 | 176 |
| DtType of string * dtyp list |
5177 | 177 |
| DtRec of int; |
178 |
||
179 |
(* information about datatypes *) |
|
180 |
||
16901 | 181 |
(* index, datatype name, type arguments, constructor name, types of constructor's arguments *) |
8404 | 182 |
type descr = (int * (string * dtyp list * (string * dtyp list) list)) list; |
183 |
||
31737 | 184 |
type info = |
5177 | 185 |
{index : int, |
8404 | 186 |
descr : descr, |
32718 | 187 |
inject : thm list, |
32900 | 188 |
distinct : thm list, |
32727 | 189 |
induct : thm, |
190 |
inducts : thm list, |
|
32718 | 191 |
exhaust : thm, |
192 |
nchotomy : thm, |
|
5177 | 193 |
rec_names : string list, |
194 |
rec_rewrites : thm list, |
|
195 |
case_name : string, |
|
196 |
case_rewrites : thm list, |
|
10120 | 197 |
case_cong : thm, |
57983
6edc3529bb4e
reordered some (co)datatype property names for more consistency
blanchet
parents:
57884
diff
changeset
|
198 |
case_cong_weak : thm, |
32727 | 199 |
split : thm, |
200 |
split_asm: thm}; |
|
5177 | 201 |
|
45700 | 202 |
fun mk_Free s T i = Free (s ^ string_of_int i, T); |
5177 | 203 |
|
45822 | 204 |
fun subst_DtTFree _ substs (T as DtTFree a) = the_default T (AList.lookup (op =) substs a) |
45700 | 205 |
| subst_DtTFree i substs (DtType (name, ts)) = DtType (name, map (subst_DtTFree i substs) ts) |
5177 | 206 |
| subst_DtTFree i _ (DtRec j) = DtRec (i + j); |
207 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
208 |
exception Datatype; |
14887 | 209 |
exception Datatype_Empty of string; |
7015
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_DtTFree (DtTFree a) = a |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
212 |
| dest_DtTFree _ = raise Datatype; |
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_DtRec (DtRec i) = i |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
215 |
| dest_DtRec _ = raise Datatype; |
5177 | 216 |
|
217 |
fun is_rec_type (DtType (_, dts)) = exists is_rec_type dts |
|
218 |
| is_rec_type (DtRec _) = true |
|
219 |
| is_rec_type _ = false; |
|
220 |
||
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
221 |
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
|
222 |
| strip_dtyp T = ([], T); |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
223 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
224 |
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
|
225 |
|
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
226 |
fun mk_fun_dtyp [] U = U |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
227 |
| 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
|
228 |
|
13707
55670a70a5f9
name_of_type now replaces non-identifiers by dummy names.
berghofe
parents:
13641
diff
changeset
|
229 |
fun name_of_typ (Type (s, Ts)) = |
45700 | 230 |
let val s' = Long_Name.base_name s in |
231 |
space_implode "_" |
|
232 |
(filter_out (equal "") (map name_of_typ Ts) @ |
|
50239 | 233 |
[if Symbol_Pos.is_identifier s' then s' else "x"]) |
13707
55670a70a5f9
name_of_type now replaces non-identifiers by dummy names.
berghofe
parents:
13641
diff
changeset
|
234 |
end |
9740 | 235 |
| name_of_typ _ = ""; |
236 |
||
45822 | 237 |
fun dtyp_of_typ _ (TFree a) = DtTFree a |
5177 | 238 |
| dtyp_of_typ _ (TVar _) = error "Illegal schematic type variable(s)" |
239 |
| dtyp_of_typ new_dts (Type (tname, Ts)) = |
|
17485 | 240 |
(case AList.lookup (op =) new_dts tname of |
45700 | 241 |
NONE => DtType (tname, map (dtyp_of_typ new_dts) Ts) |
242 |
| SOME vs => |
|
45839
43a5b86bc102
'datatype' specifications allow explicit sort constraints;
wenzelm
parents:
45822
diff
changeset
|
243 |
if map (try dest_TFree) Ts = map SOME vs then |
45700 | 244 |
DtRec (find_index (curry op = tname o fst) new_dts) |
45822 | 245 |
else error ("Illegal occurrence of recursive type " ^ quote tname)); |
5177 | 246 |
|
45822 | 247 |
fun typ_of_dtyp descr (DtTFree a) = TFree a |
248 |
| typ_of_dtyp descr (DtRec i) = |
|
45700 | 249 |
let val (s, ds, _) = the (AList.lookup (op =) descr i) |
45822 | 250 |
in Type (s, map (typ_of_dtyp descr) ds) end |
251 |
| typ_of_dtyp descr (DtType (s, ds)) = Type (s, map (typ_of_dtyp descr) ds); |
|
5177 | 252 |
|
253 |
(* find all non-recursive types in datatype description *) |
|
254 |
||
45822 | 255 |
fun get_nonrec_types descr = |
256 |
map (typ_of_dtyp descr) (fold (fn (_, (_, _, constrs)) => |
|
33244 | 257 |
fold (fn (_, cargs) => union (op =) (filter_out is_rec_type cargs)) constrs) descr []); |
5177 | 258 |
|
259 |
(* get all recursive types in datatype description *) |
|
260 |
||
45822 | 261 |
fun get_rec_types descr = map (fn (_ , (s, ds, _)) => |
262 |
Type (s, map (typ_of_dtyp descr) ds)) descr; |
|
5177 | 263 |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
264 |
(* get all branching types *) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
265 |
|
45822 | 266 |
fun get_branching_types descr = |
267 |
map (typ_of_dtyp descr) |
|
45700 | 268 |
(fold |
269 |
(fn (_, (_, _, constrs)) => |
|
270 |
fold (fn (_, cargs) => fold (strip_dtyp #> fst #> fold (insert op =)) cargs) constrs) |
|
271 |
descr []); |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
272 |
|
45700 | 273 |
fun get_arities descr = |
274 |
fold |
|
275 |
(fn (_, (_, _, constrs)) => |
|
276 |
fold (fn (_, cargs) => |
|
277 |
fold (insert op =) (map (length o fst o strip_dtyp) (filter is_rec_type cargs))) constrs) |
|
278 |
descr []; |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
279 |
|
31605 | 280 |
(* interpret construction of datatype *) |
281 |
||
45700 | 282 |
fun interpret_construction descr vs {atyp, dtyp} = |
31605 | 283 |
let |
45822 | 284 |
val typ_of = |
285 |
typ_of_dtyp descr #> |
|
286 |
map_atyps (fn TFree (a, _) => TFree (a, the (AList.lookup (op =) vs a)) | T => T); |
|
45700 | 287 |
fun interpT dT = |
288 |
(case strip_dtyp dT of |
|
289 |
(dTs, DtRec l) => |
|
31605 | 290 |
let |
45700 | 291 |
val (tyco, dTs', _) = the (AList.lookup (op =) descr l); |
45822 | 292 |
val Ts = map typ_of dTs; |
293 |
val Ts' = map typ_of dTs'; |
|
31605 | 294 |
val is_proper = forall (can dest_TFree) Ts'; |
295 |
in dtyp Ts (l, is_proper) (tyco, Ts') end |
|
45822 | 296 |
| _ => atyp (typ_of dT)); |
31605 | 297 |
fun interpC (c, dTs) = (c, map interpT dTs); |
45822 | 298 |
fun interpD (_, (tyco, dTs, cs)) = ((tyco, map typ_of dTs), map interpC cs); |
31605 | 299 |
in map interpD descr end; |
300 |
||
5177 | 301 |
(* nonemptiness check for datatypes *) |
302 |
||
303 |
fun check_nonempty descr = |
|
304 |
let |
|
32952 | 305 |
val descr' = flat descr; |
5177 | 306 |
fun is_nonempty_dt is i = |
307 |
let |
|
45700 | 308 |
val (_, _, constrs) = the (AList.lookup (op =) descr' i); |
309 |
fun arg_nonempty (_, DtRec i) = |
|
310 |
if member (op =) is i then false |
|
311 |
else is_nonempty_dt (i :: is) i |
|
5177 | 312 |
| arg_nonempty _ = true; |
45700 | 313 |
in exists (forall (arg_nonempty o strip_dtyp) o snd) constrs end |
57884
36b5691b81a5
tuned -- avoid confusion with @{assert} for system failures (exception Fail);
wenzelm
parents:
52788
diff
changeset
|
314 |
val _ = hd descr |> forall (fn (i, (s, _, _)) => |
36b5691b81a5
tuned -- avoid confusion with @{assert} for system failures (exception Fail);
wenzelm
parents:
52788
diff
changeset
|
315 |
is_nonempty_dt [i] i orelse raise Datatype_Empty s) |
36b5691b81a5
tuned -- avoid confusion with @{assert} for system failures (exception Fail);
wenzelm
parents:
52788
diff
changeset
|
316 |
in () end; |
5177 | 317 |
|
318 |
(* unfold a list of mutually recursive datatype specifications *) |
|
319 |
(* all types of the form DtType (dt_name, [..., DtRec _, ...]) *) |
|
320 |
(* need to be unfolded *) |
|
321 |
||
45822 | 322 |
fun unfold_datatypes ctxt orig_descr (dt_info : info Symtab.table) descr i = |
5177 | 323 |
let |
45700 | 324 |
fun typ_error T msg = |
325 |
error ("Non-admissible type expression\n" ^ |
|
45822 | 326 |
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
|
327 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
328 |
fun get_dt_descr T i tname dts = |
17412 | 329 |
(case Symtab.lookup dt_info tname of |
45700 | 330 |
NONE => |
45822 | 331 |
typ_error T (quote tname ^ " is not a datatype - can't use it in nested recursion") |
45700 | 332 |
| SOME {index, descr, ...} => |
333 |
let |
|
334 |
val (_, vars, _) = the (AList.lookup (op =) descr index); |
|
45743 | 335 |
val subst = map dest_DtTFree vars ~~ dts |
45700 | 336 |
handle ListPair.UnequalLengths => |
45822 | 337 |
typ_error T ("Type constructor " ^ quote tname ^ |
338 |
" used with wrong number of arguments"); |
|
45700 | 339 |
in |
340 |
(i + index, |
|
341 |
map (fn (j, (tn, args, cs)) => |
|
342 |
(i + j, (tn, map (subst_DtTFree i subst) args, |
|
343 |
map (apsnd (map (subst_DtTFree i subst))) cs))) descr) |
|
344 |
end); |
|
5177 | 345 |
|
346 |
(* unfold a single constructor argument *) |
|
347 |
||
33244 | 348 |
fun unfold_arg T (i, Ts, descrs) = |
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
349 |
if is_rec_type T then |
45700 | 350 |
let val (Us, U) = strip_dtyp T in |
351 |
if exists is_rec_type Us then |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
352 |
typ_error T "Non-strictly positive recursive occurrence of type" |
45700 | 353 |
else |
354 |
(case U of |
|
355 |
DtType (tname, dts) => |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
356 |
let |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
357 |
val (index, descr) = get_dt_descr T i tname dts; |
45700 | 358 |
val (descr', i') = |
45822 | 359 |
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
|
360 |
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
|
361 |
| _ => (i, Ts @ [T], descrs)) |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
362 |
end |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
11539
diff
changeset
|
363 |
else (i, Ts @ [T], descrs); |
5177 | 364 |
|
365 |
(* unfold a constructor *) |
|
366 |
||
33244 | 367 |
fun unfold_constr (cname, cargs) (i, constrs, descrs) = |
368 |
let val (i', cargs', descrs') = fold unfold_arg cargs (i, [], descrs) |
|
5177 | 369 |
in (i', constrs @ [(cname, cargs')], descrs') end; |
370 |
||
371 |
(* unfold a single datatype *) |
|
372 |
||
33244 | 373 |
fun unfold_datatype (j, (tname, tvars, constrs)) (i, dtypes, descrs) = |
374 |
let val (i', constrs', descrs') = fold unfold_constr constrs (i, [], descrs) |
|
375 |
in (i', dtypes @ [(j, (tname, tvars, constrs'))], descrs') end; |
|
5177 | 376 |
|
33244 | 377 |
val (i', descr', descrs) = fold unfold_datatype descr (i, [], []); |
5177 | 378 |
|
379 |
in (descr' :: descrs, i') end; |
|
380 |
||
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
|
381 |
(* 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
|
382 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
383 |
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
|
384 |
let |
45700 | 385 |
fun arg_nonempty (_, DtRec i) = |
386 |
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
|
387 |
then NONE |
45700 | 388 |
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
|
389 |
| arg_nonempty _ = SOME 0; |
33971 | 390 |
fun max_inf (SOME i) (SOME j) = SOME (Integer.max i j) |
33970 | 391 |
| max_inf _ _ = NONE; |
392 |
fun max xs = fold max_inf xs (SOME 0); |
|
393 |
val (_, _, constrs) = the (AList.lookup (op =) descr i); |
|
45700 | 394 |
val xs = |
395 |
sort (int_ord o pairself snd) |
|
396 |
(map_filter (fn (s, dts) => Option.map (pair s) |
|
397 |
(max (map (arg_nonempty o strip_dtyp) dts))) constrs) |
|
33970 | 398 |
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
|
399 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33338
diff
changeset
|
400 |
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
|
401 |
|
5177 | 402 |
end; |