author | wenzelm |
Mon, 27 Jun 2011 22:20:49 +0200 | |
changeset 43564 | 9864182c6bad |
parent 43329 | 84472e198515 |
child 43581 | c3e4d280bdeb |
permissions | -rw-r--r-- |
33970 | 1 |
(* Title: HOL/Tools/Datatype/datatype_data.ML |
11539 | 2 |
Author: Stefan Berghofer, TU Muenchen |
5177 | 3 |
|
33970 | 4 |
Datatype package: bookkeeping; interpretation of existing types as datatypes. |
5177 | 5 |
*) |
6 |
||
33970 | 7 |
signature DATATYPE_DATA = |
6360 | 8 |
sig |
33970 | 9 |
include DATATYPE_COMMON |
10 |
val derive_datatype_props : config -> string list -> string list option |
|
11 |
-> descr list -> (string * sort) list -> thm -> thm list list -> thm list list |
|
12 |
-> theory -> string list * theory |
|
13 |
val rep_datatype : config -> (string list -> Proof.context -> Proof.context) |
|
14 |
-> string list option -> term list -> theory -> Proof.state |
|
15 |
val rep_datatype_cmd : string list option -> string list -> theory -> Proof.state |
|
16 |
val get_info : theory -> string -> info option |
|
17 |
val the_info : theory -> string -> info |
|
18 |
val the_descr : theory -> string list |
|
19 |
-> descr * (string * sort) list * string list |
|
20 |
* string * (string list * string list) * (typ list * typ list) |
|
21 |
val the_spec : theory -> string -> (string * sort) list * (string * typ list) list |
|
22 |
val all_distincts : theory -> typ list -> thm 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 * typ -> info option |
|
26 |
val info_of_case : theory -> string -> info option |
|
27 |
val interpretation : (config -> string list -> theory -> theory) -> theory -> theory |
|
28 |
val make_case : Proof.context -> Datatype_Case.config -> string list -> term -> |
|
43253
fa3c61dc9f2c
removed generation of instantiated pattern set, which is never actually used
krauss
parents:
42361
diff
changeset
|
29 |
(term * term) list -> term |
33970 | 30 |
val strip_case : Proof.context -> bool -> term -> (term * (term * term) list) option |
31 |
val read_typ: theory -> string -> (string * sort) list -> typ * (string * sort) list |
|
32 |
val cert_typ: theory -> typ -> (string * sort) list -> typ * (string * sort) list |
|
33 |
val mk_case_names_induct: descr -> attribute |
|
34 |
val setup: theory -> theory |
|
32717
0e787c721fa3
re-established reasonable inner outline for module
haftmann
parents:
32712
diff
changeset
|
35 |
end; |
0e787c721fa3
re-established reasonable inner outline for module
haftmann
parents:
32712
diff
changeset
|
36 |
|
33970 | 37 |
structure Datatype_Data: DATATYPE_DATA = |
33963
977b94b64905
renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33959
diff
changeset
|
38 |
struct |
5177 | 39 |
|
33970 | 40 |
(** theory data **) |
41 |
||
42 |
(* data management *) |
|
43 |
||
44 |
structure DatatypesData = Theory_Data |
|
45 |
( |
|
46 |
type T = |
|
41423 | 47 |
{types: Datatype_Aux.info Symtab.table, |
48 |
constrs: (string * Datatype_Aux.info) list Symtab.table, |
|
49 |
cases: Datatype_Aux.info Symtab.table}; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
50 |
|
33970 | 51 |
val empty = |
52 |
{types = Symtab.empty, constrs = Symtab.empty, cases = Symtab.empty}; |
|
53 |
val extend = I; |
|
54 |
fun merge |
|
55 |
({types = types1, constrs = constrs1, cases = cases1}, |
|
56 |
{types = types2, constrs = constrs2, cases = cases2}) : T = |
|
57 |
{types = Symtab.merge (K true) (types1, types2), |
|
58 |
constrs = Symtab.join (K (AList.merge (op =) (K true))) (constrs1, constrs2), |
|
59 |
cases = Symtab.merge (K true) (cases1, cases2)}; |
|
60 |
); |
|
61 |
||
62 |
val get_all = #types o DatatypesData.get; |
|
63 |
val get_info = Symtab.lookup o get_all; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
64 |
|
33970 | 65 |
fun the_info thy name = |
66 |
(case get_info thy name of |
|
67 |
SOME info => info |
|
68 |
| NONE => error ("Unknown datatype " ^ quote name)); |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
69 |
|
33970 | 70 |
fun info_of_constr thy (c, T) = |
71 |
let |
|
72 |
val tab = Symtab.lookup_list ((#constrs o DatatypesData.get) thy) c; |
|
40844 | 73 |
val hint = case body_type T of Type (tyco, _) => SOME tyco | _ => NONE; |
41489
8e2b8649507d
standardized split_last/last_elem towards List.last;
wenzelm
parents:
41423
diff
changeset
|
74 |
val default = |
8e2b8649507d
standardized split_last/last_elem towards List.last;
wenzelm
parents:
41423
diff
changeset
|
75 |
if null tab then NONE |
8e2b8649507d
standardized split_last/last_elem towards List.last;
wenzelm
parents:
41423
diff
changeset
|
76 |
else SOME (snd (List.last tab)) |
33970 | 77 |
(*conservative wrt. overloaded constructors*); |
78 |
in case hint |
|
79 |
of NONE => default |
|
80 |
| SOME tyco => case AList.lookup (op =) tab tyco |
|
81 |
of NONE => default (*permissive*) |
|
82 |
| SOME info => SOME info |
|
83 |
end; |
|
84 |
||
85 |
val info_of_case = Symtab.lookup o #cases o DatatypesData.get; |
|
86 |
||
41423 | 87 |
fun register (dt_infos : (string * Datatype_Aux.info) list) = |
33970 | 88 |
DatatypesData.map (fn {types, constrs, cases} => |
89 |
{types = types |> fold Symtab.update dt_infos, |
|
90 |
constrs = constrs |> fold (fn (constr, dtname_info) => |
|
91 |
Symtab.map_default (constr, []) (cons dtname_info)) |
|
92 |
(maps (fn (dtname, info as {descr, index, ...}) => |
|
93 |
map (rpair (dtname, info) o fst) |
|
94 |
(#3 (the (AList.lookup op = descr index)))) dt_infos), |
|
95 |
cases = cases |> fold Symtab.update |
|
96 |
(map (fn (_, info as {case_name, ...}) => (case_name, info)) dt_infos)}); |
|
97 |
||
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
98 |
|
33970 | 99 |
(* complex queries *) |
100 |
||
101 |
fun the_spec thy dtco = |
|
102 |
let |
|
103 |
val { descr, index, sorts = raw_sorts, ... } = the_info thy dtco; |
|
104 |
val SOME (_, dtys, raw_cos) = AList.lookup (op =) descr index; |
|
105 |
val sorts = map ((fn v => (v, (the o AList.lookup (op =) raw_sorts) v)) |
|
106 |
o Datatype_Aux.dest_DtTFree) dtys; |
|
107 |
val cos = map |
|
108 |
(fn (co, tys) => (co, map (Datatype_Aux.typ_of_dtyp descr sorts) tys)) raw_cos; |
|
109 |
in (sorts, cos) end; |
|
110 |
||
111 |
fun the_descr thy (raw_tycos as raw_tyco :: _) = |
|
112 |
let |
|
113 |
val info = the_info thy raw_tyco; |
|
114 |
val descr = #descr info; |
|
115 |
||
116 |
val SOME (_, dtys, _) = AList.lookup (op =) descr (#index info); |
|
117 |
val vs = map ((fn v => (v, (the o AList.lookup (op =) (#sorts info)) v)) |
|
41423 | 118 |
o Datatype_Aux.dest_DtTFree) dtys; |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
119 |
|
41423 | 120 |
fun is_DtTFree (Datatype_Aux.DtTFree _) = true |
33970 | 121 |
| is_DtTFree _ = false |
122 |
val k = find_index (fn (_, (_, dTs, _)) => not (forall is_DtTFree dTs)) descr; |
|
41423 | 123 |
val protoTs as (dataTs, _) = |
124 |
chop k descr |
|
125 |
|> (pairself o map) |
|
126 |
(fn (_, (tyco, dTs, _)) => (tyco, map (Datatype_Aux.typ_of_dtyp descr vs) dTs)); |
|
33970 | 127 |
|
128 |
val tycos = map fst dataTs; |
|
129 |
val _ = if eq_set (op =) (tycos, raw_tycos) then () |
|
130 |
else error ("Type constructors " ^ commas (map quote raw_tycos) |
|
131 |
^ " do not belong exhaustively to one mutual recursive datatype"); |
|
132 |
||
133 |
val (Ts, Us) = (pairself o map) Type protoTs; |
|
134 |
||
135 |
val names = map Long_Name.base_name (the_default tycos (#alt_names info)); |
|
136 |
val (auxnames, _) = Name.make_context names |
|
43326
47cf4bc789aa
simplified Name.variant -- discontinued builtin fold_map;
wenzelm
parents:
43253
diff
changeset
|
137 |
|> fold_map (Name.variant o Datatype_Aux.name_of_typ) Us; |
33970 | 138 |
val prefix = space_implode "_" names; |
139 |
||
140 |
in (descr, vs, tycos, prefix, (names, auxnames), (Ts, Us)) end; |
|
141 |
||
142 |
fun all_distincts thy Ts = |
|
143 |
let |
|
144 |
fun add_tycos (Type (tyco, Ts)) = insert (op =) tyco #> fold add_tycos Ts |
|
145 |
| add_tycos _ = I; |
|
146 |
val tycos = fold add_tycos Ts []; |
|
147 |
in map_filter (Option.map #distinct o get_info thy) tycos end; |
|
148 |
||
149 |
fun get_constrs thy dtco = |
|
150 |
case try (the_spec thy) dtco |
|
151 |
of SOME (sorts, cos) => |
|
152 |
let |
|
153 |
fun subst (v, sort) = TVar ((v, 0), sort); |
|
154 |
fun subst_ty (TFree v) = subst v |
|
155 |
| subst_ty ty = ty; |
|
156 |
val dty = Type (dtco, map subst sorts); |
|
157 |
fun mk_co (co, tys) = (co, map (Term.map_atyps subst_ty) tys ---> dty); |
|
158 |
in SOME (map mk_co cos) end |
|
159 |
| NONE => NONE; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
160 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
161 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
162 |
|
33970 | 163 |
(** various auxiliary **) |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
164 |
|
33970 | 165 |
(* prepare datatype specifications *) |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
166 |
|
33970 | 167 |
fun read_typ thy str sorts = |
168 |
let |
|
42361 | 169 |
val ctxt = Proof_Context.init_global thy |
33970 | 170 |
|> fold (Variable.declare_typ o TFree) sorts; |
171 |
val T = Syntax.read_typ ctxt str; |
|
172 |
in (T, Term.add_tfreesT T sorts) end; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
173 |
|
33970 | 174 |
fun cert_typ sign raw_T sorts = |
175 |
let |
|
176 |
val T = Type.no_tvars (Sign.certify_typ sign raw_T) |
|
177 |
handle TYPE (msg, _, _) => error msg; |
|
178 |
val sorts' = Term.add_tfreesT T sorts; |
|
179 |
val _ = |
|
180 |
case duplicates (op =) (map fst sorts') of |
|
181 |
[] => () |
|
182 |
| dups => error ("Inconsistent sort constraints for " ^ commas dups) |
|
183 |
in (T, sorts') end; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
184 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
185 |
|
33970 | 186 |
(* case names *) |
187 |
||
188 |
local |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
189 |
|
41423 | 190 |
fun dt_recs (Datatype_Aux.DtTFree _) = [] |
191 |
| dt_recs (Datatype_Aux.DtType (_, dts)) = maps dt_recs dts |
|
192 |
| dt_recs (Datatype_Aux.DtRec i) = [i]; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
193 |
|
41423 | 194 |
fun dt_cases (descr: Datatype_Aux.descr) (_, args, constrs) = |
33970 | 195 |
let |
196 |
fun the_bname i = Long_Name.base_name (#1 (the (AList.lookup (op =) descr i))); |
|
197 |
val bnames = map the_bname (distinct (op =) (maps dt_recs args)); |
|
198 |
in map (fn (c, _) => space_implode "_" (Long_Name.base_name c :: bnames)) constrs end; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
199 |
|
33970 | 200 |
fun induct_cases descr = |
201 |
Datatype_Prop.indexify_names (maps (dt_cases descr) (map #2 descr)); |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
202 |
|
33970 | 203 |
fun exhaust_cases descr i = dt_cases descr (the (AList.lookup (op =) descr i)); |
204 |
||
205 |
in |
|
206 |
||
207 |
fun mk_case_names_induct descr = Rule_Cases.case_names (induct_cases descr); |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
208 |
|
33970 | 209 |
fun mk_case_names_exhausts descr new = |
210 |
map (Rule_Cases.case_names o exhaust_cases descr o #1) |
|
211 |
(filter (fn ((_, (name, _, _))) => member (op =) new name) descr); |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
212 |
|
33970 | 213 |
end; |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
214 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
215 |
|
33970 | 216 |
(* translation rules for case *) |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
217 |
|
33970 | 218 |
fun make_case ctxt = Datatype_Case.make_case |
42361 | 219 |
(info_of_constr (Proof_Context.theory_of ctxt)) ctxt; |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
220 |
|
33970 | 221 |
fun strip_case ctxt = Datatype_Case.strip_case |
42361 | 222 |
(info_of_case (Proof_Context.theory_of ctxt)); |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
223 |
|
33970 | 224 |
fun add_case_tr' case_names thy = |
225 |
Sign.add_advanced_trfuns ([], [], |
|
226 |
map (fn case_name => |
|
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
42289
diff
changeset
|
227 |
let val case_name' = Lexicon.mark_const case_name |
33970 | 228 |
in (case_name', Datatype_Case.case_tr' info_of_case case_name') |
229 |
end) case_names, []) thy; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
230 |
|
33970 | 231 |
val trfun_setup = |
232 |
Sign.add_advanced_trfuns ([], |
|
35124 | 233 |
[(@{syntax_const "_case_syntax"}, Datatype_Case.case_tr true info_of_constr)], |
33970 | 234 |
[], []); |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
235 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
236 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
237 |
|
33970 | 238 |
(** document antiquotation **) |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
239 |
|
43564
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
240 |
val antiq_setup = |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
241 |
Thy_Output.antiquotation @{binding datatype} (Args.type_name true) |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
242 |
(fn {source = src, context = ctxt, ...} => fn dtco => |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
243 |
let |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
244 |
val thy = Proof_Context.theory_of ctxt; |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
245 |
val (vs, cos) = the_spec thy dtco; |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
246 |
val ty = Type (dtco, map TFree vs); |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
247 |
val pretty_typ_bracket = Syntax.pretty_typ (Config.put pretty_priority 1001 ctxt); |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
248 |
fun pretty_constr (co, tys) = |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
249 |
Pretty.block (Pretty.breaks |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
250 |
(Syntax.pretty_term ctxt (Const (co, tys ---> ty)) :: |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
251 |
map pretty_typ_bracket tys)); |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
252 |
val pretty_datatype = |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
253 |
Pretty.block |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
254 |
(Pretty.command "datatype" :: Pretty.brk 1 :: |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
255 |
Syntax.pretty_typ ctxt ty :: |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
256 |
Pretty.str " =" :: Pretty.brk 1 :: |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
257 |
flat (separate [Pretty.brk 1, Pretty.str "| "] (map (single o pretty_constr) cos))); |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
258 |
in |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
259 |
Thy_Output.output ctxt |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
260 |
(Thy_Output.maybe_pretty_source (K (K pretty_datatype)) ctxt src [()]) |
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
261 |
end); |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
262 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
263 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
264 |
|
33970 | 265 |
(** abstract theory extensions relative to a datatype characterisation **) |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
266 |
|
33970 | 267 |
structure Datatype_Interpretation = Interpretation |
41423 | 268 |
(type T = Datatype_Aux.config * string list val eq: T * T -> bool = eq_snd op =); |
33970 | 269 |
fun interpretation f = Datatype_Interpretation.interpretation (uncurry f); |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
270 |
|
33970 | 271 |
fun make_dt_info alt_names descr sorts induct inducts rec_names rec_rewrites |
272 |
(index, (((((((((((_, (tname, _, _))), inject), distinct), |
|
273 |
exhaust), nchotomy), case_name), case_rewrites), case_cong), weak_case_cong), |
|
274 |
(split, split_asm))) = |
|
275 |
(tname, |
|
276 |
{index = index, |
|
277 |
alt_names = alt_names, |
|
278 |
descr = descr, |
|
279 |
sorts = sorts, |
|
280 |
inject = inject, |
|
281 |
distinct = distinct, |
|
282 |
induct = induct, |
|
283 |
inducts = inducts, |
|
284 |
exhaust = exhaust, |
|
285 |
nchotomy = nchotomy, |
|
286 |
rec_names = rec_names, |
|
287 |
rec_rewrites = rec_rewrites, |
|
288 |
case_name = case_name, |
|
289 |
case_rewrites = case_rewrites, |
|
290 |
case_cong = case_cong, |
|
291 |
weak_case_cong = weak_case_cong, |
|
292 |
split = split, |
|
293 |
split_asm = split_asm}); |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
294 |
|
33970 | 295 |
fun derive_datatype_props config dt_names alt_names descr sorts |
296 |
induct inject distinct thy1 = |
|
297 |
let |
|
298 |
val thy2 = thy1 |> Theory.checkpoint; |
|
299 |
val flat_descr = flat descr; |
|
300 |
val new_type_names = map Long_Name.base_name (the_default dt_names alt_names); |
|
41423 | 301 |
val _ = |
302 |
Datatype_Aux.message config |
|
303 |
("Deriving properties for datatype(s) " ^ commas_quote new_type_names); |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
304 |
|
33970 | 305 |
val (exhaust, thy3) = Datatype_Abs_Proofs.prove_casedist_thms config new_type_names |
306 |
descr sorts induct (mk_case_names_exhausts flat_descr dt_names) thy2; |
|
307 |
val (nchotomys, thy4) = Datatype_Abs_Proofs.prove_nchotomys config new_type_names |
|
308 |
descr sorts exhaust thy3; |
|
309 |
val ((rec_names, rec_rewrites), thy5) = Datatype_Abs_Proofs.prove_primrec_thms |
|
310 |
config new_type_names descr sorts (#inject o the o Symtab.lookup (get_all thy4)) |
|
41423 | 311 |
inject (distinct, all_distincts thy2 (Datatype_Aux.get_rec_types flat_descr sorts)) |
33970 | 312 |
induct thy4; |
313 |
val ((case_rewrites, case_names), thy6) = Datatype_Abs_Proofs.prove_case_thms |
|
314 |
config new_type_names descr sorts rec_names rec_rewrites thy5; |
|
315 |
val (case_congs, thy7) = Datatype_Abs_Proofs.prove_case_congs new_type_names |
|
316 |
descr sorts nchotomys case_rewrites thy6; |
|
317 |
val (weak_case_congs, thy8) = Datatype_Abs_Proofs.prove_weak_case_congs new_type_names |
|
318 |
descr sorts thy7; |
|
319 |
val (splits, thy9) = Datatype_Abs_Proofs.prove_split_thms |
|
320 |
config new_type_names descr sorts inject distinct exhaust case_rewrites thy8; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
321 |
|
42361 | 322 |
val inducts = Project_Rule.projections (Proof_Context.init_global thy2) induct; |
33970 | 323 |
val dt_infos = map_index |
324 |
(make_dt_info alt_names flat_descr sorts induct inducts rec_names rec_rewrites) |
|
325 |
(hd descr ~~ inject ~~ distinct ~~ exhaust ~~ nchotomys ~~ |
|
326 |
case_names ~~ case_rewrites ~~ case_congs ~~ weak_case_congs ~~ splits); |
|
327 |
val dt_names = map fst dt_infos; |
|
328 |
val prfx = Binding.qualify true (space_implode "_" new_type_names); |
|
329 |
val simps = flat (inject @ distinct @ case_rewrites) @ rec_rewrites; |
|
330 |
val named_rules = flat (map_index (fn (index, tname) => |
|
331 |
[((Binding.empty, [nth inducts index]), [Induct.induct_type tname]), |
|
332 |
((Binding.empty, [nth exhaust index]), [Induct.cases_type tname])]) dt_names); |
|
333 |
val unnamed_rules = map (fn induct => |
|
334 |
((Binding.empty, [induct]), [Rule_Cases.inner_rule, Induct.induct_type ""])) |
|
335 |
(drop (length dt_names) inducts); |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
336 |
in |
33970 | 337 |
thy9 |
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
38767
diff
changeset
|
338 |
|> Global_Theory.add_thmss ([((prfx (Binding.name "simps"), simps), []), |
33970 | 339 |
((prfx (Binding.name "inducts"), inducts), []), |
340 |
((prfx (Binding.name "splits"), maps (fn (x, y) => [x, y]) splits), []), |
|
341 |
((Binding.empty, flat case_rewrites @ flat distinct @ rec_rewrites), |
|
342 |
[Simplifier.simp_add]), |
|
343 |
((Binding.empty, rec_rewrites), [Code.add_default_eqn_attribute]), |
|
344 |
((Binding.empty, flat inject), [iff_add]), |
|
345 |
((Binding.empty, map (fn th => th RS notE) (flat distinct)), |
|
346 |
[Classical.safe_elim NONE]), |
|
34911
771830d3bd5e
Injectivity / distinctness theorems are now used to simplify induction rules.
berghofe
parents:
33970
diff
changeset
|
347 |
((Binding.empty, weak_case_congs), [Simplifier.attrib (op addcongs)]), |
36602
0cbcdfd9e527
slightly more standard induct_simp_add/del attributes;
wenzelm
parents:
36323
diff
changeset
|
348 |
((Binding.empty, flat (distinct @ inject)), [Induct.induct_simp_add])] @ |
0cbcdfd9e527
slightly more standard induct_simp_add/del attributes;
wenzelm
parents:
36323
diff
changeset
|
349 |
named_rules @ unnamed_rules) |
33970 | 350 |
|> snd |
351 |
|> add_case_tr' case_names |
|
352 |
|> register dt_infos |
|
353 |
|> Datatype_Interpretation.data (config, dt_names) |
|
354 |
|> pair dt_names |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
355 |
end; |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
356 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
357 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
358 |
|
33970 | 359 |
(** declare existing type as datatype **) |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
360 |
|
33970 | 361 |
fun prove_rep_datatype config dt_names alt_names descr sorts |
362 |
raw_inject half_distinct raw_induct thy1 = |
|
363 |
let |
|
364 |
val raw_distinct = (map o maps) (fn thm => [thm, thm RS not_sym]) half_distinct; |
|
365 |
val new_type_names = map Long_Name.base_name (the_default dt_names alt_names); |
|
40929
7ff03a5e044f
theorem names generated by the (rep_)datatype command now have mandatory qualifiers
huffman
parents:
40844
diff
changeset
|
366 |
val prfx = Binding.qualify true (space_implode "_" new_type_names); |
33970 | 367 |
val (((inject, distinct), [induct]), thy2) = |
368 |
thy1 |
|
41423 | 369 |
|> Datatype_Aux.store_thmss "inject" new_type_names raw_inject |
370 |
||>> Datatype_Aux.store_thmss "distinct" new_type_names raw_distinct |
|
39557
fe5722fce758
renamed structure PureThy to Pure_Thy and moved most content to Global_Theory, to emphasize that this is global-only;
wenzelm
parents:
38767
diff
changeset
|
371 |
||>> Global_Theory.add_thms |
40929
7ff03a5e044f
theorem names generated by the (rep_)datatype command now have mandatory qualifiers
huffman
parents:
40844
diff
changeset
|
372 |
[((prfx (Binding.name "induct"), raw_induct), |
7ff03a5e044f
theorem names generated by the (rep_)datatype command now have mandatory qualifiers
huffman
parents:
40844
diff
changeset
|
373 |
[mk_case_names_induct descr])]; |
33970 | 374 |
in |
375 |
thy2 |
|
376 |
|> derive_datatype_props config dt_names alt_names [descr] sorts |
|
377 |
induct inject distinct |
|
378 |
end; |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
379 |
|
33970 | 380 |
fun gen_rep_datatype prep_term config after_qed alt_names raw_ts thy = |
381 |
let |
|
42361 | 382 |
val ctxt = Proof_Context.init_global thy; |
42359 | 383 |
|
33970 | 384 |
fun constr_of_term (Const (c, T)) = (c, T) |
42359 | 385 |
| constr_of_term t = error ("Not a constant: " ^ Syntax.string_of_term ctxt t); |
386 |
fun no_constr (c, T) = |
|
42361 | 387 |
error ("Bad constructor: " ^ Proof_Context.extern_const ctxt c ^ "::" ^ |
42359 | 388 |
Syntax.string_of_typ ctxt T); |
33970 | 389 |
fun type_of_constr (cT as (_, T)) = |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
390 |
let |
33970 | 391 |
val frees = OldTerm.typ_tfrees T; |
40844 | 392 |
val (tyco, vs) = (apsnd o map) dest_TFree (dest_Type (body_type T)) |
33970 | 393 |
handle TYPE _ => no_constr cT |
394 |
val _ = if has_duplicates (eq_fst (op =)) vs then no_constr cT else (); |
|
395 |
val _ = if length frees <> length vs then no_constr cT else (); |
|
396 |
in (tyco, (vs, cT)) end; |
|
397 |
||
398 |
val raw_cs = AList.group (op =) (map (type_of_constr o constr_of_term o prep_term thy) raw_ts); |
|
399 |
val _ = case map_filter (fn (tyco, _) => |
|
400 |
if Symtab.defined (get_all thy) tyco then SOME tyco else NONE) raw_cs |
|
401 |
of [] => () |
|
402 |
| tycos => error ("Type(s) " ^ commas (map quote tycos) |
|
403 |
^ " already represented inductivly"); |
|
404 |
val raw_vss = maps (map (map snd o fst) o snd) raw_cs; |
|
405 |
val ms = case distinct (op =) (map length raw_vss) |
|
406 |
of [n] => 0 upto n - 1 |
|
407 |
| _ => error ("Different types in given constructors"); |
|
408 |
fun inter_sort m = map (fn xs => nth xs m) raw_vss |
|
409 |
|> Library.foldr1 (Sorts.inter_sort (Sign.classes_of thy)) |
|
410 |
val sorts = map inter_sort ms; |
|
43329
84472e198515
tuned signature: Name.invent and Name.invent_names;
wenzelm
parents:
43326
diff
changeset
|
411 |
val vs = Name.invent_names Name.context Name.aT sorts; |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
412 |
|
33970 | 413 |
fun norm_constr (raw_vs, (c, T)) = (c, map_atyps |
414 |
(TFree o (the o AList.lookup (op =) (map fst raw_vs ~~ vs)) o fst o dest_TFree) T); |
|
415 |
||
416 |
val cs = map (apsnd (map norm_constr)) raw_cs; |
|
40844 | 417 |
val dtyps_of_typ = |
41423 | 418 |
map (Datatype_Aux.dtyp_of_typ (map (rpair (map fst vs) o fst) cs)) o binder_types; |
33970 | 419 |
val dt_names = map fst cs; |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
420 |
|
33970 | 421 |
fun mk_spec (i, (tyco, constr)) = (i, (tyco, |
41423 | 422 |
map (Datatype_Aux.DtTFree o fst) vs, |
33970 | 423 |
(map o apsnd) dtyps_of_typ constr)) |
424 |
val descr = map_index mk_spec cs; |
|
425 |
val injs = Datatype_Prop.make_injs [descr] vs; |
|
426 |
val half_distincts = map snd (Datatype_Prop.make_distincts [descr] vs); |
|
427 |
val ind = Datatype_Prop.make_ind [descr] vs; |
|
428 |
val rules = (map o map o map) Logic.close_form [[[ind]], injs, half_distincts]; |
|
429 |
||
430 |
fun after_qed' raw_thms = |
|
431 |
let |
|
432 |
val [[[raw_induct]], raw_inject, half_distinct] = |
|
433 |
unflat rules (map Drule.zero_var_indexes_list raw_thms); |
|
434 |
(*FIXME somehow dubious*) |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
435 |
in |
42361 | 436 |
Proof_Context.background_theory_result |
33970 | 437 |
(prove_rep_datatype config dt_names alt_names descr vs |
438 |
raw_inject half_distinct raw_induct) |
|
439 |
#-> after_qed |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
440 |
end; |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
441 |
in |
42359 | 442 |
ctxt |
36323
655e2d74de3a
modernized naming conventions of main Isar proof elements;
wenzelm
parents:
35360
diff
changeset
|
443 |
|> Proof.theorem NONE after_qed' ((map o map) (rpair []) (flat rules)) |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
444 |
end; |
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
445 |
|
33970 | 446 |
val rep_datatype = gen_rep_datatype Sign.cert_term; |
41423 | 447 |
val rep_datatype_cmd = gen_rep_datatype Syntax.read_term_global Datatype_Aux.default_config (K I); |
33970 | 448 |
|
449 |
||
450 |
||
451 |
(** package setup **) |
|
452 |
||
453 |
(* setup theory *) |
|
454 |
||
455 |
val setup = |
|
456 |
trfun_setup #> |
|
43564
9864182c6bad
document antiquotations are managed as theory data, with proper name space and entity markup;
wenzelm
parents:
43329
diff
changeset
|
457 |
antiq_setup #> |
33970 | 458 |
Datatype_Interpretation.init; |
459 |
||
460 |
||
461 |
(* outer syntax *) |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
462 |
|
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33963
diff
changeset
|
463 |
val _ = |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36610
diff
changeset
|
464 |
Outer_Syntax.command "rep_datatype" "represent existing types inductively" Keyword.thy_goal |
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36610
diff
changeset
|
465 |
(Scan.option (Parse.$$$ "(" |-- Scan.repeat1 Parse.name --| Parse.$$$ ")") -- |
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36610
diff
changeset
|
466 |
Scan.repeat1 Parse.term |
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36610
diff
changeset
|
467 |
>> (fn (alt_names, ts) => |
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36610
diff
changeset
|
468 |
Toplevel.print 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
|
469 |
|
41423 | 470 |
|
471 |
open Datatype_Aux; |
|
472 |
||
30391
d930432adb13
adapted to simplified ThyOutput.antiquotation interface;
wenzelm
parents:
30364
diff
changeset
|
473 |
end; |