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