author | blanchet |
Tue, 12 Nov 2013 13:47:24 +0100 | |
changeset 54396 | 8baee6b04a7c |
parent 54386 | src/HOL/BNF/Tools/ctr_sugar.ML@3514fdfdf59b |
child 54397 | f4b4fa25ce56 |
permissions | -rw-r--r-- |
54007 | 1 |
(* Title: HOL/BNF/Tools/ctr_sugar.ML |
49017 | 2 |
Author: Jasmin Blanchette, TU Muenchen |
3 |
Copyright 2012 |
|
4 |
||
51797 | 5 |
Wrapping existing freely generated type's constructors. |
49017 | 6 |
*) |
7 |
||
54006 | 8 |
signature CTR_SUGAR = |
49017 | 9 |
sig |
51840 | 10 |
type ctr_sugar = |
51839 | 11 |
{ctrs: term list, |
52375 | 12 |
casex: term, |
51839 | 13 |
discs: term list, |
51819 | 14 |
selss: term list list, |
15 |
exhaust: thm, |
|
52375 | 16 |
nchotomy: thm, |
51819 | 17 |
injects: thm list, |
18 |
distincts: thm list, |
|
19 |
case_thms: thm list, |
|
52375 | 20 |
case_cong: thm, |
21 |
weak_case_cong: thm, |
|
22 |
split: thm, |
|
23 |
split_asm: thm, |
|
51819 | 24 |
disc_thmss: thm list list, |
25 |
discIs: thm list, |
|
53475 | 26 |
sel_thmss: thm list list, |
27 |
disc_exhausts: thm list, |
|
53916 | 28 |
sel_exhausts: thm list, |
53475 | 29 |
collapses: thm list, |
30 |
expands: thm list, |
|
53917 | 31 |
sel_splits: thm list, |
32 |
sel_split_asms: thm list, |
|
53857 | 33 |
case_conv_ifs: thm list}; |
51809 | 34 |
|
51840 | 35 |
val morph_ctr_sugar: morphism -> ctr_sugar -> ctr_sugar |
54256 | 36 |
val transfer_ctr_sugar: Proof.context -> ctr_sugar -> ctr_sugar |
53867
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
37 |
val ctr_sugar_of: Proof.context -> string -> ctr_sugar option |
53906 | 38 |
val ctr_sugars_of: Proof.context -> ctr_sugar list |
51823
38996458bc5c
create data structure for storing (co)datatype information
blanchet
parents:
51819
diff
changeset
|
39 |
|
49633 | 40 |
val rep_compat_prefix: string |
41 |
||
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
42 |
val mk_half_pairss: 'a list * 'a list -> ('a * 'a) list list |
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
43 |
val join_halves: int -> 'a list list -> 'a list list -> 'a list * 'a list list list |
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
44 |
|
49203 | 45 |
val mk_ctr: typ list -> term -> term |
53864
a48d4bd3faaa
use case rather than sequence of ifs in expansion
blanchet
parents:
53857
diff
changeset
|
46 |
val mk_case: typ list -> typ -> term -> term |
49484 | 47 |
val mk_disc_or_sel: typ list -> term -> term |
49622 | 48 |
val name_of_ctr: term -> string |
51777
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
49 |
val name_of_disc: term -> string |
53888 | 50 |
val dest_ctr: Proof.context -> string -> term -> term * term list |
53870 | 51 |
val dest_case: Proof.context -> string -> typ list -> term -> (term list * term list) option |
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
52 |
|
51781 | 53 |
val wrap_free_constructors: ({prems: thm list, context: Proof.context} -> tactic) list list -> |
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
54 |
(((bool * bool) * term list) * binding) * |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
55 |
(binding list * (binding list list * (binding * term) list list)) -> local_theory -> |
51840 | 56 |
ctr_sugar * local_theory |
52823 | 57 |
val parse_wrap_free_constructors_options: (bool * bool) parser |
49286 | 58 |
val parse_bound_term: (binding * string) parser |
49017 | 59 |
end; |
60 |
||
54006 | 61 |
structure Ctr_Sugar : CTR_SUGAR = |
49017 | 62 |
struct |
63 |
||
54008
b15cfc2864de
refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
54007
diff
changeset
|
64 |
open Ctr_Sugar_Util |
54006 | 65 |
open Ctr_Sugar_Tactics |
49017 | 66 |
|
51840 | 67 |
type ctr_sugar = |
51839 | 68 |
{ctrs: term list, |
52375 | 69 |
casex: term, |
51839 | 70 |
discs: term list, |
51819 | 71 |
selss: term list list, |
72 |
exhaust: thm, |
|
52375 | 73 |
nchotomy: thm, |
51819 | 74 |
injects: thm list, |
75 |
distincts: thm list, |
|
76 |
case_thms: thm list, |
|
52375 | 77 |
case_cong: thm, |
78 |
weak_case_cong: thm, |
|
79 |
split: thm, |
|
80 |
split_asm: thm, |
|
51819 | 81 |
disc_thmss: thm list list, |
82 |
discIs: thm list, |
|
53475 | 83 |
sel_thmss: thm list list, |
84 |
disc_exhausts: thm list, |
|
53916 | 85 |
sel_exhausts: thm list, |
53475 | 86 |
collapses: thm list, |
87 |
expands: thm list, |
|
53917 | 88 |
sel_splits: thm list, |
89 |
sel_split_asms: thm list, |
|
53857 | 90 |
case_conv_ifs: thm list}; |
51809 | 91 |
|
53906 | 92 |
fun eq_ctr_sugar ({ctrs = ctrs1, casex = case1, discs = discs1, selss = selss1, ...} : ctr_sugar, |
93 |
{ctrs = ctrs2, casex = case2, discs = discs2, selss = selss2, ...} : ctr_sugar) = |
|
94 |
ctrs1 = ctrs2 andalso case1 = case2 andalso discs1 = discs2 andalso selss1 = selss2; |
|
95 |
||
52375 | 96 |
fun morph_ctr_sugar phi {ctrs, casex, discs, selss, exhaust, nchotomy, injects, distincts, |
53475 | 97 |
case_thms, case_cong, weak_case_cong, split, split_asm, disc_thmss, discIs, sel_thmss, |
53917 | 98 |
disc_exhausts, sel_exhausts, collapses, expands, sel_splits, sel_split_asms, case_conv_ifs} = |
51839 | 99 |
{ctrs = map (Morphism.term phi) ctrs, |
52375 | 100 |
casex = Morphism.term phi casex, |
51839 | 101 |
discs = map (Morphism.term phi) discs, |
51823
38996458bc5c
create data structure for storing (co)datatype information
blanchet
parents:
51819
diff
changeset
|
102 |
selss = map (map (Morphism.term phi)) selss, |
38996458bc5c
create data structure for storing (co)datatype information
blanchet
parents:
51819
diff
changeset
|
103 |
exhaust = Morphism.thm phi exhaust, |
52375 | 104 |
nchotomy = Morphism.thm phi nchotomy, |
51823
38996458bc5c
create data structure for storing (co)datatype information
blanchet
parents:
51819
diff
changeset
|
105 |
injects = map (Morphism.thm phi) injects, |
38996458bc5c
create data structure for storing (co)datatype information
blanchet
parents:
51819
diff
changeset
|
106 |
distincts = map (Morphism.thm phi) distincts, |
38996458bc5c
create data structure for storing (co)datatype information
blanchet
parents:
51819
diff
changeset
|
107 |
case_thms = map (Morphism.thm phi) case_thms, |
52375 | 108 |
case_cong = Morphism.thm phi case_cong, |
109 |
weak_case_cong = Morphism.thm phi weak_case_cong, |
|
110 |
split = Morphism.thm phi split, |
|
111 |
split_asm = Morphism.thm phi split_asm, |
|
51823
38996458bc5c
create data structure for storing (co)datatype information
blanchet
parents:
51819
diff
changeset
|
112 |
disc_thmss = map (map (Morphism.thm phi)) disc_thmss, |
38996458bc5c
create data structure for storing (co)datatype information
blanchet
parents:
51819
diff
changeset
|
113 |
discIs = map (Morphism.thm phi) discIs, |
53475 | 114 |
sel_thmss = map (map (Morphism.thm phi)) sel_thmss, |
115 |
disc_exhausts = map (Morphism.thm phi) disc_exhausts, |
|
53916 | 116 |
sel_exhausts = map (Morphism.thm phi) sel_exhausts, |
53475 | 117 |
collapses = map (Morphism.thm phi) collapses, |
118 |
expands = map (Morphism.thm phi) expands, |
|
53917 | 119 |
sel_splits = map (Morphism.thm phi) sel_splits, |
120 |
sel_split_asms = map (Morphism.thm phi) sel_split_asms, |
|
53857 | 121 |
case_conv_ifs = map (Morphism.thm phi) case_conv_ifs}; |
51823
38996458bc5c
create data structure for storing (co)datatype information
blanchet
parents:
51819
diff
changeset
|
122 |
|
53906 | 123 |
val transfer_ctr_sugar = |
124 |
morph_ctr_sugar o Morphism.thm_morphism o Thm.transfer o Proof_Context.theory_of; |
|
53867
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
125 |
|
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
126 |
structure Data = Generic_Data |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
127 |
( |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
128 |
type T = ctr_sugar Symtab.table; |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
129 |
val empty = Symtab.empty; |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
130 |
val extend = I; |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
131 |
val merge = Symtab.merge eq_ctr_sugar; |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
132 |
); |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
133 |
|
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
134 |
fun ctr_sugar_of ctxt = |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
135 |
Symtab.lookup (Data.get (Context.Proof ctxt)) |
53906 | 136 |
#> Option.map (transfer_ctr_sugar ctxt); |
137 |
||
138 |
fun ctr_sugars_of ctxt = |
|
139 |
Symtab.fold (cons o transfer_ctr_sugar ctxt o snd) (Data.get (Context.Proof ctxt)) []; |
|
53867
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
140 |
|
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
141 |
fun register_ctr_sugar key ctr_sugar = |
54285
578371ba74cc
reverted 3e1d230f1c00 -- pervasiveness is useful, cf. Coinductive_Nat in the AFP
blanchet
parents:
54265
diff
changeset
|
142 |
Local_Theory.declaration {syntax = false, pervasive = true} |
53867
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
143 |
(fn phi => Data.map (Symtab.default (key, morph_ctr_sugar phi ctr_sugar))); |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
144 |
|
49633 | 145 |
val rep_compat_prefix = "new"; |
146 |
||
49223 | 147 |
val isN = "is_"; |
148 |
val unN = "un_"; |
|
149 |
fun mk_unN 1 1 suf = unN ^ suf |
|
150 |
| 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
|
151 |
|
49594
55e798614c45
tweaked theorem names (in particular, dropped s's)
blanchet
parents:
49591
diff
changeset
|
152 |
val caseN = "case"; |
49054 | 153 |
val case_congN = "case_cong"; |
53857 | 154 |
val case_conv_ifN = "case_conv_if"; |
49118 | 155 |
val collapseN = "collapse"; |
49122 | 156 |
val disc_excludeN = "disc_exclude"; |
49054 | 157 |
val disc_exhaustN = "disc_exhaust"; |
53694 | 158 |
val discN = "disc"; |
53700 | 159 |
val discIN = "discI"; |
49054 | 160 |
val distinctN = "distinct"; |
49075 | 161 |
val exhaustN = "exhaust"; |
49486 | 162 |
val expandN = "expand"; |
49075 | 163 |
val injectN = "inject"; |
164 |
val nchotomyN = "nchotomy"; |
|
53694 | 165 |
val selN = "sel"; |
53916 | 166 |
val sel_exhaustN = "sel_exhaust"; |
53917 | 167 |
val sel_splitN = "sel_split"; |
168 |
val sel_split_asmN = "sel_split_asm"; |
|
49054 | 169 |
val splitN = "split"; |
49633 | 170 |
val splitsN = "splits"; |
49054 | 171 |
val split_asmN = "split_asm"; |
172 |
val weak_case_cong_thmsN = "weak_case_cong"; |
|
49019 | 173 |
|
53700 | 174 |
val cong_attrs = @{attributes [cong]}; |
53836 | 175 |
val dest_attrs = @{attributes [dest]}; |
53700 | 176 |
val safe_elim_attrs = @{attributes [elim!]}; |
177 |
val iff_attrs = @{attributes [iff]}; |
|
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54008
diff
changeset
|
178 |
val inductsimp_attrs = @{attributes [induct_simp]}; |
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54008
diff
changeset
|
179 |
val nitpicksimp_attrs = @{attributes [nitpick_simp]}; |
49300 | 180 |
val simp_attrs = @{attributes [simp]}; |
54151 | 181 |
val code_nitpicksimp_attrs = Code.add_default_eqn_attrib :: nitpicksimp_attrs; |
182 |
val code_nitpicksimp_simp_attrs = code_nitpicksimp_attrs @ simp_attrs; |
|
49046
3c5eba97d93a
allow default names for selectors via wildcard (_) + fix wrong index (k)
blanchet
parents:
49045
diff
changeset
|
183 |
|
53040 | 184 |
fun unflat_lookup eq xs ys = map (fn xs' => permute_like eq xs xs' ys); |
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
185 |
|
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
186 |
fun mk_half_pairss' _ ([], []) = [] |
49591
91b228e26348
generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents:
49586
diff
changeset
|
187 |
| mk_half_pairss' indent (x :: xs, _ :: ys) = |
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
188 |
indent @ fold_rev (cons o single o pair x) ys (mk_half_pairss' ([] :: indent) (xs, ys)); |
49486 | 189 |
|
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
190 |
fun mk_half_pairss p = mk_half_pairss' [[]] p; |
49027 | 191 |
|
49486 | 192 |
fun join_halves n half_xss other_half_xss = |
193 |
let |
|
194 |
val xsss = |
|
195 |
map2 (map2 append) (Library.chop_groups n half_xss) |
|
196 |
(transpose (Library.chop_groups n other_half_xss)) |
|
49668 | 197 |
val xs = splice (flat half_xss) (flat other_half_xss); |
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
198 |
in (xs, xsss) end; |
49027 | 199 |
|
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
200 |
fun mk_undefined T = Const (@{const_name undefined}, T); |
49055 | 201 |
|
49500 | 202 |
fun mk_ctr Ts t = |
203 |
let val Type (_, Ts0) = body_type (fastype_of t) in |
|
204 |
Term.subst_atomic_types (Ts0 ~~ Ts) t |
|
49203 | 205 |
end; |
206 |
||
49536 | 207 |
fun mk_case Ts T t = |
208 |
let val (Type (_, Ts0), body) = strip_type (fastype_of t) |>> List.last in |
|
209 |
Term.subst_atomic_types ((body, T) :: (Ts0 ~~ Ts)) t |
|
210 |
end; |
|
211 |
||
53864
a48d4bd3faaa
use case rather than sequence of ifs in expansion
blanchet
parents:
53857
diff
changeset
|
212 |
fun mk_disc_or_sel Ts t = |
a48d4bd3faaa
use case rather than sequence of ifs in expansion
blanchet
parents:
53857
diff
changeset
|
213 |
Term.subst_atomic_types (snd (Term.dest_Type (domain_type (fastype_of t))) ~~ Ts) t; |
a48d4bd3faaa
use case rather than sequence of ifs in expansion
blanchet
parents:
53857
diff
changeset
|
214 |
|
51777
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
215 |
fun name_of_const what t = |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
216 |
(case head_of t of |
49622 | 217 |
Const (s, _) => s |
218 |
| Free (s, _) => s |
|
51777
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
219 |
| _ => error ("Cannot extract name of " ^ what)); |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
220 |
|
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
221 |
val name_of_ctr = name_of_const "constructor"; |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
222 |
|
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
223 |
val notN = "not_"; |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
224 |
val eqN = "eq_"; |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
225 |
val neqN = "neq_"; |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
226 |
|
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
227 |
fun name_of_disc t = |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
228 |
(case head_of t of |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
229 |
Abs (_, _, @{const Not} $ (t' $ Bound 0)) => |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
230 |
Long_Name.map_base_name (prefix notN) (name_of_disc t') |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
231 |
| Abs (_, _, Const (@{const_name HOL.eq}, _) $ Bound 0 $ t') => |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
232 |
Long_Name.map_base_name (prefix eqN) (name_of_disc t') |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
233 |
| Abs (_, _, @{const Not} $ (Const (@{const_name HOL.eq}, _) $ Bound 0 $ t')) => |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
234 |
Long_Name.map_base_name (prefix neqN) (name_of_disc t') |
48a0ae342ea0
generate proper attributes for coinduction rules
blanchet
parents:
51771
diff
changeset
|
235 |
| t' => name_of_const "destructor" t'); |
49622 | 236 |
|
237 |
val base_name_of_ctr = Long_Name.base_name o name_of_ctr; |
|
49046
3c5eba97d93a
allow default names for selectors via wildcard (_) + fix wrong index (k)
blanchet
parents:
49045
diff
changeset
|
238 |
|
53888 | 239 |
fun dest_ctr ctxt s t = |
240 |
let |
|
241 |
val (f, args) = Term.strip_comb t; |
|
242 |
in |
|
243 |
(case ctr_sugar_of ctxt s of |
|
244 |
SOME {ctrs, ...} => |
|
245 |
(case find_first (can (fo_match ctxt f)) ctrs of |
|
246 |
SOME f' => (f', args) |
|
247 |
| NONE => raise Fail "dest_ctr") |
|
248 |
| NONE => raise Fail "dest_ctr") |
|
249 |
end; |
|
250 |
||
53870 | 251 |
fun dest_case ctxt s Ts t = |
252 |
(case Term.strip_comb t of |
|
253 |
(Const (c, _), args as _ :: _) => |
|
254 |
(case ctr_sugar_of ctxt s of |
|
255 |
SOME {casex = Const (case_name, _), discs = discs0, selss = selss0, ...} => |
|
256 |
if case_name = c then |
|
53924 | 257 |
let val n = length discs0 in |
258 |
if n < length args then |
|
259 |
let |
|
260 |
val (branches, obj :: leftovers) = chop n args; |
|
261 |
val discs = map (mk_disc_or_sel Ts) discs0; |
|
262 |
val selss = map (map (mk_disc_or_sel Ts)) selss0; |
|
263 |
val conds = map (rapp obj) discs; |
|
264 |
val branch_argss = map (fn sels => map (rapp obj) sels @ leftovers) selss; |
|
265 |
val branches' = map2 (curry Term.betapplys) branches branch_argss; |
|
266 |
in |
|
267 |
SOME (conds, branches') |
|
268 |
end |
|
269 |
else |
|
270 |
NONE |
|
53870 | 271 |
end |
272 |
else |
|
273 |
NONE |
|
274 |
| _ => NONE) |
|
275 |
| _ => NONE); |
|
276 |
||
49437 | 277 |
fun eta_expand_arg xs f_xs = fold_rev Term.lambda xs f_xs; |
278 |
||
52969
f2df0730f8ac
clarified option name (since case/fold/rec are also destructors)
blanchet
parents:
52968
diff
changeset
|
279 |
fun prepare_wrap_free_constructors prep_term ((((no_discs_sels, rep_compat), raw_ctrs), |
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
280 |
raw_case_binding), (raw_disc_bindings, (raw_sel_bindingss, raw_sel_defaultss))) no_defs_lthy = |
49017 | 281 |
let |
49019 | 282 |
(* TODO: sanity checks on arguments *) |
49025 | 283 |
|
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
284 |
val n = length raw_ctrs; |
49054 | 285 |
val ks = 1 upto n; |
286 |
||
49121 | 287 |
val _ = if n > 0 then () else error "No constructors specified"; |
288 |
||
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
289 |
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
|
290 |
val sel_defaultss = |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
291 |
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
|
292 |
|
53908 | 293 |
val Type (fcT_name, As0) = body_type (fastype_of (hd ctrs0)); |
294 |
val fc_b_name = Long_Name.base_name fcT_name; |
|
295 |
val fc_b = Binding.name fc_b_name; |
|
49055 | 296 |
|
49633 | 297 |
fun qualify mandatory = |
53908 | 298 |
Binding.qualify mandatory fc_b_name o (rep_compat ? Binding.qualify false rep_compat_prefix); |
49633 | 299 |
|
54386 | 300 |
fun dest_TFree_or_TVar (TFree sS) = sS |
53268
de1dc709f9d4
handle type class annotations on (co)datatype parameters gracefully
blanchet
parents:
53210
diff
changeset
|
301 |
| dest_TFree_or_TVar (TVar ((s, _), S)) = (s, S) |
de1dc709f9d4
handle type class annotations on (co)datatype parameters gracefully
blanchet
parents:
53210
diff
changeset
|
302 |
| dest_TFree_or_TVar _ = error "Invalid type argument"; |
52965
0cd62cb233e0
handle both TVars and TFrees -- necessary for 'wrap_free_constructors'
blanchet
parents:
52963
diff
changeset
|
303 |
|
53268
de1dc709f9d4
handle type class annotations on (co)datatype parameters gracefully
blanchet
parents:
53210
diff
changeset
|
304 |
val (unsorted_As, B) = |
49055 | 305 |
no_defs_lthy |
53268
de1dc709f9d4
handle type class annotations on (co)datatype parameters gracefully
blanchet
parents:
53210
diff
changeset
|
306 |
|> variant_tfrees (map (fst o dest_TFree_or_TVar) As0) |
49055 | 307 |
||> the_single o fst o mk_TFrees 1; |
308 |
||
53268
de1dc709f9d4
handle type class annotations on (co)datatype parameters gracefully
blanchet
parents:
53210
diff
changeset
|
309 |
val As = map2 (resort_tfree o snd o dest_TFree_or_TVar) As0 unsorted_As; |
de1dc709f9d4
handle type class annotations on (co)datatype parameters gracefully
blanchet
parents:
53210
diff
changeset
|
310 |
|
53908 | 311 |
val fcT = Type (fcT_name, As); |
49055 | 312 |
val ctrs = map (mk_ctr As) ctrs0; |
313 |
val ctr_Tss = map (binder_types o fastype_of) ctrs; |
|
314 |
||
315 |
val ms = map length ctr_Tss; |
|
316 |
||
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
317 |
val raw_disc_bindings' = pad_list Binding.empty n raw_disc_bindings; |
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
318 |
|
53925 | 319 |
fun can_definitely_rely_on_disc k = not (Binding.is_empty (nth raw_disc_bindings' (k - 1))); |
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
320 |
fun can_rely_on_disc k = |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
321 |
can_definitely_rely_on_disc k orelse (k = 1 andalso not (can_definitely_rely_on_disc 2)); |
53925 | 322 |
fun should_omit_disc_binding k = n = 1 orelse (n = 2 andalso can_rely_on_disc (3 - k)); |
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
323 |
|
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
324 |
fun is_disc_binding_valid b = |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
325 |
not (Binding.is_empty b orelse Binding.eq_name (b, equal_binding)); |
51787
1267c28c7bdd
changed discriminator default: avoid mixing ctor and dtor views
blanchet
parents:
51781
diff
changeset
|
326 |
|
52322 | 327 |
val standard_disc_binding = Binding.name o prefix isN o base_name_of_ctr; |
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
328 |
|
49336 | 329 |
val disc_bindings = |
330 |
raw_disc_bindings' |
|
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
331 |
|> map4 (fn k => fn m => fn ctr => fn disc => |
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
332 |
qualify false |
51787
1267c28c7bdd
changed discriminator default: avoid mixing ctor and dtor views
blanchet
parents:
51781
diff
changeset
|
333 |
(if Binding.is_empty disc then |
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
334 |
if should_omit_disc_binding k then disc else standard_disc_binding ctr |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
335 |
else if Binding.eq_name (disc, equal_binding) then |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
336 |
if m = 0 then disc |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
337 |
else error "Cannot use \"=\" syntax for discriminating nonnullary constructor" |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
338 |
else if Binding.eq_name (disc, standard_binding) then |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
339 |
standard_disc_binding ctr |
49302
f5bd87aac224
added optional qualifiers for constructors and destructors, similarly to the old package
blanchet
parents:
49300
diff
changeset
|
340 |
else |
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
341 |
disc)) ks ms ctrs0; |
49056 | 342 |
|
51787
1267c28c7bdd
changed discriminator default: avoid mixing ctor and dtor views
blanchet
parents:
51781
diff
changeset
|
343 |
fun standard_sel_binding m l = Binding.name o mk_unN m l o base_name_of_ctr; |
49120
7f8e69fc6ac9
smarter "*" syntax -- fallback on "_" if "*" is impossible
blanchet
parents:
49119
diff
changeset
|
344 |
|
49336 | 345 |
val sel_bindingss = |
346 |
pad_list [] n raw_sel_bindingss |
|
49056 | 347 |
|> map3 (fn ctr => fn m => map2 (fn l => fn sel => |
49633 | 348 |
qualify false |
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
349 |
(if Binding.is_empty sel orelse Binding.eq_name (sel, standard_binding) then |
51787
1267c28c7bdd
changed discriminator default: avoid mixing ctor and dtor views
blanchet
parents:
51781
diff
changeset
|
350 |
standard_sel_binding m l ctr |
49302
f5bd87aac224
added optional qualifiers for constructors and destructors, similarly to the old package
blanchet
parents:
49300
diff
changeset
|
351 |
else |
49434
433dc7e028c8
separated registration of BNFs from bnf_def (BNFs are now stored only for bnf_def and (co)data commands)
traytel
parents:
49364
diff
changeset
|
352 |
sel)) (1 upto m) o pad_list Binding.empty m) ctrs0 ms; |
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
353 |
|
49201 | 354 |
val case_Ts = map (fn Ts => Ts ---> B) ctr_Tss; |
49043 | 355 |
|
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
356 |
val ((((((((xss, xss'), yss), fs), gs), [u', v']), [w]), (p, p')), names_lthy) = no_defs_lthy |> |
49364
838b5e8ede73
allow default values to refer to selector arguments -- this is useful, e.g. for tllist: ttl (TNil x) = TNil x (example by Andreas Lochbihler)
blanchet
parents:
49336
diff
changeset
|
357 |
mk_Freess' "x" ctr_Tss |
49025 | 358 |
||>> mk_Freess "y" ctr_Tss |
49201 | 359 |
||>> mk_Frees "f" case_Ts |
360 |
||>> mk_Frees "g" case_Ts |
|
53908 | 361 |
||>> (apfst (map (rpair fcT)) oo Variable.variant_fixes) [fc_b_name, fc_b_name ^ "'"] |
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
362 |
||>> mk_Frees "z" [B] |
49043 | 363 |
||>> yield_singleton (apfst (op ~~) oo mk_Frees' "P") HOLogic.boolT; |
364 |
||
49498 | 365 |
val u = Free u'; |
366 |
val v = Free v'; |
|
49463 | 367 |
val q = Free (fst p', mk_pred1T B); |
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
368 |
|
49025 | 369 |
val xctrs = map2 (curry Term.list_comb) ctrs xss; |
370 |
val yctrs = map2 (curry Term.list_comb) ctrs yss; |
|
49032 | 371 |
|
49043 | 372 |
val xfs = map2 (curry Term.list_comb) fs xss; |
373 |
val xgs = map2 (curry Term.list_comb) gs xss; |
|
374 |
||
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
375 |
(* TODO: Eta-expension is for compatibility with the old datatype package (but it also provides |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
376 |
nicer names). Consider removing. *) |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
377 |
val eta_fs = map2 eta_expand_arg xss xfs; |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
378 |
val eta_gs = map2 eta_expand_arg xss xgs; |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
379 |
|
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
380 |
val case_binding = |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
381 |
qualify false |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
382 |
(if Binding.is_empty raw_case_binding orelse |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
383 |
Binding.eq_name (raw_case_binding, standard_binding) then |
53908 | 384 |
Binding.suffix_name ("_" ^ caseN) fc_b |
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
385 |
else |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
386 |
raw_case_binding); |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
387 |
|
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
388 |
fun mk_case_disj xctr xf xs = |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
389 |
list_exists_free xs (HOLogic.mk_conj (HOLogic.mk_eq (u, xctr), HOLogic.mk_eq (w, xf))); |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
390 |
|
53925 | 391 |
val case_rhs = fold_rev (fold_rev Term.lambda) [fs, [u]] |
392 |
(Const (@{const_name The}, (B --> HOLogic.boolT) --> B) $ |
|
393 |
Term.lambda w (Library.foldr1 HOLogic.mk_disj (map3 mk_case_disj xctrs xfs xss))); |
|
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
394 |
|
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
395 |
val ((raw_case, (_, raw_case_def)), (lthy', lthy)) = no_defs_lthy |
54155 | 396 |
|> Local_Theory.define ((case_binding, NoSyn), |
397 |
((Binding.conceal (Thm.def_binding case_binding), []), case_rhs)) |
|
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
398 |
||> `Local_Theory.restore; |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
399 |
|
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
400 |
val phi = Proof_Context.export_morphism lthy lthy'; |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
401 |
|
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
402 |
val case_def = Morphism.thm phi raw_case_def; |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
403 |
|
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
404 |
val case0 = Morphism.term phi raw_case; |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
405 |
val casex = mk_case As B case0; |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
406 |
|
51759
587bba425430
eta-contracted weak congruence rules (like in the old package)
blanchet
parents:
51757
diff
changeset
|
407 |
val fcase = Term.list_comb (casex, fs); |
587bba425430
eta-contracted weak congruence rules (like in the old package)
blanchet
parents:
51757
diff
changeset
|
408 |
|
587bba425430
eta-contracted weak congruence rules (like in the old package)
blanchet
parents:
51757
diff
changeset
|
409 |
val ufcase = fcase $ u; |
587bba425430
eta-contracted weak congruence rules (like in the old package)
blanchet
parents:
51757
diff
changeset
|
410 |
val vfcase = fcase $ v; |
587bba425430
eta-contracted weak congruence rules (like in the old package)
blanchet
parents:
51757
diff
changeset
|
411 |
|
587bba425430
eta-contracted weak congruence rules (like in the old package)
blanchet
parents:
51757
diff
changeset
|
412 |
val eta_fcase = Term.list_comb (casex, eta_fs); |
587bba425430
eta-contracted weak congruence rules (like in the old package)
blanchet
parents:
51757
diff
changeset
|
413 |
val eta_gcase = Term.list_comb (casex, eta_gs); |
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
414 |
|
51759
587bba425430
eta-contracted weak congruence rules (like in the old package)
blanchet
parents:
51757
diff
changeset
|
415 |
val eta_ufcase = eta_fcase $ u; |
587bba425430
eta-contracted weak congruence rules (like in the old package)
blanchet
parents:
51757
diff
changeset
|
416 |
val eta_vgcase = eta_gcase $ v; |
49486 | 417 |
|
49591
91b228e26348
generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents:
49586
diff
changeset
|
418 |
fun mk_uu_eq () = HOLogic.mk_eq (u, u); |
49486 | 419 |
|
49591
91b228e26348
generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents:
49586
diff
changeset
|
420 |
val uv_eq = mk_Trueprop_eq (u, v); |
49486 | 421 |
|
422 |
val exist_xs_u_eq_ctrs = |
|
423 |
map2 (fn xctr => fn xs => list_exists_free xs (HOLogic.mk_eq (u, xctr))) xctrs xss; |
|
49022 | 424 |
|
51743 | 425 |
val unique_disc_no_def = TrueI; (*arbitrary marker*) |
426 |
val alternate_disc_no_def = FalseE; (*arbitrary marker*) |
|
427 |
||
49486 | 428 |
fun alternate_disc_lhs get_udisc 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
|
429 |
HOLogic.mk_not |
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
430 |
(let val b = nth disc_bindings (k - 1) in |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
431 |
if is_disc_binding_valid b then get_udisc b (k - 1) else nth exist_xs_u_eq_ctrs (k - 1) |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
432 |
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
|
433 |
|
53917 | 434 |
val (all_sels_distinct, discs, selss, disc_defs, sel_defs, sel_defss, lthy') = |
52969
f2df0730f8ac
clarified option name (since case/fold/rec are also destructors)
blanchet
parents:
52968
diff
changeset
|
435 |
if no_discs_sels then |
53917 | 436 |
(true, [], [], [], [], [], lthy) |
49278 | 437 |
else |
438 |
let |
|
53908 | 439 |
fun disc_free b = Free (Binding.name_of b, mk_pred1T fcT); |
49025 | 440 |
|
49486 | 441 |
fun disc_spec b exist_xs_u_eq_ctr = mk_Trueprop_eq (disc_free b $ u, exist_xs_u_eq_ctr); |
49278 | 442 |
|
49500 | 443 |
fun alternate_disc k = |
444 |
Term.lambda u (alternate_disc_lhs (K o rapp u o disc_free) (3 - k)); |
|
49278 | 445 |
|
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
446 |
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
|
447 |
map2 (fn Ts => fn k => |
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
448 |
(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
|
449 |
NONE => |
49364
838b5e8ede73
allow default values to refer to selector arguments -- this is useful, e.g. for tllist: ttl (TNil x) = TNil x (example by Andreas Lochbihler)
blanchet
parents:
49336
diff
changeset
|
450 |
(case AList.lookup Binding.eq_name (rev (nth sel_defaultss (k - 1))) b of |
838b5e8ede73
allow default values to refer to selector arguments -- this is useful, e.g. for tllist: ttl (TNil x) = TNil x (example by Andreas Lochbihler)
blanchet
parents:
49336
diff
changeset
|
451 |
NONE => fold_rev (Term.lambda o curry Free Name.uu) Ts (mk_undefined T) |
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
452 |
| SOME t => t |> Type.constraint (Ts ---> T) |> Syntax.check_term lthy) |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
453 |
| 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
|
454 |
|
49278 | 455 |
fun sel_spec b proto_sels = |
456 |
let |
|
457 |
val _ = |
|
458 |
(case duplicates (op =) (map fst proto_sels) of |
|
459 |
k :: _ => error ("Duplicate selector name " ^ quote (Binding.name_of b) ^ |
|
460 |
" for constructor " ^ |
|
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
461 |
quote (Syntax.string_of_term lthy (nth ctrs (k - 1)))) |
49278 | 462 |
| [] => ()) |
463 |
val T = |
|
464 |
(case distinct (op =) (map (fastype_of o snd o snd) proto_sels) of |
|
465 |
[T] => T |
|
466 |
| T :: T' :: _ => error ("Inconsistent range type for selector " ^ |
|
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
467 |
quote (Binding.name_of b) ^ ": " ^ quote (Syntax.string_of_typ lthy T) ^ " vs. " |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
468 |
^ quote (Syntax.string_of_typ lthy T'))); |
49278 | 469 |
in |
53908 | 470 |
mk_Trueprop_eq (Free (Binding.name_of b, fcT --> T) $ u, |
49536 | 471 |
Term.list_comb (mk_case As T case0, mk_sel_case_args b proto_sels T) $ u) |
49278 | 472 |
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
|
473 |
|
49336 | 474 |
val sel_bindings = flat sel_bindingss; |
475 |
val uniq_sel_bindings = distinct Binding.eq_name sel_bindings; |
|
476 |
val all_sels_distinct = (length uniq_sel_bindings = length sel_bindings); |
|
49285
036b833b99aa
removed wrong "transpose" and ensure "sel" theorems are put in the right order (grouped per selector, in the order in which the selectors appear)
blanchet
parents:
49281
diff
changeset
|
477 |
|
49336 | 478 |
val sel_binding_index = |
479 |
if all_sels_distinct then 1 upto length sel_bindings |
|
480 |
else map (fn b => find_index (curry Binding.eq_name b) uniq_sel_bindings) sel_bindings; |
|
49285
036b833b99aa
removed wrong "transpose" and ensure "sel" theorems are put in the right order (grouped per selector, in the order in which the selectors appear)
blanchet
parents:
49281
diff
changeset
|
481 |
|
036b833b99aa
removed wrong "transpose" and ensure "sel" theorems are put in the right order (grouped per selector, in the order in which the selectors appear)
blanchet
parents:
49281
diff
changeset
|
482 |
val proto_sels = flat (map3 (fn k => fn xs => map (fn x => (k, (xs, x)))) ks xss xss); |
49300 | 483 |
val sel_infos = |
49336 | 484 |
AList.group (op =) (sel_binding_index ~~ proto_sels) |
49285
036b833b99aa
removed wrong "transpose" and ensure "sel" theorems are put in the right order (grouped per selector, in the order in which the selectors appear)
blanchet
parents:
49281
diff
changeset
|
485 |
|> sort (int_ord o pairself fst) |
49336 | 486 |
|> map snd |> curry (op ~~) uniq_sel_bindings; |
487 |
val sel_bindings = map fst sel_infos; |
|
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
488 |
|
49336 | 489 |
fun unflat_selss xs = unflat_lookup Binding.eq_name sel_bindings xs sel_bindingss; |
49258
84f13469d7f0
allow same selector name for several constructors
blanchet
parents:
49257
diff
changeset
|
490 |
|
49278 | 491 |
val (((raw_discs, raw_disc_defs), (raw_sels, raw_sel_defs)), (lthy', lthy)) = |
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
492 |
lthy |
51809 | 493 |
|> apfst split_list o fold_map3 (fn k => fn exist_xs_u_eq_ctr => fn b => |
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
494 |
if Binding.is_empty b then |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
495 |
if n = 1 then pair (Term.lambda u (mk_uu_eq ()), unique_disc_no_def) |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
496 |
else pair (alternate_disc k, alternate_disc_no_def) |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
497 |
else if Binding.eq_name (b, equal_binding) then |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
498 |
pair (Term.lambda u exist_xs_u_eq_ctr, refl) |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
499 |
else |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
500 |
Specification.definition (SOME (b, NONE, NoSyn), |
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
501 |
((Thm.def_binding b, []), disc_spec b exist_xs_u_eq_ctr)) #>> apsnd snd) |
51809 | 502 |
ks exist_xs_u_eq_ctrs disc_bindings |
49278 | 503 |
||>> apfst split_list o fold_map (fn (b, proto_sels) => |
504 |
Specification.definition (SOME (b, NONE, NoSyn), |
|
49300 | 505 |
((Thm.def_binding b, []), sel_spec b proto_sels)) #>> apsnd snd) sel_infos |
49278 | 506 |
||> `Local_Theory.restore; |
49022 | 507 |
|
49278 | 508 |
val phi = Proof_Context.export_morphism lthy lthy'; |
49025 | 509 |
|
49278 | 510 |
val disc_defs = map (Morphism.thm phi) raw_disc_defs; |
49281 | 511 |
val sel_defs = map (Morphism.thm phi) raw_sel_defs; |
512 |
val sel_defss = unflat_selss sel_defs; |
|
49278 | 513 |
|
514 |
val discs0 = map (Morphism.term phi) raw_discs; |
|
515 |
val selss0 = unflat_selss (map (Morphism.term phi) raw_sels); |
|
49028 | 516 |
|
49278 | 517 |
val discs = map (mk_disc_or_sel As) discs0; |
518 |
val selss = map (map (mk_disc_or_sel As)) selss0; |
|
519 |
in |
|
53917 | 520 |
(all_sels_distinct, discs, selss, disc_defs, sel_defs, sel_defss, lthy') |
49278 | 521 |
end; |
49025 | 522 |
|
49032 | 523 |
fun mk_imp_p Qs = Logic.list_implies (Qs, HOLogic.mk_Trueprop p); |
49029 | 524 |
|
49458 | 525 |
val exhaust_goal = |
49486 | 526 |
let fun mk_prem xctr xs = fold_rev Logic.all xs (mk_imp_p [mk_Trueprop_eq (u, xctr)]) in |
527 |
fold_rev Logic.all [p, u] (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
|
528 |
end; |
49019 | 529 |
|
49484 | 530 |
val inject_goalss = |
49017 | 531 |
let |
49034
b77e1910af8a
make parallel list indexing possible for inject theorems
blanchet
parents:
49033
diff
changeset
|
532 |
fun mk_goal _ _ [] [] = [] |
49025 | 533 |
| mk_goal xctr yctr xs ys = |
49121 | 534 |
[fold_rev Logic.all (xs @ ys) (mk_Trueprop_eq (HOLogic.mk_eq (xctr, yctr), |
535 |
Library.foldr1 HOLogic.mk_conj (map2 (curry HOLogic.mk_eq) xs ys)))]; |
|
49017 | 536 |
in |
49034
b77e1910af8a
make parallel list indexing possible for inject theorems
blanchet
parents:
49033
diff
changeset
|
537 |
map4 mk_goal xctrs yctrs xss yss |
49017 | 538 |
end; |
539 |
||
49484 | 540 |
val half_distinct_goalss = |
49121 | 541 |
let |
49203 | 542 |
fun mk_goal ((xs, xc), (xs', xc')) = |
49121 | 543 |
fold_rev Logic.all (xs @ xs') |
49203 | 544 |
(HOLogic.mk_Trueprop (HOLogic.mk_not (HOLogic.mk_eq (xc, xc')))); |
49121 | 545 |
in |
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
546 |
map (map mk_goal) (mk_half_pairss (`I (xss ~~ xctrs))) |
49121 | 547 |
end; |
49019 | 548 |
|
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
549 |
val goalss = [exhaust_goal] :: inject_goalss @ half_distinct_goalss; |
49019 | 550 |
|
551 |
fun after_qed thmss lthy = |
|
552 |
let |
|
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
553 |
val ([exhaust_thm], (inject_thmss, half_distinct_thmss)) = (hd thmss, chop n (tl thmss)); |
49019 | 554 |
|
49438 | 555 |
val inject_thms = flat inject_thmss; |
556 |
||
53210
7219c61796c0
simplify code (now that ctr_sugar uses the same type variables as fp_sugar)
blanchet
parents:
53040
diff
changeset
|
557 |
val rho_As = map (pairself (certifyT lthy)) (map Logic.varifyT_global As ~~ As); |
49486 | 558 |
|
559 |
fun inst_thm t thm = |
|
560 |
Drule.instantiate' [] [SOME (certify lthy t)] |
|
53210
7219c61796c0
simplify code (now that ctr_sugar uses the same type variables as fp_sugar)
blanchet
parents:
53040
diff
changeset
|
561 |
(Thm.instantiate (rho_As, []) (Drule.zero_var_indexes thm)); |
49486 | 562 |
|
563 |
val uexhaust_thm = inst_thm u exhaust_thm; |
|
49032 | 564 |
|
49300 | 565 |
val exhaust_cases = map base_name_of_ctr ctrs; |
566 |
||
49048
4e0f0f98be02
rationalized data structure for distinctness theorems
blanchet
parents:
49046
diff
changeset
|
567 |
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
|
568 |
|
49486 | 569 |
val (distinct_thms, (distinct_thmsss', distinct_thmsss)) = |
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
570 |
join_halves n half_distinct_thmss other_half_distinct_thmss ||> `transpose; |
49019 | 571 |
|
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
572 |
val nchotomy_thm = |
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
573 |
let |
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
574 |
val goal = |
49486 | 575 |
HOLogic.mk_Trueprop (HOLogic.mk_all (fst u', snd u', |
576 |
Library.foldr1 HOLogic.mk_disj exist_xs_u_eq_ctrs)); |
|
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
577 |
in |
51551 | 578 |
Goal.prove_sorry lthy [] [] goal (fn _ => mk_nchotomy_tac n exhaust_thm) |
49667
44d85dc8ca08
use Thm.close_derivation in theorems proved using Skip_Proof.prove; tuned signature;
traytel
parents:
49633
diff
changeset
|
579 |
|> Thm.close_derivation |
49020
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
580 |
end; |
f379cf5d71bd
more work on BNF sugar -- up to derivation of nchotomy
blanchet
parents:
49019
diff
changeset
|
581 |
|
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
582 |
val case_thms = |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
583 |
let |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
584 |
val goals = |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
585 |
map3 (fn xctr => fn xf => fn xs => |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
586 |
fold_rev Logic.all (fs @ xs) (mk_Trueprop_eq (fcase $ xctr, xf))) xctrs xfs xss; |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
587 |
in |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
588 |
map4 (fn k => fn goal => fn injects => fn distinctss => |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
589 |
Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} => |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
590 |
mk_case_tac ctxt n k case_def injects distinctss) |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
591 |
|> Thm.close_derivation) |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
592 |
ks goals inject_thmss distinct_thmsss |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
593 |
end; |
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
594 |
|
53917 | 595 |
val (case_cong_thm, weak_case_cong_thm) = |
596 |
let |
|
597 |
fun mk_prem xctr xs xf xg = |
|
598 |
fold_rev Logic.all xs (Logic.mk_implies (mk_Trueprop_eq (v, xctr), |
|
599 |
mk_Trueprop_eq (xf, xg))); |
|
600 |
||
601 |
val goal = |
|
602 |
Logic.list_implies (uv_eq :: map4 mk_prem xctrs xss xfs xgs, |
|
603 |
mk_Trueprop_eq (eta_ufcase, eta_vgcase)); |
|
604 |
val weak_goal = Logic.mk_implies (uv_eq, mk_Trueprop_eq (ufcase, vfcase)); |
|
605 |
in |
|
606 |
(Goal.prove_sorry lthy [] [] goal (fn _ => mk_case_cong_tac lthy uexhaust_thm case_thms), |
|
607 |
Goal.prove_sorry lthy [] [] weak_goal (K (etac arg_cong 1))) |
|
608 |
|> pairself (Thm.close_derivation #> singleton (Proof_Context.export names_lthy lthy)) |
|
609 |
end; |
|
610 |
||
611 |
val split_lhs = q $ ufcase; |
|
612 |
||
613 |
fun mk_split_conjunct xctr xs f_xs = |
|
614 |
list_all_free xs (HOLogic.mk_imp (HOLogic.mk_eq (u, xctr), q $ f_xs)); |
|
615 |
fun mk_split_disjunct xctr xs f_xs = |
|
616 |
list_exists_free xs (HOLogic.mk_conj (HOLogic.mk_eq (u, xctr), |
|
617 |
HOLogic.mk_not (q $ f_xs))); |
|
618 |
||
619 |
fun mk_split_goal xctrs xss xfs = |
|
620 |
mk_Trueprop_eq (split_lhs, Library.foldr1 HOLogic.mk_conj |
|
621 |
(map3 mk_split_conjunct xctrs xss xfs)); |
|
622 |
fun mk_split_asm_goal xctrs xss xfs = |
|
623 |
mk_Trueprop_eq (split_lhs, HOLogic.mk_not (Library.foldr1 HOLogic.mk_disj |
|
624 |
(map3 mk_split_disjunct xctrs xss xfs))); |
|
625 |
||
626 |
fun prove_split selss goal = |
|
627 |
Goal.prove_sorry lthy [] [] goal (fn _ => |
|
628 |
mk_split_tac lthy uexhaust_thm case_thms selss inject_thmss distinct_thmsss) |
|
629 |
|> Thm.close_derivation |
|
630 |
|> singleton (Proof_Context.export names_lthy lthy); |
|
631 |
||
632 |
fun prove_split_asm asm_goal split_thm = |
|
633 |
Goal.prove_sorry lthy [] [] asm_goal (fn {context = ctxt, ...} => |
|
634 |
mk_split_asm_tac ctxt split_thm) |
|
635 |
|> Thm.close_derivation |
|
636 |
|> singleton (Proof_Context.export names_lthy lthy); |
|
637 |
||
638 |
val (split_thm, split_asm_thm) = |
|
639 |
let |
|
640 |
val goal = mk_split_goal xctrs xss xfs; |
|
641 |
val asm_goal = mk_split_asm_goal xctrs xss xfs; |
|
642 |
||
643 |
val thm = prove_split (replicate n []) goal; |
|
644 |
val asm_thm = prove_split_asm asm_goal thm; |
|
645 |
in |
|
646 |
(thm, asm_thm) |
|
647 |
end; |
|
648 |
||
53704 | 649 |
val (all_sel_thms, sel_thmss, disc_thmss, nontriv_disc_thms, discI_thms, nontriv_discI_thms, |
53916 | 650 |
disc_exclude_thms, disc_exhaust_thms, sel_exhaust_thms, all_collapse_thms, |
53917 | 651 |
safe_collapse_thms, expand_thms, sel_split_thms, sel_split_asm_thms, |
652 |
case_conv_if_thms) = |
|
52969
f2df0730f8ac
clarified option name (since case/fold/rec are also destructors)
blanchet
parents:
52968
diff
changeset
|
653 |
if no_discs_sels then |
53917 | 654 |
([], [], [], [], [], [], [], [], [], [], [], [], [], [], []) |
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
|
655 |
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
|
656 |
let |
53917 | 657 |
val udiscs = map (rapp u) discs; |
658 |
val uselss = map (map (rapp u)) selss; |
|
659 |
val usel_ctrs = map2 (curry Term.list_comb) ctrs uselss; |
|
660 |
val usel_fs = map2 (curry Term.list_comb) fs uselss; |
|
661 |
||
662 |
val vdiscs = map (rapp v) discs; |
|
663 |
val vselss = map (map (rapp v)) selss; |
|
664 |
||
49364
838b5e8ede73
allow default values to refer to selector arguments -- this is useful, e.g. for tllist: ttl (TNil x) = TNil x (example by Andreas Lochbihler)
blanchet
parents:
49336
diff
changeset
|
665 |
fun make_sel_thm xs' case_thm sel_def = |
838b5e8ede73
allow default values to refer to selector arguments -- this is useful, e.g. for tllist: ttl (TNil x) = TNil x (example by Andreas Lochbihler)
blanchet
parents:
49336
diff
changeset
|
666 |
zero_var_indexes (Drule.gen_all (Drule.rename_bvars' (map (SOME o fst) xs') |
838b5e8ede73
allow default values to refer to selector arguments -- this is useful, e.g. for tllist: ttl (TNil x) = TNil x (example by Andreas Lochbihler)
blanchet
parents:
49336
diff
changeset
|
667 |
(Drule.forall_intr_vars (case_thm RS (sel_def RS trans))))); |
49281 | 668 |
|
53704 | 669 |
val sel_thmss = map3 (map oo make_sel_thm) xss' case_thms sel_defss; |
670 |
||
49281 | 671 |
fun has_undefined_rhs thm = |
672 |
(case snd (HOLogic.dest_eq (HOLogic.dest_Trueprop (prop_of thm))) of |
|
673 |
Const (@{const_name undefined}, _) => true |
|
674 |
| _ => false); |
|
675 |
||
676 |
val all_sel_thms = |
|
49285
036b833b99aa
removed wrong "transpose" and ensure "sel" theorems are put in the right order (grouped per selector, in the order in which the selectors appear)
blanchet
parents:
49281
diff
changeset
|
677 |
(if all_sels_distinct andalso forall null sel_defaultss then |
036b833b99aa
removed wrong "transpose" and ensure "sel" theorems are put in the right order (grouped per selector, in the order in which the selectors appear)
blanchet
parents:
49281
diff
changeset
|
678 |
flat sel_thmss |
036b833b99aa
removed wrong "transpose" and ensure "sel" theorems are put in the right order (grouped per selector, in the order in which the selectors appear)
blanchet
parents:
49281
diff
changeset
|
679 |
else |
49364
838b5e8ede73
allow default values to refer to selector arguments -- this is useful, e.g. for tllist: ttl (TNil x) = TNil x (example by Andreas Lochbihler)
blanchet
parents:
49336
diff
changeset
|
680 |
map_product (fn s => fn (xs', c) => make_sel_thm xs' c s) sel_defs |
838b5e8ede73
allow default values to refer to selector arguments -- this is useful, e.g. for tllist: ttl (TNil x) = TNil x (example by Andreas Lochbihler)
blanchet
parents:
49336
diff
changeset
|
681 |
(xss' ~~ case_thms)) |
49285
036b833b99aa
removed wrong "transpose" and ensure "sel" theorems are put in the right order (grouped per selector, in the order in which the selectors appear)
blanchet
parents:
49281
diff
changeset
|
682 |
|> filter_out has_undefined_rhs; |
49278 | 683 |
|
684 |
fun mk_unique_disc_def () = |
|
685 |
let |
|
686 |
val m = the_single ms; |
|
49591
91b228e26348
generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents:
49586
diff
changeset
|
687 |
val goal = mk_Trueprop_eq (mk_uu_eq (), the_single exist_xs_u_eq_ctrs); |
49278 | 688 |
in |
51551 | 689 |
Goal.prove_sorry lthy [] [] goal (fn _ => mk_unique_disc_def_tac m uexhaust_thm) |
49692 | 690 |
|> Thm.close_derivation |
49278 | 691 |
|> singleton (Proof_Context.export names_lthy lthy) |
692 |
end; |
|
693 |
||
694 |
fun mk_alternate_disc_def k = |
|
695 |
let |
|
696 |
val goal = |
|
49486 | 697 |
mk_Trueprop_eq (alternate_disc_lhs (K (nth udiscs)) (3 - k), |
698 |
nth exist_xs_u_eq_ctrs (k - 1)); |
|
49278 | 699 |
in |
51551 | 700 |
Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} => |
49278 | 701 |
mk_alternate_disc_def_tac ctxt k (nth disc_defs (2 - k)) |
49486 | 702 |
(nth distinct_thms (2 - k)) uexhaust_thm) |
49692 | 703 |
|> Thm.close_derivation |
49278 | 704 |
|> singleton (Proof_Context.export names_lthy lthy) |
705 |
end; |
|
49028 | 706 |
|
49278 | 707 |
val has_alternate_disc_def = |
708 |
exists (fn def => Thm.eq_thm_prop (def, alternate_disc_no_def)) disc_defs; |
|
709 |
||
710 |
val disc_defs' = |
|
711 |
map2 (fn k => fn def => |
|
712 |
if Thm.eq_thm_prop (def, unique_disc_no_def) then mk_unique_disc_def () |
|
713 |
else if Thm.eq_thm_prop (def, alternate_disc_no_def) then mk_alternate_disc_def k |
|
714 |
else def) ks disc_defs; |
|
715 |
||
716 |
val discD_thms = map (fn def => def RS iffD1) disc_defs'; |
|
717 |
val discI_thms = |
|
718 |
map2 (fn m => fn def => funpow m (fn thm => exI RS thm) (def RS iffD2)) ms |
|
719 |
disc_defs'; |
|
720 |
val not_discI_thms = |
|
721 |
map2 (fn m => fn def => funpow m (fn thm => allI RS thm) |
|
49504
df9b897fb254
renamed "iter"/"coiter" to "fold"/"unfold" (cf. Wadler)
blanchet
parents:
49500
diff
changeset
|
722 |
(unfold_thms lthy @{thms not_ex} (def RS @{thm ssubst[of _ _ Not]}))) |
49278 | 723 |
ms disc_defs'; |
724 |
||
725 |
val (disc_thmss', disc_thmss) = |
|
726 |
let |
|
727 |
fun mk_thm discI _ [] = refl RS discI |
|
728 |
| mk_thm _ not_discI [distinct] = distinct RS not_discI; |
|
729 |
fun mk_thms discI not_discI distinctss = map (mk_thm discI not_discI) distinctss; |
|
730 |
in |
|
731 |
map3 mk_thms discI_thms not_discI_thms distinct_thmsss' |> `transpose |
|
732 |
end; |
|
733 |
||
53704 | 734 |
val nontriv_disc_thms = |
735 |
flat (map2 (fn b => if is_disc_binding_valid b then I else K []) |
|
736 |
disc_bindings disc_thmss); |
|
737 |
||
738 |
fun is_discI_boring b = |
|
739 |
(n = 1 andalso Binding.is_empty b) orelse Binding.eq_name (b, equal_binding); |
|
740 |
||
741 |
val nontriv_discI_thms = |
|
742 |
flat (map2 (fn b => if is_discI_boring b then K [] else single) disc_bindings |
|
743 |
discI_thms); |
|
49028 | 744 |
|
49486 | 745 |
val (disc_exclude_thms, (disc_exclude_thmsss', disc_exclude_thmsss)) = |
746 |
let |
|
747 |
fun mk_goal [] = [] |
|
748 |
| mk_goal [((_, udisc), (_, udisc'))] = |
|
749 |
[Logic.all u (Logic.mk_implies (HOLogic.mk_Trueprop udisc, |
|
750 |
HOLogic.mk_Trueprop (HOLogic.mk_not udisc')))]; |
|
751 |
||
49667
44d85dc8ca08
use Thm.close_derivation in theorems proved using Skip_Proof.prove; tuned signature;
traytel
parents:
49633
diff
changeset
|
752 |
fun prove tac goal = |
51551 | 753 |
Goal.prove_sorry lthy [] [] goal (K tac) |
49667
44d85dc8ca08
use Thm.close_derivation in theorems proved using Skip_Proof.prove; tuned signature;
traytel
parents:
49633
diff
changeset
|
754 |
|> Thm.close_derivation; |
49486 | 755 |
|
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
756 |
val half_pairss = mk_half_pairss (`I (ms ~~ discD_thms ~~ udiscs)); |
49486 | 757 |
|
758 |
val half_goalss = map mk_goal half_pairss; |
|
759 |
val half_thmss = |
|
760 |
map3 (fn [] => K (K []) | [goal] => fn [(((m, discD), _), _)] => |
|
51798 | 761 |
fn disc_thm => [prove (mk_half_disc_exclude_tac lthy m discD disc_thm) goal]) |
49486 | 762 |
half_goalss half_pairss (flat disc_thmss'); |
49278 | 763 |
|
49486 | 764 |
val other_half_goalss = map (mk_goal o map swap) half_pairss; |
765 |
val other_half_thmss = |
|
766 |
map2 (map2 (prove o mk_other_half_disc_exclude_tac)) half_thmss |
|
767 |
other_half_goalss; |
|
768 |
in |
|
49585
5c4a12550491
generate high-level "maps", "sets", and "rels" properties
blanchet
parents:
49536
diff
changeset
|
769 |
join_halves n half_thmss other_half_thmss ||> `transpose |
49486 | 770 |
|>> has_alternate_disc_def ? K [] |
771 |
end; |
|
49278 | 772 |
|
49486 | 773 |
val disc_exhaust_thm = |
774 |
let |
|
775 |
fun mk_prem udisc = mk_imp_p [HOLogic.mk_Trueprop udisc]; |
|
776 |
val goal = fold_rev Logic.all [p, u] (mk_imp_p (map mk_prem udiscs)); |
|
777 |
in |
|
51551 | 778 |
Goal.prove_sorry lthy [] [] goal (fn _ => |
49486 | 779 |
mk_disc_exhaust_tac n exhaust_thm discI_thms) |
49667
44d85dc8ca08
use Thm.close_derivation in theorems proved using Skip_Proof.prove; tuned signature;
traytel
parents:
49633
diff
changeset
|
780 |
|> Thm.close_derivation |
49486 | 781 |
end; |
49028 | 782 |
|
53740 | 783 |
val (safe_collapse_thms, all_collapse_thms) = |
49486 | 784 |
let |
54008
b15cfc2864de
refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
54007
diff
changeset
|
785 |
fun mk_goal m udisc usel_ctr = |
49486 | 786 |
let |
787 |
val prem = HOLogic.mk_Trueprop udisc; |
|
53916 | 788 |
val concl = mk_Trueprop_eq ((usel_ctr, u) |> m = 0 ? swap); |
49486 | 789 |
in |
53740 | 790 |
(prem aconv concl, Logic.all u (Logic.mk_implies (prem, concl))) |
49486 | 791 |
end; |
54008
b15cfc2864de
refactoring -- splitting between constructor sugar dependencies and true BNF dependencies
blanchet
parents:
54007
diff
changeset
|
792 |
val (trivs, goals) = map3 mk_goal ms udiscs usel_ctrs |> split_list; |
53740 | 793 |
val thms = |
794 |
map5 (fn m => fn discD => fn sel_thms => fn triv => fn goal => |
|
795 |
Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} => |
|
796 |
mk_collapse_tac ctxt m discD sel_thms ORELSE HEADGOAL atac) |
|
797 |
|> Thm.close_derivation |
|
798 |
|> not triv ? perhaps (try (fn thm => refl RS thm))) |
|
799 |
ms discD_thms sel_thmss trivs goals; |
|
49486 | 800 |
in |
53740 | 801 |
(map_filter (fn (true, _) => NONE | (false, thm) => SOME thm) (trivs ~~ thms), |
802 |
thms) |
|
49486 | 803 |
end; |
49025 | 804 |
|
53916 | 805 |
val swapped_all_collapse_thms = |
806 |
map2 (fn m => fn thm => if m = 0 then thm else thm RS sym) ms all_collapse_thms; |
|
807 |
||
808 |
val sel_exhaust_thm = |
|
809 |
let |
|
810 |
fun mk_prem usel_ctr = mk_imp_p [mk_Trueprop_eq (u, usel_ctr)]; |
|
811 |
val goal = fold_rev Logic.all [p, u] (mk_imp_p (map mk_prem usel_ctrs)); |
|
812 |
in |
|
813 |
Goal.prove_sorry lthy [] [] goal (fn _ => |
|
814 |
mk_sel_exhaust_tac n disc_exhaust_thm swapped_all_collapse_thms) |
|
815 |
|> Thm.close_derivation |
|
816 |
end; |
|
817 |
||
53919 | 818 |
val expand_thm = |
49486 | 819 |
let |
820 |
fun mk_prems k udisc usels vdisc vsels = |
|
821 |
(if k = n then [] else [mk_Trueprop_eq (udisc, vdisc)]) @ |
|
822 |
(if null usels then |
|
823 |
[] |
|
824 |
else |
|
825 |
[Logic.list_implies |
|
826 |
(if n = 1 then [] else map HOLogic.mk_Trueprop [udisc, vdisc], |
|
827 |
HOLogic.mk_Trueprop (Library.foldr1 HOLogic.mk_conj |
|
828 |
(map2 (curry HOLogic.mk_eq) usels vsels)))]); |
|
829 |
||
49591
91b228e26348
generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents:
49586
diff
changeset
|
830 |
val goal = |
91b228e26348
generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents:
49586
diff
changeset
|
831 |
Library.foldr Logic.list_implies |
91b228e26348
generate high-level "coinduct" and "strong_coinduct" properties
blanchet
parents:
49586
diff
changeset
|
832 |
(map5 mk_prems ks udiscs uselss vdiscs vselss, uv_eq); |
49486 | 833 |
val uncollapse_thms = |
53740 | 834 |
map2 (fn thm => fn [] => thm | _ => thm RS sym) all_collapse_thms uselss; |
49486 | 835 |
in |
53919 | 836 |
Goal.prove_sorry lthy [] [] goal (fn _ => |
837 |
mk_expand_tac lthy n ms (inst_thm u disc_exhaust_thm) |
|
838 |
(inst_thm v disc_exhaust_thm) uncollapse_thms disc_exclude_thmsss |
|
839 |
disc_exclude_thmsss') |
|
840 |
|> Thm.close_derivation |
|
841 |
|> singleton (Proof_Context.export names_lthy lthy) |
|
49486 | 842 |
end; |
49278 | 843 |
|
53917 | 844 |
val (sel_split_thm, sel_split_asm_thm) = |
845 |
let |
|
846 |
val zss = map (K []) xss; |
|
847 |
val goal = mk_split_goal usel_ctrs zss usel_fs; |
|
848 |
val asm_goal = mk_split_asm_goal usel_ctrs zss usel_fs; |
|
849 |
||
850 |
val thm = prove_split sel_thmss goal; |
|
851 |
val asm_thm = prove_split_asm asm_goal thm; |
|
852 |
in |
|
853 |
(thm, asm_thm) |
|
854 |
end; |
|
855 |
||
53919 | 856 |
val case_conv_if_thm = |
49486 | 857 |
let |
53917 | 858 |
val goal = mk_Trueprop_eq (ufcase, mk_IfN B udiscs usel_fs); |
49486 | 859 |
in |
53919 | 860 |
Goal.prove_sorry lthy [] [] goal (fn {context = ctxt, ...} => |
861 |
mk_case_conv_if_tac ctxt n uexhaust_thm case_thms disc_thmss' sel_thmss) |
|
862 |
|> Thm.close_derivation |
|
863 |
|> singleton (Proof_Context.export names_lthy lthy) |
|
49486 | 864 |
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
|
865 |
in |
53704 | 866 |
(all_sel_thms, sel_thmss, disc_thmss, nontriv_disc_thms, discI_thms, |
53916 | 867 |
nontriv_discI_thms, disc_exclude_thms, [disc_exhaust_thm], [sel_exhaust_thm], |
53919 | 868 |
all_collapse_thms, safe_collapse_thms, [expand_thm], [sel_split_thm], |
869 |
[sel_split_asm_thm], [case_conv_if_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
|
870 |
end; |
49025 | 871 |
|
49437 | 872 |
val exhaust_case_names_attr = Attrib.internal (K (Rule_Cases.case_names exhaust_cases)); |
53908 | 873 |
val cases_type_attr = Attrib.internal (K (Induct.cases_type fcT_name)); |
49300 | 874 |
|
54151 | 875 |
val anonymous_notes = |
876 |
[(map (fn th => th RS notE) distinct_thms, safe_elim_attrs), |
|
877 |
(map (fn th => th RS @{thm eq_False[THEN iffD2]} |
|
878 |
handle THM _ => th RS @{thm eq_True[THEN iffD2]}) nontriv_disc_thms, |
|
879 |
code_nitpicksimp_attrs)] |
|
880 |
|> map (fn (thms, attrs) => ((Binding.empty, attrs), [(thms, [])])); |
|
881 |
||
49052 | 882 |
val notes = |
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54008
diff
changeset
|
883 |
[(caseN, case_thms, code_nitpicksimp_simp_attrs), |
49594
55e798614c45
tweaked theorem names (in particular, dropped s's)
blanchet
parents:
49591
diff
changeset
|
884 |
(case_congN, [case_cong_thm], []), |
53857 | 885 |
(case_conv_ifN, case_conv_if_thms, []), |
53740 | 886 |
(collapseN, safe_collapse_thms, simp_attrs), |
53704 | 887 |
(discN, nontriv_disc_thms, simp_attrs), |
53700 | 888 |
(discIN, nontriv_discI_thms, []), |
53836 | 889 |
(disc_excludeN, disc_exclude_thms, dest_attrs), |
49300 | 890 |
(disc_exhaustN, disc_exhaust_thms, [exhaust_case_names_attr]), |
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54008
diff
changeset
|
891 |
(distinctN, distinct_thms, simp_attrs @ inductsimp_attrs), |
49300 | 892 |
(exhaustN, [exhaust_thm], [exhaust_case_names_attr, cases_type_attr]), |
49486 | 893 |
(expandN, expand_thms, []), |
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54008
diff
changeset
|
894 |
(injectN, inject_thms, iff_attrs @ inductsimp_attrs), |
49300 | 895 |
(nchotomyN, [nchotomy_thm], []), |
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54008
diff
changeset
|
896 |
(selN, all_sel_thms, code_nitpicksimp_simp_attrs), |
53916 | 897 |
(sel_exhaustN, sel_exhaust_thms, [exhaust_case_names_attr]), |
53917 | 898 |
(sel_splitN, sel_split_thms, []), |
899 |
(sel_split_asmN, sel_split_asm_thms, []), |
|
49300 | 900 |
(splitN, [split_thm], []), |
901 |
(split_asmN, [split_asm_thm], []), |
|
49633 | 902 |
(splitsN, [split_thm, split_asm_thm], []), |
49300 | 903 |
(weak_case_cong_thmsN, [weak_case_cong_thm], cong_attrs)] |
904 |
|> filter_out (null o #2) |
|
905 |
|> map (fn (thmN, thms, attrs) => |
|
49633 | 906 |
((qualify true (Binding.name thmN), attrs), [(thms, [])])); |
49300 | 907 |
|
53867
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
908 |
val ctr_sugar = |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
909 |
{ctrs = ctrs, casex = casex, discs = discs, selss = selss, exhaust = exhaust_thm, |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
910 |
nchotomy = nchotomy_thm, injects = inject_thms, distincts = distinct_thms, |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
911 |
case_thms = case_thms, case_cong = case_cong_thm, weak_case_cong = weak_case_cong_thm, |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
912 |
split = split_thm, split_asm = split_asm_thm, disc_thmss = disc_thmss, |
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
913 |
discIs = discI_thms, sel_thmss = sel_thmss, disc_exhausts = disc_exhaust_thms, |
53916 | 914 |
sel_exhausts = sel_exhaust_thms, collapses = all_collapse_thms, expands = expand_thms, |
53917 | 915 |
sel_splits = sel_split_thms, sel_split_asms = sel_split_asm_thms, |
53916 | 916 |
case_conv_ifs = case_conv_if_thms}; |
49019 | 917 |
in |
53867
8ad44ecc0d15
keep a database of free constructor type information
blanchet
parents:
53864
diff
changeset
|
918 |
(ctr_sugar, |
51819 | 919 |
lthy |
920 |
|> not rep_compat ? |
|
54285
578371ba74cc
reverted 3e1d230f1c00 -- pervasiveness is useful, cf. Coinductive_Nat in the AFP
blanchet
parents:
54265
diff
changeset
|
921 |
(Local_Theory.declaration {syntax = false, pervasive = true} |
51697
1ce319118d59
do not add case translation syntax in rep_datatype compatibility mode
traytel
parents:
51696
diff
changeset
|
922 |
(fn phi => Case_Translation.register |
51819 | 923 |
(Morphism.term phi casex) (map (Morphism.term phi) ctrs))) |
54145
297d1c603999
make sure that registered code equations are actually equations
blanchet
parents:
54008
diff
changeset
|
924 |
|> Local_Theory.notes (anonymous_notes @ notes) |> snd |
53908 | 925 |
|> register_ctr_sugar fcT_name ctr_sugar) |
49019 | 926 |
end; |
49017 | 927 |
in |
49121 | 928 |
(goalss, after_qed, lthy') |
49017 | 929 |
end; |
930 |
||
51781 | 931 |
fun wrap_free_constructors tacss = (fn (goalss, after_qed, lthy) => |
51551 | 932 |
map2 (map2 (Thm.close_derivation oo Goal.prove_sorry lthy [] [])) goalss tacss |
51781 | 933 |
|> (fn thms => after_qed thms lthy)) oo prepare_wrap_free_constructors (K I); |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
934 |
|
51781 | 935 |
val wrap_free_constructors_cmd = (fn (goalss, after_qed, lthy) => |
49297 | 936 |
Proof.theorem NONE (snd oo after_qed) (map (map (rpair [])) goalss) lthy) oo |
51781 | 937 |
prepare_wrap_free_constructors Syntax.read_term; |
49297 | 938 |
|
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
939 |
fun parse_bracket_list parser = @{keyword "["} |-- Parse.list parser --| @{keyword "]"}; |
49111 | 940 |
|
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
941 |
val parse_bindings = parse_bracket_list parse_binding; |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
942 |
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
|
943 |
|
51790
22517d04d20b
more intuitive syntax for equality-style discriminators of nullary constructors
blanchet
parents:
51787
diff
changeset
|
944 |
val parse_bound_term = (parse_binding --| @{keyword ":"}) -- Parse.term; |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
945 |
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
|
946 |
val parse_bound_termss = parse_bracket_list parse_bound_terms; |
49017 | 947 |
|
52823 | 948 |
val parse_wrap_free_constructors_options = |
52969
f2df0730f8ac
clarified option name (since case/fold/rec are also destructors)
blanchet
parents:
52968
diff
changeset
|
949 |
Scan.optional (@{keyword "("} |-- Parse.list1 ((@{keyword "no_discs_sels"} >> K (true, false)) || |
49633 | 950 |
(@{keyword "rep_compat"} >> K (false, true))) --| @{keyword ")"} |
951 |
>> (pairself (exists I) o split_list)) (false, false); |
|
49278 | 952 |
|
49017 | 953 |
val _ = |
51781 | 954 |
Outer_Syntax.local_theory_to_proof @{command_spec "wrap_free_constructors"} |
51797 | 955 |
"wrap an existing freely generated type's constructors" |
52823 | 956 |
((parse_wrap_free_constructors_options -- (@{keyword "["} |-- Parse.list Parse.term --| |
957 |
@{keyword "]"}) -- |
|
52968
2b430bbb5a1a
define case constant from other 'free constructor' axioms
blanchet
parents:
52965
diff
changeset
|
958 |
parse_binding -- Scan.optional (parse_bindings -- Scan.optional (parse_bindingss -- |
49280
52413dc96326
allow default values for selectors in low-level "wrap_data" command
blanchet
parents:
49278
diff
changeset
|
959 |
Scan.optional parse_bound_termss []) ([], [])) ([], ([], []))) |
51781 | 960 |
>> wrap_free_constructors_cmd); |
49017 | 961 |
|
962 |
end; |