author | blanchet |
Tue, 11 Sep 2012 16:08:55 +0200 | |
changeset 49280 | 52413dc96326 |
parent 49278 | 718e4ad1517e |
child 49281 | 3d87f4fd0d50 |
permissions | -rw-r--r-- |
49074 | 1 |
(* Title: HOL/Codatatype/Tools/bnf_wrap.ML |
49017 | 2 |
Author: Jasmin Blanchette, TU Muenchen |
3 |
Copyright 2012 |
|
4 |
||
49074 | 5 |
Wrapping existing datatypes. |
49017 | 6 |
*) |
7 |
||
49074 | 8 |
signature BNF_WRAP = |
49017 | 9 |
sig |
49129 | 10 |
val no_binder: binding |
49121 | 11 |
val mk_half_pairss: 'a list -> ('a * 'a) list list |
49203 | 12 |
val mk_ctr: typ list -> term -> term |
49199 | 13 |
val wrap_datatype: ({prems: thm list, context: Proof.context} -> tactic) list list -> |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
14 |
((bool * term list) * term) * |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
15 |
(binding list * (binding list list * (binding * term) list list)) -> local_theory -> |
49266 | 16 |
(term list list * thm list * thm list list) * local_theory |
49278 | 17 |
val parse_wrap_options: bool parser |
49017 | 18 |
end; |
19 |
||
49074 | 20 |
structure BNF_Wrap : BNF_WRAP = |
49017 | 21 |
struct |
22 |
||
23 |
open BNF_Util |
|
49074 | 24 |
open BNF_Wrap_Tactics |
49017 | 25 |
|
49223 | 26 |
val isN = "is_"; |
27 |
val unN = "un_"; |
|
28 |
fun mk_unN 1 1 suf = unN ^ suf |
|
29 |
| mk_unN _ l suf = unN ^ suf ^ string_of_int l; |
|
49046
3c5eba97d93a
allow default names for selectors via wildcard (_) + fix wrong index (k)
blanchet
parents:
49045
diff
changeset
|
30 |
|
49054 | 31 |
val case_congN = "case_cong"; |
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
32 |
val case_eqN = "case_eq"; |
49054 | 33 |
val casesN = "cases"; |
49118 | 34 |
val collapseN = "collapse"; |
49122 | 35 |
val disc_excludeN = "disc_exclude"; |
49054 | 36 |
val disc_exhaustN = "disc_exhaust"; |
37 |
val discsN = "discs"; |
|
38 |
val distinctN = "distinct"; |
|
49075 | 39 |
val exhaustN = "exhaust"; |
40 |
val injectN = "inject"; |
|
41 |
val nchotomyN = "nchotomy"; |
|
49054 | 42 |
val selsN = "sels"; |
43 |
val splitN = "split"; |
|
44 |
val split_asmN = "split_asm"; |
|
45 |
val weak_case_cong_thmsN = "weak_case_cong"; |
|
49019 | 46 |
|
49139 | 47 |
val no_binder = @{binding ""}; |
49129 | 48 |
val fallback_binder = @{binding _}; |
49046
3c5eba97d93a
allow default names for selectors via wildcard (_) + fix wrong index (k)
blanchet
parents:
49045
diff
changeset
|
49 |
|
49056 | 50 |
fun pad_list x n xs = xs @ replicate (n - length xs) x; |
51 |
||
49259 | 52 |
fun unflat_lookup eq ys zs = map (map (fn x => nth zs (find_index (curry eq x) ys))); |
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
53 |
|
49048
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
54 |
fun mk_half_pairss' _ [] = [] |
49056 | 55 |
| mk_half_pairss' indent (y :: ys) = |
56 |
indent @ fold_rev (cons o single o pair y) ys (mk_half_pairss' ([] :: indent) ys); |
|
49027 | 57 |
|
49048
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
58 |
fun mk_half_pairss ys = mk_half_pairss' [[]] ys; |
49027 | 59 |
|
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
60 |
fun mk_undefined T = Const (@{const_name undefined}, T); |
49055 | 61 |
|
49203 | 62 |
fun mk_ctr Ts ctr = |
63 |
let val Type (_, Ts0) = body_type (fastype_of ctr) in |
|
64 |
Term.subst_atomic_types (Ts0 ~~ Ts) ctr |
|
65 |
end; |
|
66 |
||
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
67 |
fun eta_expand_case_arg xs f_xs = fold_rev Term.lambda xs f_xs; |
49032 | 68 |
|
49203 | 69 |
fun name_of_ctr c = |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
70 |
(case head_of c of |
49046
3c5eba97d93a
allow default names for selectors via wildcard (_) + fix wrong index (k)
blanchet
parents:
49045
diff
changeset
|
71 |
Const (s, _) => s |
3c5eba97d93a
allow default names for selectors via wildcard (_) + fix wrong index (k)
blanchet
parents:
49045
diff
changeset
|
72 |
| Free (s, _) => s |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
73 |
| _ => error "Cannot extract name of constructor"); |
49046
3c5eba97d93a
allow default names for selectors via wildcard (_) + fix wrong index (k)
blanchet
parents:
49045
diff
changeset
|
74 |
|
49278 | 75 |
fun prepare_wrap_datatype prep_term (((no_dests, raw_ctrs), raw_case), |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
76 |
(raw_disc_binders, (raw_sel_binderss, raw_sel_defaultss))) no_defs_lthy = |
49017 | 77 |
let |
49019 | 78 |
(* TODO: sanity checks on arguments *) |
49113 | 79 |
(* TODO: attributes (simp, case_names, etc.) *) |
80 |
(* TODO: case syntax *) |
|
81 |
(* TODO: integration with function package ("size") *) |
|
49025 | 82 |
|
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
83 |
val n = length raw_ctrs; |
49054 | 84 |
val ks = 1 upto n; |
85 |
||
49121 | 86 |
val _ = if n > 0 then () else error "No constructors specified"; |
87 |
||
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
88 |
val ctrs0 = map (prep_term no_defs_lthy) raw_ctrs; |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
89 |
val case0 = prep_term no_defs_lthy raw_case; |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
90 |
val sel_defaultss = |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
91 |
pad_list [] n (map (map (apsnd (prep_term no_defs_lthy))) raw_sel_defaultss); |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
92 |
|
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
93 |
val Type (fpT_name, As0) = body_type (fastype_of (hd ctrs0)); |
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
94 |
val b = Binding.qualified_name fpT_name; |
49055 | 95 |
|
96 |
val (As, B) = |
|
97 |
no_defs_lthy |
|
98 |
|> mk_TFrees (length As0) |
|
99 |
||> the_single o fst o mk_TFrees 1; |
|
100 |
||
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
101 |
val fpT = Type (fpT_name, As); |
49055 | 102 |
val ctrs = map (mk_ctr As) ctrs0; |
103 |
val ctr_Tss = map (binder_types o fastype_of) ctrs; |
|
104 |
||
105 |
val ms = map length ctr_Tss; |
|
106 |
||
49129 | 107 |
val raw_disc_binders' = pad_list no_binder n raw_disc_binders; |
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
108 |
|
49174
41790d616f63
by default, only generate one discriminator for a two-value datatype
blanchet
parents:
49157
diff
changeset
|
109 |
fun can_really_rely_on_disc k = |
49152 | 110 |
not (Binding.eq_name (nth raw_disc_binders' (k - 1), no_binder)) orelse nth ms (k - 1) = 0; |
49174
41790d616f63
by default, only generate one discriminator for a two-value datatype
blanchet
parents:
49157
diff
changeset
|
111 |
fun can_rely_on_disc k = |
41790d616f63
by default, only generate one discriminator for a two-value datatype
blanchet
parents:
49157
diff
changeset
|
112 |
can_really_rely_on_disc k orelse (k = 1 andalso not (can_really_rely_on_disc 2)); |
49129 | 113 |
fun can_omit_disc_binder k m = |
49174
41790d616f63
by default, only generate one discriminator for a two-value datatype
blanchet
parents:
49157
diff
changeset
|
114 |
n = 1 orelse m = 0 orelse (n = 2 andalso can_rely_on_disc (3 - k)); |
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
115 |
|
49223 | 116 |
val fallback_disc_binder = Binding.name o prefix isN o Long_Name.base_name o name_of_ctr; |
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
117 |
|
49129 | 118 |
val disc_binders = |
119 |
raw_disc_binders' |
|
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
120 |
|> map4 (fn k => fn m => fn ctr => fn disc => |
49129 | 121 |
if Binding.eq_name (disc, no_binder) then |
122 |
if can_omit_disc_binder k m then NONE else SOME (fallback_disc_binder ctr) |
|
123 |
else if Binding.eq_name (disc, fallback_binder) then |
|
124 |
SOME (fallback_disc_binder ctr) |
|
49046
3c5eba97d93a
allow default names for selectors via wildcard (_) + fix wrong index (k)
blanchet
parents:
49045
diff
changeset
|
125 |
else |
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
126 |
SOME disc) ks ms ctrs0; |
49056 | 127 |
|
49129 | 128 |
val no_discs = map is_none disc_binders; |
49137 | 129 |
val no_discs_at_all = forall I no_discs; |
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
130 |
|
49223 | 131 |
fun fallback_sel_binder m l = Binding.name o mk_unN m l o Long_Name.base_name o name_of_ctr; |
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
132 |
|
49129 | 133 |
val sel_binderss = |
134 |
pad_list [] n raw_sel_binderss |
|
49056 | 135 |
|> map3 (fn ctr => fn m => map2 (fn l => fn sel => |
49129 | 136 |
if Binding.eq_name (sel, no_binder) orelse Binding.eq_name (sel, fallback_binder) then |
137 |
fallback_sel_binder m l ctr |
|
49056 | 138 |
else |
49129 | 139 |
sel) (1 upto m) o pad_list no_binder m) ctrs0 ms; |
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
140 |
|
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
141 |
fun mk_case Ts T = |
49121 | 142 |
let |
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
143 |
val (binders, body) = strip_type (fastype_of case0) |
49121 | 144 |
val Type (_, Ts0) = List.last binders |
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
145 |
in Term.subst_atomic_types ((body, T) :: (Ts0 ~~ Ts)) case0 end; |
49022 | 146 |
|
49201 | 147 |
val casex = mk_case As B; |
148 |
val case_Ts = map (fn Ts => Ts ---> B) ctr_Tss; |
|
49043 | 149 |
|
150 |
val (((((((xss, yss), fs), gs), (v, v')), w), (p, p')), names_lthy) = no_defs_lthy |> |
|
49025 | 151 |
mk_Freess "x" ctr_Tss |
152 |
||>> mk_Freess "y" ctr_Tss |
|
49201 | 153 |
||>> mk_Frees "f" case_Ts |
154 |
||>> mk_Frees "g" case_Ts |
|
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
155 |
||>> yield_singleton (apfst (op ~~) oo mk_Frees' "v") fpT |
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
156 |
||>> yield_singleton (mk_Frees "w") fpT |
49043 | 157 |
||>> yield_singleton (apfst (op ~~) oo mk_Frees' "P") HOLogic.boolT; |
158 |
||
159 |
val q = Free (fst p', B --> HOLogic.boolT); |
|
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
160 |
|
49210 | 161 |
fun ap_v t = t $ v; |
49137 | 162 |
fun mk_v_eq_v () = HOLogic.mk_eq (v, v); |
163 |
||
49025 | 164 |
val xctrs = map2 (curry Term.list_comb) ctrs xss; |
165 |
val yctrs = map2 (curry Term.list_comb) ctrs yss; |
|
49032 | 166 |
|
49043 | 167 |
val xfs = map2 (curry Term.list_comb) fs xss; |
168 |
val xgs = map2 (curry Term.list_comb) gs xss; |
|
169 |
||
49130
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
170 |
val eta_fs = map2 eta_expand_case_arg xss xfs; |
3c26e17b2849
implemented "mk_case_tac" -- and got rid of "cheat_tac"
blanchet
parents:
49129
diff
changeset
|
171 |
val eta_gs = map2 eta_expand_case_arg xss xgs; |
49043 | 172 |
|
49201 | 173 |
val fcase = Term.list_comb (casex, eta_fs); |
174 |
val gcase = Term.list_comb (casex, eta_gs); |
|
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
175 |
|
49025 | 176 |
val exist_xs_v_eq_ctrs = |
177 |
map2 (fn xctr => fn xs => list_exists_free xs (HOLogic.mk_eq (v, xctr))) xctrs xss; |
|
49022 | 178 |
|
49278 | 179 |
val unique_disc_no_def = TrueI; (*arbitrary marker*) |
180 |
val alternate_disc_no_def = FalseE; (*arbitrary marker*) |
|
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
181 |
|
49278 | 182 |
fun alternate_disc_lhs get_disc k = |
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
183 |
HOLogic.mk_not |
49152 | 184 |
(case nth disc_binders (k - 1) of |
185 |
NONE => nth exist_xs_v_eq_ctrs (k - 1) |
|
49278 | 186 |
| SOME b => get_disc b (k - 1) $ v); |
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
187 |
|
49278 | 188 |
val (discs, selss, disc_defs, sel_defss, lthy') = |
189 |
if no_dests then |
|
190 |
([], [], [], [], no_defs_lthy) |
|
191 |
else |
|
192 |
let |
|
193 |
fun disc_free b = Free (Binding.name_of b, fpT --> HOLogic.boolT); |
|
49025 | 194 |
|
49278 | 195 |
fun disc_spec b exist_xs_v_eq_ctr = mk_Trueprop_eq (disc_free b $ v, exist_xs_v_eq_ctr); |
196 |
||
197 |
fun alternate_disc k = Term.lambda v (alternate_disc_lhs (K o disc_free) (3 - k)); |
|
198 |
||
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
199 |
fun mk_sel_case_args b proto_sels T = |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
200 |
map2 (fn Ts => fn k => |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
201 |
(case AList.lookup (op =) proto_sels k of |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
202 |
NONE => |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
203 |
let val def_T = Ts ---> T in |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
204 |
(case AList.lookup Binding.eq_name (rev (nth sel_defaultss (k - 1))) b of |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
205 |
NONE => mk_undefined def_T |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
206 |
| SOME t => fold_rev (fn T => Term.lambda (Free (Name.uu, T))) Ts |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
207 |
(Term.subst_atomic_types [(fastype_of t, T)] t)) |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
208 |
end |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
209 |
| SOME (xs, x) => fold_rev Term.lambda xs x)) ctr_Tss ks; |
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
210 |
|
49278 | 211 |
fun sel_spec b proto_sels = |
212 |
let |
|
213 |
val _ = |
|
214 |
(case duplicates (op =) (map fst proto_sels) of |
|
215 |
k :: _ => error ("Duplicate selector name " ^ quote (Binding.name_of b) ^ |
|
216 |
" for constructor " ^ |
|
217 |
quote (Syntax.string_of_term no_defs_lthy (nth ctrs (k - 1)))) |
|
218 |
| [] => ()) |
|
219 |
val T = |
|
220 |
(case distinct (op =) (map (fastype_of o snd o snd) proto_sels) of |
|
221 |
[T] => T |
|
222 |
| T :: T' :: _ => error ("Inconsistent range type for selector " ^ |
|
223 |
quote (Binding.name_of b) ^ ": " ^ quote (Syntax.string_of_typ no_defs_lthy T) ^ |
|
224 |
" vs. " ^ quote (Syntax.string_of_typ no_defs_lthy T'))); |
|
225 |
in |
|
226 |
mk_Trueprop_eq (Free (Binding.name_of b, fpT --> T) $ v, |
|
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
227 |
Term.list_comb (mk_case As T, mk_sel_case_args b proto_sels T) $ v) |
49278 | 228 |
end; |
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
229 |
|
49278 | 230 |
val proto_selss = map3 (fn k => fn xs => map (fn x => (k, (xs, x)))) ks xss xss; |
231 |
val sel_bundles = AList.group Binding.eq_name (flat sel_binderss ~~ flat proto_selss); |
|
232 |
val sel_binders = map fst sel_bundles; |
|
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
233 |
|
49278 | 234 |
fun unflat_selss xs = unflat_lookup Binding.eq_name sel_binders xs sel_binderss; |
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
235 |
|
49278 | 236 |
val (((raw_discs, raw_disc_defs), (raw_sels, raw_sel_defs)), (lthy', lthy)) = |
237 |
no_defs_lthy |
|
238 |
|> apfst split_list o fold_map4 (fn k => fn m => fn exist_xs_v_eq_ctr => |
|
239 |
fn NONE => |
|
240 |
if n = 1 then pair (Term.lambda v (mk_v_eq_v ()), unique_disc_no_def) |
|
241 |
else if m = 0 then pair (Term.lambda v exist_xs_v_eq_ctr, refl) |
|
242 |
else pair (alternate_disc k, alternate_disc_no_def) |
|
243 |
| SOME b => Specification.definition (SOME (b, NONE, NoSyn), |
|
244 |
((Thm.def_binding b, []), disc_spec b exist_xs_v_eq_ctr)) #>> apsnd snd) |
|
245 |
ks ms exist_xs_v_eq_ctrs disc_binders |
|
246 |
||>> apfst split_list o fold_map (fn (b, proto_sels) => |
|
247 |
Specification.definition (SOME (b, NONE, NoSyn), |
|
248 |
((Thm.def_binding b, []), sel_spec b proto_sels)) #>> apsnd snd) sel_bundles |
|
249 |
||> `Local_Theory.restore; |
|
49022 | 250 |
|
49278 | 251 |
(*transforms defined frees into consts (and more)*) |
252 |
val phi = Proof_Context.export_morphism lthy lthy'; |
|
49025 | 253 |
|
49278 | 254 |
val disc_defs = map (Morphism.thm phi) raw_disc_defs; |
255 |
val sel_defss = unflat_selss (map (Morphism.thm phi) raw_sel_defs); |
|
256 |
||
257 |
val discs0 = map (Morphism.term phi) raw_discs; |
|
258 |
val selss0 = unflat_selss (map (Morphism.term phi) raw_sels); |
|
49028 | 259 |
|
49278 | 260 |
fun mk_disc_or_sel Ts c = |
261 |
Term.subst_atomic_types (snd (Term.dest_Type (domain_type (fastype_of c))) ~~ Ts) c; |
|
49025 | 262 |
|
49278 | 263 |
val discs = map (mk_disc_or_sel As) discs0; |
264 |
val selss = map (map (mk_disc_or_sel As)) selss0; |
|
265 |
in |
|
266 |
(discs, selss, disc_defs, sel_defss, lthy') |
|
267 |
end; |
|
49025 | 268 |
|
49032 | 269 |
fun mk_imp_p Qs = Logic.list_implies (Qs, HOLogic.mk_Trueprop p); |
49029 | 270 |
|
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
271 |
val goal_exhaust = |
49032 | 272 |
let fun mk_prem xctr xs = fold_rev Logic.all xs (mk_imp_p [mk_Trueprop_eq (v, xctr)]) in |
49121 | 273 |
fold_rev Logic.all [p, v] (mk_imp_p (map2 mk_prem xctrs xss)) |
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
274 |
end; |
49019 | 275 |
|
49034
b77e1910af8a
make parallel list indexing possible for inject theorems
blanchet
parents:
49033
diff
changeset
|
276 |
val goal_injectss = |
49017 | 277 |
let |
49034
b77e1910af8a
make parallel list indexing possible for inject theorems
blanchet
parents:
49033
diff
changeset
|
278 |
fun mk_goal _ _ [] [] = [] |
49025 | 279 |
| mk_goal xctr yctr xs ys = |
49121 | 280 |
[fold_rev Logic.all (xs @ ys) (mk_Trueprop_eq (HOLogic.mk_eq (xctr, yctr), |
281 |
Library.foldr1 HOLogic.mk_conj (map2 (curry HOLogic.mk_eq) xs ys)))]; |
|
49017 | 282 |
in |
49034
b77e1910af8a
make parallel list indexing possible for inject theorems
blanchet
parents:
49033
diff
changeset
|
283 |
map4 mk_goal xctrs yctrs xss yss |
49017 | 284 |
end; |
285 |
||
49048
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
286 |
val goal_half_distinctss = |
49121 | 287 |
let |
49203 | 288 |
fun mk_goal ((xs, xc), (xs', xc')) = |
49121 | 289 |
fold_rev Logic.all (xs @ xs') |
49203 | 290 |
(HOLogic.mk_Trueprop (HOLogic.mk_not (HOLogic.mk_eq (xc, xc')))); |
49121 | 291 |
in |
292 |
map (map mk_goal) (mk_half_pairss (xss ~~ xctrs)) |
|
293 |
end; |
|
49019 | 294 |
|
49121 | 295 |
val goal_cases = |
296 |
map3 (fn xs => fn xctr => fn xf => |
|
49201 | 297 |
fold_rev Logic.all (fs @ xs) (mk_Trueprop_eq (fcase $ xctr, xf))) xss xctrs xfs; |
49025 | 298 |
|
49121 | 299 |
val goalss = [goal_exhaust] :: goal_injectss @ goal_half_distinctss @ [goal_cases]; |
49019 | 300 |
|
301 |
fun after_qed thmss lthy = |
|
302 |
let |
|
49048
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
303 |
val ([exhaust_thm], (inject_thmss, (half_distinct_thmss, [case_thms]))) = |
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
304 |
(hd thmss, apsnd (chop (n * n)) (chop n (tl thmss))); |
49019 | 305 |
|
49032 | 306 |
val exhaust_thm' = |
307 |
let val Tinst = map (pairself (certifyT lthy)) (map Logic.varifyT_global As ~~ As) in |
|
308 |
Drule.instantiate' [] [SOME (certify lthy v)] |
|
309 |
(Thm.instantiate (Tinst, []) (Drule.zero_var_indexes exhaust_thm)) |
|
310 |
end; |
|
311 |
||
49048
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
312 |
val other_half_distinct_thmss = map (map (fn thm => thm RS not_sym)) half_distinct_thmss; |
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
313 |
|
49052 | 314 |
val (distinct_thmsss', distinct_thmsss) = |
49048
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
315 |
map2 (map2 append) (Library.chop_groups n half_distinct_thmss) |
49052 | 316 |
(transpose (Library.chop_groups n other_half_distinct_thmss)) |
317 |
|> `transpose; |
|
49048
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
318 |
val distinct_thms = interleave (flat half_distinct_thmss) (flat other_half_distinct_thmss); |
49019 | 319 |
|
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
320 |
val nchotomy_thm = |
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
321 |
let |
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
322 |
val goal = |
49022 | 323 |
HOLogic.mk_Trueprop (HOLogic.mk_all (fst v', snd v', |
49029 | 324 |
Library.foldr1 HOLogic.mk_disj exist_xs_v_eq_ctrs)); |
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
325 |
in |
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
326 |
Skip_Proof.prove lthy [] [] goal (fn _ => mk_nchotomy_tac n exhaust_thm) |
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
327 |
end; |
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
328 |
|
49278 | 329 |
val (sel_thmss, disc_thms, discI_thms, disc_exclude_thms, disc_exhaust_thms, collapse_thms, |
330 |
case_eq_thms) = |
|
331 |
if no_dests then |
|
332 |
([], [], [], [], [], [], []) |
|
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
333 |
else |
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
334 |
let |
49278 | 335 |
val sel_thmss = |
336 |
map2 (fn case_thm => map (fn sel_def => case_thm RS (sel_def RS trans))) case_thms |
|
337 |
sel_defss; |
|
338 |
||
339 |
fun mk_unique_disc_def () = |
|
340 |
let |
|
341 |
val m = the_single ms; |
|
342 |
val goal = mk_Trueprop_eq (mk_v_eq_v (), the_single exist_xs_v_eq_ctrs); |
|
343 |
in |
|
344 |
Skip_Proof.prove lthy [] [] goal (fn _ => mk_unique_disc_def_tac m exhaust_thm') |
|
345 |
|> singleton (Proof_Context.export names_lthy lthy) |
|
346 |
|> Thm.close_derivation |
|
347 |
end; |
|
348 |
||
349 |
fun mk_alternate_disc_def k = |
|
350 |
let |
|
351 |
val goal = |
|
352 |
mk_Trueprop_eq (alternate_disc_lhs (K (nth discs)) (3 - k), |
|
353 |
nth exist_xs_v_eq_ctrs (k - 1)); |
|
354 |
in |
|
355 |
Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} => |
|
356 |
mk_alternate_disc_def_tac ctxt k (nth disc_defs (2 - k)) |
|
357 |
(nth distinct_thms (2 - k)) exhaust_thm') |
|
358 |
|> singleton (Proof_Context.export names_lthy lthy) |
|
359 |
|> Thm.close_derivation |
|
360 |
end; |
|
49028 | 361 |
|
49278 | 362 |
val has_alternate_disc_def = |
363 |
exists (fn def => Thm.eq_thm_prop (def, alternate_disc_no_def)) disc_defs; |
|
364 |
||
365 |
val disc_defs' = |
|
366 |
map2 (fn k => fn def => |
|
367 |
if Thm.eq_thm_prop (def, unique_disc_no_def) then mk_unique_disc_def () |
|
368 |
else if Thm.eq_thm_prop (def, alternate_disc_no_def) then mk_alternate_disc_def k |
|
369 |
else def) ks disc_defs; |
|
370 |
||
371 |
val discD_thms = map (fn def => def RS iffD1) disc_defs'; |
|
372 |
val discI_thms = |
|
373 |
map2 (fn m => fn def => funpow m (fn thm => exI RS thm) (def RS iffD2)) ms |
|
374 |
disc_defs'; |
|
375 |
val not_discI_thms = |
|
376 |
map2 (fn m => fn def => funpow m (fn thm => allI RS thm) |
|
377 |
(Local_Defs.unfold lthy @{thms not_ex} (def RS @{thm ssubst[of _ _ Not]}))) |
|
378 |
ms disc_defs'; |
|
379 |
||
380 |
val (disc_thmss', disc_thmss) = |
|
381 |
let |
|
382 |
fun mk_thm discI _ [] = refl RS discI |
|
383 |
| mk_thm _ not_discI [distinct] = distinct RS not_discI; |
|
384 |
fun mk_thms discI not_discI distinctss = map (mk_thm discI not_discI) distinctss; |
|
385 |
in |
|
386 |
map3 mk_thms discI_thms not_discI_thms distinct_thmsss' |> `transpose |
|
387 |
end; |
|
388 |
||
389 |
val disc_thms = flat (map2 (fn true => K [] | false => I) no_discs disc_thmss); |
|
49028 | 390 |
|
49278 | 391 |
val disc_exclude_thms = |
392 |
if has_alternate_disc_def then |
|
393 |
[] |
|
394 |
else |
|
395 |
let |
|
396 |
fun mk_goal [] = [] |
|
397 |
| mk_goal [((_, true), (_, true))] = [] |
|
398 |
| mk_goal [(((_, disc), _), ((_, disc'), _))] = |
|
399 |
[Logic.all v (Logic.mk_implies (HOLogic.mk_Trueprop (betapply (disc, v)), |
|
400 |
HOLogic.mk_Trueprop (HOLogic.mk_not (betapply (disc', v)))))]; |
|
401 |
fun prove tac goal = Skip_Proof.prove lthy [] [] goal (K tac); |
|
402 |
||
403 |
val bundles = ms ~~ discD_thms ~~ discs ~~ no_discs; |
|
404 |
val half_pairss = mk_half_pairss bundles; |
|
405 |
||
406 |
val goal_halvess = map mk_goal half_pairss; |
|
407 |
val half_thmss = |
|
408 |
map3 (fn [] => K (K []) | [goal] => fn [((((m, discD), _), _), _)] => |
|
409 |
fn disc_thm => [prove (mk_half_disc_exclude_tac m discD disc_thm) goal]) |
|
410 |
goal_halvess half_pairss (flat disc_thmss'); |
|
411 |
||
412 |
val goal_other_halvess = map (mk_goal o map swap) half_pairss; |
|
413 |
val other_half_thmss = |
|
414 |
map2 (map2 (prove o mk_other_half_disc_exclude_tac)) half_thmss |
|
415 |
goal_other_halvess; |
|
416 |
in |
|
417 |
interleave (flat half_thmss) (flat other_half_thmss) |
|
418 |
end; |
|
49028 | 419 |
|
49278 | 420 |
val disc_exhaust_thms = |
421 |
if has_alternate_disc_def orelse no_discs_at_all then |
|
422 |
[] |
|
423 |
else |
|
424 |
let |
|
425 |
fun mk_prem disc = mk_imp_p [HOLogic.mk_Trueprop (betapply (disc, v))]; |
|
426 |
val goal = fold_rev Logic.all [p, v] (mk_imp_p (map mk_prem discs)); |
|
427 |
in |
|
428 |
[Skip_Proof.prove lthy [] [] goal (fn _ => |
|
429 |
mk_disc_exhaust_tac n exhaust_thm discI_thms)] |
|
430 |
end; |
|
49025 | 431 |
|
49278 | 432 |
val collapse_thms = |
433 |
if no_dests then |
|
434 |
[] |
|
435 |
else |
|
436 |
let |
|
437 |
fun mk_goal ctr disc sels = |
|
438 |
let |
|
439 |
val prem = HOLogic.mk_Trueprop (betapply (disc, v)); |
|
440 |
val concl = |
|
441 |
mk_Trueprop_eq ((null sels ? swap) |
|
442 |
(Term.list_comb (ctr, map ap_v sels), v)); |
|
443 |
in |
|
444 |
if prem aconv concl then NONE |
|
445 |
else SOME (Logic.all v (Logic.mk_implies (prem, concl))) |
|
446 |
end; |
|
447 |
val goals = map3 mk_goal ctrs discs selss; |
|
448 |
in |
|
449 |
map4 (fn m => fn discD => fn sel_thms => Option.map (fn goal => |
|
450 |
Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} => |
|
451 |
mk_collapse_tac ctxt m discD sel_thms) |
|
452 |
|> perhaps (try (fn thm => refl RS thm)))) ms discD_thms sel_thmss goals |
|
453 |
|> map_filter I |
|
454 |
end; |
|
455 |
||
456 |
val case_eq_thms = |
|
457 |
if no_dests then |
|
458 |
[] |
|
459 |
else |
|
460 |
let |
|
461 |
fun mk_body f sels = Term.list_comb (f, map ap_v sels); |
|
462 |
val goal = |
|
463 |
mk_Trueprop_eq (fcase $ v, mk_IfN B (map ap_v discs) (map2 mk_body fs selss)); |
|
464 |
in |
|
465 |
[Skip_Proof.prove lthy [] [] goal (fn {context = ctxt, ...} => |
|
466 |
mk_case_eq_tac ctxt n exhaust_thm' case_thms disc_thmss' sel_thmss)] |
|
467 |
|> Proof_Context.export names_lthy lthy |
|
468 |
end; |
|
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
469 |
in |
49278 | 470 |
(sel_thmss, disc_thms, discI_thms, disc_exclude_thms, disc_exhaust_thms, |
471 |
collapse_thms, case_eq_thms) |
|
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
472 |
end; |
49025 | 473 |
|
49033 | 474 |
val (case_cong_thm, weak_case_cong_thm) = |
49032 | 475 |
let |
476 |
fun mk_prem xctr xs f g = |
|
49045
7d9631754bba
minor fixes (for compatibility with existing datatype package)
blanchet
parents:
49044
diff
changeset
|
477 |
fold_rev Logic.all xs (Logic.mk_implies (mk_Trueprop_eq (w, xctr), |
49032 | 478 |
mk_Trueprop_eq (f, g))); |
49033 | 479 |
|
480 |
val v_eq_w = mk_Trueprop_eq (v, w); |
|
49032 | 481 |
|
482 |
val goal = |
|
49033 | 483 |
Logic.list_implies (v_eq_w :: map4 mk_prem xctrs xss fs gs, |
49201 | 484 |
mk_Trueprop_eq (fcase $ v, gcase $ w)); |
485 |
val goal_weak = Logic.mk_implies (v_eq_w, mk_Trueprop_eq (fcase $ v, fcase $ w)); |
|
49032 | 486 |
in |
49049 | 487 |
(Skip_Proof.prove lthy [] [] goal (fn _ => mk_case_cong_tac exhaust_thm' case_thms), |
49033 | 488 |
Skip_Proof.prove lthy [] [] goal_weak (K (etac arg_cong 1))) |
489 |
|> pairself (singleton (Proof_Context.export names_lthy lthy)) |
|
49032 | 490 |
end; |
49025 | 491 |
|
49044 | 492 |
val (split_thm, split_asm_thm) = |
49043 | 493 |
let |
49044 | 494 |
fun mk_conjunct xctr xs f_xs = |
49043 | 495 |
list_all_free xs (HOLogic.mk_imp (HOLogic.mk_eq (v, xctr), q $ f_xs)); |
49044 | 496 |
fun mk_disjunct xctr xs f_xs = |
497 |
list_exists_free xs (HOLogic.mk_conj (HOLogic.mk_eq (v, xctr), |
|
498 |
HOLogic.mk_not (q $ f_xs))); |
|
499 |
||
49201 | 500 |
val lhs = q $ (fcase $ v); |
49044 | 501 |
|
49043 | 502 |
val goal = |
49044 | 503 |
mk_Trueprop_eq (lhs, Library.foldr1 HOLogic.mk_conj (map3 mk_conjunct xctrs xss xfs)); |
504 |
val goal_asm = |
|
505 |
mk_Trueprop_eq (lhs, HOLogic.mk_not (Library.foldr1 HOLogic.mk_disj |
|
506 |
(map3 mk_disjunct xctrs xss xfs))); |
|
507 |
||
508 |
val split_thm = |
|
49049 | 509 |
Skip_Proof.prove lthy [] [] goal |
49052 | 510 |
(fn _ => mk_split_tac exhaust_thm' case_thms inject_thmss distinct_thmsss) |
49044 | 511 |
|> singleton (Proof_Context.export names_lthy lthy) |
512 |
val split_asm_thm = |
|
513 |
Skip_Proof.prove lthy [] [] goal_asm (fn {context = ctxt, ...} => |
|
514 |
mk_split_asm_tac ctxt split_thm) |
|
515 |
|> singleton (Proof_Context.export names_lthy lthy) |
|
49043 | 516 |
in |
49044 | 517 |
(split_thm, split_asm_thm) |
49043 | 518 |
end; |
49025 | 519 |
|
49052 | 520 |
val notes = |
521 |
[(case_congN, [case_cong_thm]), |
|
49278 | 522 |
(case_eqN, case_eq_thms), |
49052 | 523 |
(casesN, case_thms), |
49118 | 524 |
(collapseN, collapse_thms), |
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
525 |
(discsN, disc_thms), |
49122 | 526 |
(disc_excludeN, disc_exclude_thms), |
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
527 |
(disc_exhaustN, disc_exhaust_thms), |
49052 | 528 |
(distinctN, distinct_thms), |
529 |
(exhaustN, [exhaust_thm]), |
|
49121 | 530 |
(injectN, flat inject_thmss), |
49052 | 531 |
(nchotomyN, [nchotomy_thm]), |
49121 | 532 |
(selsN, flat sel_thmss), |
49052 | 533 |
(splitN, [split_thm]), |
534 |
(split_asmN, [split_asm_thm]), |
|
535 |
(weak_case_cong_thmsN, [weak_case_cong_thm])] |
|
49116
3d520eec2746
allow pseudo-definition of is_Cons in terms of is_Nil (and similarly for other two-constructor datatypes)
blanchet
parents:
49114
diff
changeset
|
536 |
|> filter_out (null o snd) |
49052 | 537 |
|> map (fn (thmN, thms) => |
538 |
((Binding.qualify true (Binding.name_of b) (Binding.name thmN), []), [(thms, [])])); |
|
49019 | 539 |
in |
49266 | 540 |
((selss, discI_thms, sel_thmss), lthy |> Local_Theory.notes notes |> snd) |
49019 | 541 |
end; |
49017 | 542 |
in |
49121 | 543 |
(goalss, after_qed, lthy') |
49017 | 544 |
end; |
545 |
||
49199 | 546 |
fun wrap_datatype tacss = (fn (goalss, after_qed, lthy) => |
49111 | 547 |
map2 (map2 (Skip_Proof.prove lthy [] [])) goalss tacss |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
548 |
|> (fn thms => after_qed thms lthy)) oo prepare_wrap_datatype (K I); |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
549 |
|
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
550 |
fun parse_bracket_list parser = @{keyword "["} |-- Parse.list parser --| @{keyword "]"}; |
49111 | 551 |
|
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
552 |
val parse_bindings = parse_bracket_list Parse.binding; |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
553 |
val parse_bindingss = parse_bracket_list parse_bindings; |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
554 |
|
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
555 |
val parse_bound_term = (Parse.binding --| @{keyword ":"}) -- Parse.term; |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
556 |
val parse_bound_terms = parse_bracket_list parse_bound_term; |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
557 |
val parse_bound_termss = parse_bracket_list parse_bound_terms; |
49017 | 558 |
|
49199 | 559 |
val wrap_datatype_cmd = (fn (goalss, after_qed, lthy) => |
49266 | 560 |
Proof.theorem NONE (snd oo after_qed) (map (map (rpair [])) goalss) lthy) oo |
49199 | 561 |
prepare_wrap_datatype Syntax.read_term; |
49017 | 562 |
|
49278 | 563 |
val parse_wrap_options = |
564 |
Scan.optional (@{keyword "("} |-- (@{keyword "no_dests"} >> K true) --| @{keyword ")"}) false; |
|
565 |
||
49017 | 566 |
val _ = |
49074 | 567 |
Outer_Syntax.local_theory_to_proof @{command_spec "wrap_data"} "wraps an existing datatype" |
49278 | 568 |
((parse_wrap_options -- (@{keyword "["} |-- Parse.list Parse.term --| @{keyword "]"}) -- |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
569 |
Parse.term -- Scan.optional (parse_bindings -- Scan.optional (parse_bindingss -- |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
570 |
Scan.optional parse_bound_termss []) ([], [])) ([], ([], []))) |
49199 | 571 |
>> wrap_datatype_cmd); |
49017 | 572 |
|
573 |
end; |