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