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