author | wenzelm |
Sat, 14 Jan 2012 17:45:04 +0100 | |
changeset 46215 | 0da9433f959e |
parent 45896 | 100fb1f33e3e |
child 46218 | ecf6375e2abb |
permissions | -rw-r--r-- |
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
1 |
(* Title: HOL/Tools/Datatype/datatype_prop.ML |
11539 | 2 |
Author: Stefan Berghofer, TU Muenchen |
5177 | 3 |
|
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
4 |
Datatype package: characteristic properties of datatypes. |
5177 | 5 |
*) |
6 |
||
7 |
signature DATATYPE_PROP = |
|
8 |
sig |
|
45896 | 9 |
type descr = Datatype_Aux.descr |
8434 | 10 |
val indexify_names: string list -> string list |
13465 | 11 |
val make_tnames: typ list -> string list |
45822 | 12 |
val make_injs : descr list -> term list list |
45889 | 13 |
val make_distincts : descr list -> term list list (*no symmetric inequalities*) |
45822 | 14 |
val make_ind : descr list -> term |
15 |
val make_casedists : descr list -> term list |
|
16 |
val make_primrec_Ts : descr list -> string list -> typ list * typ list |
|
17 |
val make_primrecs : string list -> descr list -> theory -> term list |
|
18 |
val make_cases : string list -> descr list -> theory -> term list list |
|
19 |
val make_splits : string list -> descr list -> theory -> (term * term) list |
|
20 |
val make_case_combs : string list -> descr list -> theory -> string -> term list |
|
21 |
val make_weak_case_congs : string list -> descr list -> theory -> term list |
|
22 |
val make_case_congs : string list -> descr list -> theory -> term list |
|
23 |
val make_nchotomys : descr list -> term list |
|
5177 | 24 |
end; |
25 |
||
33968
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
haftmann
parents:
33957
diff
changeset
|
26 |
structure Datatype_Prop : DATATYPE_PROP = |
5177 | 27 |
struct |
28 |
||
45896 | 29 |
type descr = Datatype_Aux.descr; |
30 |
||
31 |
||
8434 | 32 |
fun indexify_names names = |
33 |
let |
|
34 |
fun index (x :: xs) tab = |
|
45739 | 35 |
(case AList.lookup (op =) tab x of |
36 |
NONE => |
|
37 |
if member (op =) xs x |
|
38 |
then (x ^ "1") :: index xs ((x, 2) :: tab) |
|
39 |
else x :: index xs tab |
|
40 |
| SOME i => (x ^ string_of_int i) :: index xs ((x, i + 1) :: tab)) |
|
41 |
| index [] _ = []; |
|
8434 | 42 |
in index names [] end; |
43 |
||
5177 | 44 |
fun make_tnames Ts = |
45 |
let |
|
40720 | 46 |
fun type_name (TFree (name, _)) = unprefix "'" name |
47 |
| type_name (Type (name, _)) = |
|
30364
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents:
30280
diff
changeset
|
48 |
let val name' = Long_Name.base_name name |
42290
b1f544c84040
discontinued special treatment of structure Lexicon;
wenzelm
parents:
41423
diff
changeset
|
49 |
in if Lexicon.is_identifier name' then name' else "x" end; |
8434 | 50 |
in indexify_names (map type_name Ts) end; |
5177 | 51 |
|
52 |
||
53 |
(************************* injectivity of constructors ************************) |
|
54 |
||
45822 | 55 |
fun make_injs descr = |
5177 | 56 |
let |
21078 | 57 |
val descr' = flat descr; |
58 |
fun make_inj T (cname, cargs) = |
|
45700 | 59 |
if null cargs then I |
60 |
else |
|
5177 | 61 |
let |
45822 | 62 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr') cargs; |
5177 | 63 |
val constr_t = Const (cname, Ts ---> T); |
64 |
val tnames = make_tnames Ts; |
|
65 |
val frees = map Free (tnames ~~ Ts); |
|
45743 | 66 |
val frees' = map Free (map (suffix "'") tnames ~~ Ts); |
67 |
in |
|
68 |
cons (HOLogic.mk_Trueprop (HOLogic.mk_eq |
|
69 |
(HOLogic.mk_eq (list_comb (constr_t, frees), list_comb (constr_t, frees')), |
|
70 |
foldr1 (HOLogic.mk_binop @{const_name HOL.conj}) |
|
71 |
(map HOLogic.mk_eq (frees ~~ frees'))))) |
|
5177 | 72 |
end; |
21078 | 73 |
in |
74 |
map2 (fn d => fn T => fold_rev (make_inj T) (#3 (snd d)) []) |
|
45822 | 75 |
(hd descr) (take (length (hd descr)) (Datatype_Aux.get_rec_types descr')) |
5177 | 76 |
end; |
77 |
||
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
78 |
|
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
79 |
(************************* distinctness of constructors ***********************) |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
80 |
|
45822 | 81 |
fun make_distincts descr = |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
82 |
let |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
83 |
val descr' = flat descr; |
45822 | 84 |
val recTs = Datatype_Aux.get_rec_types descr'; |
33957 | 85 |
val newTs = take (length (hd descr)) recTs; |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
86 |
|
45822 | 87 |
fun prep_constr (cname, cargs) = (cname, map (Datatype_Aux.typ_of_dtyp descr') cargs); |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
88 |
|
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
89 |
fun make_distincts' _ [] = [] |
45700 | 90 |
| make_distincts' T ((cname, cargs) :: constrs) = |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
91 |
let |
45743 | 92 |
val frees = map Free (make_tnames cargs ~~ cargs); |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
93 |
val t = list_comb (Const (cname, cargs ---> T), frees); |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
94 |
|
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
95 |
fun make_distincts'' (cname', cargs') = |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
96 |
let |
45700 | 97 |
val frees' = map Free (map (suffix "'") (make_tnames cargs') ~~ cargs'); |
98 |
val t' = list_comb (Const (cname', cargs' ---> T), frees'); |
|
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
99 |
in |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
100 |
HOLogic.mk_Trueprop (HOLogic.Not $ HOLogic.mk_eq (t, t')) |
45700 | 101 |
end; |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
102 |
in map make_distincts'' constrs @ make_distincts' T constrs end; |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
103 |
in |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
104 |
map2 (fn ((_, (_, _, constrs))) => fn T => |
45889 | 105 |
make_distincts' T (map prep_constr constrs)) (hd descr) newTs |
27300
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
106 |
end; |
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
107 |
|
4cb3101d2bf7
DatatypeProp.make_distincts: only one half of each symmetric pair is constructed
haftmann
parents:
27002
diff
changeset
|
108 |
|
5177 | 109 |
(********************************* induction **********************************) |
110 |
||
45822 | 111 |
fun make_ind descr = |
5177 | 112 |
let |
32952 | 113 |
val descr' = flat descr; |
45822 | 114 |
val recTs = Datatype_Aux.get_rec_types descr'; |
41423 | 115 |
val pnames = |
116 |
if length descr' = 1 then ["P"] |
|
5177 | 117 |
else map (fn i => "P" ^ string_of_int i) (1 upto length descr'); |
118 |
||
119 |
fun make_pred i T = |
|
120 |
let val T' = T --> HOLogic.boolT |
|
42364 | 121 |
in Free (nth pnames i, T') end; |
5177 | 122 |
|
123 |
fun make_ind_prem k T (cname, cargs) = |
|
124 |
let |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
125 |
fun mk_prem ((dt, s), T) = |
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
126 |
let val (Us, U) = strip_type T |
41423 | 127 |
in |
128 |
list_all (map (pair "x") Us, |
|
129 |
HOLogic.mk_Trueprop |
|
130 |
(make_pred (Datatype_Aux.body_index dt) U $ |
|
131 |
Datatype_Aux.app_bnds (Free (s, T)) (length Us))) |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
132 |
end; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
133 |
|
41423 | 134 |
val recs = filter Datatype_Aux.is_rec_type cargs; |
45822 | 135 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr') cargs; |
136 |
val recTs' = map (Datatype_Aux.typ_of_dtyp descr') recs; |
|
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
137 |
val tnames = Name.variant_list pnames (make_tnames Ts); |
41423 | 138 |
val rec_tnames = map fst (filter (Datatype_Aux.is_rec_type o snd) (tnames ~~ cargs)); |
5177 | 139 |
val frees = tnames ~~ Ts; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
140 |
val prems = map mk_prem (recs ~~ rec_tnames ~~ recTs'); |
45700 | 141 |
in |
46215
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
142 |
fold_rev (Logic.all o Free) frees |
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
143 |
(Logic.list_implies (prems, |
45700 | 144 |
HOLogic.mk_Trueprop (make_pred k T $ |
145 |
list_comb (Const (cname, Ts ---> T), map Free frees)))) |
|
5177 | 146 |
end; |
147 |
||
45700 | 148 |
val prems = |
149 |
maps (fn ((i, (_, _, constrs)), T) => map (make_ind_prem i T) constrs) (descr' ~~ recTs); |
|
5177 | 150 |
val tnames = make_tnames recTs; |
45700 | 151 |
val concl = |
152 |
HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop @{const_name HOL.conj}) |
|
153 |
(map (fn (((i, _), T), tname) => make_pred i T $ Free (tname, T)) |
|
154 |
(descr' ~~ recTs ~~ tnames))); |
|
5177 | 155 |
|
156 |
in Logic.list_implies (prems, concl) end; |
|
157 |
||
158 |
(******************************* case distinction *****************************) |
|
159 |
||
45822 | 160 |
fun make_casedists descr = |
5177 | 161 |
let |
32952 | 162 |
val descr' = flat descr; |
5177 | 163 |
|
164 |
fun make_casedist_prem T (cname, cargs) = |
|
165 |
let |
|
45822 | 166 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr') cargs; |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
167 |
val frees = Name.variant_list ["P", "y"] (make_tnames Ts) ~~ Ts; |
45700 | 168 |
val free_ts = map Free frees; |
169 |
in |
|
46215
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
170 |
fold_rev (Logic.all o Free) frees |
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
171 |
(Logic.mk_implies (HOLogic.mk_Trueprop |
45700 | 172 |
(HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))), |
173 |
HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT)))) |
|
5177 | 174 |
end; |
175 |
||
33957 | 176 |
fun make_casedist ((_, (_, _, constrs))) T = |
5177 | 177 |
let val prems = map (make_casedist_prem T) constrs |
45700 | 178 |
in Logic.list_implies (prems, HOLogic.mk_Trueprop (Free ("P", HOLogic.boolT))) end; |
5177 | 179 |
|
41423 | 180 |
in |
181 |
map2 make_casedist (hd descr) |
|
45822 | 182 |
(take (length (hd descr)) (Datatype_Aux.get_rec_types descr')) |
41423 | 183 |
end; |
5177 | 184 |
|
185 |
(*************** characteristic equations for primrec combinator **************) |
|
186 |
||
45822 | 187 |
fun make_primrec_Ts descr used = |
5177 | 188 |
let |
32952 | 189 |
val descr' = flat descr; |
5177 | 190 |
|
45700 | 191 |
val rec_result_Ts = |
192 |
map TFree |
|
193 |
(Name.variant_list used (replicate (length descr') "'t") ~~ |
|
194 |
replicate (length descr') HOLogic.typeS); |
|
5177 | 195 |
|
32952 | 196 |
val reccomb_fn_Ts = maps (fn (i, (_, _, constrs)) => |
5177 | 197 |
map (fn (_, cargs) => |
198 |
let |
|
45822 | 199 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr') cargs; |
41423 | 200 |
val recs = filter (Datatype_Aux.is_rec_type o fst) (cargs ~~ Ts); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
201 |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
202 |
fun mk_argT (dt, T) = |
42364 | 203 |
binder_types T ---> nth rec_result_Ts (Datatype_Aux.body_index dt); |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
204 |
|
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
205 |
val argTs = Ts @ map mk_argT recs |
42364 | 206 |
in argTs ---> nth rec_result_Ts i end) constrs) descr'; |
5177 | 207 |
|
15459
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
208 |
in (rec_result_Ts, reccomb_fn_Ts) end; |
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
209 |
|
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
210 |
fun make_primrecs reccomb_names descr thy = |
15459
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
211 |
let |
32952 | 212 |
val descr' = flat descr; |
45822 | 213 |
val recTs = Datatype_Aux.get_rec_types descr'; |
45738 | 214 |
val used = fold Term.add_tfree_namesT recTs []; |
15459
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
215 |
|
45822 | 216 |
val (rec_result_Ts, reccomb_fn_Ts) = make_primrec_Ts descr used; |
15459
16dd63c78049
Introduced function DatatypeProp.make_primrec_Ts to avoid code duplication.
berghofe
parents:
14981
diff
changeset
|
217 |
|
45700 | 218 |
val rec_fns = |
219 |
map (uncurry (Datatype_Aux.mk_Free "f")) |
|
220 |
(reccomb_fn_Ts ~~ (1 upto (length reccomb_fn_Ts))); |
|
5177 | 221 |
|
45700 | 222 |
val reccombs = |
223 |
map (fn ((name, T), T') => list_comb (Const (name, reccomb_fn_Ts @ [T] ---> T'), rec_fns)) |
|
5177 | 224 |
(reccomb_names ~~ recTs ~~ rec_result_Ts); |
225 |
||
45700 | 226 |
fun make_primrec T comb_t (cname, cargs) (ts, f :: fs) = |
5177 | 227 |
let |
41423 | 228 |
val recs = filter Datatype_Aux.is_rec_type cargs; |
45822 | 229 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr') cargs; |
230 |
val recTs' = map (Datatype_Aux.typ_of_dtyp descr') recs; |
|
5177 | 231 |
val tnames = make_tnames Ts; |
41423 | 232 |
val rec_tnames = map fst (filter (Datatype_Aux.is_rec_type o snd) (tnames ~~ cargs)); |
5177 | 233 |
val frees = map Free (tnames ~~ Ts); |
234 |
val frees' = map Free (rec_tnames ~~ recTs'); |
|
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
235 |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
236 |
fun mk_reccomb ((dt, T), t) = |
42364 | 237 |
let val (Us, U) = strip_type T in |
238 |
list_abs (map (pair "x") Us, |
|
239 |
nth reccombs (Datatype_Aux.body_index dt) $ Datatype_Aux.app_bnds t (length Us)) |
|
13641
63d1790a43ed
Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents:
13585
diff
changeset
|
240 |
end; |
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
241 |
|
45700 | 242 |
val reccombs' = map mk_reccomb (recs ~~ recTs' ~~ frees'); |
5177 | 243 |
|
45700 | 244 |
in |
245 |
(ts @ [HOLogic.mk_Trueprop |
|
246 |
(HOLogic.mk_eq (comb_t $ list_comb (Const (cname, Ts ---> T), frees), |
|
247 |
list_comb (f, frees @ reccombs')))], fs) |
|
33244 | 248 |
end; |
249 |
in |
|
250 |
fold (fn ((dt, T), comb_t) => fold (make_primrec T comb_t) (#3 (snd dt))) |
|
251 |
(descr' ~~ recTs ~~ reccombs) ([], rec_fns) |
|
252 |
|> fst |
|
5177 | 253 |
end; |
254 |
||
255 |
(****************** make terms of form t_case f1 ... fn *********************) |
|
256 |
||
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
257 |
fun make_case_combs case_names descr thy fname = |
5177 | 258 |
let |
32952 | 259 |
val descr' = flat descr; |
45822 | 260 |
val recTs = Datatype_Aux.get_rec_types descr'; |
45738 | 261 |
val used = fold Term.add_tfree_namesT recTs []; |
33957 | 262 |
val newTs = take (length (hd descr)) recTs; |
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
42364
diff
changeset
|
263 |
val T' = TFree (singleton (Name.variant_list used) "'t", HOLogic.typeS); |
5177 | 264 |
|
265 |
val case_fn_Ts = map (fn (i, (_, _, constrs)) => |
|
266 |
map (fn (_, cargs) => |
|
45822 | 267 |
let val Ts = map (Datatype_Aux.typ_of_dtyp descr') cargs |
5177 | 268 |
in Ts ---> T' end) constrs) (hd descr); |
269 |
in |
|
270 |
map (fn ((name, Ts), T) => list_comb |
|
271 |
(Const (name, Ts @ [T] ---> T'), |
|
41423 | 272 |
map (uncurry (Datatype_Aux.mk_Free fname)) (Ts ~~ (1 upto length Ts)))) |
5177 | 273 |
(case_names ~~ case_fn_Ts ~~ newTs) |
274 |
end; |
|
275 |
||
276 |
(**************** characteristic equations for case combinator ****************) |
|
277 |
||
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
278 |
fun make_cases case_names descr thy = |
5177 | 279 |
let |
32952 | 280 |
val descr' = flat descr; |
45822 | 281 |
val recTs = Datatype_Aux.get_rec_types descr'; |
33957 | 282 |
val newTs = take (length (hd descr)) recTs; |
5177 | 283 |
|
284 |
fun make_case T comb_t ((cname, cargs), f) = |
|
285 |
let |
|
45822 | 286 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr') cargs; |
45700 | 287 |
val frees = map Free ((make_tnames Ts) ~~ Ts); |
288 |
in |
|
289 |
HOLogic.mk_Trueprop |
|
290 |
(HOLogic.mk_eq (comb_t $ list_comb (Const (cname, Ts ---> T), frees), |
|
291 |
list_comb (f, frees))) |
|
292 |
end; |
|
293 |
in |
|
294 |
map (fn (((_, (_, _, constrs)), T), comb_t) => |
|
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
295 |
map (make_case T comb_t) (constrs ~~ snd (strip_comb comb_t))) |
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
296 |
(hd descr ~~ newTs ~~ make_case_combs case_names descr thy "f") |
5177 | 297 |
end; |
298 |
||
7015
85be09eb136c
- Datatype package now also supports arbitrarily branching datatypes
berghofe
parents:
6394
diff
changeset
|
299 |
|
5177 | 300 |
(*************************** the "split" - equations **************************) |
301 |
||
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
302 |
fun make_splits case_names descr thy = |
5177 | 303 |
let |
32952 | 304 |
val descr' = flat descr; |
45822 | 305 |
val recTs = Datatype_Aux.get_rec_types descr'; |
45738 | 306 |
val used' = fold Term.add_tfree_namesT recTs []; |
33957 | 307 |
val newTs = take (length (hd descr)) recTs; |
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
42364
diff
changeset
|
308 |
val T' = TFree (singleton (Name.variant_list used') "'t", HOLogic.typeS); |
5177 | 309 |
val P = Free ("P", T' --> HOLogic.boolT); |
310 |
||
311 |
fun make_split (((_, (_, _, constrs)), T), comb_t) = |
|
312 |
let |
|
313 |
val (_, fs) = strip_comb comb_t; |
|
45700 | 314 |
val used = ["P", "x"] @ map (fst o dest_Free) fs; |
5177 | 315 |
|
33338 | 316 |
fun process_constr ((cname, cargs), f) (t1s, t2s) = |
5177 | 317 |
let |
45822 | 318 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr') cargs; |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
319 |
val frees = map Free (Name.variant_list used (make_tnames Ts) ~~ Ts); |
45700 | 320 |
val eqn = HOLogic.mk_eq (Free ("x", T), list_comb (Const (cname, Ts ---> T), frees)); |
321 |
val P' = P $ list_comb (f, frees); |
|
322 |
in |
|
323 |
(fold_rev (fn Free (s, T) => fn t => HOLogic.mk_all (s, T, t)) frees |
|
324 |
(HOLogic.imp $ eqn $ P') :: t1s, |
|
325 |
fold_rev (fn Free (s, T) => fn t => HOLogic.mk_exists (s, T, t)) frees |
|
326 |
(HOLogic.conj $ eqn $ (HOLogic.Not $ P')) :: t2s) |
|
5177 | 327 |
end; |
328 |
||
33338 | 329 |
val (t1s, t2s) = fold_rev process_constr (constrs ~~ fs) ([], []); |
45700 | 330 |
val lhs = P $ (comb_t $ Free ("x", T)); |
5177 | 331 |
in |
41423 | 332 |
(HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, Datatype_Aux.mk_conj t1s)), |
333 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, HOLogic.Not $ Datatype_Aux.mk_disj t2s))) |
|
5177 | 334 |
end |
335 |
||
45700 | 336 |
in |
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
337 |
map make_split (hd descr ~~ newTs ~~ make_case_combs case_names descr thy "f") |
5177 | 338 |
end; |
339 |
||
340 |
(************************* additional rules for TFL ***************************) |
|
341 |
||
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
342 |
fun make_weak_case_congs case_names descr thy = |
8601 | 343 |
let |
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
344 |
val case_combs = make_case_combs case_names descr thy "f"; |
8601 | 345 |
|
346 |
fun mk_case_cong comb = |
|
45700 | 347 |
let |
8601 | 348 |
val Type ("fun", [T, _]) = fastype_of comb; |
349 |
val M = Free ("M", T); |
|
350 |
val M' = Free ("M'", T); |
|
351 |
in |
|
352 |
Logic.mk_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')), |
|
353 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb $ M'))) |
|
45700 | 354 |
end; |
8601 | 355 |
in |
356 |
map mk_case_cong case_combs |
|
357 |
end; |
|
45700 | 358 |
|
8601 | 359 |
|
5177 | 360 |
(*--------------------------------------------------------------------------- |
361 |
* Structure of case congruence theorem looks like this: |
|
362 |
* |
|
45700 | 363 |
* (M = M') |
364 |
* ==> (!!x1,...,xk. (M' = C1 x1..xk) ==> (f1 x1..xk = g1 x1..xk)) |
|
365 |
* ==> ... |
|
366 |
* ==> (!!x1,...,xj. (M' = Cn x1..xj) ==> (fn x1..xj = gn x1..xj)) |
|
5177 | 367 |
* ==> |
368 |
* (ty_case f1..fn M = ty_case g1..gn M') |
|
369 |
*---------------------------------------------------------------------------*) |
|
370 |
||
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
371 |
fun make_case_congs case_names descr thy = |
5177 | 372 |
let |
45879
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
373 |
val case_combs = make_case_combs case_names descr thy "f"; |
71b8d0d170b1
avoid fragile Sign.intern_const -- pass internal names directly;
wenzelm
parents:
45822
diff
changeset
|
374 |
val case_combs' = make_case_combs case_names descr thy "g"; |
5177 | 375 |
|
376 |
fun mk_case_cong ((comb, comb'), (_, (_, _, constrs))) = |
|
377 |
let |
|
378 |
val Type ("fun", [T, _]) = fastype_of comb; |
|
379 |
val (_, fs) = strip_comb comb; |
|
380 |
val (_, gs) = strip_comb comb'; |
|
381 |
val used = ["M", "M'"] @ map (fst o dest_Free) (fs @ gs); |
|
382 |
val M = Free ("M", T); |
|
383 |
val M' = Free ("M'", T); |
|
384 |
||
385 |
fun mk_clause ((f, g), (cname, _)) = |
|
386 |
let |
|
40844 | 387 |
val Ts = binder_types (fastype_of f); |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
388 |
val tnames = Name.variant_list used (make_tnames Ts); |
45743 | 389 |
val frees = map Free (tnames ~~ Ts); |
5177 | 390 |
in |
46215
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
391 |
fold_rev Logic.all frees |
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
392 |
(Logic.mk_implies |
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
393 |
(HOLogic.mk_Trueprop |
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
394 |
(HOLogic.mk_eq (M', list_comb (Const (cname, Ts ---> T), frees))), |
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
395 |
HOLogic.mk_Trueprop |
0da9433f959e
discontinued old-style Term.list_all_free in favour of plain Logic.all;
wenzelm
parents:
45896
diff
changeset
|
396 |
(HOLogic.mk_eq (list_comb (f, frees), list_comb (g, frees))))) |
45700 | 397 |
end; |
5177 | 398 |
in |
399 |
Logic.list_implies (HOLogic.mk_Trueprop (HOLogic.mk_eq (M, M')) :: |
|
400 |
map mk_clause (fs ~~ gs ~~ constrs), |
|
401 |
HOLogic.mk_Trueprop (HOLogic.mk_eq (comb $ M, comb' $ M'))) |
|
45700 | 402 |
end; |
5177 | 403 |
in |
404 |
map mk_case_cong (case_combs ~~ case_combs' ~~ hd descr) |
|
405 |
end; |
|
406 |
||
407 |
(*--------------------------------------------------------------------------- |
|
408 |
* Structure of exhaustion theorem looks like this: |
|
409 |
* |
|
410 |
* !v. (? y1..yi. v = C1 y1..yi) | ... | (? y1..yj. v = Cn y1..yj) |
|
411 |
*---------------------------------------------------------------------------*) |
|
412 |
||
45822 | 413 |
fun make_nchotomys descr = |
5177 | 414 |
let |
32952 | 415 |
val descr' = flat descr; |
45822 | 416 |
val recTs = Datatype_Aux.get_rec_types descr'; |
33957 | 417 |
val newTs = take (length (hd descr)) recTs; |
5177 | 418 |
|
419 |
fun mk_eqn T (cname, cargs) = |
|
420 |
let |
|
45822 | 421 |
val Ts = map (Datatype_Aux.typ_of_dtyp descr') cargs; |
20071
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
wenzelm
parents:
19233
diff
changeset
|
422 |
val tnames = Name.variant_list ["v"] (make_tnames Ts); |
45743 | 423 |
val frees = tnames ~~ Ts; |
5177 | 424 |
in |
33338 | 425 |
fold_rev (fn (s, T') => fn t => HOLogic.mk_exists (s, T', t)) frees |
15574
b1d1b5bfc464
Removed practically all references to Library.foldr.
skalberg
parents:
15570
diff
changeset
|
426 |
(HOLogic.mk_eq (Free ("v", T), |
33338 | 427 |
list_comb (Const (cname, Ts ---> T), map Free frees))) |
45700 | 428 |
end; |
429 |
in |
|
430 |
map (fn ((_, (_, _, constrs)), T) => |
|
431 |
HOLogic.mk_Trueprop |
|
432 |
(HOLogic.mk_all ("v", T, Datatype_Aux.mk_disj (map (mk_eqn T) constrs)))) |
|
5177 | 433 |
(hd descr ~~ newTs) |
434 |
end; |
|
435 |
||
436 |
end; |