author | wenzelm |
Tue, 26 Dec 2023 20:06:30 +0100 | |
changeset 79360 | da22c8ab0112 |
parent 79329 | 992c494bda25 |
child 79361 | c28d4d1986f1 |
permissions | -rw-r--r-- |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/zterm.ML |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
3 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
4 |
Tight representation of types / terms / proof terms, notably for proof recording. |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
5 |
*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
6 |
|
79124 | 7 |
(*** global ***) |
8 |
||
9 |
(* types and terms *) |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
10 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
11 |
datatype ztyp = |
79119 | 12 |
ZTVar of indexname * sort (*free: index ~1*) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
13 |
| ZFun of ztyp * ztyp |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
14 |
| ZProp |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
15 |
| ZItself of ztyp |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
16 |
| ZType0 of string (*type constant*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
17 |
| ZType1 of string * ztyp (*type constructor: 1 argument*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
18 |
| ZType of string * ztyp list (*type constructor: >= 2 arguments*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
19 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
20 |
datatype zterm = |
79119 | 21 |
ZVar of indexname * ztyp (*free: index ~1*) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
22 |
| ZBound of int |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
23 |
| ZConst0 of string (*monomorphic constant*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
24 |
| ZConst1 of string * ztyp (*polymorphic constant: 1 type argument*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
25 |
| ZConst of string * ztyp list (*polymorphic constant: >= 2 type arguments*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
26 |
| ZAbs of string * ztyp * zterm |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
27 |
| ZApp of zterm * zterm |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
28 |
| ZClass of ztyp * class (*OFCLASS proposition*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
29 |
|
79124 | 30 |
structure ZTerm = |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
31 |
struct |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
32 |
|
79119 | 33 |
(* fold *) |
34 |
||
35 |
fun fold_tvars f (ZTVar v) = f v |
|
36 |
| fold_tvars f (ZFun (T, U)) = fold_tvars f T #> fold_tvars f U |
|
37 |
| fold_tvars f (ZItself T) = fold_tvars f T |
|
38 |
| fold_tvars f (ZType1 (_, T)) = fold_tvars f T |
|
39 |
| fold_tvars f (ZType (_, Ts)) = fold (fold_tvars f) Ts |
|
40 |
| fold_tvars _ _ = I; |
|
41 |
||
42 |
fun fold_aterms f (ZApp (t, u)) = fold_aterms f t #> fold_aterms f u |
|
43 |
| fold_aterms f (ZAbs (_, _, t)) = fold_aterms f t |
|
44 |
| fold_aterms f a = f a; |
|
45 |
||
46 |
fun fold_types f (ZVar (_, T)) = f T |
|
47 |
| fold_types f (ZConst1 (_, T)) = f T |
|
48 |
| fold_types f (ZConst (_, As)) = fold f As |
|
49 |
| fold_types f (ZAbs (_, T, b)) = f T #> fold_types f b |
|
50 |
| fold_types f (ZApp (t, u)) = fold_types f t #> fold_types f u |
|
51 |
| fold_types f (ZClass (T, _)) = f T |
|
52 |
| fold_types _ _ = I; |
|
53 |
||
54 |
||
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
55 |
(* type ordering *) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
56 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
57 |
local |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
58 |
|
79119 | 59 |
fun cons_nr (ZTVar _) = 0 |
60 |
| cons_nr (ZFun _) = 1 |
|
61 |
| cons_nr ZProp = 2 |
|
62 |
| cons_nr (ZItself _) = 3 |
|
63 |
| cons_nr (ZType0 _) = 4 |
|
64 |
| cons_nr (ZType1 _) = 5 |
|
65 |
| cons_nr (ZType _) = 6; |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
66 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
67 |
val fast_indexname_ord = Term_Ord.fast_indexname_ord; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
68 |
val sort_ord = Term_Ord.sort_ord; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
69 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
70 |
in |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
71 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
72 |
fun ztyp_ord TU = |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
73 |
if pointer_eq TU then EQUAL |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
74 |
else |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
75 |
(case TU of |
79119 | 76 |
(ZTVar (a, A), ZTVar (b, B)) => |
77 |
(case fast_indexname_ord (a, b) of EQUAL => sort_ord (A, B) | ord => ord) |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
78 |
| (ZFun (T, T'), ZFun (U, U')) => |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
79 |
(case ztyp_ord (T, U) of EQUAL => ztyp_ord (T', U') | ord => ord) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
80 |
| (ZProp, ZProp) => EQUAL |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
81 |
| (ZItself T, ZItself U) => ztyp_ord (T, U) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
82 |
| (ZType0 a, ZType0 b) => fast_string_ord (a, b) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
83 |
| (ZType1 (a, T), ZType1 (b, U)) => |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
84 |
(case fast_string_ord (a, b) of EQUAL => ztyp_ord (T, U) | ord => ord) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
85 |
| (ZType (a, Ts), ZType (b, Us)) => |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
86 |
(case fast_string_ord (a, b) of EQUAL => dict_ord ztyp_ord (Ts, Us) | ord => ord) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
87 |
| (T, U) => int_ord (cons_nr T, cons_nr U)); |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
88 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
89 |
end; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
90 |
|
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
91 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
92 |
(* term ordering and alpha-conversion *) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
93 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
94 |
local |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
95 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
96 |
fun cons_nr (ZVar _) = 0 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
97 |
| cons_nr (ZBound _) = 1 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
98 |
| cons_nr (ZConst0 _) = 2 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
99 |
| cons_nr (ZConst1 _) = 3 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
100 |
| cons_nr (ZConst _) = 4 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
101 |
| cons_nr (ZAbs _) = 5 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
102 |
| cons_nr (ZApp _) = 6 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
103 |
| cons_nr (ZClass _) = 7; |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
104 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
105 |
fun struct_ord tu = |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
106 |
if pointer_eq tu then EQUAL |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
107 |
else |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
108 |
(case tu of |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
109 |
(ZAbs (_, _, t), ZAbs (_, _, u)) => struct_ord (t, u) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
110 |
| (ZApp (t1, t2), ZApp (u1, u2)) => |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
111 |
(case struct_ord (t1, u1) of EQUAL => struct_ord (t2, u2) | ord => ord) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
112 |
| (t, u) => int_ord (cons_nr t, cons_nr u)); |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
113 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
114 |
fun atoms_ord tu = |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
115 |
if pointer_eq tu then EQUAL |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
116 |
else |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
117 |
(case tu of |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
118 |
(ZAbs (_, _, t), ZAbs (_, _, u)) => atoms_ord (t, u) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
119 |
| (ZApp (t1, t2), ZApp (u1, u2)) => |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
120 |
(case atoms_ord (t1, u1) of EQUAL => atoms_ord (t2, u2) | ord => ord) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
121 |
| (ZConst0 a, ZConst0 b) => fast_string_ord (a, b) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
122 |
| (ZConst1 (a, _), ZConst1 (b, _)) => fast_string_ord (a, b) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
123 |
| (ZConst (a, _), ZConst (b, _)) => fast_string_ord (a, b) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
124 |
| (ZVar (xi, _), ZVar (yj, _)) => Term_Ord.fast_indexname_ord (xi, yj) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
125 |
| (ZBound i, ZBound j) => int_ord (i, j) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
126 |
| (ZClass (_, a), ZClass (_, b)) => fast_string_ord (a, b) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
127 |
| _ => EQUAL); |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
128 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
129 |
fun types_ord tu = |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
130 |
if pointer_eq tu then EQUAL |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
131 |
else |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
132 |
(case tu of |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
133 |
(ZAbs (_, T, t), ZAbs (_, U, u)) => |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
134 |
(case ztyp_ord (T, U) of EQUAL => types_ord (t, u) | ord => ord) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
135 |
| (ZApp (t1, t2), ZApp (u1, u2)) => |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
136 |
(case types_ord (t1, u1) of EQUAL => types_ord (t2, u2) | ord => ord) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
137 |
| (ZConst1 (_, T), ZConst1 (_, U)) => ztyp_ord (T, U) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
138 |
| (ZConst (_, Ts), ZConst (_, Us)) => dict_ord ztyp_ord (Ts, Us) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
139 |
| (ZVar (_, T), ZVar (_, U)) => ztyp_ord (T, U) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
140 |
| (ZClass (T, _), ZClass (U, _)) => ztyp_ord (T, U) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
141 |
| _ => EQUAL); |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
142 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
143 |
in |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
144 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
145 |
val fast_zterm_ord = struct_ord ||| atoms_ord ||| types_ord; |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
146 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
147 |
end; |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
148 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
149 |
fun aconv_zterm (tm1, tm2) = |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
150 |
pointer_eq (tm1, tm2) orelse |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
151 |
(case (tm1, tm2) of |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
152 |
(ZApp (t1, u1), ZApp (t2, u2)) => aconv_zterm (t1, t2) andalso aconv_zterm (u1, u2) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
153 |
| (ZAbs (_, T1, t1), ZAbs (_, T2, t2)) => aconv_zterm (t1, t2) andalso T1 = T2 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
154 |
| (a1, a2) => a1 = a2); |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
155 |
|
79124 | 156 |
end; |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
157 |
|
79124 | 158 |
|
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
159 |
(* tables and term items *) |
79124 | 160 |
|
79163 | 161 |
structure ZTypes = Table(type key = ztyp val ord = ZTerm.ztyp_ord); |
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
162 |
structure ZTerms = Table(type key = zterm val ord = ZTerm.fast_zterm_ord); |
79163 | 163 |
|
79124 | 164 |
structure ZTVars: |
165 |
sig |
|
166 |
include TERM_ITEMS |
|
167 |
val add_tvarsT: ztyp -> set -> set |
|
168 |
val add_tvars: zterm -> set -> set |
|
169 |
end = |
|
170 |
struct |
|
171 |
open TVars; |
|
172 |
val add_tvarsT = ZTerm.fold_tvars add_set; |
|
173 |
val add_tvars = ZTerm.fold_types add_tvarsT; |
|
174 |
end; |
|
175 |
||
176 |
structure ZVars: |
|
177 |
sig |
|
178 |
include TERM_ITEMS |
|
179 |
val add_vars: zterm -> set -> set |
|
180 |
end = |
|
181 |
struct |
|
182 |
||
183 |
structure Term_Items = Term_Items |
|
184 |
( |
|
185 |
type key = indexname * ztyp; |
|
186 |
val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord ZTerm.ztyp_ord); |
|
187 |
); |
|
188 |
open Term_Items; |
|
189 |
||
190 |
val add_vars = ZTerm.fold_aterms (fn ZVar v => add_set v | _ => I); |
|
191 |
||
192 |
end; |
|
193 |
||
194 |
||
195 |
(* proofs *) |
|
196 |
||
79126 | 197 |
datatype zproof_name = |
198 |
ZAxiom of string |
|
199 |
| ZOracle of string |
|
79313
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
200 |
| ZBox of serial |
79360 | 201 |
| ZThm of {theory_name: string, thm_name: Thm_Name.T, pos: Position.T, serial: int}; |
79126 | 202 |
|
79124 | 203 |
datatype zproof = |
204 |
ZDummy (*dummy proof*) |
|
79212 | 205 |
| ZConstp of zproof_name * zterm * ztyp ZTVars.table * zterm ZVars.table |
206 |
| ZBoundp of int |
|
79124 | 207 |
| ZHyp of zterm |
208 |
| ZAbst of string * ztyp * zproof |
|
79212 | 209 |
| ZAbsp of string * zterm * zproof |
79124 | 210 |
| ZAppt of zproof * zterm |
79212 | 211 |
| ZAppp of zproof * zproof |
212 |
| ZClassp of ztyp * class; (*OFCLASS proof from sorts algebra*) |
|
79124 | 213 |
|
214 |
||
215 |
||
216 |
(*** local ***) |
|
217 |
||
218 |
signature ZTERM = |
|
219 |
sig |
|
220 |
datatype ztyp = datatype ztyp |
|
221 |
datatype zterm = datatype zterm |
|
222 |
datatype zproof = datatype zproof |
|
79265 | 223 |
exception ZTERM of string * ztyp list * zterm list * zproof list |
79263 | 224 |
type zproof_const = zproof_name * zterm * ztyp ZTVars.table * zterm ZVars.table |
79124 | 225 |
val fold_tvars: (indexname * sort -> 'a -> 'a) -> ztyp -> 'a -> 'a |
226 |
val fold_aterms: (zterm -> 'a -> 'a) -> zterm -> 'a -> 'a |
|
227 |
val fold_types: (ztyp -> 'a -> 'a) -> zterm -> 'a -> 'a |
|
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
228 |
val ztyp_ord: ztyp ord |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
229 |
val fast_zterm_ord: zterm ord |
79124 | 230 |
val aconv_zterm: zterm * zterm -> bool |
79212 | 231 |
val ZAbsts: (string * ztyp) list -> zproof -> zproof |
232 |
val ZAbsps: zterm list -> zproof -> zproof |
|
233 |
val ZAppts: zproof * zterm list -> zproof |
|
234 |
val ZAppps: zproof * zproof list -> zproof |
|
79200 | 235 |
val incr_bv_same: int -> int -> zterm Same.operation |
79283 | 236 |
val incr_proof_bv_same: int -> int -> int -> int -> zproof Same.operation |
79200 | 237 |
val incr_bv: int -> int -> zterm -> zterm |
238 |
val incr_boundvars: int -> zterm -> zterm |
|
79283 | 239 |
val incr_proof_bv: int -> int -> int -> int -> zproof -> zproof |
240 |
val incr_proof_boundvars: int -> int -> zproof -> zproof |
|
241 |
val subst_term_bound_same: zterm -> int -> zterm Same.operation |
|
242 |
val subst_proof_bound_same: zterm -> int -> zproof Same.operation |
|
243 |
val subst_proof_boundp_same: zproof -> int -> int -> zproof Same.operation |
|
244 |
val subst_term_bound: zterm -> zterm -> zterm |
|
245 |
val subst_proof_bound: zterm -> zproof -> zproof |
|
246 |
val subst_proof_boundp: zproof -> zproof -> zproof |
|
79318 | 247 |
exception BAD_INST of ((indexname * ztyp) * zterm) list |
79273 | 248 |
val map_proof: ztyp Same.operation -> zterm Same.operation -> zproof -> zproof |
249 |
val map_proof_types: ztyp Same.operation -> zproof -> zproof |
|
79124 | 250 |
val ztyp_of: typ -> ztyp |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
251 |
val zterm_cache: theory -> {zterm: term -> zterm, ztyp: typ -> ztyp} |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
252 |
val zterm_of: theory -> term -> zterm |
79124 | 253 |
val typ_of: ztyp -> typ |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
254 |
val term_of: theory -> zterm -> term |
79298 | 255 |
val beta_norm_term_same: zterm Same.operation |
256 |
val beta_norm_proof_same: zproof Same.operation |
|
79302
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
257 |
val beta_norm_prooft_same: zproof -> zproof |
79298 | 258 |
val beta_norm_term: zterm -> zterm |
259 |
val beta_norm_proof: zproof -> zproof |
|
79302
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
260 |
val beta_norm_prooft: zproof -> zproof |
79200 | 261 |
val norm_type: Type.tyenv -> ztyp -> ztyp |
262 |
val norm_term: theory -> Envir.env -> zterm -> zterm |
|
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
263 |
val norm_proof: theory -> Envir.env -> term list -> zproof -> zproof |
79311
e4a9a861856b
omit pointless future: proof terms are built sequentially;
wenzelm
parents:
79303
diff
changeset
|
264 |
type zbox = serial * (zterm * zproof) |
79279 | 265 |
val zbox_ord: zbox ord |
266 |
type zboxes = zbox Ord_List.T |
|
79265 | 267 |
val union_zboxes: zboxes -> zboxes -> zboxes |
79279 | 268 |
val unions_zboxes: zboxes list -> zboxes |
79312 | 269 |
val add_box_proof: theory -> term list -> term -> zboxes * zproof -> zboxes * zproof |
79329 | 270 |
val thm_proof: theory -> Thm_Name.T * Position.T -> term list -> term -> zproof -> zbox * zproof |
79126 | 271 |
val axiom_proof: theory -> string -> term -> zproof |
272 |
val oracle_proof: theory -> string -> term -> zproof |
|
79124 | 273 |
val assume_proof: theory -> term -> zproof |
274 |
val trivial_proof: theory -> term -> zproof |
|
275 |
val implies_intr_proof: theory -> term -> zproof -> zproof |
|
276 |
val forall_intr_proof: theory -> typ -> string * term -> zproof -> zproof |
|
277 |
val forall_elim_proof: theory -> term -> zproof -> zproof |
|
79128 | 278 |
val of_class_proof: typ * class -> zproof |
79124 | 279 |
val reflexive_proof: theory -> typ -> term -> zproof |
280 |
val symmetric_proof: theory -> typ -> term -> term -> zproof -> zproof |
|
281 |
val transitive_proof: theory -> typ -> term -> term -> term -> zproof -> zproof -> zproof |
|
282 |
val equal_intr_proof: theory -> term -> term -> zproof -> zproof -> zproof |
|
283 |
val equal_elim_proof: theory -> term -> term -> zproof -> zproof -> zproof |
|
284 |
val abstract_rule_proof: theory -> typ -> typ -> string * term -> term -> term -> zproof -> zproof |
|
285 |
val combination_proof: theory -> typ -> typ -> term -> term -> term -> term -> |
|
286 |
zproof -> zproof -> zproof |
|
79133 | 287 |
val generalize_proof: Names.set * Names.set -> int -> zproof -> zproof |
79149 | 288 |
val instantiate_proof: theory -> |
289 |
((indexname * sort) * typ) list * ((indexname * typ) * term) list -> zproof -> zproof |
|
79135 | 290 |
val varifyT_proof: ((string * sort) * (indexname * sort)) list -> zproof -> zproof |
79152 | 291 |
val legacy_freezeT_proof: term -> zproof -> zproof |
79155 | 292 |
val rotate_proof: theory -> term list -> term -> (string * typ) list -> |
293 |
term list -> int -> zproof -> zproof |
|
294 |
val permute_prems_proof: theory -> term list -> int -> int -> zproof -> zproof |
|
79234 | 295 |
val incr_indexes_proof: int -> zproof -> zproof |
79237 | 296 |
val lift_proof: theory -> term -> int -> term list -> zproof -> zproof |
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
297 |
val assumption_proof: theory -> Envir.env -> term list -> term -> int -> term list -> |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
298 |
zproof -> zproof |
79261 | 299 |
val bicompose_proof: theory -> Envir.env -> int -> bool -> term list -> term list -> |
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
300 |
term option -> term list -> int -> int -> term list -> zproof -> zproof -> zproof |
79124 | 301 |
end; |
302 |
||
303 |
structure ZTerm: ZTERM = |
|
304 |
struct |
|
305 |
||
306 |
datatype ztyp = datatype ztyp; |
|
307 |
datatype zterm = datatype zterm; |
|
308 |
datatype zproof = datatype zproof; |
|
309 |
||
79265 | 310 |
exception ZTERM of string * ztyp list * zterm list * zproof list; |
311 |
||
79124 | 312 |
open ZTerm; |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
313 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
314 |
|
79155 | 315 |
(* derived operations *) |
316 |
||
79234 | 317 |
val ZFuns = Library.foldr ZFun; |
318 |
||
79212 | 319 |
val ZAbsts = fold_rev (fn (x, T) => fn prf => ZAbst (x, T, prf)); |
320 |
val ZAbsps = fold_rev (fn t => fn prf => ZAbsp ("H", t, prf)); |
|
79155 | 321 |
|
79212 | 322 |
val ZAppts = Library.foldl ZAppt; |
323 |
val ZAppps = Library.foldl ZAppp; |
|
79155 | 324 |
|
79234 | 325 |
fun combound (t, n, k) = |
326 |
if k > 0 then ZApp (combound (t, n + 1, k - 1), ZBound n) else t; |
|
327 |
||
79155 | 328 |
|
79283 | 329 |
(* increment bound variables *) |
79200 | 330 |
|
331 |
fun incr_bv_same inc = |
|
332 |
if inc = 0 then fn _ => Same.same |
|
333 |
else |
|
334 |
let |
|
79285 | 335 |
fun term lev (ZBound i) = |
336 |
if i >= lev then ZBound (i + inc) else raise Same.SAME |
|
79200 | 337 |
| term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t) |
338 |
| term lev (ZApp (t, u)) = |
|
79285 | 339 |
(ZApp (term lev t, Same.commit (term lev) u) |
340 |
handle Same.SAME => ZApp (t, term lev u)) |
|
79200 | 341 |
| term _ _ = raise Same.SAME; |
342 |
in term end; |
|
343 |
||
79283 | 344 |
fun incr_proof_bv_same incp inct = |
345 |
if incp = 0 andalso inct = 0 then fn _ => fn _ => Same.same |
|
346 |
else |
|
347 |
let |
|
348 |
val term = incr_bv_same inct; |
|
79200 | 349 |
|
79283 | 350 |
fun proof plev _ (ZBoundp i) = |
351 |
if i >= plev then ZBoundp (i + incp) else raise Same.SAME |
|
352 |
| proof plev tlev (ZAbsp (a, t, p)) = |
|
353 |
(ZAbsp (a, term tlev t, Same.commit (proof (plev + 1) tlev) p) |
|
354 |
handle Same.SAME => ZAbsp (a, t, proof (plev + 1) tlev p)) |
|
355 |
| proof plev tlev (ZAbst (a, T, p)) = ZAbst (a, T, proof plev (tlev + 1) p) |
|
356 |
| proof plev tlev (ZAppp (p, q)) = |
|
357 |
(ZAppp (proof plev tlev p, Same.commit (proof plev tlev) q) |
|
358 |
handle Same.SAME => ZAppp (p, proof plev tlev q)) |
|
359 |
| proof plev tlev (ZAppt (p, t)) = |
|
360 |
(ZAppt (proof plev tlev p, Same.commit (term tlev) t) |
|
361 |
handle Same.SAME => ZAppt (p, term tlev t)) |
|
362 |
| proof _ _ _ = raise Same.SAME; |
|
363 |
in proof end; |
|
364 |
||
365 |
fun incr_bv inc lev = Same.commit (incr_bv_same inc lev); |
|
79200 | 366 |
fun incr_boundvars inc = incr_bv inc 0; |
367 |
||
79283 | 368 |
fun incr_proof_bv incp inct plev tlev = Same.commit (incr_proof_bv_same incp inct plev tlev); |
369 |
fun incr_proof_boundvars incp inct = incr_proof_bv incp inct 0 0; |
|
370 |
||
371 |
||
372 |
(* substitution of bound variables *) |
|
373 |
||
79281
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
374 |
fun subst_term_bound_same arg = |
79200 | 375 |
let |
376 |
fun term lev (ZBound i) = |
|
377 |
if i < lev then raise Same.SAME (*var is locally bound*) |
|
378 |
else if i = lev then incr_boundvars lev arg |
|
379 |
else ZBound (i - 1) (*loose: change it*) |
|
380 |
| term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t) |
|
381 |
| term lev (ZApp (t, u)) = |
|
382 |
(ZApp (term lev t, Same.commit (term lev) u) |
|
383 |
handle Same.SAME => ZApp (t, term lev u)) |
|
384 |
| term _ _ = raise Same.SAME; |
|
79281
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
385 |
in term end; |
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
386 |
|
79283 | 387 |
fun subst_proof_bound_same arg = |
388 |
let |
|
389 |
val term = subst_term_bound_same arg; |
|
390 |
||
391 |
fun proof lev (ZAbsp (a, t, p)) = |
|
392 |
(ZAbsp (a, term lev t, Same.commit (proof lev) p) |
|
393 |
handle Same.SAME => ZAbsp (a, t, proof lev p)) |
|
394 |
| proof lev (ZAbst (a, T, p)) = ZAbst (a, T, proof (lev + 1) p) |
|
395 |
| proof lev (ZAppp (p, q)) = |
|
396 |
(ZAppp (proof lev p, Same.commit (proof lev) q) |
|
397 |
handle Same.SAME => ZAppp (p, proof lev q)) |
|
398 |
| proof lev (ZAppt (p, t)) = |
|
399 |
(ZAppt (proof lev p, Same.commit (term lev) t) |
|
400 |
handle Same.SAME => ZAppt (p, term lev t)) |
|
401 |
| proof _ _ = raise Same.SAME; |
|
402 |
in proof end; |
|
403 |
||
404 |
fun subst_proof_boundp_same arg = |
|
405 |
let |
|
406 |
fun proof plev tlev (ZBoundp i) = |
|
407 |
if i < plev then raise Same.SAME (*var is locally bound*) |
|
408 |
else if i = plev then incr_proof_boundvars plev tlev arg |
|
409 |
else ZBoundp (i - 1) (*loose: change it*) |
|
410 |
| proof plev tlev (ZAbsp (a, t, p)) = ZAbsp (a, t, proof (plev + 1) tlev p) |
|
411 |
| proof plev tlev (ZAbst (a, T, p)) = ZAbst (a, T, proof plev (tlev + 1) p) |
|
412 |
| proof plev tlev (ZAppp (p, q)) = |
|
413 |
(ZAppp (proof plev tlev p, Same.commit (proof plev tlev) q) |
|
414 |
handle Same.SAME => ZAppp (p, proof plev tlev q)) |
|
415 |
| proof plev tlev (ZAppt (p, t)) = ZAppt (proof plev tlev p, t) |
|
416 |
| proof _ _ _ = raise Same.SAME; |
|
417 |
in proof end; |
|
418 |
||
419 |
fun subst_term_bound arg body = Same.commit (subst_term_bound_same arg 0) body; |
|
420 |
fun subst_proof_bound arg body = Same.commit (subst_proof_bound_same arg 0) body; |
|
421 |
fun subst_proof_boundp arg body = Same.commit (subst_proof_boundp_same arg 0 0) body; |
|
79200 | 422 |
|
423 |
||
79268 | 424 |
(* substitution of free or schematic variables *) |
79132 | 425 |
|
426 |
fun subst_type_same tvar = |
|
427 |
let |
|
428 |
fun typ (ZTVar x) = tvar x |
|
79326 | 429 |
| typ (ZFun (T, U)) = |
430 |
(ZFun (typ T, Same.commit typ U) |
|
431 |
handle Same.SAME => ZFun (T, typ U)) |
|
79132 | 432 |
| typ ZProp = raise Same.SAME |
433 |
| typ (ZItself T) = ZItself (typ T) |
|
434 |
| typ (ZType0 _) = raise Same.SAME |
|
435 |
| typ (ZType1 (a, T)) = ZType1 (a, typ T) |
|
436 |
| typ (ZType (a, Ts)) = ZType (a, Same.map typ Ts); |
|
437 |
in typ end; |
|
438 |
||
439 |
fun subst_term_same typ var = |
|
440 |
let |
|
441 |
fun term (ZVar (x, T)) = |
|
442 |
let val (T', same) = Same.commit_id typ T in |
|
443 |
(case Same.catch var (x, T') of |
|
444 |
NONE => if same then raise Same.SAME else ZVar (x, T') |
|
445 |
| SOME t' => t') |
|
446 |
end |
|
447 |
| term (ZBound _) = raise Same.SAME |
|
448 |
| term (ZConst0 _) = raise Same.SAME |
|
449 |
| term (ZConst1 (a, T)) = ZConst1 (a, typ T) |
|
450 |
| term (ZConst (a, Ts)) = ZConst (a, Same.map typ Ts) |
|
451 |
| term (ZAbs (a, T, t)) = |
|
79326 | 452 |
(ZAbs (a, typ T, Same.commit term t) |
453 |
handle Same.SAME => ZAbs (a, T, term t)) |
|
79132 | 454 |
| term (ZApp (t, u)) = |
79326 | 455 |
(ZApp (term t, Same.commit term u) |
456 |
handle Same.SAME => ZApp (t, term u)) |
|
79132 | 457 |
| term (ZClass (T, c)) = ZClass (typ T, c); |
458 |
in term end; |
|
459 |
||
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
460 |
fun instantiate_type_same instT = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
461 |
if ZTVars.is_empty instT then Same.same |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
462 |
else ZTypes.unsynchronized_cache (subst_type_same (Same.function (ZTVars.lookup instT))); |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
463 |
|
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
464 |
fun instantiate_term_same typ inst = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
465 |
subst_term_same typ (Same.function (ZVars.lookup inst)); |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
466 |
|
79268 | 467 |
|
468 |
(* map types/terms within proof *) |
|
469 |
||
79318 | 470 |
exception BAD_INST of ((indexname * ztyp) * zterm) list |
471 |
||
472 |
fun make_inst inst = |
|
79320
bbac3e8a5070
more permissive: allow collapse of term variables for equal results, e.g. relevant for metis (line 1882 of "~~/src/HOL/List.thy");
wenzelm
parents:
79318
diff
changeset
|
473 |
ZVars.build (inst |> fold (ZVars.insert (op aconv_zterm))) |
bbac3e8a5070
more permissive: allow collapse of term variables for equal results, e.g. relevant for metis (line 1882 of "~~/src/HOL/List.thy");
wenzelm
parents:
79318
diff
changeset
|
474 |
handle ZVars.DUP _ => raise BAD_INST inst; |
79318 | 475 |
|
79145 | 476 |
fun map_insts_same typ term (instT, inst) = |
477 |
let |
|
478 |
val changed = Unsynchronized.ref false; |
|
79146
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
479 |
fun apply f x = |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
480 |
(case Same.catch f x of |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
481 |
NONE => NONE |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
482 |
| some => (changed := true; some)); |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
483 |
|
79145 | 484 |
val instT' = |
485 |
(instT, instT) |-> ZTVars.fold (fn (v, T) => |
|
79146
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
486 |
(case apply typ T of |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
487 |
NONE => I |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
488 |
| SOME T' => ZTVars.update (v, T'))); |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
489 |
|
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
490 |
val vars' = |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
491 |
(inst, ZVars.empty) |-> ZVars.fold (fn ((v, T), _) => |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
492 |
(case apply typ T of |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
493 |
NONE => I |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
494 |
| SOME T' => ZVars.add ((v, T), (v, T')))); |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
495 |
|
79145 | 496 |
val inst' = |
79146
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
497 |
if ZVars.is_empty vars' then |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
498 |
(inst, inst) |-> ZVars.fold (fn (v, t) => |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
499 |
(case apply term t of |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
500 |
NONE => I |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
501 |
| SOME t' => ZVars.update (v, t'))) |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
502 |
else |
79325 | 503 |
build (inst |> ZVars.fold_rev (fn (v, t) => |
504 |
cons (the_default v (ZVars.lookup vars' v), the_default t (apply term t)))) |
|
79318 | 505 |
|> make_inst; |
79145 | 506 |
in if ! changed then (instT', inst') else raise Same.SAME end; |
507 |
||
79132 | 508 |
fun map_proof_same typ term = |
509 |
let |
|
510 |
fun proof ZDummy = raise Same.SAME |
|
79212 | 511 |
| proof (ZConstp (a, A, instT, inst)) = |
79148
99201e7b1d94
proper treatment of ZConstP: term represents body of closure;
wenzelm
parents:
79147
diff
changeset
|
512 |
let val (instT', inst') = map_insts_same typ term (instT, inst) |
79212 | 513 |
in ZConstp (a, A, instT', inst') end |
514 |
| proof (ZBoundp _) = raise Same.SAME |
|
79275
8368160d3c65
proper treatment of proof hyps: unchangeable, like bound;
wenzelm
parents:
79273
diff
changeset
|
515 |
| proof (ZHyp _) = raise Same.SAME |
79132 | 516 |
| proof (ZAbst (a, T, p)) = |
79326 | 517 |
(ZAbst (a, typ T, Same.commit proof p) |
518 |
handle Same.SAME => ZAbst (a, T, proof p)) |
|
79212 | 519 |
| proof (ZAbsp (a, t, p)) = |
79326 | 520 |
(ZAbsp (a, term t, Same.commit proof p) |
521 |
handle Same.SAME => ZAbsp (a, t, proof p)) |
|
79132 | 522 |
| proof (ZAppt (p, t)) = |
79326 | 523 |
(ZAppt (proof p, Same.commit term t) |
524 |
handle Same.SAME => ZAppt (p, term t)) |
|
79212 | 525 |
| proof (ZAppp (p, q)) = |
79326 | 526 |
(ZAppp (proof p, Same.commit proof q) |
527 |
handle Same.SAME => ZAppp (p, proof q)) |
|
79212 | 528 |
| proof (ZClassp (T, c)) = ZClassp (typ T, c); |
79132 | 529 |
in proof end; |
530 |
||
79273 | 531 |
fun map_proof typ term = Same.commit (map_proof_same typ term); |
532 |
fun map_proof_types typ = map_proof typ (subst_term_same typ Same.same); |
|
79144 | 533 |
|
79124 | 534 |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
535 |
(* convert ztyp / zterm vs. regular typ / term *) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
536 |
|
79119 | 537 |
fun ztyp_of (TFree (a, S)) = ZTVar ((a, ~1), S) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
538 |
| ztyp_of (TVar v) = ZTVar v |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
539 |
| ztyp_of (Type ("fun", [T, U])) = ZFun (ztyp_of T, ztyp_of U) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
540 |
| ztyp_of (Type (c, [])) = if c = "prop" then ZProp else ZType0 c |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
541 |
| ztyp_of (Type (c, [T])) = if c = "itself" then ZItself (ztyp_of T) else ZType1 (c, ztyp_of T) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
542 |
| ztyp_of (Type (c, ts)) = ZType (c, map ztyp_of ts); |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
543 |
|
79163 | 544 |
fun ztyp_cache () = Typtab.unsynchronized_cache ztyp_of; |
545 |
||
79226 | 546 |
fun zterm_cache thy = |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
547 |
let |
79226 | 548 |
val typargs = Consts.typargs (Sign.consts_of thy); |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
549 |
|
79163 | 550 |
val ztyp = ztyp_cache (); |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
551 |
|
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
552 |
fun zterm (Free (x, T)) = ZVar ((x, ~1), ztyp T) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
553 |
| zterm (Var (xi, T)) = ZVar (xi, ztyp T) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
554 |
| zterm (Bound i) = ZBound i |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
555 |
| zterm (Const (c, T)) = |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
556 |
(case typargs (c, T) of |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
557 |
[] => ZConst0 c |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
558 |
| [T] => ZConst1 (c, ztyp T) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
559 |
| Ts => ZConst (c, map ztyp Ts)) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
560 |
| zterm (Abs (a, T, b)) = ZAbs (a, ztyp T, zterm b) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
561 |
| zterm ((t as Const (c, _)) $ (u as Const ("Pure.type", _))) = |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
562 |
if String.isSuffix Logic.class_suffix c then |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
563 |
ZClass (ztyp (Logic.dest_type u), Logic.class_of_const c) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
564 |
else ZApp (zterm t, zterm u) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
565 |
| zterm (t $ u) = ZApp (zterm t, zterm u); |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
566 |
in {ztyp = ztyp, zterm = zterm} end; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
567 |
|
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
568 |
val zterm_of = #zterm o zterm_cache; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
569 |
|
79119 | 570 |
fun typ_of (ZTVar ((a, ~1), S)) = TFree (a, S) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
571 |
| typ_of (ZTVar v) = TVar v |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
572 |
| typ_of (ZFun (T, U)) = typ_of T --> typ_of U |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
573 |
| typ_of ZProp = propT |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
574 |
| typ_of (ZItself T) = Term.itselfT (typ_of T) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
575 |
| typ_of (ZType0 c) = Type (c, []) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
576 |
| typ_of (ZType1 (c, T)) = Type (c, [typ_of T]) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
577 |
| typ_of (ZType (c, Ts)) = Type (c, map typ_of Ts); |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
578 |
|
79201 | 579 |
fun typ_cache () = ZTypes.unsynchronized_cache typ_of; |
580 |
||
79226 | 581 |
fun term_of thy = |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
582 |
let |
79226 | 583 |
val instance = Consts.instance (Sign.consts_of thy); |
584 |
||
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
585 |
fun const (c, Ts) = Const (c, instance (c, Ts)); |
79226 | 586 |
|
79119 | 587 |
fun term (ZVar ((x, ~1), T)) = Free (x, typ_of T) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
588 |
| term (ZVar (xi, T)) = Var (xi, typ_of T) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
589 |
| term (ZBound i) = Bound i |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
590 |
| term (ZConst0 c) = const (c, []) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
591 |
| term (ZConst1 (c, T)) = const (c, [typ_of T]) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
592 |
| term (ZConst (c, Ts)) = const (c, map typ_of Ts) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
593 |
| term (ZAbs (a, T, b)) = Abs (a, typ_of T, term b) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
594 |
| term (ZApp (t, u)) = term t $ term u |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
595 |
| term (ZClass (T, c)) = Logic.mk_of_class (typ_of T, c); |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
596 |
in term end; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
597 |
|
79119 | 598 |
|
79298 | 599 |
(* beta contraction *) |
79200 | 600 |
|
79286 | 601 |
val beta_norm_term_same = |
79271 | 602 |
let |
79299 | 603 |
fun norm (ZAbs (a, T, t)) = ZAbs (a, T, Same.commit norm t) |
604 |
| norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_term_bound t body) |
|
79271 | 605 |
| norm (ZApp (f, t)) = |
606 |
((case norm f of |
|
79286 | 607 |
ZAbs (_, _, body) => Same.commit norm (subst_term_bound t body) |
79271 | 608 |
| nf => ZApp (nf, Same.commit norm t)) |
609 |
handle Same.SAME => ZApp (f, norm t)) |
|
610 |
| norm _ = raise Same.SAME; |
|
611 |
in norm end; |
|
612 |
||
79302
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
613 |
val beta_norm_prooft_same = |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
614 |
let |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
615 |
val term = beta_norm_term_same; |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
616 |
|
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
617 |
fun norm (ZAbst (a, T, p)) = ZAbst (a, T, norm p) |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
618 |
| norm (ZAppt (ZAbst (_, _, body), t)) = Same.commit norm (subst_proof_bound t body) |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
619 |
| norm (ZAppt (f, t)) = |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
620 |
((case norm f of |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
621 |
ZAbst (_, _, body) => Same.commit norm (subst_proof_bound t body) |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
622 |
| nf => ZAppt (nf, Same.commit term t)) |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
623 |
handle Same.SAME => ZAppt (f, term t)) |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
624 |
| norm _ = raise Same.SAME; |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
625 |
in norm end; |
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
626 |
|
79286 | 627 |
val beta_norm_proof_same = |
628 |
let |
|
629 |
val term = beta_norm_term_same; |
|
630 |
||
79300
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
wenzelm
parents:
79299
diff
changeset
|
631 |
fun norm (ZAbst (a, T, p)) = ZAbst (a, T, norm p) |
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
wenzelm
parents:
79299
diff
changeset
|
632 |
| norm (ZAbsp (a, t, p)) = |
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
wenzelm
parents:
79299
diff
changeset
|
633 |
(ZAbsp (a, term t, Same.commit norm p) |
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
wenzelm
parents:
79299
diff
changeset
|
634 |
handle Same.SAME => ZAbsp (a, t, norm p)) |
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
wenzelm
parents:
79299
diff
changeset
|
635 |
| norm (ZAppt (ZAbst (_, _, body), t)) = Same.commit norm (subst_proof_bound t body) |
79301 | 636 |
| norm (ZAppp (ZAbsp (_, _, body), p)) = Same.commit norm (subst_proof_boundp p body) |
79286 | 637 |
| norm (ZAppt (f, t)) = |
638 |
((case norm f of |
|
639 |
ZAbst (_, _, body) => Same.commit norm (subst_proof_bound t body) |
|
640 |
| nf => ZAppt (nf, Same.commit term t)) |
|
641 |
handle Same.SAME => ZAppt (f, term t)) |
|
642 |
| norm (ZAppp (f, p)) = |
|
643 |
((case norm f of |
|
644 |
ZAbsp (_, _, body) => Same.commit norm (subst_proof_boundp p body) |
|
645 |
| nf => ZAppp (nf, Same.commit norm p)) |
|
646 |
handle Same.SAME => ZAppp (f, norm p)) |
|
647 |
| norm _ = raise Same.SAME; |
|
648 |
in norm end; |
|
649 |
||
79298 | 650 |
val beta_norm_term = Same.commit beta_norm_term_same; |
651 |
val beta_norm_proof = Same.commit beta_norm_proof_same; |
|
79302
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
652 |
val beta_norm_prooft = Same.commit beta_norm_prooft_same; |
79298 | 653 |
|
654 |
||
655 |
(* normalization wrt. environment and beta contraction *) |
|
656 |
||
79200 | 657 |
fun norm_type_same ztyp tyenv = |
658 |
if Vartab.is_empty tyenv then Same.same |
|
659 |
else |
|
660 |
let |
|
661 |
fun norm (ZTVar v) = |
|
662 |
(case Type.lookup tyenv v of |
|
663 |
SOME U => Same.commit norm (ztyp U) |
|
664 |
| NONE => raise Same.SAME) |
|
665 |
| norm (ZFun (T, U)) = |
|
666 |
(ZFun (norm T, Same.commit norm U) |
|
667 |
handle Same.SAME => ZFun (T, norm U)) |
|
668 |
| norm ZProp = raise Same.SAME |
|
669 |
| norm (ZItself T) = ZItself (norm T) |
|
670 |
| norm (ZType0 _) = raise Same.SAME |
|
671 |
| norm (ZType1 (a, T)) = ZType1 (a, norm T) |
|
672 |
| norm (ZType (a, Ts)) = ZType (a, Same.map norm Ts); |
|
673 |
in norm end; |
|
674 |
||
79269 | 675 |
fun norm_term_same {ztyp, zterm, typ} (envir as Envir.Envir {tyenv, tenv, ...}) = |
676 |
let |
|
677 |
val lookup = |
|
678 |
if Vartab.is_empty tenv then K NONE |
|
679 |
else ZVars.unsynchronized_cache (apsnd typ #> Envir.lookup envir #> Option.map zterm); |
|
79200 | 680 |
|
79269 | 681 |
val normT = norm_type_same ztyp tyenv; |
79200 | 682 |
|
79269 | 683 |
fun norm (ZVar (xi, T)) = |
684 |
(case lookup (xi, T) of |
|
685 |
SOME u => Same.commit norm u |
|
686 |
| NONE => ZVar (xi, normT T)) |
|
687 |
| norm (ZBound _) = raise Same.SAME |
|
688 |
| norm (ZConst0 _) = raise Same.SAME |
|
689 |
| norm (ZConst1 (a, T)) = ZConst1 (a, normT T) |
|
690 |
| norm (ZConst (a, Ts)) = ZConst (a, Same.map normT Ts) |
|
691 |
| norm (ZAbs (a, T, t)) = |
|
692 |
(ZAbs (a, normT T, Same.commit norm t) |
|
693 |
handle Same.SAME => ZAbs (a, T, norm t)) |
|
79281
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
694 |
| norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_term_bound t body) |
79269 | 695 |
| norm (ZApp (f, t)) = |
696 |
((case norm f of |
|
79281
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
697 |
ZAbs (_, _, body) => Same.commit norm (subst_term_bound t body) |
79269 | 698 |
| nf => ZApp (nf, Same.commit norm t)) |
699 |
handle Same.SAME => ZApp (f, norm t)) |
|
700 |
| norm _ = raise Same.SAME; |
|
79286 | 701 |
in fn t => if Envir.is_empty envir then beta_norm_term_same t else norm t end; |
79200 | 702 |
|
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
703 |
fun norm_proof_cache (cache as {ztyp, ...}) (envir as Envir.Envir {tyenv, ...}) = |
79269 | 704 |
let |
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
705 |
val norm_tvar = ZTVar #> Same.commit (norm_type_same ztyp tyenv); |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
706 |
val norm_var = ZVar #> Same.commit (norm_term_same cache envir); |
79269 | 707 |
in |
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
708 |
fn visible => fn prf => |
79302
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
709 |
if Envir.is_empty envir orelse null visible then beta_norm_prooft prf |
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
710 |
else |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
711 |
let |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
712 |
fun add_tvar v tab = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
713 |
if ZTVars.defined tab v then tab else ZTVars.update (v, norm_tvar v) tab; |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
714 |
|
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
715 |
val inst_typ = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
716 |
if Vartab.is_empty tyenv then Same.same |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
717 |
else |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
718 |
ZTVars.build (visible |> (fold o Term.fold_types o Term.fold_atyps) |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
719 |
(fn TVar v => add_tvar v | _ => I)) |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
720 |
|> instantiate_type_same; |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
721 |
|
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
722 |
fun add_var (a, T) tab = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
723 |
let val v = (a, Same.commit inst_typ (ztyp T)) |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
724 |
in if ZVars.defined tab v then tab else ZVars.update (v, norm_var v) tab end; |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
725 |
|
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
726 |
val inst_term = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
727 |
ZVars.build (visible |> (fold o Term.fold_aterms) (fn Var v => add_var v | _ => I)) |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
728 |
|> instantiate_term_same inst_typ; |
79272
899f37f6d218
proper beta_norm after instantiation (amending 90c5aadcc4b2);
wenzelm
parents:
79271
diff
changeset
|
729 |
|
79286 | 730 |
val norm_term = Same.compose beta_norm_term_same inst_term; |
79302
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
731 |
in beta_norm_prooft (map_proof inst_typ norm_term prf) end |
79269 | 732 |
end; |
79210 | 733 |
|
734 |
fun norm_cache thy = |
|
79205 | 735 |
let |
79226 | 736 |
val {ztyp, zterm} = zterm_cache thy; |
79205 | 737 |
val typ = typ_cache (); |
738 |
in {ztyp = ztyp, zterm = zterm, typ = typ} end; |
|
739 |
||
79214 | 740 |
fun norm_type tyenv = Same.commit (norm_type_same (ztyp_cache ()) tyenv); |
741 |
fun norm_term thy envir = Same.commit (norm_term_same (norm_cache thy) envir); |
|
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
742 |
fun norm_proof thy envir = norm_proof_cache (norm_cache thy) envir; |
79200 | 743 |
|
744 |
||
79119 | 745 |
|
746 |
(** proof construction **) |
|
79113
5109e4b2a292
pro-forma support for optional zproof: no proper content yet;
wenzelm
parents:
79098
diff
changeset
|
747 |
|
79161 | 748 |
(* constants *) |
79124 | 749 |
|
79263 | 750 |
type zproof_const = zproof_name * zterm * ztyp ZTVars.table * zterm ZVars.table; |
751 |
||
752 |
fun zproof_const a A : zproof_const = |
|
79119 | 753 |
let |
79154 | 754 |
val instT = |
79263 | 755 |
ZTVars.build (A |> (fold_types o fold_tvars) (fn v => fn tab => |
79154 | 756 |
if ZTVars.defined tab v then tab else ZTVars.update (v, ZTVar v) tab)); |
757 |
val inst = |
|
79263 | 758 |
ZVars.build (A |> fold_aterms (fn t => fn tab => |
759 |
(case t of |
|
760 |
ZVar v => if ZVars.defined tab v then tab else ZVars.update (v, t) tab |
|
79154 | 761 |
| _ => tab))); |
79263 | 762 |
in (a, A, instT, inst) end; |
79126 | 763 |
|
79315 | 764 |
fun make_const_proof (f, g) (a, A, instT, inst) = |
79263 | 765 |
let |
766 |
val instT' = ZTVars.map (fn ((x, _), _) => fn y => the_default y (try f x)) instT; |
|
767 |
val inst' = ZVars.map (fn ((x, _), _) => fn y => the_default y (try g x)) inst; |
|
768 |
in ZConstp (a, A, instT', inst') end; |
|
79161 | 769 |
|
770 |
||
79265 | 771 |
(* closed proof boxes *) |
772 |
||
79311
e4a9a861856b
omit pointless future: proof terms are built sequentially;
wenzelm
parents:
79303
diff
changeset
|
773 |
type zbox = serial * (zterm * zproof); |
79279 | 774 |
val zbox_ord: zbox ord = fn ((i, _), (j, _)) => int_ord (j, i); |
79265 | 775 |
|
79279 | 776 |
type zboxes = zbox Ord_List.T; |
777 |
val union_zboxes = Ord_List.union zbox_ord; |
|
778 |
val unions_zboxes = Ord_List.unions zbox_ord; |
|
779 |
val add_zboxes = Ord_List.insert zbox_ord; |
|
79265 | 780 |
|
781 |
local |
|
782 |
||
783 |
fun close_prop hyps concl = |
|
784 |
fold_rev (fn A => fn B => ZApp (ZApp (ZConst0 "Pure.imp", A), B)) hyps concl; |
|
785 |
||
786 |
fun close_proof hyps prf = |
|
787 |
let |
|
788 |
val m = length hyps - 1; |
|
789 |
val bounds = ZTerms.build (hyps |> fold_index (fn (i, h) => ZTerms.update (h, m - i))); |
|
790 |
||
791 |
fun proof lev (ZHyp t) = |
|
792 |
(case ZTerms.lookup bounds t of |
|
793 |
SOME i => ZBoundp (lev + i) |
|
794 |
| NONE => raise ZTERM ("Unbound proof hypothesis", [], [t], [])) |
|
795 |
| proof lev (ZAbst (x, T, p)) = ZAbst (x, T, proof lev p) |
|
796 |
| proof lev (ZAbsp (x, t, p)) = ZAbsp (x, t, proof (lev + 1) p) |
|
797 |
| proof lev (ZAppt (p, t)) = ZAppt (proof lev p, t) |
|
798 |
| proof lev (ZAppp (p, q)) = |
|
79326 | 799 |
(ZAppp (proof lev p, Same.commit (proof lev) q) |
800 |
handle Same.SAME => ZAppp (p, proof lev q)) |
|
79265 | 801 |
| proof _ _ = raise Same.SAME; |
802 |
in ZAbsps hyps (Same.commit (proof 0) prf) end; |
|
803 |
||
79313
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
804 |
fun box_proof zproof_name thy hyps concl prf = |
79265 | 805 |
let |
806 |
val {zterm, ...} = zterm_cache thy; |
|
807 |
val hyps' = map zterm hyps; |
|
808 |
val concl' = zterm concl; |
|
809 |
||
79303 | 810 |
val prop' = beta_norm_term (close_prop hyps' concl'); |
811 |
val prf' = beta_norm_prooft (close_proof hyps' prf); |
|
79265 | 812 |
|
813 |
val i = serial (); |
|
79312 | 814 |
val zbox: zbox = (i, (prop', prf')); |
79313
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
815 |
val zbox_prf = ZAppts (ZConstp (zproof_const (zproof_name i) prop'), hyps'); |
79312 | 816 |
in (zbox, zbox_prf) end; |
817 |
||
79313
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
818 |
in |
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
819 |
|
79329 | 820 |
fun thm_proof thy (name, pos) = |
821 |
let |
|
822 |
val thy_name = Context.theory_long_name thy; |
|
79360 | 823 |
fun zproof_name i = ZThm {theory_name = thy_name, thm_name = name, pos = pos, serial = i}; |
79329 | 824 |
in box_proof zproof_name thy end; |
79313
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
825 |
|
79312 | 826 |
fun add_box_proof thy hyps concl (zboxes, prf) = |
79313
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
827 |
let val (zbox, zbox_prf) = box_proof ZBox thy hyps concl prf |
79312 | 828 |
in (add_zboxes zbox zboxes, zbox_prf) end; |
79265 | 829 |
|
830 |
end; |
|
831 |
||
832 |
||
79161 | 833 |
(* basic logic *) |
834 |
||
79263 | 835 |
fun axiom_proof thy name A = |
836 |
ZConstp (zproof_const (ZAxiom name) (zterm_of thy A)); |
|
79161 | 837 |
|
79263 | 838 |
fun oracle_proof thy name A = |
839 |
ZConstp (zproof_const (ZOracle name) (zterm_of thy A)); |
|
79119 | 840 |
|
841 |
fun assume_proof thy A = |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
842 |
ZHyp (zterm_of thy A); |
79119 | 843 |
|
844 |
fun trivial_proof thy A = |
|
79212 | 845 |
ZAbsp ("H", zterm_of thy A, ZBoundp 0); |
79119 | 846 |
|
847 |
fun implies_intr_proof thy A prf = |
|
848 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
849 |
val h = zterm_of thy A; |
79277 | 850 |
fun proof lev (ZHyp t) = if aconv_zterm (h, t) then ZBoundp lev else raise Same.SAME |
851 |
| proof lev (ZAbst (x, T, p)) = ZAbst (x, T, proof lev p) |
|
852 |
| proof lev (ZAbsp (x, t, p)) = ZAbsp (x, t, proof (lev + 1) p) |
|
853 |
| proof lev (ZAppt (p, t)) = ZAppt (proof lev p, t) |
|
854 |
| proof lev (ZAppp (p, q)) = |
|
79326 | 855 |
(ZAppp (proof lev p, Same.commit (proof lev) q) |
856 |
handle Same.SAME => ZAppp (p, proof lev q)) |
|
79157 | 857 |
| proof _ _ = raise Same.SAME; |
79212 | 858 |
in ZAbsp ("H", h, Same.commit (proof 0) prf) end; |
79119 | 859 |
|
79124 | 860 |
fun forall_intr_proof thy T (a, x) prf = |
79119 | 861 |
let |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
862 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
863 |
val Z = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
864 |
val z = zterm x; |
79119 | 865 |
|
79157 | 866 |
fun term i b = |
79119 | 867 |
if aconv_zterm (b, z) then ZBound i |
868 |
else |
|
869 |
(case b of |
|
79157 | 870 |
ZAbs (x, T, t) => ZAbs (x, T, term (i + 1) t) |
79156 | 871 |
| ZApp (t, u) => |
79326 | 872 |
(ZApp (term i t, Same.commit (term i) u) |
873 |
handle Same.SAME => ZApp (t, term i u)) |
|
79156 | 874 |
| _ => raise Same.SAME); |
79119 | 875 |
|
79157 | 876 |
fun proof i (ZAbst (x, T, prf)) = ZAbst (x, T, proof (i + 1) prf) |
79212 | 877 |
| proof i (ZAbsp (x, t, prf)) = |
79326 | 878 |
(ZAbsp (x, term i t, Same.commit (proof i) prf) |
879 |
handle Same.SAME => ZAbsp (x, t, proof i prf)) |
|
79157 | 880 |
| proof i (ZAppt (p, t)) = |
79326 | 881 |
(ZAppt (proof i p, Same.commit (term i) t) |
882 |
handle Same.SAME => ZAppt (p, term i t)) |
|
79212 | 883 |
| proof i (ZAppp (p, q)) = |
79326 | 884 |
(ZAppp (proof i p, Same.commit (proof i) q) |
885 |
handle Same.SAME => ZAppp (p, proof i q)) |
|
79157 | 886 |
| proof _ _ = raise Same.SAME; |
79119 | 887 |
|
79157 | 888 |
in ZAbst (a, Z, Same.commit (proof 0) prf) end; |
79119 | 889 |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
890 |
fun forall_elim_proof thy t p = ZAppt (p, zterm_of thy t); |
79119 | 891 |
|
79212 | 892 |
fun of_class_proof (T, c) = ZClassp (ztyp_of T, c); |
79128 | 893 |
|
79124 | 894 |
|
895 |
(* equality *) |
|
896 |
||
897 |
local |
|
898 |
||
899 |
val thy0 = |
|
900 |
Context.the_global_context () |
|
901 |
|> Sign.add_types_global [(Binding.name "fun", 2, NoSyn), (Binding.name "prop", 0, NoSyn)] |
|
902 |
|> Sign.local_path |
|
903 |
|> Sign.add_consts |
|
904 |
[(Binding.name "all", (Term.aT [] --> propT) --> propT, NoSyn), |
|
905 |
(Binding.name "imp", propT --> propT --> propT, NoSyn), |
|
906 |
(Binding.name "eq", Term.aT [] --> Term.aT [] --> propT, NoSyn)]; |
|
907 |
||
908 |
val [reflexive_axiom, symmetric_axiom, transitive_axiom, equal_intr_axiom, equal_elim_axiom, |
|
909 |
abstract_rule_axiom, combination_axiom] = |
|
79263 | 910 |
Theory.equality_axioms |> map (fn (b, t) => |
911 |
let val ZConstp c = axiom_proof thy0 (Sign.full_name thy0 b) t in c end); |
|
79124 | 912 |
|
913 |
in |
|
914 |
||
915 |
val is_reflexive_proof = |
|
79212 | 916 |
fn ZConstp (ZAxiom "Pure.reflexive", _, _, _) => true | _ => false; |
79124 | 917 |
|
918 |
fun reflexive_proof thy T t = |
|
919 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
920 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
921 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
922 |
val x = zterm t; |
79263 | 923 |
in make_const_proof (fn "'a" => A, fn "x" => x) reflexive_axiom end; |
79124 | 924 |
|
925 |
fun symmetric_proof thy T t u prf = |
|
926 |
if is_reflexive_proof prf then prf |
|
927 |
else |
|
928 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
929 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
930 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
931 |
val x = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
932 |
val y = zterm u; |
79263 | 933 |
val ax = make_const_proof (fn "'a" => A, fn "x" => x | "y" => y) symmetric_axiom; |
79212 | 934 |
in ZAppp (ax, prf) end; |
79124 | 935 |
|
936 |
fun transitive_proof thy T t u v prf1 prf2 = |
|
937 |
if is_reflexive_proof prf1 then prf2 |
|
938 |
else if is_reflexive_proof prf2 then prf1 |
|
939 |
else |
|
940 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
941 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
942 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
943 |
val x = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
944 |
val y = zterm u; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
945 |
val z = zterm v; |
79263 | 946 |
val ax = make_const_proof (fn "'a" => A, fn "x" => x | "y" => y | "z" => z) transitive_axiom; |
79212 | 947 |
in ZAppp (ZAppp (ax, prf1), prf2) end; |
79124 | 948 |
|
949 |
fun equal_intr_proof thy t u prf1 prf2 = |
|
950 |
let |
|
79160 | 951 |
val {zterm, ...} = zterm_cache thy; |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
952 |
val A = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
953 |
val B = zterm u; |
79263 | 954 |
val ax = make_const_proof (undefined, fn "A" => A | "B" => B) equal_intr_axiom; |
79212 | 955 |
in ZAppp (ZAppp (ax, prf1), prf2) end; |
79124 | 956 |
|
957 |
fun equal_elim_proof thy t u prf1 prf2 = |
|
958 |
let |
|
79160 | 959 |
val {zterm, ...} = zterm_cache thy; |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
960 |
val A = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
961 |
val B = zterm u; |
79263 | 962 |
val ax = make_const_proof (undefined, fn "A" => A | "B" => B) equal_elim_axiom; |
79212 | 963 |
in ZAppp (ZAppp (ax, prf1), prf2) end; |
79124 | 964 |
|
965 |
fun abstract_rule_proof thy T U x t u prf = |
|
966 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
967 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
968 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
969 |
val B = ztyp U; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
970 |
val f = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
971 |
val g = zterm u; |
79126 | 972 |
val ax = |
79263 | 973 |
make_const_proof (fn "'a" => A | "'b" => B, fn "f" => f | "g" => g) |
79126 | 974 |
abstract_rule_axiom; |
79212 | 975 |
in ZAppp (ax, forall_intr_proof thy T x prf) end; |
79124 | 976 |
|
977 |
fun combination_proof thy T U f g t u prf1 prf2 = |
|
978 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
979 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
980 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
981 |
val B = ztyp U; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
982 |
val f' = zterm f; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
983 |
val g' = zterm g; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
984 |
val x = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
985 |
val y = zterm u; |
79124 | 986 |
val ax = |
79263 | 987 |
make_const_proof (fn "'a" => A | "'b" => B, fn "f" => f' | "g" => g' | "x" => x | "y" => y) |
79124 | 988 |
combination_axiom; |
79212 | 989 |
in ZAppp (ZAppp (ax, prf1), prf2) end; |
79124 | 990 |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
991 |
end; |
79124 | 992 |
|
79133 | 993 |
|
994 |
(* substitution *) |
|
995 |
||
996 |
fun generalize_proof (tfrees, frees) idx prf = |
|
997 |
let |
|
998 |
val typ = |
|
79326 | 999 |
if Names.is_empty tfrees then Same.same |
1000 |
else |
|
79163 | 1001 |
ZTypes.unsynchronized_cache |
1002 |
(subst_type_same (fn ((a, i), S) => |
|
1003 |
if i = ~1 andalso Names.defined tfrees a then ZTVar ((a, idx), S) |
|
1004 |
else raise Same.SAME)); |
|
79136 | 1005 |
val term = |
79147 | 1006 |
subst_term_same typ (fn ((x, i), T) => |
1007 |
if i = ~1 andalso Names.defined frees x then ZVar ((x, idx), T) |
|
1008 |
else raise Same.SAME); |
|
79273 | 1009 |
in map_proof typ term prf end; |
79133 | 1010 |
|
79149 | 1011 |
fun instantiate_proof thy (Ts, ts) prf = |
1012 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1013 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1014 |
val instT = ZTVars.build (Ts |> fold (fn (v, T) => ZTVars.add (v, ztyp T))); |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1015 |
val inst = ZVars.build (ts |> fold (fn ((v, T), t) => ZVars.add ((v, ztyp T), zterm t))); |
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
1016 |
val typ = instantiate_type_same instT; |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
1017 |
val term = instantiate_term_same typ inst; |
79273 | 1018 |
in map_proof typ term prf end; |
79149 | 1019 |
|
79135 | 1020 |
fun varifyT_proof names prf = |
1021 |
if null names then prf |
|
1022 |
else |
|
1023 |
let |
|
1024 |
val tab = ZTVars.build (names |> fold (fn ((a, S), b) => ZTVars.add (((a, ~1), S), b))); |
|
79136 | 1025 |
val typ = |
79163 | 1026 |
ZTypes.unsynchronized_cache (subst_type_same (fn v => |
79136 | 1027 |
(case ZTVars.lookup tab v of |
1028 |
NONE => raise Same.SAME |
|
79163 | 1029 |
| SOME w => ZTVar w))); |
79273 | 1030 |
in map_proof_types typ prf end; |
79135 | 1031 |
|
79152 | 1032 |
fun legacy_freezeT_proof t prf = |
1033 |
(case Type.legacy_freezeT t of |
|
1034 |
NONE => prf |
|
1035 |
| SOME f => |
|
1036 |
let |
|
1037 |
val tvar = ztyp_of o Same.function f; |
|
79163 | 1038 |
val typ = ZTypes.unsynchronized_cache (subst_type_same tvar); |
79273 | 1039 |
in map_proof_types typ prf end); |
79152 | 1040 |
|
79155 | 1041 |
|
1042 |
(* permutations *) |
|
1043 |
||
1044 |
fun rotate_proof thy Bs Bi' params asms m prf = |
|
1045 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1046 |
val {ztyp, zterm} = zterm_cache thy; |
79155 | 1047 |
val i = length asms; |
1048 |
val j = length Bs; |
|
1049 |
in |
|
79212 | 1050 |
ZAbsps (map zterm Bs @ [zterm Bi']) (ZAppps (prf, map ZBoundp |
1051 |
(j downto 1) @ [ZAbsts (map (apsnd ztyp) params) (ZAbsps (map zterm asms) |
|
1052 |
(ZAppps (ZAppts (ZBoundp i, map ZBound ((length params - 1) downto 0)), |
|
79326 | 1053 |
map ZBoundp (((i - m - 1) downto 0) @ ((i - 1) downto (i - m))))))])) |
79155 | 1054 |
end; |
1055 |
||
1056 |
fun permute_prems_proof thy prems' j k prf = |
|
1057 |
let |
|
79160 | 1058 |
val {zterm, ...} = zterm_cache thy; |
79155 | 1059 |
val n = length prems'; |
1060 |
in |
|
79212 | 1061 |
ZAbsps (map zterm prems') |
79326 | 1062 |
(ZAppps (prf, map ZBoundp ((n - 1 downto n - j) @ (k - 1 downto 0) @ (n - j - 1 downto k)))) |
79155 | 1063 |
end; |
1064 |
||
79211 | 1065 |
|
79234 | 1066 |
(* lifting *) |
1067 |
||
1068 |
fun incr_tvar_same inc = |
|
1069 |
if inc = 0 then Same.same |
|
1070 |
else |
|
1071 |
let fun tvar ((a, i), S) = if i >= 0 then ZTVar ((a, i + inc), S) else raise Same.SAME |
|
1072 |
in ZTypes.unsynchronized_cache (subst_type_same tvar) end; |
|
1073 |
||
1074 |
fun incr_indexes_proof inc prf = |
|
1075 |
let |
|
1076 |
val typ = incr_tvar_same inc; |
|
1077 |
fun var ((x, i), T) = if i >= 0 then ZVar ((x, i + inc), T) else raise Same.SAME; |
|
1078 |
val term = subst_term_same typ var; |
|
79273 | 1079 |
in map_proof typ term prf end; |
79234 | 1080 |
|
1081 |
fun lift_proof thy gprop inc prems prf = |
|
1082 |
let |
|
1083 |
val {ztyp, zterm} = zterm_cache thy; |
|
1084 |
||
1085 |
val typ = incr_tvar_same inc; |
|
1086 |
||
1087 |
fun term Ts lev = |
|
1088 |
if null Ts andalso inc = 0 then Same.same |
|
1089 |
else |
|
1090 |
let |
|
1091 |
val n = length Ts; |
|
1092 |
fun incr lev (ZVar ((x, i), T)) = |
|
1093 |
if i >= 0 then combound (ZVar ((x, i + inc), ZFuns (Ts, Same.commit typ T)), lev, n) |
|
1094 |
else raise Same.SAME |
|
1095 |
| incr _ (ZBound _) = raise Same.SAME |
|
1096 |
| incr _ (ZConst0 _) = raise Same.SAME |
|
1097 |
| incr _ (ZConst1 (c, T)) = ZConst1 (c, typ T) |
|
1098 |
| incr _ (ZConst (c, Ts)) = ZConst (c, Same.map typ Ts) |
|
1099 |
| incr lev (ZAbs (x, T, t)) = |
|
1100 |
(ZAbs (x, typ T, Same.commit (incr (lev + 1)) t) |
|
1101 |
handle Same.SAME => ZAbs (x, T, incr (lev + 1) t)) |
|
1102 |
| incr lev (ZApp (t, u)) = |
|
1103 |
(ZApp (incr lev t, Same.commit (incr lev) u) |
|
1104 |
handle Same.SAME => ZApp (t, incr lev u)) |
|
1105 |
| incr _ (ZClass (T, c)) = ZClass (typ T, c); |
|
1106 |
in incr lev end; |
|
1107 |
||
1108 |
fun proof Ts lev (ZAbst (a, T, p)) = |
|
1109 |
(ZAbst (a, typ T, Same.commit (proof Ts (lev + 1)) p) |
|
1110 |
handle Same.SAME => ZAbst (a, T, proof Ts (lev + 1) p)) |
|
1111 |
| proof Ts lev (ZAbsp (a, t, p)) = |
|
1112 |
(ZAbsp (a, term Ts lev t, Same.commit (proof Ts lev) p) |
|
1113 |
handle Same.SAME => ZAbsp (a, t, proof Ts lev p)) |
|
1114 |
| proof Ts lev (ZAppt (p, t)) = |
|
1115 |
(ZAppt (proof Ts lev p, Same.commit (term Ts lev) t) |
|
1116 |
handle Same.SAME => ZAppt (p, term Ts lev t)) |
|
1117 |
| proof Ts lev (ZAppp (p, q)) = |
|
1118 |
(ZAppp (proof Ts lev p, Same.commit (proof Ts lev) q) |
|
1119 |
handle Same.SAME => ZAppp (p, proof Ts lev q)) |
|
1120 |
| proof Ts lev (ZConstp (a, A, instT, inst)) = |
|
1121 |
let val (instT', insts') = map_insts_same typ (term Ts lev) (instT, inst) |
|
1122 |
in ZConstp (a, A, instT', insts') end |
|
1123 |
| proof _ _ (ZClassp (T, c)) = ZClassp (typ T, c) |
|
1124 |
| proof _ _ _ = raise Same.SAME; |
|
1125 |
||
1126 |
val k = length prems; |
|
1127 |
||
1128 |
fun mk_app b (i, j, p) = |
|
1129 |
if b then (i - 1, j, ZAppp (p, ZBoundp i)) else (i, j - 1, ZAppt (p, ZBound j)); |
|
1130 |
||
1131 |
fun lift Ts bs i j (Const ("Pure.imp", _) $ A $ B) = |
|
1132 |
ZAbsp ("H", Same.commit (term Ts 0) (zterm A), lift Ts (true :: bs) (i + 1) j B) |
|
1133 |
| lift Ts bs i j (Const ("Pure.all", _) $ Abs (a, T, t)) = |
|
1134 |
let val T' = ztyp T |
|
1135 |
in ZAbst (a, T', lift (T' :: Ts) (false :: bs) i (j + 1) t) end |
|
1136 |
| lift Ts bs i j _ = |
|
1137 |
ZAppps (Same.commit (proof (rev Ts) 0) prf, |
|
1138 |
map (fn k => (#3 (fold_rev mk_app bs (i - 1, j - 1, ZBoundp k)))) (i + k - 1 downto i)); |
|
79237 | 1139 |
in ZAbsps (map zterm prems) (lift [] [] 0 0 gprop) end; |
79234 | 1140 |
|
1141 |
||
79211 | 1142 |
(* higher-order resolution *) |
1143 |
||
1144 |
fun mk_asm_prf C i m = |
|
1145 |
let |
|
79212 | 1146 |
fun imp _ i 0 = ZBoundp i |
1147 |
| imp (ZApp (ZApp (ZConst0 "Pure.imp", A), B)) i m = ZAbsp ("H", A, imp B (i + 1) (m - 1)) |
|
1148 |
| imp _ i _ = ZBoundp i; |
|
79211 | 1149 |
fun all (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t))) = ZAbst (a, T, all t) |
1150 |
| all t = imp t (~ i) m |
|
1151 |
in all C end; |
|
1152 |
||
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
1153 |
fun assumption_proof thy envir Bs Bi n visible prf = |
79211 | 1154 |
let |
1155 |
val cache as {zterm, ...} = norm_cache thy; |
|
1156 |
val normt = zterm #> Same.commit (norm_term_same cache envir); |
|
1157 |
in |
|
79212 | 1158 |
ZAbsps (map normt Bs) |
1159 |
(ZAppps (prf, map ZBoundp (length Bs - 1 downto 0) @ [mk_asm_prf (normt Bi) n ~1])) |
|
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
1160 |
|> norm_proof_cache cache envir visible |
79211 | 1161 |
end; |
1162 |
||
79261 | 1163 |
fun flatten_params_proof i j n (ZApp (ZApp (ZConst0 "Pure.imp", A), B), k) = |
1164 |
ZAbsp ("H", A, flatten_params_proof (i + 1) j n (B, k)) |
|
1165 |
| flatten_params_proof i j n (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t)), k) = |
|
1166 |
ZAbst (a, T, flatten_params_proof i (j + 1) n (t, k)) |
|
1167 |
| flatten_params_proof i j n (_, k) = |
|
1168 |
ZAppps (ZAppts (ZBoundp (k + i), |
|
1169 |
map ZBound (j - 1 downto 0)), map ZBoundp (remove (op =) (i - n) (i - 1 downto 0))); |
|
1170 |
||
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
1171 |
fun bicompose_proof thy env smax flatten Bs As A oldAs n m visible rprf sprf = |
79261 | 1172 |
let |
1173 |
val cache as {zterm, ...} = norm_cache thy; |
|
1174 |
val normt = zterm #> Same.commit (norm_term_same cache env); |
|
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
1175 |
val normp = norm_proof_cache cache env visible; |
79261 | 1176 |
|
1177 |
val lb = length Bs; |
|
1178 |
val la = length As; |
|
1179 |
||
1180 |
fun proof p q = |
|
1181 |
ZAbsps (map normt (Bs @ As)) |
|
1182 |
(ZAppp (ZAppps (q, map ZBoundp (lb + la - 1 downto la)), |
|
1183 |
ZAppps (p, (if n > 0 then [mk_asm_prf (normt (the A)) n m] else []) @ |
|
1184 |
map (if flatten then flatten_params_proof 0 0 n else ZBoundp o snd) |
|
1185 |
(map normt oldAs ~~ (la - 1 downto 0))))); |
|
1186 |
in |
|
1187 |
if Envir.is_empty env then proof rprf sprf |
|
1188 |
else proof (normp rprf) (if Envir.above env smax then sprf else normp sprf) |
|
1189 |
end; |
|
1190 |
||
79124 | 1191 |
end; |