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