author | blanchet |
Tue, 04 Sep 2012 13:06:41 +0200 | |
changeset 49123 | 263b0e330d8b |
parent 49121 | 9e0acaa470ab |
child 49124 | 968e1b7de057 |
permissions | -rw-r--r-- |
49112 | 1 |
(* Title: HOL/Codatatype/Tools/bnf_fp_sugar.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Copyright 2012 |
|
4 |
||
5 |
Sugar for constructing LFPs and GFPs. |
|
6 |
*) |
|
7 |
||
8 |
signature BNF_FP_SUGAR = |
|
9 |
sig |
|
10 |
end; |
|
11 |
||
12 |
structure BNF_FP_Sugar : BNF_FP_SUGAR = |
|
13 |
struct |
|
14 |
||
49119 | 15 |
open BNF_Util |
16 |
open BNF_Wrap |
|
17 |
open BNF_FP_Util |
|
18 |
open BNF_LFP |
|
19 |
open BNF_GFP |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
20 |
open BNF_FP_Sugar_Tactics |
49119 | 21 |
|
22 |
fun cannot_merge_types () = error "Mutually recursive (co)datatypes must have same type parameters"; |
|
23 |
||
24 |
fun merge_type_arg_constrained ctxt (T, c) (T', c') = |
|
25 |
if T = T' then |
|
26 |
(case (c, c') of |
|
27 |
(_, NONE) => (T, c) |
|
28 |
| (NONE, _) => (T, c') |
|
29 |
| _ => |
|
30 |
if c = c' then |
|
31 |
(T, c) |
|
32 |
else |
|
33 |
error ("Inconsistent sort constraints for type variable " ^ |
|
34 |
quote (Syntax.string_of_typ ctxt T))) |
|
35 |
else |
|
36 |
cannot_merge_types (); |
|
37 |
||
38 |
fun merge_type_args_constrained ctxt (cAs, cAs') = |
|
39 |
if length cAs = length cAs' then map2 (merge_type_arg_constrained ctxt) cAs cAs' |
|
40 |
else cannot_merge_types (); |
|
41 |
||
49121 | 42 |
fun type_args_constrained_of (((cAs, _), _), _) = cAs; |
43 |
val type_args_of = map fst o type_args_constrained_of; |
|
44 |
fun type_name_of (((_, b), _), _) = b; |
|
45 |
fun mixfix_of_typ ((_, mx), _) = mx; |
|
46 |
fun ctr_specs_of (_, ctr_specs) = ctr_specs; |
|
49119 | 47 |
|
49121 | 48 |
fun disc_of (((disc, _), _), _) = disc; |
49 |
fun ctr_of (((_, ctr), _), _) = ctr; |
|
50 |
fun args_of ((_, args), _) = args; |
|
51 |
fun mixfix_of_ctr (_, mx) = mx; |
|
49119 | 52 |
|
53 |
val lfp_info = bnf_lfp; |
|
54 |
val gfp_info = bnf_gfp; |
|
55 |
||
49121 | 56 |
fun prepare_data prepare_typ construct specs fake_lthy lthy = |
49112 | 57 |
let |
49121 | 58 |
val constrained_As = |
59 |
map (map (apfst (prepare_typ fake_lthy)) o type_args_constrained_of) specs |
|
49119 | 60 |
|> Library.foldr1 (merge_type_args_constrained lthy); |
49121 | 61 |
val As = map fst constrained_As; |
49119 | 62 |
|
49121 | 63 |
val _ = (case duplicates (op =) As of [] => () |
49119 | 64 |
| T :: _ => error ("Duplicate type parameter " ^ quote (Syntax.string_of_typ lthy T))); |
65 |
||
66 |
(* TODO: check that no type variables occur in the rhss that's not in the lhss *) |
|
67 |
(* TODO: use sort constraints on type args *) |
|
68 |
||
69 |
val N = length specs; |
|
70 |
||
49121 | 71 |
fun mk_T b = |
72 |
Type (fst (Term.dest_Type (Proof_Context.read_type_name fake_lthy true (Binding.name_of b))), |
|
73 |
As); |
|
74 |
||
75 |
val bs = map type_name_of specs; |
|
76 |
val Ts = map mk_T bs; |
|
77 |
||
78 |
val mixfixes = map mixfix_of_typ specs; |
|
49119 | 79 |
|
80 |
val _ = (case duplicates Binding.eq_name bs of [] => () |
|
81 |
| b :: _ => error ("Duplicate type name declaration " ^ quote (Binding.name_of b))); |
|
82 |
||
49121 | 83 |
val ctr_specss = map ctr_specs_of specs; |
49119 | 84 |
|
49121 | 85 |
val disc_namess = map (map disc_of) ctr_specss; |
86 |
val ctr_namess = map (map ctr_of) ctr_specss; |
|
87 |
val ctr_argsss = map (map args_of) ctr_specss; |
|
88 |
val ctr_mixfixess = map (map mixfix_of_ctr) ctr_specss; |
|
49119 | 89 |
|
90 |
val sel_namesss = map (map (map fst)) ctr_argsss; |
|
49121 | 91 |
val ctr_Tsss = map (map (map (prepare_typ fake_lthy o snd))) ctr_argsss; |
92 |
||
93 |
val (Bs, C) = |
|
94 |
lthy |
|
95 |
|> fold (fold (fn s => Variable.declare_typ (TFree (s, dummyS))) o type_args_of) specs |
|
96 |
|> mk_TFrees N |
|
97 |
||> the_single o fst o mk_TFrees 1; |
|
49119 | 98 |
|
49121 | 99 |
fun freeze_rec (T as Type (s, Ts')) = |
100 |
(case find_index (curry (op =) T) Ts of |
|
101 |
~1 => Type (s, map freeze_rec Ts') |
|
102 |
| i => nth Bs i) |
|
103 |
| freeze_rec T = T; |
|
104 |
||
105 |
val ctr_TsssBs = map (map (map freeze_rec)) ctr_Tsss; |
|
106 |
val sum_prod_TsBs = map (mk_sumTN o map HOLogic.mk_tupleT) ctr_TsssBs; |
|
49119 | 107 |
|
49121 | 108 |
val eqs = map dest_TFree Bs ~~ sum_prod_TsBs; |
109 |
||
110 |
val (raw_flds, lthy') = fp_bnf construct bs eqs lthy; |
|
111 |
||
112 |
fun mk_fld Ts fld = |
|
113 |
let val Type (_, Ts0) = body_type (fastype_of fld) in |
|
114 |
Term.subst_atomic_types (Ts0 ~~ Ts) fld |
|
115 |
end; |
|
49119 | 116 |
|
49121 | 117 |
val flds = map (mk_fld As) raw_flds; |
118 |
||
119 |
fun wrap_type (((((T, fld), ctr_names), ctr_Tss), disc_names), sel_namess) no_defs_lthy = |
|
120 |
let |
|
121 |
val n = length ctr_names; |
|
122 |
val ks = 1 upto n; |
|
123 |
val ms = map length ctr_Tss; |
|
124 |
||
125 |
val prod_Ts = map HOLogic.mk_tupleT ctr_Tss; |
|
49119 | 126 |
|
49121 | 127 |
val (xss, _) = lthy |> mk_Freess "x" ctr_Tss; |
128 |
||
129 |
val rhss = |
|
130 |
map2 (fn k => fn xs => |
|
131 |
fold_rev Term.lambda xs (fld $ mk_InN prod_Ts (HOLogic.mk_tuple xs) k)) ks xss; |
|
132 |
||
133 |
val ((raw_ctrs, raw_ctr_defs), (lthy', lthy)) = no_defs_lthy |
|
134 |
|> apfst split_list o fold_map2 (fn b => fn rhs => |
|
135 |
Local_Theory.define ((b, NoSyn), ((Thm.def_binding b, []), rhs)) #>> apsnd snd) |
|
136 |
ctr_names rhss |
|
137 |
||> `Local_Theory.restore; |
|
138 |
||
139 |
val raw_caseof = |
|
140 |
Const (@{const_name undefined}, map (fn Ts => Ts ---> C) ctr_Tss ---> T --> C); |
|
141 |
||
142 |
(*transforms defined frees into consts (and more)*) |
|
143 |
val phi = Proof_Context.export_morphism lthy lthy'; |
|
144 |
||
145 |
val ctr_defs = map (Morphism.thm phi) raw_ctr_defs; |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
146 |
val ctrs = map (Morphism.term phi) raw_ctrs; |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
147 |
val caseof = Morphism.term phi raw_caseof; |
49121 | 148 |
|
49123
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
149 |
val fld_iff_unf_thm = |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
150 |
let |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
151 |
val fld = @{term "undefined::'a=>'b"}; |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
152 |
val unf = @{term True}; |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
153 |
val (T, T') = dest_funT (fastype_of fld); |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
154 |
val fld_unf = TrueI; |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
155 |
val unf_fld = TrueI; |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
156 |
val goal = @{term True}; |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
157 |
in |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
158 |
Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} => |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
159 |
mk_fld_iff_unf_tac ctxt (map (SOME o certifyT lthy) [T, T']) (certify lthy fld) |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
160 |
(certify lthy unf) fld_unf unf_fld) |
263b0e330d8b
more work on sugar + simplify Trueprop + eq idiom everywhere
blanchet
parents:
49121
diff
changeset
|
161 |
end; |
49121 | 162 |
|
163 |
(* ### *) |
|
164 |
fun cheat_tac {context = ctxt, ...} = Skip_Proof.cheat_tac (Proof_Context.theory_of ctxt); |
|
165 |
||
166 |
val exhaust_tac = cheat_tac; |
|
167 |
||
168 |
val inject_tacss = map (fn 0 => [] | _ => [cheat_tac]) ms; |
|
169 |
||
170 |
val half_distinct_tacss = map (map (K cheat_tac)) (mk_half_pairss ks); |
|
171 |
||
172 |
val case_tacs = map (K cheat_tac) ks; |
|
173 |
||
174 |
val tacss = [exhaust_tac] :: inject_tacss @ half_distinct_tacss @ [case_tacs]; |
|
49119 | 175 |
in |
49121 | 176 |
wrap_data tacss ((ctrs, caseof), (disc_names, sel_namess)) lthy' |
49119 | 177 |
end; |
49112 | 178 |
in |
49121 | 179 |
lthy' |> fold wrap_type (Ts ~~ flds ~~ ctr_namess ~~ ctr_Tsss ~~ disc_namess ~~ sel_namesss) |
49112 | 180 |
end; |
181 |
||
49121 | 182 |
fun data_cmd info specs lthy = |
183 |
let |
|
184 |
val fake_lthy = |
|
185 |
Proof_Context.theory_of lthy |
|
186 |
|> Theory.copy |
|
187 |
|> Sign.add_types_global (map (fn spec => |
|
188 |
(type_name_of spec, length (type_args_constrained_of spec), mixfix_of_typ spec)) specs) |
|
189 |
|> Proof_Context.init_global |
|
190 |
in |
|
191 |
prepare_data Syntax.read_typ info specs fake_lthy lthy |
|
192 |
end; |
|
49119 | 193 |
|
194 |
val parse_opt_binding_colon = Scan.optional (Parse.binding --| Parse.$$$ ":") no_name |
|
195 |
||
49112 | 196 |
val parse_ctr_arg = |
49119 | 197 |
Parse.$$$ "(" |-- parse_opt_binding_colon -- Parse.typ --| Parse.$$$ ")" || |
198 |
(Parse.typ >> pair no_name); |
|
49112 | 199 |
|
200 |
val parse_single_spec = |
|
201 |
Parse.type_args_constrained -- Parse.binding -- Parse.opt_mixfix -- |
|
49119 | 202 |
(@{keyword "="} |-- Parse.enum1 "|" (parse_opt_binding_colon -- Parse.binding -- |
203 |
Scan.repeat parse_ctr_arg -- Parse.opt_mixfix)); |
|
49112 | 204 |
|
205 |
val _ = |
|
206 |
Outer_Syntax.local_theory @{command_spec "data"} "define BNF-based inductive datatypes" |
|
49119 | 207 |
(Parse.and_list1 parse_single_spec >> data_cmd lfp_info); |
49112 | 208 |
|
209 |
val _ = |
|
210 |
Outer_Syntax.local_theory @{command_spec "codata"} "define BNF-based coinductive datatypes" |
|
49119 | 211 |
(Parse.and_list1 parse_single_spec >> data_cmd gfp_info); |
49112 | 212 |
|
213 |
end; |