author | wenzelm |
Wed, 24 Jun 2009 21:28:02 +0200 | |
changeset 31794 | 71af1fd6a5e4 |
parent 31784 | bd3486c57ba3 |
child 31868 | edd1f30c0477 |
permissions | -rw-r--r-- |
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31689
diff
changeset
|
1 |
(* Title: HOL/Tools/datatype.ML |
11539 | 2 |
Author: Stefan Berghofer, TU Muenchen |
5177 | 3 |
|
6360 | 4 |
Datatype package for Isabelle/HOL. |
5177 | 5 |
*) |
6 |
||
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31689
diff
changeset
|
7 |
signature DATATYPE = |
6360 | 8 |
sig |
31735 | 9 |
include DATATYPE_COMMON |
10 |
val add_datatype : config -> string list -> (string list * binding * mixfix * |
|
31783
cfbe9609ceb1
add_datatypes does not yield particular rules any longer
haftmann
parents:
31781
diff
changeset
|
11 |
(binding * typ list * mixfix) list) list -> theory -> string list * theory |
31735 | 12 |
val datatype_cmd : string list -> (string list * binding * mixfix * |
13 |
(binding * string list * mixfix) list) list -> theory -> theory |
|
31781
861e675f01e6
add_datatype interface yields type names and less rules
haftmann
parents:
31775
diff
changeset
|
14 |
val rep_datatype : config -> (string list -> Proof.context -> Proof.context) |
31735 | 15 |
-> string list option -> term list -> theory -> Proof.state |
16 |
val rep_datatype_cmd : string list option -> string list -> theory -> Proof.state |
|
31784 | 17 |
val get_info : theory -> string -> info option |
18 |
val the_info : theory -> string -> info |
|
19 |
val the_descr : theory -> string list |
|
31735 | 20 |
-> descr * (string * sort) list * string list |
21 |
* (string list * string list) * (typ list * typ list) |
|
31784 | 22 |
val the_spec : theory -> string -> (string * sort) list * (string * typ list) list |
23 |
val get_constrs : theory -> string -> (string * typ) list option |
|
24 |
val get_all : theory -> info Symtab.table |
|
25 |
val info_of_constr : theory -> string -> info option |
|
26 |
val info_of_case : theory -> string -> info option |
|
31735 | 27 |
val interpretation : (config -> string list -> theory -> theory) -> theory -> theory |
28 |
val distinct_simproc : simproc |
|
29 |
val make_case : Proof.context -> bool -> string list -> term -> |
|
30 |
(term * term) list -> term * (term * (int * bool)) list |
|
31 |
val strip_case : Proof.context -> bool -> term -> (term * (term * term) list) option |
|
32 |
val read_typ: theory -> |
|
33 |
(typ list * (string * sort) list) * string -> typ list * (string * sort) list |
|
18708 | 34 |
val setup: theory -> theory |
5177 | 35 |
end; |
36 |
||
31723
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
haftmann
parents:
31689
diff
changeset
|
37 |
structure Datatype : DATATYPE = |
5177 | 38 |
struct |
39 |
||
40 |
open DatatypeAux; |
|
41 |
||
6360 | 42 |
|
22846 | 43 |
(* theory data *) |
5177 | 44 |
|
16430 | 45 |
structure DatatypesData = TheoryDataFun |
22846 | 46 |
( |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
47 |
type T = |
31735 | 48 |
{types: info Symtab.table, |
49 |
constrs: info Symtab.table, |
|
50 |
cases: info Symtab.table}; |
|
5177 | 51 |
|
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
52 |
val empty = |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
53 |
{types = Symtab.empty, constrs = Symtab.empty, cases = Symtab.empty}; |
6556 | 54 |
val copy = I; |
16430 | 55 |
val extend = I; |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
56 |
fun merge _ |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
57 |
({types = types1, constrs = constrs1, cases = cases1}, |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
58 |
{types = types2, constrs = constrs2, cases = cases2}) = |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
59 |
{types = Symtab.merge (K true) (types1, types2), |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
60 |
constrs = Symtab.merge (K true) (constrs1, constrs2), |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
61 |
cases = Symtab.merge (K true) (cases1, cases2)}; |
22846 | 62 |
); |
5177 | 63 |
|
31784 | 64 |
val get_all = #types o DatatypesData.get; |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
65 |
val map_datatypes = DatatypesData.map; |
22846 | 66 |
|
5177 | 67 |
|
68 |
(** theory information about datatypes **) |
|
69 |
||
31735 | 70 |
fun put_dt_infos (dt_infos : (string * info) list) = |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
71 |
map_datatypes (fn {types, constrs, cases} => |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
72 |
{types = fold Symtab.update dt_infos types, |
31208
1ef2f0af7f26
parse translations for cases are conservative wrt. overloaded constructors
haftmann
parents:
30431
diff
changeset
|
73 |
constrs = fold Symtab.default (*conservative wrt. overloaded constructors*) |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
74 |
(maps (fn (_, info as {descr, index, ...}) => map (rpair info o fst) |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
75 |
(#3 (the (AList.lookup op = descr index)))) dt_infos) constrs, |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
76 |
cases = fold Symtab.update |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
77 |
(map (fn (_, info as {case_name, ...}) => (case_name, info)) dt_infos) |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
78 |
cases}); |
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
79 |
|
31784 | 80 |
val get_info = Symtab.lookup o get_all; |
5177 | 81 |
|
31784 | 82 |
fun the_info thy name = (case get_info thy name of |
15531 | 83 |
SOME info => info |
84 |
| NONE => error ("Unknown datatype " ^ quote name)); |
|
5177 | 85 |
|
31784 | 86 |
val info_of_constr = Symtab.lookup o #constrs o DatatypesData.get; |
87 |
val info_of_case = Symtab.lookup o #cases o DatatypesData.get; |
|
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
88 |
|
31784 | 89 |
fun get_info_descr thy dtco = |
90 |
get_info thy dtco |
|
24711 | 91 |
|> Option.map (fn info as { descr, index, ... } => |
19346 | 92 |
(info, (((fn SOME (_, dtys, cos) => (dtys, cos)) o AList.lookup (op =) descr) index))); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
93 |
|
31784 | 94 |
fun the_spec thy dtco = |
18518 | 95 |
let |
31784 | 96 |
val info as { descr, index, sorts = raw_sorts, ... } = the_info thy dtco; |
26093 | 97 |
val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr index; |
98 |
val sorts = map ((fn v => (v, (the o AList.lookup (op =) raw_sorts) v)) |
|
99 |
o DatatypeAux.dest_DtTFree) dtys; |
|
100 |
val cos = map |
|
101 |
(fn (co, tys) => (co, map (DatatypeAux.typ_of_dtyp descr sorts) tys)) raw_cos; |
|
102 |
in (sorts, cos) end; |
|
18518 | 103 |
|
31784 | 104 |
fun the_descr thy (raw_tycos as raw_tyco :: _) = |
31604 | 105 |
let |
31784 | 106 |
val info = the_info thy raw_tyco; |
31604 | 107 |
val descr = #descr info; |
108 |
||
109 |
val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr (#index info); |
|
110 |
val vs = map ((fn v => (v, (the o AList.lookup (op =) (#sorts info)) v)) |
|
111 |
o dest_DtTFree) dtys; |
|
112 |
||
113 |
fun is_DtTFree (DtTFree _) = true |
|
114 |
| is_DtTFree _ = false |
|
115 |
val k = find_index (fn (_, (_, dTs, _)) => not (forall is_DtTFree dTs)) descr; |
|
116 |
val protoTs as (dataTs, _) = chop k descr |
|
117 |
|> (pairself o map) (fn (_, (tyco, dTs, _)) => (tyco, map (typ_of_dtyp descr vs) dTs)); |
|
118 |
||
119 |
val tycos = map fst dataTs; |
|
120 |
val _ = if gen_eq_set (op =) (tycos, raw_tycos) then () |
|
121 |
else error ("Type constructors " ^ commas (map quote raw_tycos) |
|
122 |
^ "do not belong exhaustively to one mutual recursive datatype"); |
|
123 |
||
124 |
val (Ts, Us) = (pairself o map) Type protoTs; |
|
125 |
||
126 |
val names = map Long_Name.base_name (the_default tycos (#alt_names info)); |
|
127 |
val (auxnames, _) = Name.make_context names |
|
128 |
|> fold_map (yield_singleton Name.variants o name_of_typ) Us |
|
129 |
||
130 |
in (descr, vs, tycos, (names, auxnames), (Ts, Us)) end; |
|
131 |
||
31784 | 132 |
fun get_constrs thy dtco = |
133 |
case try (the_spec thy) dtco |
|
19346 | 134 |
of SOME (sorts, cos) => |
135 |
let |
|
136 |
fun subst (v, sort) = TVar ((v, 0), sort); |
|
137 |
fun subst_ty (TFree v) = subst v |
|
138 |
| subst_ty ty = ty; |
|
139 |
val dty = Type (dtco, map subst sorts); |
|
140 |
fun mk_co (co, tys) = (co, map (Term.map_atyps subst_ty) tys ---> dty); |
|
141 |
in SOME (map mk_co cos) end |
|
142 |
| NONE => NONE; |
|
18518 | 143 |
|
5177 | 144 |
|
8437 | 145 |
(** induct method setup **) |
146 |
||
147 |
(* case names *) |
|
148 |
||
149 |
local |
|
150 |
||
151 |
fun dt_recs (DtTFree _) = [] |
|
21045 | 152 |
| dt_recs (DtType (_, dts)) = maps dt_recs dts |
8437 | 153 |
| dt_recs (DtRec i) = [i]; |
154 |
||
155 |
fun dt_cases (descr: descr) (_, args, constrs) = |
|
156 |
let |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30345
diff
changeset
|
157 |
fun the_bname i = Long_Name.base_name (#1 (the (AList.lookup (op =) descr i))); |
21045 | 158 |
val bnames = map the_bname (distinct (op =) (maps dt_recs args)); |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30345
diff
changeset
|
159 |
in map (fn (c, _) => space_implode "_" (Long_Name.base_name c :: bnames)) constrs end; |
8437 | 160 |
|
161 |
||
162 |
fun induct_cases descr = |
|
21045 | 163 |
DatatypeProp.indexify_names (maps (dt_cases descr) (map #2 descr)); |
8437 | 164 |
|
21045 | 165 |
fun exhaust_cases descr i = dt_cases descr (the (AList.lookup (op =) descr i)); |
8437 | 166 |
|
167 |
in |
|
168 |
||
169 |
fun mk_case_names_induct descr = RuleCases.case_names (induct_cases descr); |
|
170 |
||
171 |
fun mk_case_names_exhausts descr new = |
|
172 |
map (RuleCases.case_names o exhaust_cases descr o #1) |
|
21045 | 173 |
(filter (fn ((_, (name, _, _))) => member (op =) new name) descr); |
8437 | 174 |
|
175 |
end; |
|
176 |
||
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
24626
diff
changeset
|
177 |
fun add_rules simps case_thms rec_thms inject distinct |
11345
cd605c85e421
improved iff_add_global, new function add_rules factoring out common behaviour
oheimb
parents:
10930
diff
changeset
|
178 |
weak_case_congs cong_att = |
29579 | 179 |
PureThy.add_thmss [((Binding.name "simps", simps), []), |
180 |
((Binding.empty, flat case_thms @ |
|
21045 | 181 |
flat distinct @ rec_thms), [Simplifier.simp_add]), |
29579 | 182 |
((Binding.empty, rec_thms), [Code.add_default_eqn_attribute]), |
183 |
((Binding.empty, flat inject), [iff_add]), |
|
184 |
((Binding.empty, map (fn th => th RS notE) (flat distinct)), [Classical.safe_elim NONE]), |
|
185 |
((Binding.empty, weak_case_congs), [cong_att])] |
|
21127 | 186 |
#> snd; |
11345
cd605c85e421
improved iff_add_global, new function add_rules factoring out common behaviour
oheimb
parents:
10930
diff
changeset
|
187 |
|
8437 | 188 |
|
189 |
(* add_cases_induct *) |
|
8306 | 190 |
|
19874 | 191 |
fun add_cases_induct infos induction thy = |
8306 | 192 |
let |
19874 | 193 |
val inducts = ProjectRule.projections (ProofContext.init thy) induction; |
8405 | 194 |
|
31735 | 195 |
fun named_rules (name, {index, exhaustion, ...}: info) = |
29579 | 196 |
[((Binding.empty, nth inducts index), [Induct.induct_type name]), |
197 |
((Binding.empty, exhaustion), [Induct.cases_type name])]; |
|
11805
b110a1ea90da
declare projected induction rules stemming from nested recursion;
wenzelm
parents:
11725
diff
changeset
|
198 |
fun unnamed_rule i = |
29579 | 199 |
((Binding.empty, nth inducts i), [Thm.kind_internal, Induct.induct_type ""]); |
18462 | 200 |
in |
19874 | 201 |
thy |> PureThy.add_thms |
21045 | 202 |
(maps named_rules infos @ |
19874 | 203 |
map unnamed_rule (length infos upto length inducts - 1)) |> snd |
29579 | 204 |
|> PureThy.add_thmss [((Binding.name "inducts", inducts), [])] |> snd |
18462 | 205 |
end; |
8306 | 206 |
|
5177 | 207 |
|
8405 | 208 |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
209 |
(**** simplification procedure for showing distinctness of constructors ****) |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
210 |
|
7060 | 211 |
fun stripT (i, Type ("fun", [_, T])) = stripT (i + 1, T) |
212 |
| stripT p = p; |
|
213 |
||
214 |
fun stripC (i, f $ x) = stripC (i + 1, f) |
|
215 |
| stripC p = p; |
|
216 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
217 |
val distinctN = "constr_distinct"; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
218 |
|
31784 | 219 |
fun distinct_rule thy ss tname eq_t = case #distinct (the_info thy tname) of |
26533
aeef55a3d1d5
Deleted code for axiomatic introduction of datatypes. Instead, the package
berghofe
parents:
26496
diff
changeset
|
220 |
FewConstrs thms => Goal.prove (Simplifier.the_context ss) [] [] eq_t (K |
25537 | 221 |
(EVERY [rtac eq_reflection 1, rtac iffI 1, rtac notE 1, |
222 |
atac 2, resolve_tac thms 1, etac FalseE 1])) |
|
223 |
| ManyConstrs (thm, simpset) => |
|
224 |
let |
|
225 |
val [In0_inject, In1_inject, In0_not_In1, In1_not_In0] = |
|
26343
0dd2eab7b296
simplified get_thm(s): back to plain name argument;
wenzelm
parents:
26336
diff
changeset
|
226 |
map (PureThy.get_thm (ThyInfo.the_theory "Datatype" thy)) |
25537 | 227 |
["In0_inject", "In1_inject", "In0_not_In1", "In1_not_In0"]; |
228 |
in |
|
229 |
Goal.prove (Simplifier.the_context ss) [] [] eq_t (K |
|
230 |
(EVERY [rtac eq_reflection 1, rtac iffI 1, dtac thm 1, |
|
231 |
full_simp_tac (Simplifier.inherit_context ss simpset) 1, |
|
232 |
REPEAT (dresolve_tac [In0_inject, In1_inject] 1), |
|
233 |
eresolve_tac [In0_not_In1 RS notE, In1_not_In0 RS notE] 1, |
|
234 |
etac FalseE 1])) |
|
235 |
end; |
|
236 |
||
19539 | 237 |
fun distinct_proc thy ss (t as Const ("op =", _) $ t1 $ t2) = |
7060 | 238 |
(case (stripC (0, t1), stripC (0, t2)) of |
239 |
((i, Const (cname1, T1)), (j, Const (cname2, T2))) => |
|
240 |
(case (stripT (0, T1), stripT (0, T2)) of |
|
241 |
((i', Type (tname1, _)), (j', Type (tname2, _))) => |
|
242 |
if tname1 = tname2 andalso not (cname1 = cname2) andalso i = i' andalso j = j' then |
|
31784 | 243 |
(case (get_info_descr thy) tname1 of |
19346 | 244 |
SOME (_, (_, constrs)) => let val cnames = map fst constrs |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
245 |
in if cname1 mem cnames andalso cname2 mem cnames then |
25537 | 246 |
SOME (distinct_rule thy ss tname1 |
247 |
(Logic.mk_equals (t, Const ("False", HOLogic.boolT)))) |
|
15531 | 248 |
else NONE |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
249 |
end |
15531 | 250 |
| NONE => NONE) |
251 |
else NONE |
|
252 |
| _ => NONE) |
|
253 |
| _ => NONE) |
|
20054 | 254 |
| distinct_proc _ _ _ = NONE; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
255 |
|
13462 | 256 |
val distinct_simproc = |
29064 | 257 |
Simplifier.simproc @{theory HOL} distinctN ["s = t"] distinct_proc; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
258 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
259 |
val dist_ss = HOL_ss addsimprocs [distinct_simproc]; |
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
260 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
261 |
val simproc_setup = |
26496
49ae9456eba9
purely functional setup of claset/simpset/clasimpset;
wenzelm
parents:
26343
diff
changeset
|
262 |
Simplifier.map_simpset (fn ss => ss addsimprocs [distinct_simproc]); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
263 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6851
diff
changeset
|
264 |
|
14799
a405aadff16c
Added more flexible parse / print translations for case expressions.
berghofe
parents:
14471
diff
changeset
|
265 |
(**** translation rules for case ****) |
a405aadff16c
Added more flexible parse / print translations for case expressions.
berghofe
parents:
14471
diff
changeset
|
266 |
|
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
267 |
fun make_case ctxt = DatatypeCase.make_case |
31784 | 268 |
(info_of_constr (ProofContext.theory_of ctxt)) ctxt; |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
269 |
|
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
270 |
fun strip_case ctxt = DatatypeCase.strip_case |
31784 | 271 |
(info_of_case (ProofContext.theory_of ctxt)); |
14799
a405aadff16c
Added more flexible parse / print translations for case expressions.
berghofe
parents:
14471
diff
changeset
|
272 |
|
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
273 |
fun add_case_tr' case_names thy = |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
274 |
Sign.add_advanced_trfuns ([], [], |
24711 | 275 |
map (fn case_name => |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
276 |
let val case_name' = Sign.const_syntax_name thy case_name |
31784 | 277 |
in (case_name', DatatypeCase.case_tr' info_of_case case_name') |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
278 |
end) case_names, []) thy; |
14799
a405aadff16c
Added more flexible parse / print translations for case expressions.
berghofe
parents:
14471
diff
changeset
|
279 |
|
a405aadff16c
Added more flexible parse / print translations for case expressions.
berghofe
parents:
14471
diff
changeset
|
280 |
val trfun_setup = |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
281 |
Sign.add_advanced_trfuns ([], |
31784 | 282 |
[("_case_syntax", DatatypeCase.case_tr true info_of_constr)], |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
283 |
[], []); |
14799
a405aadff16c
Added more flexible parse / print translations for case expressions.
berghofe
parents:
14471
diff
changeset
|
284 |
|
a405aadff16c
Added more flexible parse / print translations for case expressions.
berghofe
parents:
14471
diff
changeset
|
285 |
|
5177 | 286 |
(* prepare types *) |
287 |
||
27277
7b7ce2d7fafe
export read_typ/cert_typ -- version with regular context operations;
wenzelm
parents:
27261
diff
changeset
|
288 |
fun read_typ thy ((Ts, sorts), str) = |
5177 | 289 |
let |
27277
7b7ce2d7fafe
export read_typ/cert_typ -- version with regular context operations;
wenzelm
parents:
27261
diff
changeset
|
290 |
val ctxt = ProofContext.init thy |
7b7ce2d7fafe
export read_typ/cert_typ -- version with regular context operations;
wenzelm
parents:
27261
diff
changeset
|
291 |
|> fold (Variable.declare_typ o TFree) sorts; |
7b7ce2d7fafe
export read_typ/cert_typ -- version with regular context operations;
wenzelm
parents:
27261
diff
changeset
|
292 |
val T = Syntax.read_typ ctxt str; |
7b7ce2d7fafe
export read_typ/cert_typ -- version with regular context operations;
wenzelm
parents:
27261
diff
changeset
|
293 |
in (Ts @ [T], Term.add_tfreesT T sorts) end; |
5177 | 294 |
|
295 |
fun cert_typ sign ((Ts, sorts), raw_T) = |
|
296 |
let |
|
297 |
val T = Type.no_tvars (Sign.certify_typ sign raw_T) handle |
|
298 |
TYPE (msg, _, _) => error msg; |
|
27277
7b7ce2d7fafe
export read_typ/cert_typ -- version with regular context operations;
wenzelm
parents:
27261
diff
changeset
|
299 |
val sorts' = Term.add_tfreesT T sorts; |
5177 | 300 |
in (Ts @ [T], |
18964 | 301 |
case duplicates (op =) (map fst sorts') of |
5177 | 302 |
[] => sorts' |
303 |
| dups => error ("Inconsistent sort constraints for " ^ commas dups)) |
|
304 |
end; |
|
305 |
||
306 |
||
307 |
(**** make datatype info ****) |
|
308 |
||
25677
8b2ddc6e7be1
- Removed redundant head_len field in datatype_info
berghofe
parents:
25537
diff
changeset
|
309 |
fun make_dt_info alt_names descr sorts induct reccomb_names rec_thms |
10121 | 310 |
(((((((((i, (_, (tname, _, _))), case_name), case_thms), |
311 |
exhaustion_thm), distinct_thm), inject), nchotomy), case_cong), weak_case_cong) = |
|
312 |
(tname, |
|
313 |
{index = i, |
|
25677
8b2ddc6e7be1
- Removed redundant head_len field in datatype_info
berghofe
parents:
25537
diff
changeset
|
314 |
alt_names = alt_names, |
10121 | 315 |
descr = descr, |
18319
c52b139ebde0
Added new component "sorts" to record datatype_info.
berghofe
parents:
18314
diff
changeset
|
316 |
sorts = sorts, |
10121 | 317 |
rec_names = reccomb_names, |
318 |
rec_rewrites = rec_thms, |
|
319 |
case_name = case_name, |
|
320 |
case_rewrites = case_thms, |
|
321 |
induction = induct, |
|
322 |
exhaustion = exhaustion_thm, |
|
323 |
distinct = distinct_thm, |
|
324 |
inject = inject, |
|
325 |
nchotomy = nchotomy, |
|
326 |
case_cong = case_cong, |
|
327 |
weak_case_cong = weak_case_cong}); |
|
5177 | 328 |
|
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
329 |
structure DatatypeInterpretation = InterpretationFun |
31735 | 330 |
(type T = config * string list val eq: T * T -> bool = eq_snd op =); |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
331 |
fun interpretation f = DatatypeInterpretation.interpretation (uncurry f); |
24626
85eceef2edc7
introduced generic concepts for theory interpretators
haftmann
parents:
24624
diff
changeset
|
332 |
|
5177 | 333 |
|
334 |
(******************* definitional introduction of datatypes *******************) |
|
335 |
||
31735 | 336 |
fun add_datatype_def (config : config) new_type_names descr sorts types_syntax constr_syntax dt_info |
8437 | 337 |
case_names_induct case_names_exhausts thy = |
5177 | 338 |
let |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
339 |
val _ = message config ("Proofs for datatype(s) " ^ commas_quote new_type_names); |
5177 | 340 |
|
18314 | 341 |
val ((inject, distinct, dist_rewrites, simproc_dists, induct), thy2) = thy |> |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
342 |
DatatypeRepProofs.representation_proofs config dt_info new_type_names descr sorts |
8437 | 343 |
types_syntax constr_syntax case_names_induct; |
5177 | 344 |
|
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
345 |
val (casedist_thms, thy3) = DatatypeAbsProofs.prove_casedist_thms config new_type_names descr |
8437 | 346 |
sorts induct case_names_exhausts thy2; |
18314 | 347 |
val ((reccomb_names, rec_thms), thy4) = DatatypeAbsProofs.prove_primrec_thms |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
348 |
config new_type_names descr sorts dt_info inject dist_rewrites |
20054 | 349 |
(Simplifier.theory_context thy3 dist_ss) induct thy3; |
18314 | 350 |
val ((case_thms, case_names), thy6) = DatatypeAbsProofs.prove_case_thms |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
351 |
config new_type_names descr sorts reccomb_names rec_thms thy4; |
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
352 |
val (split_thms, thy7) = DatatypeAbsProofs.prove_split_thms config new_type_names |
5177 | 353 |
descr sorts inject dist_rewrites casedist_thms case_thms thy6; |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
354 |
val (nchotomys, thy8) = DatatypeAbsProofs.prove_nchotomys config new_type_names |
5177 | 355 |
descr sorts casedist_thms thy7; |
18314 | 356 |
val (case_congs, thy9) = DatatypeAbsProofs.prove_case_congs new_type_names |
5177 | 357 |
descr sorts nchotomys case_thms thy8; |
18314 | 358 |
val (weak_case_congs, thy10) = DatatypeAbsProofs.prove_weak_case_congs new_type_names |
8601 | 359 |
descr sorts thy9; |
5177 | 360 |
|
31361
3e900a2acaed
Fixed broken code dealing with alternative names.
berghofe
parents:
31247
diff
changeset
|
361 |
val dt_infos = map |
3e900a2acaed
Fixed broken code dealing with alternative names.
berghofe
parents:
31247
diff
changeset
|
362 |
(make_dt_info (SOME new_type_names) (flat descr) sorts induct reccomb_names rec_thms) |
31781
861e675f01e6
add_datatype interface yields type names and less rules
haftmann
parents:
31775
diff
changeset
|
363 |
((0 upto length (hd descr) - 1) ~~ hd descr ~~ case_names ~~ case_thms ~~ |
10121 | 364 |
casedist_thms ~~ simproc_dists ~~ inject ~~ nchotomys ~~ case_congs ~~ weak_case_congs); |
5177 | 365 |
|
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
24626
diff
changeset
|
366 |
val simps = flat (distinct @ inject @ case_thms) @ rec_thms; |
31781
861e675f01e6
add_datatype interface yields type names and less rules
haftmann
parents:
31775
diff
changeset
|
367 |
val dt_names = map fst dt_infos; |
5177 | 368 |
|
18518 | 369 |
val thy12 = |
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
24626
diff
changeset
|
370 |
thy10 |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
371 |
|> add_case_tr' case_names |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
372 |
|> Sign.add_path (space_implode "_" new_type_names) |
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
24626
diff
changeset
|
373 |
|> add_rules simps case_thms rec_thms inject distinct |
18728 | 374 |
weak_case_congs (Simplifier.attrib (op addcongs)) |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
375 |
|> put_dt_infos dt_infos |
18518 | 376 |
|> add_cases_induct dt_infos induct |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
377 |
|> Sign.parent_path |
18518 | 378 |
|> store_thmss "splits" new_type_names (map (fn (x, y) => [x, y]) split_thms) |> snd |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
379 |
|> DatatypeInterpretation.data (config, map fst dt_infos); |
31783
cfbe9609ceb1
add_datatypes does not yield particular rules any longer
haftmann
parents:
31781
diff
changeset
|
380 |
in (dt_names, thy12) end; |
5177 | 381 |
|
382 |
||
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
383 |
(*********************** declare existing type as datatype *********************) |
5177 | 384 |
|
31735 | 385 |
fun prove_rep_datatype (config : config) alt_names new_type_names descr sorts induct inject half_distinct thy = |
5177 | 386 |
let |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
387 |
val ((_, [induct']), _) = |
31794
71af1fd6a5e4
renamed Variable.import_thms to Variable.import (back again cf. ed7aa5a350ef -- Alice is no longer supported);
wenzelm
parents:
31784
diff
changeset
|
388 |
Variable.importT [induct] (Variable.thm_context induct); |
5177 | 389 |
|
390 |
fun err t = error ("Ill-formed predicate in induction rule: " ^ |
|
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
391 |
Syntax.string_of_term_global thy t); |
5177 | 392 |
|
393 |
fun get_typ (t as _ $ Var (_, Type (tname, Ts))) = |
|
25888
48cc198b9ac5
Eliminated DatatypeAux.dest_TFree to avoid clashes
berghofe
parents:
25677
diff
changeset
|
394 |
((tname, map (fst o dest_TFree) Ts) handle TERM _ => err t) |
5177 | 395 |
| get_typ t = err t; |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
396 |
val dtnames = map get_typ (HOLogic.dest_conj (HOLogic.dest_Trueprop (Thm.concl_of induct'))); |
5177 | 397 |
|
31784 | 398 |
val dt_info = get_all thy; |
5177 | 399 |
|
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27277
diff
changeset
|
400 |
val distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct; |
21419 | 401 |
val (case_names_induct, case_names_exhausts) = |
402 |
(mk_case_names_induct descr, mk_case_names_exhausts descr (map #1 dtnames)); |
|
8437 | 403 |
|
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
404 |
val _ = message config ("Proofs for datatype(s) " ^ commas_quote new_type_names); |
5177 | 405 |
|
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
406 |
val (casedist_thms, thy2) = thy |> |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
407 |
DatatypeAbsProofs.prove_casedist_thms config new_type_names [descr] sorts induct |
8437 | 408 |
case_names_exhausts; |
18314 | 409 |
val ((reccomb_names, rec_thms), thy3) = DatatypeAbsProofs.prove_primrec_thms |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
410 |
config new_type_names [descr] sorts dt_info inject distinct |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
411 |
(Simplifier.theory_context thy2 dist_ss) induct thy2; |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
412 |
val ((case_thms, case_names), thy4) = DatatypeAbsProofs.prove_case_thms |
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
413 |
config new_type_names [descr] sorts reccomb_names rec_thms thy3; |
18314 | 414 |
val (split_thms, thy5) = DatatypeAbsProofs.prove_split_thms |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
415 |
config new_type_names [descr] sorts inject distinct casedist_thms case_thms thy4; |
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
416 |
val (nchotomys, thy6) = DatatypeAbsProofs.prove_nchotomys config new_type_names |
5177 | 417 |
[descr] sorts casedist_thms thy5; |
18314 | 418 |
val (case_congs, thy7) = DatatypeAbsProofs.prove_case_congs new_type_names |
5177 | 419 |
[descr] sorts nchotomys case_thms thy6; |
18314 | 420 |
val (weak_case_congs, thy8) = DatatypeAbsProofs.prove_weak_case_congs new_type_names |
8601 | 421 |
[descr] sorts thy7; |
5177 | 422 |
|
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
423 |
val ((_, [induct']), thy10) = |
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
24626
diff
changeset
|
424 |
thy8 |
18377 | 425 |
|> store_thmss "inject" new_type_names inject |
426 |
||>> store_thmss "distinct" new_type_names distinct |
|
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
427 |
||> Sign.add_path (space_implode "_" new_type_names) |
29579 | 428 |
||>> PureThy.add_thms [((Binding.name "induct", induct), [case_names_induct])]; |
9149 | 429 |
|
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
430 |
val dt_infos = map (make_dt_info alt_names descr sorts induct' reccomb_names rec_thms) |
10121 | 431 |
((0 upto length descr - 1) ~~ descr ~~ case_names ~~ case_thms ~~ casedist_thms ~~ |
432 |
map FewConstrs distinct ~~ inject ~~ nchotomys ~~ case_congs ~~ weak_case_congs); |
|
5177 | 433 |
|
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
24626
diff
changeset
|
434 |
val simps = flat (distinct @ inject @ case_thms) @ rec_thms; |
31781
861e675f01e6
add_datatype interface yields type names and less rules
haftmann
parents:
31775
diff
changeset
|
435 |
val dt_names = map fst dt_infos; |
5177 | 436 |
|
19599 | 437 |
val thy11 = |
438 |
thy10 |
|
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
439 |
|> add_case_tr' case_names |
24699
c6674504103f
datatype interpretators for size and datatype_realizer
haftmann
parents:
24626
diff
changeset
|
440 |
|> add_rules simps case_thms rec_thms inject distinct |
19599 | 441 |
weak_case_congs (Simplifier.attrib (op addcongs)) |
22777
2fc921376a86
- Moved parse / print translations for case to datatype_case.ML
berghofe
parents:
22709
diff
changeset
|
442 |
|> put_dt_infos dt_infos |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
443 |
|> add_cases_induct dt_infos induct' |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
444 |
|> Sign.parent_path |
19599 | 445 |
|> store_thmss "splits" new_type_names (map (fn (x, y) => [x, y]) split_thms) |
446 |
|> snd |
|
31781
861e675f01e6
add_datatype interface yields type names and less rules
haftmann
parents:
31775
diff
changeset
|
447 |
|> DatatypeInterpretation.data (config, dt_names); |
861e675f01e6
add_datatype interface yields type names and less rules
haftmann
parents:
31775
diff
changeset
|
448 |
in (dt_names, thy11) end; |
5177 | 449 |
|
31735 | 450 |
fun gen_rep_datatype prep_term (config : config) after_qed alt_names raw_ts thy = |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
451 |
let |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
452 |
fun constr_of_term (Const (c, T)) = (c, T) |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
453 |
| constr_of_term t = |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
454 |
error ("Not a constant: " ^ Syntax.string_of_term_global thy t); |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
455 |
fun no_constr (c, T) = error ("Bad constructor: " |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
456 |
^ Sign.extern_const thy c ^ "::" |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
457 |
^ Syntax.string_of_typ_global thy T); |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
458 |
fun type_of_constr (cT as (_, T)) = |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
459 |
let |
29270
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
29064
diff
changeset
|
460 |
val frees = OldTerm.typ_tfrees T; |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
461 |
val (tyco, vs) = ((apsnd o map) (dest_TFree) o dest_Type o snd o strip_type) T |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
462 |
handle TYPE _ => no_constr cT |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
463 |
val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else (); |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
464 |
val _ = if length frees <> length vs then no_constr cT else (); |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
465 |
in (tyco, (vs, cT)) end; |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
466 |
|
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
467 |
val raw_cs = AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts); |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
468 |
val _ = case map_filter (fn (tyco, _) => |
31784 | 469 |
if Symtab.defined (get_all thy) tyco then SOME tyco else NONE) raw_cs |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
470 |
of [] => () |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
471 |
| tycos => error ("Type(s) " ^ commas (map quote tycos) |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
472 |
^ " already represented inductivly"); |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
473 |
val raw_vss = maps (map (map snd o fst) o snd) raw_cs; |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
474 |
val ms = case distinct (op =) (map length raw_vss) |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
475 |
of [n] => 0 upto n - 1 |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
476 |
| _ => error ("Different types in given constructors"); |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
477 |
fun inter_sort m = map (fn xs => nth xs m) raw_vss |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
478 |
|> Library.foldr1 (Sorts.inter_sort (Sign.classes_of thy)) |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
479 |
val sorts = map inter_sort ms; |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
480 |
val vs = Name.names Name.context Name.aT sorts; |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
481 |
|
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
482 |
fun norm_constr (raw_vs, (c, T)) = (c, map_atyps |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
483 |
(TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T); |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
484 |
|
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
485 |
val cs = map (apsnd (map norm_constr)) raw_cs; |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
486 |
val dtyps_of_typ = map (dtyp_of_typ (map (rpair (map fst vs) o fst) cs)) |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
487 |
o fst o strip_type; |
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30345
diff
changeset
|
488 |
val new_type_names = map Long_Name.base_name (the_default (map fst cs) alt_names); |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
489 |
|
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
490 |
fun mk_spec (i, (tyco, constr)) = (i, (tyco, |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
491 |
map (DtTFree o fst) vs, |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
492 |
(map o apsnd) dtyps_of_typ constr)) |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
493 |
val descr = map_index mk_spec cs; |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
494 |
val injs = DatatypeProp.make_injs [descr] vs; |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27277
diff
changeset
|
495 |
val half_distincts = map snd (DatatypeProp.make_distincts [descr] vs); |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
496 |
val ind = DatatypeProp.make_ind [descr] vs; |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27277
diff
changeset
|
497 |
val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts]; |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
498 |
|
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
499 |
fun after_qed' raw_thms = |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
500 |
let |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27277
diff
changeset
|
501 |
val [[[induct]], injs, half_distincts] = |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
502 |
unflat rules (map Drule.zero_var_indexes_list raw_thms); |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
503 |
(*FIXME somehow dubious*) |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
504 |
in |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
505 |
ProofContext.theory_result |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
506 |
(prove_rep_datatype config alt_names new_type_names descr vs induct injs half_distincts) |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
507 |
#-> after_qed |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
508 |
end; |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
509 |
in |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
510 |
thy |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
511 |
|> ProofContext.init |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
512 |
|> Proof.theorem_i NONE after_qed' ((map o map) (rpair []) (flat rules)) |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
513 |
end; |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
514 |
|
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
515 |
val rep_datatype = gen_rep_datatype Sign.cert_term; |
31735 | 516 |
val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global default_config (K I); |
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
517 |
|
5177 | 518 |
|
11958 | 519 |
|
5177 | 520 |
(******************************** add datatype ********************************) |
521 |
||
31735 | 522 |
fun gen_add_datatype prep_typ (config : config) new_type_names dts thy = |
5177 | 523 |
let |
20820
58693343905f
removed obsolete Datatype_Universe.thy (cf. Datatype.thy);
wenzelm
parents:
20715
diff
changeset
|
524 |
val _ = Theory.requires thy "Datatype" "datatype definitions"; |
5177 | 525 |
|
526 |
(* this theory is used just for parsing *) |
|
527 |
||
528 |
val tmp_thy = thy |> |
|
5892 | 529 |
Theory.copy |> |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24711
diff
changeset
|
530 |
Sign.add_types (map (fn (tvs, tname, mx, _) => |
5177 | 531 |
(tname, length tvs, mx)) dts); |
532 |
||
5661 | 533 |
val (tyvars, _, _, _)::_ = dts; |
5177 | 534 |
val (new_dts, types_syntax) = ListPair.unzip (map (fn (tvs, tname, mx, _) => |
30345 | 535 |
let val full_tname = Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname) |
18964 | 536 |
in (case duplicates (op =) tvs of |
5661 | 537 |
[] => if eq_set (tyvars, tvs) then ((full_tname, tvs), (tname, mx)) |
538 |
else error ("Mutually recursive datatypes must have same type parameters") |
|
30345 | 539 |
| dups => error ("Duplicate parameter(s) for datatype " ^ quote (Binding.str_of tname) ^ |
5177 | 540 |
" : " ^ commas dups)) |
541 |
end) dts); |
|
542 |
||
18964 | 543 |
val _ = (case duplicates (op =) (map fst new_dts) @ duplicates (op =) new_type_names of |
5177 | 544 |
[] => () | dups => error ("Duplicate datatypes: " ^ commas dups)); |
545 |
||
31361
3e900a2acaed
Fixed broken code dealing with alternative names.
berghofe
parents:
31247
diff
changeset
|
546 |
fun prep_dt_spec ((tvs, tname, mx, constrs), tname') (dts', constr_syntax, sorts, i) = |
5177 | 547 |
let |
21045 | 548 |
fun prep_constr (cname, cargs, mx') (constrs, constr_syntax', sorts') = |
5279 | 549 |
let |
20597 | 550 |
val (cargs', sorts'') = Library.foldl (prep_typ tmp_thy) (([], sorts'), cargs); |
29270
0eade173f77e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents:
29064
diff
changeset
|
551 |
val _ = (case fold (curry OldTerm.add_typ_tfree_names) cargs' [] \\ tvs of |
5279 | 552 |
[] => () |
553 |
| vs => error ("Extra type variables on rhs: " ^ commas vs)) |
|
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
554 |
in (constrs @ [((if #flat_names config then Sign.full_name tmp_thy else |
31361
3e900a2acaed
Fixed broken code dealing with alternative names.
berghofe
parents:
31247
diff
changeset
|
555 |
Sign.full_name_path tmp_thy tname') |
30345 | 556 |
(Binding.map_name (Syntax.const_name mx') cname), |
5177 | 557 |
map (dtyp_of_typ new_dts) cargs')], |
558 |
constr_syntax' @ [(cname, mx')], sorts'') |
|
30345 | 559 |
end handle ERROR msg => cat_error msg |
560 |
("The error above occured in constructor " ^ quote (Binding.str_of cname) ^ |
|
561 |
" of datatype " ^ quote (Binding.str_of tname)); |
|
5177 | 562 |
|
563 |
val (constrs', constr_syntax', sorts') = |
|
21045 | 564 |
fold prep_constr constrs ([], [], sorts) |
5177 | 565 |
|
8405 | 566 |
in |
18964 | 567 |
case duplicates (op =) (map fst constrs') of |
5177 | 568 |
[] => |
30345 | 569 |
(dts' @ [(i, (Sign.full_name tmp_thy (Binding.map_name (Syntax.type_name mx) tname), |
5177 | 570 |
map DtTFree tvs, constrs'))], |
571 |
constr_syntax @ [constr_syntax'], sorts', i + 1) |
|
572 |
| dups => error ("Duplicate constructors " ^ commas dups ^ |
|
30345 | 573 |
" in datatype " ^ quote (Binding.str_of tname)) |
5177 | 574 |
end; |
575 |
||
31361
3e900a2acaed
Fixed broken code dealing with alternative names.
berghofe
parents:
31247
diff
changeset
|
576 |
val (dts', constr_syntax, sorts', i) = |
3e900a2acaed
Fixed broken code dealing with alternative names.
berghofe
parents:
31247
diff
changeset
|
577 |
fold prep_dt_spec (dts ~~ new_type_names) ([], [], [], 0); |
20597 | 578 |
val sorts = sorts' @ (map (rpair (Sign.defaultS tmp_thy)) (tyvars \\ map fst sorts')); |
31784 | 579 |
val dt_info = get_all thy; |
20597 | 580 |
val (descr, _) = unfold_datatypes tmp_thy dts' sorts dt_info dts' i; |
14887 | 581 |
val _ = check_nonempty descr handle (exn as Datatype_Empty s) => |
31668
a616e56a5ec8
datatype packages: record datatype_config for configuration flags; less verbose signatures
haftmann
parents:
31604
diff
changeset
|
582 |
if #strict config then error ("Nonemptiness check failed for datatype " ^ s) |
14887 | 583 |
else raise exn; |
5177 | 584 |
|
21045 | 585 |
val descr' = flat descr; |
8437 | 586 |
val case_names_induct = mk_case_names_induct descr'; |
587 |
val case_names_exhausts = mk_case_names_exhausts descr' (map #1 new_dts); |
|
5177 | 588 |
in |
26533
aeef55a3d1d5
Deleted code for axiomatic introduction of datatypes. Instead, the package
berghofe
parents:
26496
diff
changeset
|
589 |
add_datatype_def |
31735 | 590 |
(config : config) new_type_names descr sorts types_syntax constr_syntax dt_info |
8437 | 591 |
case_names_induct case_names_exhausts thy |
5177 | 592 |
end; |
593 |
||
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
594 |
val add_datatype = gen_add_datatype cert_typ; |
31735 | 595 |
val datatype_cmd = snd ooo gen_add_datatype read_typ default_config; |
5177 | 596 |
|
6360 | 597 |
|
18451 | 598 |
|
6360 | 599 |
(** package setup **) |
600 |
||
601 |
(* setup theory *) |
|
602 |
||
18708 | 603 |
val setup = |
27002
215d64dc971e
moved distinctness_limit to datatype_rep_proofs.ML
haftmann
parents:
26939
diff
changeset
|
604 |
DatatypeRepProofs.distinctness_limit_setup #> |
24098
f1eb34ae33af
replaced dtK ref by datatype_distinctness_limit configuration option;
wenzelm
parents:
22994
diff
changeset
|
605 |
simproc_setup #> |
24626
85eceef2edc7
introduced generic concepts for theory interpretators
haftmann
parents:
24624
diff
changeset
|
606 |
trfun_setup #> |
24711 | 607 |
DatatypeInterpretation.init; |
6360 | 608 |
|
609 |
||
610 |
(* outer syntax *) |
|
611 |
||
31735 | 612 |
local |
6360 | 613 |
|
31735 | 614 |
structure P = OuterParse and K = OuterKeyword |
6360 | 615 |
|
31735 | 616 |
fun prep_datatype_decls args = |
6360 | 617 |
let |
30345 | 618 |
val names = map |
619 |
(fn ((((NONE, _), t), _), _) => Binding.name_of t | ((((SOME t, _), _), _), _) => t) args; |
|
12876
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12708
diff
changeset
|
620 |
val specs = map (fn ((((_, vs), t), mx), cons) => |
a70df1e5bf10
got rid of explicit marginal comments (now stripped earlier from input);
wenzelm
parents:
12708
diff
changeset
|
621 |
(vs, t, mx, map (fn ((x, y), z) => (x, y, z)) cons)) args; |
31735 | 622 |
in (names, specs) end; |
623 |
||
624 |
val parse_datatype_decl = |
|
625 |
(Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- P.type_args -- P.binding -- P.opt_infix -- |
|
626 |
(P.$$$ "=" |-- P.enum1 "|" (P.binding -- Scan.repeat P.typ -- P.opt_mixfix))); |
|
627 |
||
628 |
val parse_datatype_decls = P.and_list1 parse_datatype_decl >> prep_datatype_decls; |
|
629 |
||
630 |
in |
|
6360 | 631 |
|
24867 | 632 |
val _ = |
6723 | 633 |
OuterSyntax.command "datatype" "define inductive datatypes" K.thy_decl |
31735 | 634 |
(parse_datatype_decls >> (fn (names, specs) => Toplevel.theory (datatype_cmd names specs))); |
6360 | 635 |
|
24867 | 636 |
val _ = |
27104
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
637 |
OuterSyntax.command "rep_datatype" "represent existing types inductively" K.thy_goal |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
638 |
(Scan.option (P.$$$ "(" |-- Scan.repeat1 P.name --| P.$$$ ")") -- Scan.repeat1 P.term |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
639 |
>> (fn (alt_names, ts) => Toplevel.print |
791607529f6d
rep_datatype command now takes list of constructors as input arguments
haftmann
parents:
27097
diff
changeset
|
640 |
o Toplevel.theory_to_proof (rep_datatype_cmd alt_names ts))); |
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
641 |
|
30391
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
642 |
end; |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
643 |
|
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
644 |
|
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
645 |
(* document antiquotation *) |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
646 |
|
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
647 |
val _ = ThyOutput.antiquotation "datatype" Args.tyname |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
648 |
(fn {source = src, context = ctxt, ...} => fn dtco => |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
649 |
let |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
650 |
val thy = ProofContext.theory_of ctxt; |
31784 | 651 |
val (vs, cos) = the_spec thy dtco; |
30391
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
652 |
val ty = Type (dtco, map TFree vs); |
30431 | 653 |
fun pretty_typ_bracket (ty as Type (_, _ :: _)) = |
654 |
Pretty.enclose "(" ")" [Syntax.pretty_typ ctxt ty] |
|
655 |
| pretty_typ_bracket ty = |
|
656 |
Syntax.pretty_typ ctxt ty; |
|
30391
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
657 |
fun pretty_constr (co, tys) = |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
658 |
(Pretty.block o Pretty.breaks) |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
659 |
(Syntax.pretty_term ctxt (Const (co, tys ---> ty)) :: |
30431 | 660 |
map pretty_typ_bracket tys); |
30391
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
661 |
val pretty_datatype = |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
662 |
Pretty.block |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
663 |
(Pretty.command "datatype" :: Pretty.brk 1 :: |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
664 |
Syntax.pretty_typ ctxt ty :: |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
665 |
Pretty.str " =" :: Pretty.brk 1 :: |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
666 |
flat (separate [Pretty.brk 1, Pretty.str "| "] |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
667 |
(map (single o pretty_constr) cos))); |
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
668 |
in ThyOutput.output (ThyOutput.maybe_pretty_source (K pretty_datatype) src [()]) end); |
26111 | 669 |
|
6385
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
670 |
end; |
5a6570458d9e
rep_datatype: '_i' version, attributes, outer syntax;
wenzelm
parents:
6360
diff
changeset
|
671 |