author | wenzelm |
Wed, 14 Aug 2024 18:59:49 +0200 | |
changeset 80708 | 3f2c371a3de9 |
parent 80608 | 0b8922e351a3 |
child 81540 | 58073f3d748a |
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 |
| ZType0 of string (*type constant*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
16 |
| ZType1 of string * ztyp (*type constructor: 1 argument*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
17 |
| ZType of string * ztyp list (*type constructor: >= 2 arguments*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
18 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
19 |
datatype zterm = |
79119 | 20 |
ZVar of indexname * ztyp (*free: index ~1*) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
21 |
| ZBound of int |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
22 |
| ZConst0 of string (*monomorphic constant*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
23 |
| ZConst1 of string * ztyp (*polymorphic constant: 1 type argument*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
24 |
| ZConst of string * ztyp list (*polymorphic constant: >= 2 type arguments*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
25 |
| ZAbs of string * ztyp * zterm |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
26 |
| ZApp of zterm * zterm |
80293 | 27 |
| OFCLASS of ztyp * class |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
28 |
|
79124 | 29 |
structure ZTerm = |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
30 |
struct |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
31 |
|
79119 | 32 |
(* fold *) |
33 |
||
34 |
fun fold_tvars f (ZTVar v) = f v |
|
35 |
| fold_tvars f (ZFun (T, U)) = fold_tvars f T #> fold_tvars f U |
|
36 |
| fold_tvars f (ZType1 (_, T)) = fold_tvars f T |
|
37 |
| fold_tvars f (ZType (_, Ts)) = fold (fold_tvars f) Ts |
|
38 |
| fold_tvars _ _ = I; |
|
39 |
||
40 |
fun fold_aterms f (ZApp (t, u)) = fold_aterms f t #> fold_aterms f u |
|
41 |
| fold_aterms f (ZAbs (_, _, t)) = fold_aterms f t |
|
42 |
| fold_aterms f a = f a; |
|
43 |
||
80266 | 44 |
fun fold_vars f = fold_aterms (fn ZVar v => f v | _ => I); |
45 |
||
79119 | 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 |
|
80293 | 51 |
| fold_types f (OFCLASS (T, _)) = f T |
79119 | 52 |
| fold_types _ _ = I; |
53 |
||
54 |
||
80598 | 55 |
(* map *) |
56 |
||
57 |
fun map_tvars_same f = |
|
58 |
let |
|
59 |
fun typ (ZTVar v) = f v |
|
60 |
| typ (ZFun (T, U)) = |
|
61 |
(ZFun (typ T, Same.commit typ U) |
|
62 |
handle Same.SAME => ZFun (T, typ U)) |
|
63 |
| typ ZProp = raise Same.SAME |
|
64 |
| typ (ZType0 _) = raise Same.SAME |
|
65 |
| typ (ZType1 (a, T)) = ZType1 (a, typ T) |
|
66 |
| typ (ZType (a, Ts)) = ZType (a, Same.map typ Ts); |
|
67 |
in typ end; |
|
68 |
||
69 |
fun map_aterms_same f = |
|
70 |
let |
|
71 |
fun term (ZAbs (x, T, t)) = ZAbs (x, T, term t) |
|
72 |
| term (ZApp (t, u)) = |
|
73 |
(ZApp (term t, Same.commit term u) |
|
74 |
handle Same.SAME => ZApp (t, term u)) |
|
75 |
| term a = f a; |
|
76 |
in term end; |
|
77 |
||
80603 | 78 |
fun map_vars_same f = map_aterms_same (fn ZVar v => f v | _ => raise Same.SAME); |
79 |
||
80598 | 80 |
fun map_types_same f = |
81 |
let |
|
82 |
fun term (ZVar (xi, T)) = ZVar (xi, f T) |
|
83 |
| term (ZBound _) = raise Same.SAME |
|
84 |
| term (ZConst0 _) = raise Same.SAME |
|
85 |
| term (ZConst1 (c, T)) = ZConst1 (c, f T) |
|
86 |
| term (ZConst (c, Ts)) = ZConst (c, Same.map f Ts) |
|
87 |
| term (ZAbs (x, T, t)) = |
|
88 |
(ZAbs (x, f T, Same.commit term t) |
|
89 |
handle Same.SAME => ZAbs (x, T, term t)) |
|
90 |
| term (ZApp (t, u)) = |
|
91 |
(ZApp (term t, Same.commit term u) |
|
92 |
handle Same.SAME => ZApp (t, term u)) |
|
93 |
| term (OFCLASS (T, c)) = OFCLASS (f T, c); |
|
94 |
in term end; |
|
95 |
||
96 |
val map_tvars = Same.commit o map_tvars_same; |
|
97 |
val map_aterms = Same.commit o map_aterms_same; |
|
80603 | 98 |
val map_vars = Same.commit o map_vars_same; |
80598 | 99 |
val map_types = Same.commit o map_types_same; |
100 |
||
101 |
||
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
102 |
(* type ordering *) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
103 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
104 |
local |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
105 |
|
79119 | 106 |
fun cons_nr (ZTVar _) = 0 |
107 |
| cons_nr (ZFun _) = 1 |
|
108 |
| cons_nr ZProp = 2 |
|
79420
4c3346b4b100
clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
wenzelm
parents:
79419
diff
changeset
|
109 |
| cons_nr (ZType0 _) = 3 |
4c3346b4b100
clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
wenzelm
parents:
79419
diff
changeset
|
110 |
| cons_nr (ZType1 _) = 4 |
4c3346b4b100
clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
wenzelm
parents:
79419
diff
changeset
|
111 |
| cons_nr (ZType _) = 5; |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
112 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
113 |
val fast_indexname_ord = Term_Ord.fast_indexname_ord; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
114 |
val sort_ord = Term_Ord.sort_ord; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
115 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
116 |
in |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
117 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
118 |
fun ztyp_ord TU = |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
119 |
if pointer_eq TU then EQUAL |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
120 |
else |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
121 |
(case TU of |
79119 | 122 |
(ZTVar (a, A), ZTVar (b, B)) => |
123 |
(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
|
124 |
| (ZFun (T, T'), ZFun (U, U')) => |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
125 |
(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
|
126 |
| (ZProp, ZProp) => EQUAL |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
127 |
| (ZType0 a, ZType0 b) => fast_string_ord (a, b) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
128 |
| (ZType1 (a, T), ZType1 (b, U)) => |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
129 |
(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
|
130 |
| (ZType (a, Ts), ZType (b, Us)) => |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
131 |
(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
|
132 |
| (T, U) => int_ord (cons_nr T, cons_nr U)); |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
133 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
134 |
end; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
135 |
|
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
136 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
137 |
(* term ordering and alpha-conversion *) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
138 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
139 |
local |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
140 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
141 |
fun cons_nr (ZVar _) = 0 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
142 |
| cons_nr (ZBound _) = 1 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
143 |
| cons_nr (ZConst0 _) = 2 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
144 |
| cons_nr (ZConst1 _) = 3 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
145 |
| cons_nr (ZConst _) = 4 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
146 |
| cons_nr (ZAbs _) = 5 |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
147 |
| cons_nr (ZApp _) = 6 |
80293 | 148 |
| cons_nr (OFCLASS _) = 7; |
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
149 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
150 |
fun struct_ord tu = |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
151 |
if pointer_eq tu then EQUAL |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
152 |
else |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
153 |
(case tu of |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
154 |
(ZAbs (_, _, t), ZAbs (_, _, u)) => struct_ord (t, u) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
155 |
| (ZApp (t1, t2), ZApp (u1, u2)) => |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
156 |
(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
|
157 |
| (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
|
158 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
159 |
fun atoms_ord tu = |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
160 |
if pointer_eq tu then EQUAL |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
161 |
else |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
162 |
(case tu of |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
163 |
(ZAbs (_, _, t), ZAbs (_, _, u)) => atoms_ord (t, u) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
164 |
| (ZApp (t1, t2), ZApp (u1, u2)) => |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
165 |
(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
|
166 |
| (ZConst0 a, ZConst0 b) => fast_string_ord (a, b) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
167 |
| (ZConst1 (a, _), ZConst1 (b, _)) => fast_string_ord (a, b) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
168 |
| (ZConst (a, _), ZConst (b, _)) => fast_string_ord (a, b) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
169 |
| (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
|
170 |
| (ZBound i, ZBound j) => int_ord (i, j) |
80293 | 171 |
| (OFCLASS (_, a), OFCLASS (_, b)) => fast_string_ord (a, b) |
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
172 |
| _ => EQUAL); |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
173 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
174 |
fun types_ord tu = |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
175 |
if pointer_eq tu then EQUAL |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
176 |
else |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
177 |
(case tu of |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
178 |
(ZAbs (_, T, t), ZAbs (_, U, u)) => |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
179 |
(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
|
180 |
| (ZApp (t1, t2), ZApp (u1, u2)) => |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
181 |
(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
|
182 |
| (ZConst1 (_, T), ZConst1 (_, U)) => ztyp_ord (T, U) |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
183 |
| (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
|
184 |
| (ZVar (_, T), ZVar (_, U)) => ztyp_ord (T, U) |
80293 | 185 |
| (OFCLASS (T, _), OFCLASS (U, _)) => ztyp_ord (T, U) |
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
186 |
| _ => EQUAL); |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
187 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
188 |
in |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
189 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
190 |
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
|
191 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
192 |
end; |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
193 |
|
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
194 |
fun aconv_zterm (tm1, tm2) = |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
195 |
pointer_eq (tm1, tm2) orelse |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
196 |
(case (tm1, tm2) of |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
197 |
(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
|
198 |
| (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
|
199 |
| (a1, a2) => a1 = a2); |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
200 |
|
79124 | 201 |
end; |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
202 |
|
79124 | 203 |
|
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
204 |
(* tables and term items *) |
79124 | 205 |
|
79163 | 206 |
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
|
207 |
structure ZTerms = Table(type key = zterm val ord = ZTerm.fast_zterm_ord); |
79163 | 208 |
|
79124 | 209 |
structure ZTVars: |
210 |
sig |
|
211 |
include TERM_ITEMS |
|
212 |
val add_tvarsT: ztyp -> set -> set |
|
213 |
val add_tvars: zterm -> set -> set |
|
214 |
end = |
|
215 |
struct |
|
216 |
open TVars; |
|
217 |
val add_tvarsT = ZTerm.fold_tvars add_set; |
|
218 |
val add_tvars = ZTerm.fold_types add_tvarsT; |
|
219 |
end; |
|
220 |
||
221 |
structure ZVars: |
|
222 |
sig |
|
223 |
include TERM_ITEMS |
|
224 |
val add_vars: zterm -> set -> set |
|
225 |
end = |
|
226 |
struct |
|
227 |
||
228 |
structure Term_Items = Term_Items |
|
229 |
( |
|
230 |
type key = indexname * ztyp; |
|
231 |
val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord ZTerm.ztyp_ord); |
|
232 |
); |
|
233 |
open Term_Items; |
|
234 |
||
80266 | 235 |
val add_vars = ZTerm.fold_vars add_set; |
79124 | 236 |
|
237 |
end; |
|
238 |
||
239 |
||
240 |
(* proofs *) |
|
241 |
||
79126 | 242 |
datatype zproof_name = |
243 |
ZAxiom of string |
|
244 |
| ZOracle of string |
|
80289 | 245 |
| ZThm of {theory_name: string, thm_name: Thm_Name.P, serial: serial}; |
79126 | 246 |
|
80264 | 247 |
type zproof_const = (zproof_name * zterm) * (ztyp ZTVars.table * zterm ZVars.table); |
248 |
||
79124 | 249 |
datatype zproof = |
80560 | 250 |
ZNop |
80264 | 251 |
| ZConstp of zproof_const |
79212 | 252 |
| ZBoundp of int |
79124 | 253 |
| ZAbst of string * ztyp * zproof |
79212 | 254 |
| ZAbsp of string * zterm * zproof |
79124 | 255 |
| ZAppt of zproof * zterm |
79212 | 256 |
| ZAppp of zproof * zproof |
79476 | 257 |
| ZHyp of zterm |
80293 | 258 |
| OFCLASSp of ztyp * class; (*OFCLASS proof from sorts algebra*) |
79124 | 259 |
|
260 |
||
261 |
||
262 |
(*** local ***) |
|
263 |
||
264 |
signature ZTERM = |
|
265 |
sig |
|
266 |
datatype ztyp = datatype ztyp |
|
267 |
datatype zterm = datatype zterm |
|
268 |
datatype zproof = datatype zproof |
|
79265 | 269 |
exception ZTERM of string * ztyp list * zterm list * zproof list |
79124 | 270 |
val fold_tvars: (indexname * sort -> 'a -> 'a) -> ztyp -> 'a -> 'a |
271 |
val fold_aterms: (zterm -> 'a -> 'a) -> zterm -> 'a -> 'a |
|
80266 | 272 |
val fold_vars: (indexname * ztyp -> 'a -> 'a) -> zterm -> 'a -> 'a |
79124 | 273 |
val fold_types: (ztyp -> 'a -> 'a) -> zterm -> 'a -> 'a |
80598 | 274 |
val map_tvars: (indexname * sort -> ztyp) -> ztyp -> ztyp |
275 |
val map_aterms: (zterm -> zterm) -> zterm -> zterm |
|
80603 | 276 |
val map_vars: (indexname * ztyp -> zterm) -> zterm -> zterm |
80598 | 277 |
val map_types: (ztyp -> ztyp) -> zterm -> zterm |
79264
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
278 |
val ztyp_ord: ztyp ord |
07b93dee105f
more operations: zterm ordering that follows fast_term_ord;
wenzelm
parents:
79263
diff
changeset
|
279 |
val fast_zterm_ord: zterm ord |
79124 | 280 |
val aconv_zterm: zterm * zterm -> bool |
79212 | 281 |
val ZAbsts: (string * ztyp) list -> zproof -> zproof |
282 |
val ZAbsps: zterm list -> zproof -> zproof |
|
283 |
val ZAppts: zproof * zterm list -> zproof |
|
284 |
val ZAppps: zproof * zproof list -> zproof |
|
80605 | 285 |
val strip_sortsT: ztyp -> ztyp |
286 |
val strip_sorts: zterm -> zterm |
|
79200 | 287 |
val incr_bv_same: int -> int -> zterm Same.operation |
79283 | 288 |
val incr_proof_bv_same: int -> int -> int -> int -> zproof Same.operation |
79200 | 289 |
val incr_bv: int -> int -> zterm -> zterm |
290 |
val incr_boundvars: int -> zterm -> zterm |
|
79283 | 291 |
val incr_proof_bv: int -> int -> int -> int -> zproof -> zproof |
292 |
val incr_proof_boundvars: int -> int -> zproof -> zproof |
|
293 |
val subst_term_bound_same: zterm -> int -> zterm Same.operation |
|
294 |
val subst_proof_bound_same: zterm -> int -> zproof Same.operation |
|
295 |
val subst_proof_boundp_same: zproof -> int -> int -> zproof Same.operation |
|
296 |
val subst_term_bound: zterm -> zterm -> zterm |
|
297 |
val subst_proof_bound: zterm -> zproof -> zproof |
|
298 |
val subst_proof_boundp: zproof -> zproof -> zproof |
|
79388 | 299 |
val subst_type_same: (indexname * sort, ztyp) Same.function -> ztyp Same.operation |
300 |
val subst_term_same: |
|
301 |
ztyp Same.operation -> (indexname * ztyp, zterm) Same.function -> zterm Same.operation |
|
79318 | 302 |
exception BAD_INST of ((indexname * ztyp) * zterm) list |
80598 | 303 |
val fold_proof: {hyps: bool} -> (ztyp -> 'a -> 'a) -> (zterm -> 'a -> 'a) -> zproof -> 'a -> 'a |
304 |
val fold_proof_types: {hyps: bool} -> (ztyp -> 'a -> 'a) -> zproof -> 'a -> 'a |
|
79418 | 305 |
val map_proof: {hyps: bool} -> ztyp Same.operation -> zterm Same.operation -> zproof -> zproof |
306 |
val map_proof_types: {hyps: bool} -> ztyp Same.operation -> zproof -> zproof |
|
79414 | 307 |
val map_class_proof: (ztyp * class, zproof) Same.function -> zproof -> zproof |
80605 | 308 |
val maxidx_type: ztyp -> int -> int |
309 |
val maxidx_term: zterm -> int -> int |
|
310 |
val maxidx_proof: zproof -> int -> int |
|
79124 | 311 |
val ztyp_of: typ -> ztyp |
80586 | 312 |
val ztyp_dummy: ztyp |
80580 | 313 |
val typ_of_tvar: indexname * sort -> typ |
314 |
val typ_of: ztyp -> typ |
|
80608
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
315 |
val init_cache: theory -> theory option |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
316 |
val exit_cache: theory -> theory option |
80581 | 317 |
val reset_cache: theory -> theory |
80583 | 318 |
val check_cache: theory -> {ztyp: int, typ: int} option |
80581 | 319 |
val ztyp_cache: theory -> typ -> ztyp |
320 |
val typ_cache: theory -> ztyp -> typ |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
321 |
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
|
322 |
val zterm_of: theory -> term -> zterm |
80608
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
323 |
val zterm_dummy_pattern: ztyp -> zterm |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
324 |
val zterm_type: ztyp -> zterm |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
325 |
val term_of: theory -> zterm -> term |
79298 | 326 |
val beta_norm_term_same: zterm Same.operation |
327 |
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
|
328 |
val beta_norm_prooft_same: zproof -> zproof |
79298 | 329 |
val beta_norm_term: zterm -> zterm |
330 |
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
|
331 |
val beta_norm_prooft: zproof -> zproof |
80581 | 332 |
val norm_type: theory -> Type.tyenv -> ztyp -> ztyp |
79200 | 333 |
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
|
334 |
val norm_proof: theory -> Envir.env -> term list -> zproof -> zproof |
80267 | 335 |
val zproof_const_typargs: zproof_const -> ((indexname * sort) * ztyp) list |
336 |
val zproof_const_args: zproof_const -> ((indexname * ztyp) * zterm) list |
|
79311
e4a9a861856b
omit pointless future: proof terms are built sequentially;
wenzelm
parents:
79303
diff
changeset
|
337 |
type zbox = serial * (zterm * zproof) |
79279 | 338 |
val zbox_ord: zbox ord |
339 |
type zboxes = zbox Ord_List.T |
|
79265 | 340 |
val union_zboxes: zboxes -> zboxes -> zboxes |
79279 | 341 |
val unions_zboxes: zboxes list -> zboxes |
79425 | 342 |
val add_box_proof: {unconstrain: bool} -> theory -> |
343 |
term list -> term -> zproof -> zboxes -> zproof * zboxes |
|
80289 | 344 |
val thm_proof: theory -> Thm_Name.P -> term list -> term -> zproof -> zbox * zproof |
79126 | 345 |
val axiom_proof: theory -> string -> term -> zproof |
346 |
val oracle_proof: theory -> string -> term -> zproof |
|
79124 | 347 |
val assume_proof: theory -> term -> zproof |
348 |
val trivial_proof: theory -> term -> zproof |
|
79414 | 349 |
val implies_intr_proof': zterm -> zproof -> zproof |
79124 | 350 |
val implies_intr_proof: theory -> term -> zproof -> zproof |
351 |
val forall_intr_proof: theory -> typ -> string * term -> zproof -> zproof |
|
352 |
val forall_elim_proof: theory -> term -> zproof -> zproof |
|
79128 | 353 |
val of_class_proof: typ * class -> zproof |
79124 | 354 |
val reflexive_proof: theory -> typ -> term -> zproof |
355 |
val symmetric_proof: theory -> typ -> term -> term -> zproof -> zproof |
|
356 |
val transitive_proof: theory -> typ -> term -> term -> term -> zproof -> zproof -> zproof |
|
357 |
val equal_intr_proof: theory -> term -> term -> zproof -> zproof -> zproof |
|
358 |
val equal_elim_proof: theory -> term -> term -> zproof -> zproof -> zproof |
|
359 |
val abstract_rule_proof: theory -> typ -> typ -> string * term -> term -> term -> zproof -> zproof |
|
360 |
val combination_proof: theory -> typ -> typ -> term -> term -> term -> term -> |
|
361 |
zproof -> zproof -> zproof |
|
79133 | 362 |
val generalize_proof: Names.set * Names.set -> int -> zproof -> zproof |
79149 | 363 |
val instantiate_proof: theory -> |
364 |
((indexname * sort) * typ) list * ((indexname * typ) * term) list -> zproof -> zproof |
|
79135 | 365 |
val varifyT_proof: ((string * sort) * (indexname * sort)) list -> zproof -> zproof |
79152 | 366 |
val legacy_freezeT_proof: term -> zproof -> zproof |
79155 | 367 |
val rotate_proof: theory -> term list -> term -> (string * typ) list -> |
368 |
term list -> int -> zproof -> zproof |
|
369 |
val permute_prems_proof: theory -> term list -> int -> int -> zproof -> zproof |
|
79234 | 370 |
val incr_indexes_proof: int -> zproof -> zproof |
79237 | 371 |
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
|
372 |
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
|
373 |
zproof -> zproof |
79261 | 374 |
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
|
375 |
term option -> term list -> int -> int -> term list -> zproof -> zproof -> zproof |
79425 | 376 |
type sorts_proof = (class * class -> zproof) * (string * class list list * class -> zproof) |
377 |
val of_sort_proof: Sorts.algebra -> sorts_proof -> (typ * class -> zproof) -> |
|
79405 | 378 |
typ * sort -> zproof list |
79425 | 379 |
val unconstrainT_proof: theory -> sorts_proof -> Logic.unconstrain_context -> zproof -> zproof |
80586 | 380 |
val encode_ztyp: ztyp XML.Encode.T |
381 |
val encode_zterm: {typed_vars: bool} -> zterm XML.Encode.T |
|
382 |
val encode_zproof: {typed_vars: bool} -> zproof XML.Encode.T |
|
80608
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
383 |
val standard_vars: Name.context -> zterm * zproof option -> |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
384 |
{typargs: (string * sort) list, args: (string * ztyp) list, prop: zterm, proof: zproof option} |
79124 | 385 |
end; |
386 |
||
387 |
structure ZTerm: ZTERM = |
|
388 |
struct |
|
389 |
||
390 |
datatype ztyp = datatype ztyp; |
|
391 |
datatype zterm = datatype zterm; |
|
392 |
datatype zproof = datatype zproof; |
|
393 |
||
79265 | 394 |
exception ZTERM of string * ztyp list * zterm list * zproof list; |
395 |
||
79124 | 396 |
open ZTerm; |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
397 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
398 |
|
79155 | 399 |
(* derived operations *) |
400 |
||
79234 | 401 |
val ZFuns = Library.foldr ZFun; |
402 |
||
79212 | 403 |
val ZAbsts = fold_rev (fn (x, T) => fn prf => ZAbst (x, T, prf)); |
404 |
val ZAbsps = fold_rev (fn t => fn prf => ZAbsp ("H", t, prf)); |
|
79155 | 405 |
|
79212 | 406 |
val ZAppts = Library.foldl ZAppt; |
407 |
val ZAppps = Library.foldl ZAppp; |
|
79155 | 408 |
|
79234 | 409 |
fun combound (t, n, k) = |
410 |
if k > 0 then ZApp (combound (t, n + 1, k - 1), ZBound n) else t; |
|
411 |
||
80605 | 412 |
val strip_sortsT_same = map_tvars_same (fn (_, []) => raise Same.SAME | (a, _) => ZTVar (a, [])); |
413 |
val strip_sorts_same = map_types_same strip_sortsT_same; |
|
414 |
||
415 |
val strip_sortsT = Same.commit strip_sortsT_same; |
|
416 |
val strip_sorts = Same.commit strip_sorts_same; |
|
417 |
||
79155 | 418 |
|
79283 | 419 |
(* increment bound variables *) |
79200 | 420 |
|
421 |
fun incr_bv_same inc = |
|
422 |
if inc = 0 then fn _ => Same.same |
|
423 |
else |
|
424 |
let |
|
79285 | 425 |
fun term lev (ZBound i) = |
426 |
if i >= lev then ZBound (i + inc) else raise Same.SAME |
|
79200 | 427 |
| term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t) |
428 |
| term lev (ZApp (t, u)) = |
|
79285 | 429 |
(ZApp (term lev t, Same.commit (term lev) u) |
430 |
handle Same.SAME => ZApp (t, term lev u)) |
|
79200 | 431 |
| term _ _ = raise Same.SAME; |
432 |
in term end; |
|
433 |
||
79283 | 434 |
fun incr_proof_bv_same incp inct = |
435 |
if incp = 0 andalso inct = 0 then fn _ => fn _ => Same.same |
|
436 |
else |
|
437 |
let |
|
438 |
val term = incr_bv_same inct; |
|
79200 | 439 |
|
79283 | 440 |
fun proof plev _ (ZBoundp i) = |
441 |
if i >= plev then ZBoundp (i + incp) else raise Same.SAME |
|
442 |
| proof plev tlev (ZAbsp (a, t, p)) = |
|
443 |
(ZAbsp (a, term tlev t, Same.commit (proof (plev + 1) tlev) p) |
|
444 |
handle Same.SAME => ZAbsp (a, t, proof (plev + 1) tlev p)) |
|
445 |
| proof plev tlev (ZAbst (a, T, p)) = ZAbst (a, T, proof plev (tlev + 1) p) |
|
446 |
| proof plev tlev (ZAppp (p, q)) = |
|
447 |
(ZAppp (proof plev tlev p, Same.commit (proof plev tlev) q) |
|
448 |
handle Same.SAME => ZAppp (p, proof plev tlev q)) |
|
449 |
| proof plev tlev (ZAppt (p, t)) = |
|
450 |
(ZAppt (proof plev tlev p, Same.commit (term tlev) t) |
|
451 |
handle Same.SAME => ZAppt (p, term tlev t)) |
|
452 |
| proof _ _ _ = raise Same.SAME; |
|
453 |
in proof end; |
|
454 |
||
455 |
fun incr_bv inc lev = Same.commit (incr_bv_same inc lev); |
|
79200 | 456 |
fun incr_boundvars inc = incr_bv inc 0; |
457 |
||
79283 | 458 |
fun incr_proof_bv incp inct plev tlev = Same.commit (incr_proof_bv_same incp inct plev tlev); |
459 |
fun incr_proof_boundvars incp inct = incr_proof_bv incp inct 0 0; |
|
460 |
||
461 |
||
462 |
(* substitution of bound variables *) |
|
463 |
||
79281
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
464 |
fun subst_term_bound_same arg = |
79200 | 465 |
let |
466 |
fun term lev (ZBound i) = |
|
467 |
if i < lev then raise Same.SAME (*var is locally bound*) |
|
468 |
else if i = lev then incr_boundvars lev arg |
|
469 |
else ZBound (i - 1) (*loose: change it*) |
|
470 |
| term lev (ZAbs (a, T, t)) = ZAbs (a, T, term (lev + 1) t) |
|
471 |
| term lev (ZApp (t, u)) = |
|
472 |
(ZApp (term lev t, Same.commit (term lev) u) |
|
473 |
handle Same.SAME => ZApp (t, term lev u)) |
|
474 |
| term _ _ = raise Same.SAME; |
|
79281
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
475 |
in term end; |
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
476 |
|
79283 | 477 |
fun subst_proof_bound_same arg = |
478 |
let |
|
479 |
val term = subst_term_bound_same arg; |
|
480 |
||
481 |
fun proof lev (ZAbsp (a, t, p)) = |
|
482 |
(ZAbsp (a, term lev t, Same.commit (proof lev) p) |
|
483 |
handle Same.SAME => ZAbsp (a, t, proof lev p)) |
|
484 |
| proof lev (ZAbst (a, T, p)) = ZAbst (a, T, proof (lev + 1) p) |
|
485 |
| proof lev (ZAppp (p, q)) = |
|
486 |
(ZAppp (proof lev p, Same.commit (proof lev) q) |
|
487 |
handle Same.SAME => ZAppp (p, proof lev q)) |
|
488 |
| proof lev (ZAppt (p, t)) = |
|
489 |
(ZAppt (proof lev p, Same.commit (term lev) t) |
|
490 |
handle Same.SAME => ZAppt (p, term lev t)) |
|
491 |
| proof _ _ = raise Same.SAME; |
|
492 |
in proof end; |
|
493 |
||
494 |
fun subst_proof_boundp_same arg = |
|
495 |
let |
|
496 |
fun proof plev tlev (ZBoundp i) = |
|
497 |
if i < plev then raise Same.SAME (*var is locally bound*) |
|
498 |
else if i = plev then incr_proof_boundvars plev tlev arg |
|
499 |
else ZBoundp (i - 1) (*loose: change it*) |
|
500 |
| proof plev tlev (ZAbsp (a, t, p)) = ZAbsp (a, t, proof (plev + 1) tlev p) |
|
501 |
| proof plev tlev (ZAbst (a, T, p)) = ZAbst (a, T, proof plev (tlev + 1) p) |
|
502 |
| proof plev tlev (ZAppp (p, q)) = |
|
503 |
(ZAppp (proof plev tlev p, Same.commit (proof plev tlev) q) |
|
504 |
handle Same.SAME => ZAppp (p, proof plev tlev q)) |
|
505 |
| proof plev tlev (ZAppt (p, t)) = ZAppt (proof plev tlev p, t) |
|
506 |
| proof _ _ _ = raise Same.SAME; |
|
507 |
in proof end; |
|
508 |
||
509 |
fun subst_term_bound arg body = Same.commit (subst_term_bound_same arg 0) body; |
|
510 |
fun subst_proof_bound arg body = Same.commit (subst_proof_bound_same arg 0) body; |
|
511 |
fun subst_proof_boundp arg body = Same.commit (subst_proof_boundp_same arg 0 0) body; |
|
79200 | 512 |
|
513 |
||
79268 | 514 |
(* substitution of free or schematic variables *) |
79132 | 515 |
|
516 |
fun subst_type_same tvar = |
|
517 |
let |
|
518 |
fun typ (ZTVar x) = tvar x |
|
79326 | 519 |
| typ (ZFun (T, U)) = |
520 |
(ZFun (typ T, Same.commit typ U) |
|
521 |
handle Same.SAME => ZFun (T, typ U)) |
|
79132 | 522 |
| typ ZProp = raise Same.SAME |
523 |
| typ (ZType0 _) = raise Same.SAME |
|
524 |
| typ (ZType1 (a, T)) = ZType1 (a, typ T) |
|
525 |
| typ (ZType (a, Ts)) = ZType (a, Same.map typ Ts); |
|
526 |
in typ end; |
|
527 |
||
528 |
fun subst_term_same typ var = |
|
529 |
let |
|
530 |
fun term (ZVar (x, T)) = |
|
531 |
let val (T', same) = Same.commit_id typ T in |
|
532 |
(case Same.catch var (x, T') of |
|
533 |
NONE => if same then raise Same.SAME else ZVar (x, T') |
|
534 |
| SOME t' => t') |
|
535 |
end |
|
536 |
| term (ZBound _) = raise Same.SAME |
|
537 |
| term (ZConst0 _) = raise Same.SAME |
|
538 |
| term (ZConst1 (a, T)) = ZConst1 (a, typ T) |
|
539 |
| term (ZConst (a, Ts)) = ZConst (a, Same.map typ Ts) |
|
540 |
| term (ZAbs (a, T, t)) = |
|
79326 | 541 |
(ZAbs (a, typ T, Same.commit term t) |
542 |
handle Same.SAME => ZAbs (a, T, term t)) |
|
79132 | 543 |
| term (ZApp (t, u)) = |
79326 | 544 |
(ZApp (term t, Same.commit term u) |
545 |
handle Same.SAME => ZApp (t, term u)) |
|
80293 | 546 |
| term (OFCLASS (T, c)) = OFCLASS (typ T, c); |
79132 | 547 |
in term end; |
548 |
||
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
549 |
fun instantiate_type_same instT = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
550 |
if ZTVars.is_empty instT then Same.same |
80579 | 551 |
else #apply (ZTypes.unsynchronized_cache (subst_type_same (Same.function (ZTVars.lookup instT)))); |
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
552 |
|
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
553 |
fun instantiate_term_same typ inst = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
554 |
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
|
555 |
|
79268 | 556 |
|
79475
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
557 |
(* fold types/terms within proof *) |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
558 |
|
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
559 |
fun fold_proof {hyps} typ term = |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
560 |
let |
80560 | 561 |
fun proof ZNop = I |
80262 | 562 |
| proof (ZConstp (_, (instT, inst))) = |
79475
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
563 |
ZTVars.fold (typ o #2) instT #> ZVars.fold (term o #2) inst |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
564 |
| proof (ZBoundp _) = I |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
565 |
| proof (ZAbst (_, T, p)) = typ T #> proof p |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
566 |
| proof (ZAbsp (_, t, p)) = term t #> proof p |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
567 |
| proof (ZAppt (p, t)) = proof p #> term t |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
568 |
| proof (ZAppp (p, q)) = proof p #> proof q |
79476 | 569 |
| proof (ZHyp h) = hyps ? term h |
80293 | 570 |
| proof (OFCLASSp (T, _)) = hyps ? typ T; |
79475
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
571 |
in proof end; |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
572 |
|
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
573 |
fun fold_proof_types hyps typ = |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
574 |
fold_proof hyps typ (fold_types typ); |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
575 |
|
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
576 |
|
79268 | 577 |
(* map types/terms within proof *) |
578 |
||
79318 | 579 |
exception BAD_INST of ((indexname * ztyp) * zterm) list |
580 |
||
581 |
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
|
582 |
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
|
583 |
handle ZVars.DUP _ => raise BAD_INST inst; |
79318 | 584 |
|
79145 | 585 |
fun map_insts_same typ term (instT, inst) = |
586 |
let |
|
587 |
val changed = Unsynchronized.ref false; |
|
79146
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
588 |
fun apply f x = |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
589 |
(case Same.catch f x of |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
590 |
NONE => NONE |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
591 |
| some => (changed := true; some)); |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
592 |
|
79145 | 593 |
val instT' = |
594 |
(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
|
595 |
(case apply typ T of |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
596 |
NONE => I |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
597 |
| SOME T' => ZTVars.update (v, T'))); |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
598 |
|
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
599 |
val vars' = |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
600 |
(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
|
601 |
(case apply typ T of |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
602 |
NONE => I |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
603 |
| 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
|
604 |
|
79145 | 605 |
val inst' = |
79146
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
606 |
if ZVars.is_empty vars' then |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
607 |
(inst, inst) |-> ZVars.fold (fn (v, t) => |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
608 |
(case apply term t of |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
609 |
NONE => I |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
610 |
| SOME t' => ZVars.update (v, t'))) |
feb94ac5df41
more accurate treatment of term variables after instantiation of type variables;
wenzelm
parents:
79145
diff
changeset
|
611 |
else |
79325 | 612 |
build (inst |> ZVars.fold_rev (fn (v, t) => |
613 |
cons (the_default v (ZVars.lookup vars' v), the_default t (apply term t)))) |
|
79318 | 614 |
|> make_inst; |
79145 | 615 |
in if ! changed then (instT', inst') else raise Same.SAME end; |
616 |
||
79418 | 617 |
fun map_proof_same {hyps} typ term = |
79132 | 618 |
let |
80560 | 619 |
fun proof ZNop = raise Same.SAME |
80262 | 620 |
| proof (ZConstp (a, (instT, inst))) = |
621 |
ZConstp (a, map_insts_same typ term (instT, inst)) |
|
79212 | 622 |
| proof (ZBoundp _) = raise Same.SAME |
79132 | 623 |
| proof (ZAbst (a, T, p)) = |
79326 | 624 |
(ZAbst (a, typ T, Same.commit proof p) |
625 |
handle Same.SAME => ZAbst (a, T, proof p)) |
|
79212 | 626 |
| proof (ZAbsp (a, t, p)) = |
79326 | 627 |
(ZAbsp (a, term t, Same.commit proof p) |
628 |
handle Same.SAME => ZAbsp (a, t, proof p)) |
|
79132 | 629 |
| proof (ZAppt (p, t)) = |
79326 | 630 |
(ZAppt (proof p, Same.commit term t) |
631 |
handle Same.SAME => ZAppt (p, term t)) |
|
79212 | 632 |
| proof (ZAppp (p, q)) = |
79326 | 633 |
(ZAppp (proof p, Same.commit proof q) |
634 |
handle Same.SAME => ZAppp (p, proof q)) |
|
79476 | 635 |
| proof (ZHyp h) = if hyps then ZHyp (term h) else raise Same.SAME |
80293 | 636 |
| proof (OFCLASSp (T, c)) = if hyps then OFCLASSp (typ T, c) else raise Same.SAME; |
79132 | 637 |
in proof end; |
638 |
||
79418 | 639 |
fun map_proof hyps typ term = Same.commit (map_proof_same hyps typ term); |
640 |
fun map_proof_types hyps typ = map_proof hyps typ (subst_term_same typ Same.same); |
|
79144 | 641 |
|
79124 | 642 |
|
79414 | 643 |
(* map class proofs *) |
644 |
||
645 |
fun map_class_proof class = |
|
646 |
let |
|
80293 | 647 |
fun proof (OFCLASSp C) = class C |
79414 | 648 |
| proof (ZAbst (a, T, p)) = ZAbst (a, T, proof p) |
649 |
| proof (ZAbsp (a, t, p)) = ZAbsp (a, t, proof p) |
|
650 |
| proof (ZAppt (p, t)) = ZAppt (proof p, t) |
|
651 |
| proof (ZAppp (p, q)) = |
|
652 |
(ZAppp (proof p, Same.commit proof q) |
|
653 |
handle Same.SAME => ZAppp (p, proof q)) |
|
654 |
| proof _ = raise Same.SAME; |
|
655 |
in Same.commit proof end; |
|
656 |
||
657 |
||
80605 | 658 |
(* maximum index of variables *) |
659 |
||
660 |
val maxidx_type = fold_tvars (fn ((_, i), _) => Integer.max i); |
|
661 |
||
662 |
fun maxidx_term t = |
|
663 |
fold_types maxidx_type t #> |
|
664 |
fold_aterms (fn ZVar ((_, i), _) => Integer.max i | _ => I) t; |
|
665 |
||
666 |
val maxidx_proof = fold_proof {hyps = false} maxidx_type maxidx_term; |
|
667 |
||
668 |
||
80580 | 669 |
(* convert ztyp vs. regular typ *) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
670 |
|
79119 | 671 |
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
|
672 |
| ztyp_of (TVar v) = ZTVar v |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
673 |
| ztyp_of (Type ("fun", [T, U])) = ZFun (ztyp_of T, ztyp_of U) |
79421 | 674 |
| ztyp_of (Type ("prop", [])) = ZProp |
675 |
| ztyp_of (Type (c, [])) = ZType0 c |
|
79420
4c3346b4b100
clarified datatype ztyp: omit special case that rarely occurs (thanks to ZClass and ZClassp);
wenzelm
parents:
79419
diff
changeset
|
676 |
| ztyp_of (Type (c, [T])) = ZType1 (c, ztyp_of T) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
677 |
| 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
|
678 |
|
80586 | 679 |
val ztyp_dummy = ztyp_of dummyT; |
680 |
||
80580 | 681 |
fun typ_of_tvar ((a, ~1), S) = TFree (a, S) |
682 |
| typ_of_tvar v = TVar v; |
|
683 |
||
684 |
fun typ_of (ZTVar v) = typ_of_tvar v |
|
685 |
| typ_of (ZFun (T, U)) = typ_of T --> typ_of U |
|
686 |
| typ_of ZProp = propT |
|
687 |
| typ_of (ZType0 c) = Type (c, []) |
|
688 |
| typ_of (ZType1 (c, T)) = Type (c, [typ_of T]) |
|
689 |
| typ_of (ZType (c, Ts)) = Type (c, map typ_of Ts); |
|
690 |
||
80581 | 691 |
|
692 |
(* cache within theory context *) |
|
693 |
||
694 |
local |
|
695 |
||
696 |
structure Data = Theory_Data |
|
697 |
( |
|
698 |
type T = (ztyp Typtab.cache_ops * typ ZTypes.cache_ops) option; |
|
699 |
val empty = NONE; |
|
700 |
val merge = K NONE; |
|
701 |
); |
|
702 |
||
703 |
fun make_ztyp_cache () = Typtab.unsynchronized_cache ztyp_of; |
|
704 |
fun make_typ_cache () = ZTypes.unsynchronized_cache typ_of; |
|
705 |
||
80608
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
706 |
in |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
707 |
|
80581 | 708 |
fun init_cache thy = |
709 |
if is_some (Data.get thy) then NONE |
|
710 |
else SOME (Data.put (SOME (make_ztyp_cache (), make_typ_cache ())) thy); |
|
711 |
||
712 |
fun exit_cache thy = |
|
713 |
(case Data.get thy of |
|
714 |
SOME (cache1, cache2) => (#reset cache1 (); #reset cache2 (); SOME (Data.put NONE thy)) |
|
715 |
| NONE => NONE); |
|
716 |
||
717 |
val _ = Theory.setup (Theory.at_begin init_cache #> Theory.at_end exit_cache); |
|
718 |
||
719 |
fun reset_cache thy = |
|
720 |
(case Data.get thy of |
|
721 |
SOME (cache1, cache2) => (#reset cache1 (); #reset cache2 (); thy) |
|
722 |
| NONE => thy); |
|
723 |
||
80583 | 724 |
fun check_cache thy = |
725 |
Data.get thy |
|
726 |
|> Option.map (fn (cache1, cache2) => {ztyp = #size cache1 (), typ = #size cache2 ()}); |
|
727 |
||
80581 | 728 |
fun ztyp_cache thy = |
729 |
(case Data.get thy of |
|
730 |
SOME (cache, _) => cache |
|
731 |
| NONE => make_ztyp_cache ()) |> #apply; |
|
732 |
||
733 |
fun typ_cache thy = |
|
734 |
(case Data.get thy of |
|
735 |
SOME (_, cache) => cache |
|
736 |
| NONE => make_typ_cache ()) |> #apply; |
|
737 |
||
738 |
end; |
|
80580 | 739 |
|
740 |
||
741 |
(* convert zterm vs. regular term *) |
|
79163 | 742 |
|
79226 | 743 |
fun zterm_cache thy = |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
744 |
let |
79226 | 745 |
val typargs = Consts.typargs (Sign.consts_of thy); |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
746 |
|
80581 | 747 |
val ztyp = ztyp_cache thy; |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
748 |
|
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
749 |
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
|
750 |
| zterm (Var (xi, T)) = ZVar (xi, ztyp T) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
751 |
| zterm (Bound i) = ZBound i |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
752 |
| zterm (Const (c, T)) = |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
753 |
(case typargs (c, T) of |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
754 |
[] => ZConst0 c |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
755 |
| [T] => ZConst1 (c, ztyp T) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
756 |
| Ts => ZConst (c, map ztyp Ts)) |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
757 |
| zterm (Abs (a, T, b)) = ZAbs (a, ztyp T, zterm b) |
80294 | 758 |
| zterm (tm as t $ u) = |
759 |
(case try Logic.match_of_class tm of |
|
760 |
SOME (T, c) => OFCLASS (ztyp T, c) |
|
761 |
| NONE => ZApp (zterm t, zterm u)); |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
762 |
in {ztyp = ztyp, zterm = zterm} end; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
763 |
|
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
764 |
val zterm_of = #zterm o zterm_cache; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
765 |
|
80608
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
766 |
fun zterm_dummy_pattern T = ZConst1 ("Pure.dummy_pattern", T); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
767 |
fun zterm_type T = ZConst1 ("Pure.type", T); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
768 |
|
79226 | 769 |
fun term_of thy = |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
770 |
let |
79226 | 771 |
val instance = Consts.instance (Sign.consts_of thy); |
772 |
||
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
773 |
fun const (c, Ts) = Const (c, instance (c, Ts)); |
79226 | 774 |
|
79119 | 775 |
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
|
776 |
| term (ZVar (xi, T)) = Var (xi, typ_of T) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
777 |
| term (ZBound i) = Bound i |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
778 |
| term (ZConst0 c) = const (c, []) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
779 |
| term (ZConst1 (c, T)) = const (c, [typ_of T]) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
780 |
| term (ZConst (c, Ts)) = const (c, map typ_of Ts) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
781 |
| 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
|
782 |
| term (ZApp (t, u)) = term t $ term u |
80293 | 783 |
| term (OFCLASS (T, c)) = Logic.mk_of_class (typ_of T, c); |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
784 |
in term end; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
785 |
|
79119 | 786 |
|
79298 | 787 |
(* beta contraction *) |
79200 | 788 |
|
79286 | 789 |
val beta_norm_term_same = |
79271 | 790 |
let |
79299 | 791 |
fun norm (ZAbs (a, T, t)) = ZAbs (a, T, Same.commit norm t) |
792 |
| norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_term_bound t body) |
|
79271 | 793 |
| norm (ZApp (f, t)) = |
794 |
((case norm f of |
|
79286 | 795 |
ZAbs (_, _, body) => Same.commit norm (subst_term_bound t body) |
79271 | 796 |
| nf => ZApp (nf, Same.commit norm t)) |
797 |
handle Same.SAME => ZApp (f, norm t)) |
|
798 |
| norm _ = raise Same.SAME; |
|
799 |
in norm end; |
|
800 |
||
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
|
801 |
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
|
802 |
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
|
803 |
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
|
804 |
|
fed9791928bf
less ambitious normalization: term abstractions over terms and proofs, but not proofs over proofs (which may be large);
wenzelm
parents:
79301
diff
changeset
|
805 |
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
|
806 |
| 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
|
807 |
| 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
|
808 |
((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
|
809 |
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
|
810 |
| 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
|
811 |
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
|
812 |
| 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
|
813 |
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
|
814 |
|
79286 | 815 |
val beta_norm_proof_same = |
816 |
let |
|
817 |
val term = beta_norm_term_same; |
|
818 |
||
79300
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
wenzelm
parents:
79299
diff
changeset
|
819 |
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
|
820 |
| norm (ZAbsp (a, t, p)) = |
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
wenzelm
parents:
79299
diff
changeset
|
821 |
(ZAbsp (a, term t, Same.commit norm p) |
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
wenzelm
parents:
79299
diff
changeset
|
822 |
handle Same.SAME => ZAbsp (a, t, norm p)) |
236fa4b32afb
more thorough beta contraction, following Envir.norm_term;
wenzelm
parents:
79299
diff
changeset
|
823 |
| norm (ZAppt (ZAbst (_, _, body), t)) = Same.commit norm (subst_proof_bound t body) |
79301 | 824 |
| norm (ZAppp (ZAbsp (_, _, body), p)) = Same.commit norm (subst_proof_boundp p body) |
79286 | 825 |
| norm (ZAppt (f, t)) = |
826 |
((case norm f of |
|
827 |
ZAbst (_, _, body) => Same.commit norm (subst_proof_bound t body) |
|
828 |
| nf => ZAppt (nf, Same.commit term t)) |
|
829 |
handle Same.SAME => ZAppt (f, term t)) |
|
830 |
| norm (ZAppp (f, p)) = |
|
831 |
((case norm f of |
|
832 |
ZAbsp (_, _, body) => Same.commit norm (subst_proof_boundp p body) |
|
833 |
| nf => ZAppp (nf, Same.commit norm p)) |
|
834 |
handle Same.SAME => ZAppp (f, norm p)) |
|
835 |
| norm _ = raise Same.SAME; |
|
836 |
in norm end; |
|
837 |
||
79298 | 838 |
val beta_norm_term = Same.commit beta_norm_term_same; |
839 |
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
|
840 |
val beta_norm_prooft = Same.commit beta_norm_prooft_same; |
79298 | 841 |
|
842 |
||
843 |
(* normalization wrt. environment and beta contraction *) |
|
844 |
||
79200 | 845 |
fun norm_type_same ztyp tyenv = |
846 |
if Vartab.is_empty tyenv then Same.same |
|
847 |
else |
|
848 |
let |
|
849 |
fun norm (ZTVar v) = |
|
850 |
(case Type.lookup tyenv v of |
|
851 |
SOME U => Same.commit norm (ztyp U) |
|
852 |
| NONE => raise Same.SAME) |
|
853 |
| norm (ZFun (T, U)) = |
|
854 |
(ZFun (norm T, Same.commit norm U) |
|
855 |
handle Same.SAME => ZFun (T, norm U)) |
|
856 |
| norm ZProp = raise Same.SAME |
|
857 |
| norm (ZType0 _) = raise Same.SAME |
|
858 |
| norm (ZType1 (a, T)) = ZType1 (a, norm T) |
|
859 |
| norm (ZType (a, Ts)) = ZType (a, Same.map norm Ts); |
|
860 |
in norm end; |
|
861 |
||
80582
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
862 |
fun norm_term_same {ztyp, zterm} (envir as Envir.Envir {tyenv, tenv, ...}) = |
79269 | 863 |
let |
864 |
val lookup = |
|
865 |
if Vartab.is_empty tenv then K NONE |
|
80582
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
866 |
else |
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
867 |
#apply (ZVars.unsynchronized_cache |
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
868 |
(apsnd typ_of #> Envir.lookup envir #> Option.map zterm)); |
79200 | 869 |
|
79269 | 870 |
val normT = norm_type_same ztyp tyenv; |
79200 | 871 |
|
79269 | 872 |
fun norm (ZVar (xi, T)) = |
873 |
(case lookup (xi, T) of |
|
874 |
SOME u => Same.commit norm u |
|
875 |
| NONE => ZVar (xi, normT T)) |
|
876 |
| norm (ZBound _) = raise Same.SAME |
|
877 |
| norm (ZConst0 _) = raise Same.SAME |
|
878 |
| norm (ZConst1 (a, T)) = ZConst1 (a, normT T) |
|
879 |
| norm (ZConst (a, Ts)) = ZConst (a, Same.map normT Ts) |
|
880 |
| norm (ZAbs (a, T, t)) = |
|
881 |
(ZAbs (a, normT T, Same.commit norm t) |
|
882 |
handle Same.SAME => ZAbs (a, T, norm t)) |
|
79281
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
883 |
| norm (ZApp (ZAbs (_, _, body), t)) = Same.commit norm (subst_term_bound t body) |
79269 | 884 |
| norm (ZApp (f, t)) = |
885 |
((case norm f of |
|
79281
28342f38da5c
clarified signature, following Term.subst_bounds_same;
wenzelm
parents:
79279
diff
changeset
|
886 |
ZAbs (_, _, body) => Same.commit norm (subst_term_bound t body) |
79269 | 887 |
| nf => ZApp (nf, Same.commit norm t)) |
888 |
handle Same.SAME => ZApp (f, norm t)) |
|
889 |
| norm _ = raise Same.SAME; |
|
79286 | 890 |
in fn t => if Envir.is_empty envir then beta_norm_term_same t else norm t end; |
79200 | 891 |
|
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
892 |
fun norm_proof_cache (cache as {ztyp, ...}) (envir as Envir.Envir {tyenv, ...}) = |
79269 | 893 |
let |
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
894 |
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
|
895 |
val norm_var = ZVar #> Same.commit (norm_term_same cache envir); |
79269 | 896 |
in |
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
897 |
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
|
898 |
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
|
899 |
else |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
900 |
let |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
901 |
fun add_tvar v tab = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
902 |
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
|
903 |
|
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
904 |
val inst_typ = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
905 |
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
|
906 |
else |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
907 |
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
|
908 |
(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
|
909 |
|> instantiate_type_same; |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
910 |
|
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
911 |
fun add_var (a, T) tab = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
912 |
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
|
913 |
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
|
914 |
|
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
915 |
val inst_term = |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
916 |
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
|
917 |
|> instantiate_term_same inst_typ; |
79272
899f37f6d218
proper beta_norm after instantiation (amending 90c5aadcc4b2);
wenzelm
parents:
79271
diff
changeset
|
918 |
|
79286 | 919 |
val norm_term = Same.compose beta_norm_term_same inst_term; |
79418 | 920 |
in beta_norm_prooft (map_proof {hyps = false} inst_typ norm_term prf) end |
79269 | 921 |
end; |
79210 | 922 |
|
80581 | 923 |
fun norm_type thy tyenv = Same.commit (norm_type_same (ztyp_cache thy) tyenv); |
80582
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
924 |
fun norm_term thy envir = Same.commit (norm_term_same (zterm_cache thy) envir); |
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
925 |
fun norm_proof thy envir = norm_proof_cache (zterm_cache thy) envir; |
79200 | 926 |
|
927 |
||
79119 | 928 |
|
929 |
(** proof construction **) |
|
79113
5109e4b2a292
pro-forma support for optional zproof: no proper content yet;
wenzelm
parents:
79098
diff
changeset
|
930 |
|
79161 | 931 |
(* constants *) |
79124 | 932 |
|
80262 | 933 |
fun zproof_const (a, A) : zproof_const = |
79119 | 934 |
let |
79154 | 935 |
val instT = |
79263 | 936 |
ZTVars.build (A |> (fold_types o fold_tvars) (fn v => fn tab => |
79154 | 937 |
if ZTVars.defined tab v then tab else ZTVars.update (v, ZTVar v) tab)); |
938 |
val inst = |
|
80266 | 939 |
ZVars.build (A |> fold_vars (fn v => fn tab => |
940 |
if ZVars.defined tab v then tab else ZVars.update (v, ZVar v) tab)); |
|
80262 | 941 |
in ((a, A), (instT, inst)) end; |
79126 | 942 |
|
80267 | 943 |
fun zproof_const_typargs (((_, A), (instT, _)): zproof_const) = |
944 |
ZTVars.build (A |> ZTVars.add_tvars) |> ZTVars.list_set |
|
945 |
|> map (fn v => (v, the_default (ZTVar v) (ZTVars.lookup instT v))); |
|
946 |
||
947 |
fun zproof_const_args (((_, A), (_, inst)): zproof_const) = |
|
948 |
ZVars.build (A |> ZVars.add_vars) |> ZVars.list_set |
|
949 |
|> map (fn v => (v, the_default (ZVar v) (ZVars.lookup inst v))); |
|
950 |
||
80264 | 951 |
fun make_const_proof (f, g) ((a, insts): zproof_const) = |
79263 | 952 |
let |
79417
a4eae462f224
proper support for complex types, not just type variables (amending 623789141e39);
wenzelm
parents:
79416
diff
changeset
|
953 |
val typ = subst_type_same (Same.function (fn ((x, _), _) => try f x)); |
79416
623789141e39
proper instantiation for make_const_proof, notably change of types for term variables;
wenzelm
parents:
79415
diff
changeset
|
954 |
val term = Same.function (fn ZVar ((x, _), _) => try g x | _ => NONE); |
80262 | 955 |
in ZConstp (a, Same.commit (map_insts_same typ term) insts) end; |
79161 | 956 |
|
957 |
||
79265 | 958 |
(* closed proof boxes *) |
959 |
||
79311
e4a9a861856b
omit pointless future: proof terms are built sequentially;
wenzelm
parents:
79303
diff
changeset
|
960 |
type zbox = serial * (zterm * zproof); |
79279 | 961 |
val zbox_ord: zbox ord = fn ((i, _), (j, _)) => int_ord (j, i); |
79265 | 962 |
|
79279 | 963 |
type zboxes = zbox Ord_List.T; |
964 |
val union_zboxes = Ord_List.union zbox_ord; |
|
965 |
val unions_zboxes = Ord_List.unions zbox_ord; |
|
966 |
val add_zboxes = Ord_List.insert zbox_ord; |
|
79265 | 967 |
|
968 |
local |
|
969 |
||
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
970 |
fun close_prop prems prop = |
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
971 |
fold_rev (fn A => fn B => ZApp (ZApp (ZConst0 "Pure.imp", A), B)) prems prop; |
79265 | 972 |
|
79428
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents:
79427
diff
changeset
|
973 |
fun close_proof prems prf = |
79265 | 974 |
let |
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
975 |
val m = length prems - 1; |
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
976 |
val bounds = ZTerms.build (prems |> fold_index (fn (i, h) => ZTerms.update (h, m - i))); |
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
977 |
fun get_bound lev t = ZTerms.lookup bounds t |> Option.map (fn i => ZBoundp (lev + i)); |
79265 | 978 |
|
979 |
fun proof lev (ZHyp t) = |
|
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
980 |
(case get_bound lev t of |
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
981 |
SOME p => p |
79428
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents:
79427
diff
changeset
|
982 |
| NONE => raise ZTERM ("Loose bound in proof term", [], [t], [prf])) |
80293 | 983 |
| proof lev (OFCLASSp C) = |
984 |
(case get_bound lev (OFCLASS C) of |
|
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
985 |
SOME p => p |
79428
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents:
79427
diff
changeset
|
986 |
| NONE => raise Same.SAME) |
79265 | 987 |
| proof lev (ZAbst (x, T, p)) = ZAbst (x, T, proof lev p) |
988 |
| proof lev (ZAbsp (x, t, p)) = ZAbsp (x, t, proof (lev + 1) p) |
|
989 |
| proof lev (ZAppt (p, t)) = ZAppt (proof lev p, t) |
|
990 |
| proof lev (ZAppp (p, q)) = |
|
79326 | 991 |
(ZAppp (proof lev p, Same.commit (proof lev) q) |
992 |
handle Same.SAME => ZAppp (p, proof lev q)) |
|
79265 | 993 |
| proof _ _ = raise Same.SAME; |
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
994 |
in ZAbsps prems (Same.commit (proof 0) prf) end; |
79265 | 995 |
|
80286 | 996 |
fun box_proof {unconstrain} thy thm_name hyps concl prf = |
79265 | 997 |
let |
79428
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents:
79427
diff
changeset
|
998 |
val {zterm, ...} = zterm_cache thy; |
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
999 |
|
79477 | 1000 |
val present_set_prf = |
1001 |
ZTVars.build ((fold_proof_types {hyps = true} o fold_tvars) ZTVars.add_set prf); |
|
79475
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
1002 |
val present_set = |
9eb9882f1845
more thorough treatment of hidden type variables within zproof;
wenzelm
parents:
79474
diff
changeset
|
1003 |
Types.build (Types.add_atyps concl #> fold Types.add_atyps hyps #> |
79478 | 1004 |
ZTVars.fold (Types.add_set o typ_of_tvar o #1) present_set_prf); |
79429
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1005 |
val ucontext as {constraints, outer_constraints, ...} = |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1006 |
Logic.unconstrain_context [] present_set; |
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1007 |
|
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1008 |
val typ_operation = #typ_operation ucontext {strip_sorts = true}; |
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1009 |
val unconstrain_typ = Same.commit typ_operation; |
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1010 |
val unconstrain_ztyp = |
80579 | 1011 |
#apply (ZTypes.unsynchronized_cache |
1012 |
(Same.function_eq (op =) (typ_of #> unconstrain_typ #> ztyp_of))); |
|
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1013 |
val unconstrain_zterm = zterm o Term.map_types typ_operation; |
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1014 |
val unconstrain_proof = Same.commit (map_proof_types {hyps = true} unconstrain_ztyp); |
79265 | 1015 |
|
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1016 |
val constrain_instT = |
79429
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1017 |
if unconstrain then ZTVars.empty |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1018 |
else |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1019 |
ZTVars.build (present_set |> Types.fold (fn (T, _) => |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1020 |
let |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1021 |
val ZTVar v = ztyp_of (unconstrain_typ T) and U = ztyp_of T; |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1022 |
val equal = (case U of ZTVar u => u = v | _ => false); |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1023 |
in not equal ? ZTVars.add (v, U) end)); |
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1024 |
val constrain_proof = |
79426 | 1025 |
if ZTVars.is_empty constrain_instT then I |
1026 |
else |
|
1027 |
map_proof_types {hyps = true} |
|
1028 |
(subst_type_same (Same.function (ZTVars.lookup constrain_instT))); |
|
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1029 |
|
79429
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1030 |
val hyps' = map unconstrain_zterm hyps; |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1031 |
val prems = map (zterm o #2) constraints @ hyps'; |
79419
93f26d333fb5
clarified box_proof: use sort constraints within the logic;
wenzelm
parents:
79418
diff
changeset
|
1032 |
val prop' = beta_norm_term (close_prop prems (unconstrain_zterm concl)); |
79428
4cd892d1a676
omit syntactic of_class check, which is in conflict with sort constraints within the logic;
wenzelm
parents:
79427
diff
changeset
|
1033 |
val prf' = beta_norm_prooft (close_proof prems (unconstrain_proof prf)); |
79265 | 1034 |
|
1035 |
val i = serial (); |
|
79312 | 1036 |
val zbox: zbox = (i, (prop', prf')); |
79429
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1037 |
|
80286 | 1038 |
val proof_name = |
1039 |
ZThm {theory_name = Context.theory_long_name thy, thm_name = thm_name, serial = i}; |
|
1040 |
||
1041 |
val const = constrain_proof (ZConstp (zproof_const (proof_name, prop'))); |
|
79429
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1042 |
val args1 = |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1043 |
outer_constraints |> map (fn (T, c) => |
80293 | 1044 |
OFCLASSp (ztyp_of (if unconstrain then unconstrain_typ T else T), c)); |
79429
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1045 |
val args2 = if unconstrain then map ZHyp hyps' else map (ZHyp o zterm) hyps; |
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1046 |
in (zbox, ZAppps (ZAppps (const, args1), args2)) end; |
79312 | 1047 |
|
79313
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
1048 |
in |
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
1049 |
|
80286 | 1050 |
val thm_proof = box_proof {unconstrain = false}; |
79313
3b03af5125de
use strict Global_Theory.register_proofs as checkpoint for stored zproof, and thus reduce current zboxes;
wenzelm
parents:
79312
diff
changeset
|
1051 |
|
79429
637d7d896929
more zproofs, underlying Proofterm.unconstrain_thm_proof / Thm.unconstrainT;
wenzelm
parents:
79428
diff
changeset
|
1052 |
fun add_box_proof unconstrain thy hyps concl prf zboxes = |
79422 | 1053 |
let |
80286 | 1054 |
val (zbox, prf') = box_proof unconstrain thy Thm_Name.none hyps concl prf; |
79422 | 1055 |
val zboxes' = add_zboxes zbox zboxes; |
1056 |
in (prf', zboxes') end; |
|
79265 | 1057 |
|
1058 |
end; |
|
1059 |
||
1060 |
||
79161 | 1061 |
(* basic logic *) |
1062 |
||
80265 | 1063 |
fun zproof_axiom thy name A = zproof_const (ZAxiom name, zterm_of thy A); |
1064 |
val axiom_proof = ZConstp ooo zproof_axiom; |
|
79161 | 1065 |
|
79263 | 1066 |
fun oracle_proof thy name A = |
80262 | 1067 |
ZConstp (zproof_const (ZOracle name, zterm_of thy A)); |
79119 | 1068 |
|
1069 |
fun assume_proof thy A = |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1070 |
ZHyp (zterm_of thy A); |
79119 | 1071 |
|
1072 |
fun trivial_proof thy A = |
|
79212 | 1073 |
ZAbsp ("H", zterm_of thy A, ZBoundp 0); |
79119 | 1074 |
|
79414 | 1075 |
fun implies_intr_proof' h prf = |
79119 | 1076 |
let |
79277 | 1077 |
fun proof lev (ZHyp t) = if aconv_zterm (h, t) then ZBoundp lev else raise Same.SAME |
1078 |
| proof lev (ZAbst (x, T, p)) = ZAbst (x, T, proof lev p) |
|
1079 |
| proof lev (ZAbsp (x, t, p)) = ZAbsp (x, t, proof (lev + 1) p) |
|
1080 |
| proof lev (ZAppt (p, t)) = ZAppt (proof lev p, t) |
|
1081 |
| proof lev (ZAppp (p, q)) = |
|
79326 | 1082 |
(ZAppp (proof lev p, Same.commit (proof lev) q) |
1083 |
handle Same.SAME => ZAppp (p, proof lev q)) |
|
79157 | 1084 |
| proof _ _ = raise Same.SAME; |
79212 | 1085 |
in ZAbsp ("H", h, Same.commit (proof 0) prf) end; |
79119 | 1086 |
|
79414 | 1087 |
fun implies_intr_proof thy = implies_intr_proof' o zterm_of thy; |
1088 |
||
79124 | 1089 |
fun forall_intr_proof thy T (a, x) prf = |
79119 | 1090 |
let |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1091 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1092 |
val Z = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1093 |
val z = zterm x; |
79119 | 1094 |
|
79157 | 1095 |
fun term i b = |
79119 | 1096 |
if aconv_zterm (b, z) then ZBound i |
1097 |
else |
|
1098 |
(case b of |
|
79157 | 1099 |
ZAbs (x, T, t) => ZAbs (x, T, term (i + 1) t) |
79156 | 1100 |
| ZApp (t, u) => |
79326 | 1101 |
(ZApp (term i t, Same.commit (term i) u) |
1102 |
handle Same.SAME => ZApp (t, term i u)) |
|
79156 | 1103 |
| _ => raise Same.SAME); |
79119 | 1104 |
|
79157 | 1105 |
fun proof i (ZAbst (x, T, prf)) = ZAbst (x, T, proof (i + 1) prf) |
79212 | 1106 |
| proof i (ZAbsp (x, t, prf)) = |
79326 | 1107 |
(ZAbsp (x, term i t, Same.commit (proof i) prf) |
1108 |
handle Same.SAME => ZAbsp (x, t, proof i prf)) |
|
79157 | 1109 |
| proof i (ZAppt (p, t)) = |
79326 | 1110 |
(ZAppt (proof i p, Same.commit (term i) t) |
1111 |
handle Same.SAME => ZAppt (p, term i t)) |
|
79212 | 1112 |
| proof i (ZAppp (p, q)) = |
79326 | 1113 |
(ZAppp (proof i p, Same.commit (proof i) q) |
1114 |
handle Same.SAME => ZAppp (p, proof i q)) |
|
79157 | 1115 |
| proof _ _ = raise Same.SAME; |
79119 | 1116 |
|
79157 | 1117 |
in ZAbst (a, Z, Same.commit (proof 0) prf) end; |
79119 | 1118 |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1119 |
fun forall_elim_proof thy t p = ZAppt (p, zterm_of thy t); |
79119 | 1120 |
|
80293 | 1121 |
fun of_class_proof (T, c) = OFCLASSp (ztyp_of T, c); |
79128 | 1122 |
|
79124 | 1123 |
|
1124 |
(* equality *) |
|
1125 |
||
1126 |
local |
|
1127 |
||
1128 |
val thy0 = |
|
1129 |
Context.the_global_context () |
|
1130 |
|> Sign.add_types_global [(Binding.name "fun", 2, NoSyn), (Binding.name "prop", 0, NoSyn)] |
|
1131 |
|> Sign.local_path |
|
1132 |
|> Sign.add_consts |
|
1133 |
[(Binding.name "all", (Term.aT [] --> propT) --> propT, NoSyn), |
|
1134 |
(Binding.name "imp", propT --> propT --> propT, NoSyn), |
|
1135 |
(Binding.name "eq", Term.aT [] --> Term.aT [] --> propT, NoSyn)]; |
|
1136 |
||
1137 |
val [reflexive_axiom, symmetric_axiom, transitive_axiom, equal_intr_axiom, equal_elim_axiom, |
|
1138 |
abstract_rule_axiom, combination_axiom] = |
|
80265 | 1139 |
Theory.equality_axioms |> map (fn (b, t) => zproof_axiom thy0 (Sign.full_name thy0 b) t); |
79124 | 1140 |
|
1141 |
in |
|
1142 |
||
1143 |
val is_reflexive_proof = |
|
80262 | 1144 |
fn ZConstp ((ZAxiom "Pure.reflexive", _), _) => true | _ => false; |
79124 | 1145 |
|
1146 |
fun reflexive_proof thy T t = |
|
1147 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1148 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1149 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1150 |
val x = zterm t; |
79263 | 1151 |
in make_const_proof (fn "'a" => A, fn "x" => x) reflexive_axiom end; |
79124 | 1152 |
|
1153 |
fun symmetric_proof thy T t u prf = |
|
1154 |
if is_reflexive_proof prf then prf |
|
1155 |
else |
|
1156 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1157 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1158 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1159 |
val x = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1160 |
val y = zterm u; |
79263 | 1161 |
val ax = make_const_proof (fn "'a" => A, fn "x" => x | "y" => y) symmetric_axiom; |
79212 | 1162 |
in ZAppp (ax, prf) end; |
79124 | 1163 |
|
1164 |
fun transitive_proof thy T t u v prf1 prf2 = |
|
1165 |
if is_reflexive_proof prf1 then prf2 |
|
1166 |
else if is_reflexive_proof prf2 then prf1 |
|
1167 |
else |
|
1168 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1169 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1170 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1171 |
val x = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1172 |
val y = zterm u; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1173 |
val z = zterm v; |
79263 | 1174 |
val ax = make_const_proof (fn "'a" => A, fn "x" => x | "y" => y | "z" => z) transitive_axiom; |
79212 | 1175 |
in ZAppp (ZAppp (ax, prf1), prf2) end; |
79124 | 1176 |
|
1177 |
fun equal_intr_proof thy t u prf1 prf2 = |
|
1178 |
let |
|
79160 | 1179 |
val {zterm, ...} = zterm_cache thy; |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1180 |
val A = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1181 |
val B = zterm u; |
79263 | 1182 |
val ax = make_const_proof (undefined, fn "A" => A | "B" => B) equal_intr_axiom; |
79212 | 1183 |
in ZAppp (ZAppp (ax, prf1), prf2) end; |
79124 | 1184 |
|
1185 |
fun equal_elim_proof thy t u prf1 prf2 = |
|
1186 |
let |
|
79160 | 1187 |
val {zterm, ...} = zterm_cache thy; |
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1188 |
val A = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1189 |
val B = zterm u; |
79263 | 1190 |
val ax = make_const_proof (undefined, fn "A" => A | "B" => B) equal_elim_axiom; |
79212 | 1191 |
in ZAppp (ZAppp (ax, prf1), prf2) end; |
79124 | 1192 |
|
1193 |
fun abstract_rule_proof thy T U x t u prf = |
|
1194 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1195 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1196 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1197 |
val B = ztyp U; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1198 |
val f = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1199 |
val g = zterm u; |
79126 | 1200 |
val ax = |
79263 | 1201 |
make_const_proof (fn "'a" => A | "'b" => B, fn "f" => f | "g" => g) |
79126 | 1202 |
abstract_rule_axiom; |
79212 | 1203 |
in ZAppp (ax, forall_intr_proof thy T x prf) end; |
79124 | 1204 |
|
1205 |
fun combination_proof thy T U f g t u prf1 prf2 = |
|
1206 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1207 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1208 |
val A = ztyp T; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1209 |
val B = ztyp U; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1210 |
val f' = zterm f; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1211 |
val g' = zterm g; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1212 |
val x = zterm t; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1213 |
val y = zterm u; |
79124 | 1214 |
val ax = |
79263 | 1215 |
make_const_proof (fn "'a" => A | "'b" => B, fn "f" => f' | "g" => g' | "x" => x | "y" => y) |
79124 | 1216 |
combination_axiom; |
79212 | 1217 |
in ZAppp (ZAppp (ax, prf1), prf2) end; |
79124 | 1218 |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
1219 |
end; |
79124 | 1220 |
|
79133 | 1221 |
|
1222 |
(* substitution *) |
|
1223 |
||
1224 |
fun generalize_proof (tfrees, frees) idx prf = |
|
1225 |
let |
|
1226 |
val typ = |
|
79326 | 1227 |
if Names.is_empty tfrees then Same.same |
1228 |
else |
|
80579 | 1229 |
#apply (ZTypes.unsynchronized_cache |
79163 | 1230 |
(subst_type_same (fn ((a, i), S) => |
1231 |
if i = ~1 andalso Names.defined tfrees a then ZTVar ((a, idx), S) |
|
80579 | 1232 |
else raise Same.SAME))); |
79136 | 1233 |
val term = |
79147 | 1234 |
subst_term_same typ (fn ((x, i), T) => |
1235 |
if i = ~1 andalso Names.defined frees x then ZVar ((x, idx), T) |
|
1236 |
else raise Same.SAME); |
|
79418 | 1237 |
in map_proof {hyps = false} typ term prf end; |
79133 | 1238 |
|
79149 | 1239 |
fun instantiate_proof thy (Ts, ts) prf = |
1240 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1241 |
val {ztyp, zterm} = zterm_cache thy; |
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1242 |
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
|
1243 |
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
|
1244 |
val typ = instantiate_type_same instT; |
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
1245 |
val term = instantiate_term_same typ inst; |
79418 | 1246 |
in map_proof {hyps = false} typ term prf end; |
79149 | 1247 |
|
79135 | 1248 |
fun varifyT_proof names prf = |
1249 |
if null names then prf |
|
1250 |
else |
|
1251 |
let |
|
1252 |
val tab = ZTVars.build (names |> fold (fn ((a, S), b) => ZTVars.add (((a, ~1), S), b))); |
|
79136 | 1253 |
val typ = |
80579 | 1254 |
#apply (ZTypes.unsynchronized_cache (subst_type_same (fn v => |
79136 | 1255 |
(case ZTVars.lookup tab v of |
1256 |
NONE => raise Same.SAME |
|
80579 | 1257 |
| SOME w => ZTVar w)))); |
79418 | 1258 |
in map_proof_types {hyps = false} typ prf end; |
79135 | 1259 |
|
79152 | 1260 |
fun legacy_freezeT_proof t prf = |
1261 |
(case Type.legacy_freezeT t of |
|
1262 |
NONE => prf |
|
1263 |
| SOME f => |
|
1264 |
let |
|
1265 |
val tvar = ztyp_of o Same.function f; |
|
80579 | 1266 |
val typ = #apply (ZTypes.unsynchronized_cache (subst_type_same tvar)); |
79418 | 1267 |
in map_proof_types {hyps = false} typ prf end); |
79152 | 1268 |
|
79155 | 1269 |
|
1270 |
(* permutations *) |
|
1271 |
||
1272 |
fun rotate_proof thy Bs Bi' params asms m prf = |
|
1273 |
let |
|
79158
3c7ab17380a8
performance tuning: cache for ztyp_of within zterm_of;
wenzelm
parents:
79157
diff
changeset
|
1274 |
val {ztyp, zterm} = zterm_cache thy; |
79155 | 1275 |
val i = length asms; |
1276 |
val j = length Bs; |
|
1277 |
in |
|
79212 | 1278 |
ZAbsps (map zterm Bs @ [zterm Bi']) (ZAppps (prf, map ZBoundp |
1279 |
(j downto 1) @ [ZAbsts (map (apsnd ztyp) params) (ZAbsps (map zterm asms) |
|
1280 |
(ZAppps (ZAppts (ZBoundp i, map ZBound ((length params - 1) downto 0)), |
|
79326 | 1281 |
map ZBoundp (((i - m - 1) downto 0) @ ((i - 1) downto (i - m))))))])) |
79155 | 1282 |
end; |
1283 |
||
1284 |
fun permute_prems_proof thy prems' j k prf = |
|
1285 |
let |
|
79160 | 1286 |
val {zterm, ...} = zterm_cache thy; |
79155 | 1287 |
val n = length prems'; |
1288 |
in |
|
79212 | 1289 |
ZAbsps (map zterm prems') |
79326 | 1290 |
(ZAppps (prf, map ZBoundp ((n - 1 downto n - j) @ (k - 1 downto 0) @ (n - j - 1 downto k)))) |
79155 | 1291 |
end; |
1292 |
||
79211 | 1293 |
|
79234 | 1294 |
(* lifting *) |
1295 |
||
1296 |
fun incr_tvar_same inc = |
|
1297 |
if inc = 0 then Same.same |
|
1298 |
else |
|
1299 |
let fun tvar ((a, i), S) = if i >= 0 then ZTVar ((a, i + inc), S) else raise Same.SAME |
|
80579 | 1300 |
in #apply (ZTypes.unsynchronized_cache (subst_type_same tvar)) end; |
79234 | 1301 |
|
1302 |
fun incr_indexes_proof inc prf = |
|
1303 |
let |
|
1304 |
val typ = incr_tvar_same inc; |
|
1305 |
fun var ((x, i), T) = if i >= 0 then ZVar ((x, i + inc), T) else raise Same.SAME; |
|
1306 |
val term = subst_term_same typ var; |
|
79418 | 1307 |
in map_proof {hyps = false} typ term prf end; |
79234 | 1308 |
|
1309 |
fun lift_proof thy gprop inc prems prf = |
|
1310 |
let |
|
1311 |
val {ztyp, zterm} = zterm_cache thy; |
|
1312 |
||
1313 |
val typ = incr_tvar_same inc; |
|
1314 |
||
1315 |
fun term Ts lev = |
|
1316 |
if null Ts andalso inc = 0 then Same.same |
|
1317 |
else |
|
1318 |
let |
|
1319 |
val n = length Ts; |
|
1320 |
fun incr lev (ZVar ((x, i), T)) = |
|
1321 |
if i >= 0 then combound (ZVar ((x, i + inc), ZFuns (Ts, Same.commit typ T)), lev, n) |
|
1322 |
else raise Same.SAME |
|
1323 |
| incr _ (ZBound _) = raise Same.SAME |
|
1324 |
| incr _ (ZConst0 _) = raise Same.SAME |
|
1325 |
| incr _ (ZConst1 (c, T)) = ZConst1 (c, typ T) |
|
1326 |
| incr _ (ZConst (c, Ts)) = ZConst (c, Same.map typ Ts) |
|
1327 |
| incr lev (ZAbs (x, T, t)) = |
|
1328 |
(ZAbs (x, typ T, Same.commit (incr (lev + 1)) t) |
|
1329 |
handle Same.SAME => ZAbs (x, T, incr (lev + 1) t)) |
|
1330 |
| incr lev (ZApp (t, u)) = |
|
1331 |
(ZApp (incr lev t, Same.commit (incr lev) u) |
|
1332 |
handle Same.SAME => ZApp (t, incr lev u)) |
|
80293 | 1333 |
| incr _ (OFCLASS (T, c)) = OFCLASS (typ T, c); |
79234 | 1334 |
in incr lev end; |
1335 |
||
1336 |
fun proof Ts lev (ZAbst (a, T, p)) = |
|
1337 |
(ZAbst (a, typ T, Same.commit (proof Ts (lev + 1)) p) |
|
1338 |
handle Same.SAME => ZAbst (a, T, proof Ts (lev + 1) p)) |
|
1339 |
| proof Ts lev (ZAbsp (a, t, p)) = |
|
1340 |
(ZAbsp (a, term Ts lev t, Same.commit (proof Ts lev) p) |
|
1341 |
handle Same.SAME => ZAbsp (a, t, proof Ts lev p)) |
|
1342 |
| proof Ts lev (ZAppt (p, t)) = |
|
1343 |
(ZAppt (proof Ts lev p, Same.commit (term Ts lev) t) |
|
1344 |
handle Same.SAME => ZAppt (p, term Ts lev t)) |
|
1345 |
| proof Ts lev (ZAppp (p, q)) = |
|
1346 |
(ZAppp (proof Ts lev p, Same.commit (proof Ts lev) q) |
|
1347 |
handle Same.SAME => ZAppp (p, proof Ts lev q)) |
|
80262 | 1348 |
| proof Ts lev (ZConstp (a, insts)) = |
1349 |
ZConstp (a, map_insts_same typ (term Ts lev) insts) |
|
80293 | 1350 |
| proof _ _ (OFCLASSp (T, c)) = OFCLASSp (typ T, c) |
79234 | 1351 |
| proof _ _ _ = raise Same.SAME; |
1352 |
||
1353 |
val k = length prems; |
|
1354 |
||
1355 |
fun mk_app b (i, j, p) = |
|
1356 |
if b then (i - 1, j, ZAppp (p, ZBoundp i)) else (i, j - 1, ZAppt (p, ZBound j)); |
|
1357 |
||
1358 |
fun lift Ts bs i j (Const ("Pure.imp", _) $ A $ B) = |
|
1359 |
ZAbsp ("H", Same.commit (term Ts 0) (zterm A), lift Ts (true :: bs) (i + 1) j B) |
|
1360 |
| lift Ts bs i j (Const ("Pure.all", _) $ Abs (a, T, t)) = |
|
1361 |
let val T' = ztyp T |
|
1362 |
in ZAbst (a, T', lift (T' :: Ts) (false :: bs) i (j + 1) t) end |
|
1363 |
| lift Ts bs i j _ = |
|
1364 |
ZAppps (Same.commit (proof (rev Ts) 0) prf, |
|
1365 |
map (fn k => (#3 (fold_rev mk_app bs (i - 1, j - 1, ZBoundp k)))) (i + k - 1 downto i)); |
|
79237 | 1366 |
in ZAbsps (map zterm prems) (lift [] [] 0 0 gprop) end; |
79234 | 1367 |
|
1368 |
||
79211 | 1369 |
(* higher-order resolution *) |
1370 |
||
1371 |
fun mk_asm_prf C i m = |
|
1372 |
let |
|
79212 | 1373 |
fun imp _ i 0 = ZBoundp i |
1374 |
| imp (ZApp (ZApp (ZConst0 "Pure.imp", A), B)) i m = ZAbsp ("H", A, imp B (i + 1) (m - 1)) |
|
1375 |
| imp _ i _ = ZBoundp i; |
|
79211 | 1376 |
fun all (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t))) = ZAbst (a, T, all t) |
1377 |
| all t = imp t (~ i) m |
|
1378 |
in all C end; |
|
1379 |
||
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
1380 |
fun assumption_proof thy envir Bs Bi n visible prf = |
79211 | 1381 |
let |
80582
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
1382 |
val cache as {zterm, ...} = zterm_cache thy; |
79211 | 1383 |
val normt = zterm #> Same.commit (norm_term_same cache envir); |
1384 |
in |
|
79212 | 1385 |
ZAbsps (map normt Bs) |
1386 |
(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
|
1387 |
|> norm_proof_cache cache envir visible |
79211 | 1388 |
end; |
1389 |
||
79261 | 1390 |
fun flatten_params_proof i j n (ZApp (ZApp (ZConst0 "Pure.imp", A), B), k) = |
1391 |
ZAbsp ("H", A, flatten_params_proof (i + 1) j n (B, k)) |
|
1392 |
| flatten_params_proof i j n (ZApp (ZConst1 ("Pure.all", _), ZAbs (a, T, t)), k) = |
|
1393 |
ZAbst (a, T, flatten_params_proof i (j + 1) n (t, k)) |
|
1394 |
| flatten_params_proof i j n (_, k) = |
|
1395 |
ZAppps (ZAppts (ZBoundp (k + i), |
|
1396 |
map ZBound (j - 1 downto 0)), map ZBoundp (remove (op =) (i - n) (i - 1 downto 0))); |
|
1397 |
||
79270
90c5aadcc4b2
more robust norm_proof: turn env into instantiation, based on visible statement;
wenzelm
parents:
79269
diff
changeset
|
1398 |
fun bicompose_proof thy env smax flatten Bs As A oldAs n m visible rprf sprf = |
79261 | 1399 |
let |
80582
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
1400 |
val cache as {zterm, ...} = zterm_cache thy; |
79261 | 1401 |
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
|
1402 |
val normp = norm_proof_cache cache env visible; |
79261 | 1403 |
|
1404 |
val lb = length Bs; |
|
1405 |
val la = length As; |
|
1406 |
||
1407 |
fun proof p q = |
|
1408 |
ZAbsps (map normt (Bs @ As)) |
|
1409 |
(ZAppp (ZAppps (q, map ZBoundp (lb + la - 1 downto la)), |
|
1410 |
ZAppps (p, (if n > 0 then [mk_asm_prf (normt (the A)) n m] else []) @ |
|
1411 |
map (if flatten then flatten_params_proof 0 0 n else ZBoundp o snd) |
|
1412 |
(map normt oldAs ~~ (la - 1 downto 0))))); |
|
1413 |
in |
|
1414 |
if Envir.is_empty env then proof rprf sprf |
|
1415 |
else proof (normp rprf) (if Envir.above env smax then sprf else normp sprf) |
|
1416 |
end; |
|
1417 |
||
79405 | 1418 |
|
1419 |
(* sort constraints within the logic *) |
|
1420 |
||
79425 | 1421 |
type sorts_proof = (class * class -> zproof) * (string * class list list * class -> zproof); |
79405 | 1422 |
|
1423 |
fun of_sort_proof algebra (classrel_proof, arity_proof) hyps = |
|
1424 |
Sorts.of_sort_derivation algebra |
|
1425 |
{class_relation = fn _ => fn _ => fn (prf, c1) => fn c2 => |
|
1426 |
if c1 = c2 then prf else ZAppp (classrel_proof (c1, c2), prf), |
|
1427 |
type_constructor = fn (a, _) => fn dom => fn c => |
|
1428 |
let val Ss = map (map snd) dom and prfs = maps (map fst) dom |
|
1429 |
in ZAppps (arity_proof (a, Ss, c), prfs) end, |
|
1430 |
type_variable = fn typ => map (fn c => (hyps (typ, c), c)) (Type.sort_of_atyp typ)}; |
|
1431 |
||
79414 | 1432 |
fun unconstrainT_proof thy sorts_proof (ucontext: Logic.unconstrain_context) = |
1433 |
let |
|
80584 | 1434 |
val algebra = Sign.classes_of thy; |
1435 |
||
80582
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
1436 |
val cache = zterm_cache thy; |
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
1437 |
val typ_cache = typ_cache thy; |
79414 | 1438 |
|
1439 |
val typ = |
|
80579 | 1440 |
#apply (ZTypes.unsynchronized_cache |
1441 |
(typ_of #> #typ_operation ucontext {strip_sorts = true} #> ztyp_of)); |
|
79414 | 1442 |
|
1443 |
val typ_sort = #typ_operation ucontext {strip_sorts = false}; |
|
1444 |
||
1445 |
fun hyps T = |
|
1446 |
(case AList.lookup (op =) (#constraints ucontext) T of |
|
1447 |
SOME t => ZHyp (#zterm cache t) |
|
1448 |
| NONE => raise Fail "unconstrainT_proof: missing constraint"); |
|
1449 |
||
1450 |
fun class (T, c) = |
|
80582
1bc7eef961ff
clarified scope of cache: avoid nested typ_cache;
wenzelm
parents:
80581
diff
changeset
|
1451 |
let val T' = Same.commit typ_sort (typ_cache T) |
79414 | 1452 |
in the_single (of_sort_proof algebra sorts_proof hyps (T', [c])) end; |
1453 |
in |
|
79418 | 1454 |
map_proof_types {hyps = false} typ #> map_class_proof class #> beta_norm_prooft |
79414 | 1455 |
#> fold_rev (implies_intr_proof' o #zterm cache o #2) (#constraints ucontext) |
1456 |
end; |
|
1457 |
||
80586 | 1458 |
|
1459 |
||
1460 |
(** XML data representation **) |
|
1461 |
||
1462 |
(* encode *) |
|
1463 |
||
1464 |
local |
|
1465 |
||
1466 |
open XML.Encode Term_XML.Encode; |
|
1467 |
||
1468 |
fun ztyp T = T |> variant |
|
1469 |
[fn ZFun args => (["fun"], pair ztyp ztyp args) |
|
1470 |
| ZProp => (["prop"], []) |
|
1471 |
| ZType0 a => ([a], []) |
|
1472 |
| ZType1 (a, b) => ([a], list ztyp [b]) |
|
1473 |
| ZType (a, b) => ([a], list ztyp b), |
|
1474 |
fn ZTVar ((a, ~1), b) => ([a], sort b), |
|
1475 |
fn ZTVar (a, b) => (indexname a, sort b)]; |
|
1476 |
||
1477 |
fun zvar_type {typed_vars} T = |
|
1478 |
if typed_vars andalso T <> ztyp_dummy then ztyp T else []; |
|
1479 |
||
1480 |
fun zterm flag t = t |> variant |
|
1481 |
[fn ZConst0 a => ([a], []) |
|
1482 |
| ZConst1 (a, b) => ([a], list ztyp [b]) |
|
1483 |
| ZConst (a, b) => ([a], list ztyp b), |
|
1484 |
fn ZVar ((a, ~1), b) => ([a], zvar_type flag b), |
|
1485 |
fn ZVar (a, b) => (indexname a, zvar_type flag b), |
|
1486 |
fn ZBound a => ([], int a), |
|
1487 |
fn ZAbs (a, b, c) => ([a], pair ztyp (zterm flag) (b, c)), |
|
1488 |
fn ZApp a => ([], pair (zterm flag) (zterm flag) a), |
|
1489 |
fn OFCLASS (a, b) => ([b], ztyp a)]; |
|
1490 |
||
1491 |
fun zproof flag prf = prf |> variant |
|
1492 |
[fn ZNop => ([], []), |
|
1493 |
fn ZBoundp a => ([], int a), |
|
1494 |
fn ZAbst (a, b, c) => ([a], pair ztyp (zproof flag) (b, c)), |
|
1495 |
fn ZAbsp (a, b, c) => ([a], pair (zterm flag) (zproof flag) (b, c)), |
|
1496 |
fn ZAppt a => ([], pair (zproof flag) (zterm flag) a), |
|
1497 |
fn ZAppp a => ([], pair (zproof flag) (zproof flag) a), |
|
1498 |
fn ZHyp a => ([], zterm flag a), |
|
1499 |
fn ZConstp (c as ((ZAxiom a, b), _)) => ([a], list (ztyp o #2) (zproof_const_typargs c)), |
|
1500 |
fn OFCLASSp (a, b) => ([b], ztyp a), |
|
1501 |
fn ZConstp (c as ((ZOracle a, b), _)) => ([a], list (ztyp o #2) (zproof_const_typargs c)), |
|
1502 |
fn ZConstp (c as ((ZThm {theory_name, thm_name = (name, _), serial}, b), _)) => |
|
1503 |
([int_atom serial, theory_name, #1 name, int_atom (#2 name)], |
|
1504 |
list (ztyp o #2) (zproof_const_typargs c))]; |
|
1505 |
||
1506 |
in |
|
1507 |
||
1508 |
val encode_ztyp = ztyp; |
|
1509 |
val encode_zterm = zterm; |
|
1510 |
val encode_zproof = zproof; |
|
1511 |
||
79124 | 1512 |
end; |
80586 | 1513 |
|
80608
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1514 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1515 |
(* standardization of variables for export: only frees and named bounds *) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1516 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1517 |
local |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1518 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1519 |
fun declare_frees_term t = fold_vars (fn ((x, ~1), _) => Name.declare x | _ => I) t; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1520 |
val declare_frees_proof = fold_proof {hyps = true} (K I) declare_frees_term; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1521 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1522 |
val (variant_abs_term, variant_abs_proof) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1523 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1524 |
fun term bs (ZAbs (x, T, t)) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1525 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1526 |
val x' = #1 (Name.variant x (declare_frees_term t bs)); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1527 |
val bs' = Name.declare x' bs; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1528 |
in ZAbs (x', T, Same.commit_if (x <> x') (term bs') t) end |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1529 |
| term bs (ZApp (t, u)) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1530 |
(ZApp (term bs t, Same.commit (term bs) u) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1531 |
handle Same.SAME => ZApp (t, term bs u)) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1532 |
| term _ _ = raise Same.SAME; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1533 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1534 |
fun proof bs (ZAbst (x, T, p)) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1535 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1536 |
val x' = #1 (Name.variant x (declare_frees_proof p bs)); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1537 |
val bs' = Name.declare x' bs; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1538 |
in ZAbst (x', T, Same.commit_if (x <> x') (proof bs') p) end |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1539 |
| proof bs (ZAbsp (x, t, p)) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1540 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1541 |
val x' = #1 (Name.variant x (declare_frees_term t (declare_frees_proof p bs))); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1542 |
val (t', term_eq) = Same.commit_id (term bs) t; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1543 |
val bs' = Name.declare x' bs; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1544 |
in ZAbsp (x', t', Same.commit_if (x <> x' orelse not term_eq) (proof bs') p) end |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1545 |
| proof bs (ZAppt (p, t)) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1546 |
(ZAppt (proof bs p, Same.commit (term bs) t) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1547 |
handle Same.SAME => ZAppt (p, term bs t)) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1548 |
| proof bs (ZAppp (p, q)) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1549 |
(ZAppp (proof bs p, Same.commit (proof bs) q) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1550 |
handle Same.SAME => ZAppp (p, proof bs q)) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1551 |
| proof bs (ZHyp t) = ZHyp (term bs t) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1552 |
| proof _ _ = raise Same.SAME; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1553 |
in (term Name.context, proof Name.context) end; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1554 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1555 |
val used_frees_type = fold_tvars (fn ((a, ~1), _) => Name.declare a | _ => I); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1556 |
fun used_frees_term t = fold_types used_frees_type t #> declare_frees_term t; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1557 |
val used_frees_proof = fold_proof {hyps = true} used_frees_type used_frees_term; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1558 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1559 |
fun hidden_types prop proof = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1560 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1561 |
val visible = (fold_types o fold_tvars) (insert (op =)) prop []; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1562 |
val add_hiddenT = fold_tvars (fn v => not (member (op =) visible v) ? insert (op =) v); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1563 |
in rev (fold_proof {hyps = true} add_hiddenT (fold_types add_hiddenT) proof []) end; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1564 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1565 |
fun standard_hidden_types prop proof = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1566 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1567 |
val hidden = hidden_types prop proof; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1568 |
val idx = maxidx_term prop (maxidx_proof proof ~1) + 1; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1569 |
fun zvar (v as (_, S)) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1570 |
if not (member (op =) hidden v) then raise Same.SAME |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1571 |
else if null S then ztyp_dummy |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1572 |
else ZTVar (("'", idx), S); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1573 |
val typ = map_tvars_same zvar; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1574 |
in proof |> not (null hidden) ? map_proof {hyps = true} typ (map_types typ) end; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1575 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1576 |
fun standard_hidden_terms prop proof = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1577 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1578 |
fun add_unless excluded (ZVar v) = not (member (op =) excluded v) ? insert (op =) v |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1579 |
| add_unless _ _ = I; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1580 |
val visible = fold_aterms (add_unless []) prop []; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1581 |
val hidden = fold_proof {hyps = true} (K I) (fold_aterms (add_unless visible)) proof []; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1582 |
fun var (v as (_, T)) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1583 |
if member (op =) hidden v then zterm_dummy_pattern T else raise Same.SAME; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1584 |
val term = map_vars_same var; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1585 |
in proof |> not (null hidden) ? map_proof {hyps = true} Same.same term end; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1586 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1587 |
fun standard_inst add mk (v as ((x, i), T)) (inst, used) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1588 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1589 |
val (x', used') = Name.variant_bound x used; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1590 |
val inst' = if x = x' andalso i = ~1 then inst else add (v, mk ((x', ~1), T)) inst; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1591 |
in (inst', used') end; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1592 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1593 |
fun standard_inst_type used prf = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1594 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1595 |
val add_tvars = fold_tvars (fn v => ZTVars.add (v, ())); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1596 |
val (instT, _) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1597 |
(ZTVars.empty, used) |> ZTVars.fold (standard_inst ZTVars.add ZTVar o #1) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1598 |
(TVars.build (prf |> fold_proof {hyps = true} add_tvars (fold_types add_tvars))); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1599 |
in instantiate_type_same instT end; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1600 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1601 |
fun standard_inst_term used inst_type prf = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1602 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1603 |
val add_vars = fold_vars (fn (x, T) => ZVars.add ((x, Same.commit inst_type T), ())); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1604 |
val (inst, _) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1605 |
(ZVars.empty, used) |> ZVars.fold (standard_inst ZVars.add ZVar o #1) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1606 |
(ZVars.build (prf |> fold_proof {hyps = true} (K I) add_vars)); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1607 |
in instantiate_term_same inst_type inst end; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1608 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1609 |
val typargs_type = fold_tvars (fn ((a, ~1), S) => TFrees.add_set (a, S) | _ => I); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1610 |
val typargs_term = fold_types typargs_type; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1611 |
val typargs_proof = fold_proof {hyps = true} typargs_type typargs_term; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1612 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1613 |
val add_standard_vars_term = fold_aterms |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1614 |
(fn ZVar ((x, ~1), T) => |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1615 |
(fn env => |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1616 |
(case AList.lookup (op =) env x of |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1617 |
NONE => (x, T) :: env |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1618 |
| SOME T' => |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1619 |
if T = T' then env |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1620 |
else |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1621 |
raise TYPE ("standard_vars_env: type conflict for variable " ^ quote x, |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1622 |
[typ_of T, typ_of T'], []))) |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1623 |
| _ => I); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1624 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1625 |
val add_standard_vars = fold_proof {hyps = true} (K I) add_standard_vars_term; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1626 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1627 |
in |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1628 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1629 |
fun standard_vars used (prop, opt_prf) = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1630 |
let |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1631 |
val prf = the_default ZNop opt_prf |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1632 |
|> standard_hidden_types prop |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1633 |
|> standard_hidden_terms prop; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1634 |
val prop_prf = ZAppp (ZHyp prop, prf); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1635 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1636 |
val used' = used |> used_frees_proof prop_prf; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1637 |
val inst_type = standard_inst_type used' prop_prf; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1638 |
val inst_term = standard_inst_term used' inst_type prop_prf; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1639 |
val inst_proof = map_proof_same {hyps = true} inst_type inst_term; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1640 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1641 |
val prop' = prop |> Same.commit (Same.compose variant_abs_term inst_term); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1642 |
val opt_proof' = |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1643 |
if is_none opt_prf then NONE |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1644 |
else SOME (prf |> Same.commit (Same.compose variant_abs_proof inst_proof)); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1645 |
val proofs' = the_list opt_proof'; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1646 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1647 |
val args = build_rev (add_standard_vars_term prop' #> fold add_standard_vars proofs'); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1648 |
val typargs = TFrees.list_set (TFrees.build (typargs_term prop' #> fold typargs_proof proofs')); |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1649 |
in {typargs = typargs, args = args, prop = prop', proof = opt_proof'} end; |
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1650 |
|
80588 | 1651 |
end; |
80608
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1652 |
|
0b8922e351a3
clarified export: replaced Proofterm.standard_vars by ZTerm.standard_vars;
wenzelm
parents:
80605
diff
changeset
|
1653 |
end; |