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