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