author | wenzelm |
Tue, 05 Dec 2023 21:31:28 +0100 | |
changeset 79136 | bbef5d3ed56b |
parent 79135 | db2dc7634d62 |
child 79144 | 42ca72f06632 |
permissions | -rw-r--r-- |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/zterm.ML |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
3 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
4 |
Tight representation of types / terms / proof terms, notably for proof recording. |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
5 |
*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
6 |
|
79124 | 7 |
(*** global ***) |
8 |
||
9 |
(* types and terms *) |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
10 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
11 |
datatype ztyp = |
79119 | 12 |
ZTVar of indexname * sort (*free: index ~1*) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
13 |
| ZFun of ztyp * ztyp |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
14 |
| ZProp |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
15 |
| ZItself of ztyp |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
16 |
| ZType0 of string (*type constant*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
17 |
| ZType1 of string * ztyp (*type constructor: 1 argument*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
18 |
| ZType of string * ztyp list (*type constructor: >= 2 arguments*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
19 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
20 |
datatype zterm = |
79119 | 21 |
ZVar of indexname * ztyp (*free: index ~1*) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
22 |
| ZBound of int |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
23 |
| ZConst0 of string (*monomorphic constant*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
24 |
| ZConst1 of string * ztyp (*polymorphic constant: 1 type argument*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
25 |
| ZConst of string * ztyp list (*polymorphic constant: >= 2 type arguments*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
26 |
| ZAbs of string * ztyp * zterm |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
27 |
| ZApp of zterm * zterm |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
28 |
| ZClass of ztyp * class (*OFCLASS proposition*) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
29 |
|
79124 | 30 |
structure ZTerm = |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
31 |
struct |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
32 |
|
79119 | 33 |
(* fold *) |
34 |
||
35 |
fun fold_tvars f (ZTVar v) = f v |
|
36 |
| fold_tvars f (ZFun (T, U)) = fold_tvars f T #> fold_tvars f U |
|
37 |
| fold_tvars f (ZItself T) = fold_tvars f T |
|
38 |
| fold_tvars f (ZType1 (_, T)) = fold_tvars f T |
|
39 |
| fold_tvars f (ZType (_, Ts)) = fold (fold_tvars f) Ts |
|
40 |
| fold_tvars _ _ = I; |
|
41 |
||
42 |
fun fold_aterms f (ZApp (t, u)) = fold_aterms f t #> fold_aterms f u |
|
43 |
| fold_aterms f (ZAbs (_, _, t)) = fold_aterms f t |
|
44 |
| fold_aterms f a = f a; |
|
45 |
||
46 |
fun fold_types f (ZVar (_, T)) = f T |
|
47 |
| fold_types f (ZConst1 (_, T)) = f T |
|
48 |
| fold_types f (ZConst (_, As)) = fold f As |
|
49 |
| fold_types f (ZAbs (_, T, b)) = f T #> fold_types f b |
|
50 |
| fold_types f (ZApp (t, u)) = fold_types f t #> fold_types f u |
|
51 |
| fold_types f (ZClass (T, _)) = f T |
|
52 |
| fold_types _ _ = I; |
|
53 |
||
54 |
||
79124 | 55 |
(* ordering *) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
56 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
57 |
local |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
58 |
|
79119 | 59 |
fun cons_nr (ZTVar _) = 0 |
60 |
| cons_nr (ZFun _) = 1 |
|
61 |
| cons_nr ZProp = 2 |
|
62 |
| cons_nr (ZItself _) = 3 |
|
63 |
| cons_nr (ZType0 _) = 4 |
|
64 |
| cons_nr (ZType1 _) = 5 |
|
65 |
| cons_nr (ZType _) = 6; |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
66 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
67 |
val fast_indexname_ord = Term_Ord.fast_indexname_ord; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
68 |
val sort_ord = Term_Ord.sort_ord; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
69 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
70 |
in |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
71 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
72 |
fun ztyp_ord TU = |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
73 |
if pointer_eq TU then EQUAL |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
74 |
else |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
75 |
(case TU of |
79119 | 76 |
(ZTVar (a, A), ZTVar (b, B)) => |
77 |
(case fast_indexname_ord (a, b) of EQUAL => sort_ord (A, B) | ord => ord) |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
78 |
| (ZFun (T, T'), ZFun (U, U')) => |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
79 |
(case ztyp_ord (T, U) of EQUAL => ztyp_ord (T', U') | ord => ord) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
80 |
| (ZProp, ZProp) => EQUAL |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
81 |
| (ZItself T, ZItself U) => ztyp_ord (T, U) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
82 |
| (ZType0 a, ZType0 b) => fast_string_ord (a, b) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
83 |
| (ZType1 (a, T), ZType1 (b, U)) => |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
84 |
(case fast_string_ord (a, b) of EQUAL => ztyp_ord (T, U) | ord => ord) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
85 |
| (ZType (a, Ts), ZType (b, Us)) => |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
86 |
(case fast_string_ord (a, b) of EQUAL => dict_ord ztyp_ord (Ts, Us) | ord => ord) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
87 |
| (T, U) => int_ord (cons_nr T, cons_nr U)); |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
88 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
89 |
end; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
90 |
|
79124 | 91 |
end; |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
92 |
|
79124 | 93 |
|
94 |
(* term items *) |
|
95 |
||
96 |
structure ZTVars: |
|
97 |
sig |
|
98 |
include TERM_ITEMS |
|
99 |
val add_tvarsT: ztyp -> set -> set |
|
100 |
val add_tvars: zterm -> set -> set |
|
101 |
end = |
|
102 |
struct |
|
103 |
open TVars; |
|
104 |
val add_tvarsT = ZTerm.fold_tvars add_set; |
|
105 |
val add_tvars = ZTerm.fold_types add_tvarsT; |
|
106 |
end; |
|
107 |
||
108 |
structure ZVars: |
|
109 |
sig |
|
110 |
include TERM_ITEMS |
|
111 |
val add_vars: zterm -> set -> set |
|
112 |
end = |
|
113 |
struct |
|
114 |
||
115 |
structure Term_Items = Term_Items |
|
116 |
( |
|
117 |
type key = indexname * ztyp; |
|
118 |
val ord = pointer_eq_ord (prod_ord Term_Ord.fast_indexname_ord ZTerm.ztyp_ord); |
|
119 |
); |
|
120 |
open Term_Items; |
|
121 |
||
122 |
val add_vars = ZTerm.fold_aterms (fn ZVar v => add_set v | _ => I); |
|
123 |
||
124 |
end; |
|
125 |
||
126 |
||
127 |
(* proofs *) |
|
128 |
||
79126 | 129 |
datatype zproof_name = |
130 |
ZAxiom of string |
|
131 |
| ZOracle of string |
|
132 |
| ZBox of serial; |
|
133 |
||
79124 | 134 |
datatype zproof = |
135 |
ZDummy (*dummy proof*) |
|
79126 | 136 |
| ZConstP of zproof_name * zterm * ztyp ZTVars.table * zterm ZVars.table |
79124 | 137 |
| ZBoundP of int |
138 |
| ZHyp of zterm |
|
139 |
| ZAbst of string * ztyp * zproof |
|
140 |
| ZAbsP of string * zterm * zproof |
|
141 |
| ZAppt of zproof * zterm |
|
142 |
| ZAppP of zproof * zproof |
|
79126 | 143 |
| ZClassP of ztyp * class; (*OFCLASS proof from sorts algebra*) |
79124 | 144 |
|
145 |
||
146 |
||
147 |
(*** local ***) |
|
148 |
||
149 |
signature ZTERM = |
|
150 |
sig |
|
151 |
datatype ztyp = datatype ztyp |
|
152 |
datatype zterm = datatype zterm |
|
153 |
datatype zproof = datatype zproof |
|
154 |
val fold_tvars: (indexname * sort -> 'a -> 'a) -> ztyp -> 'a -> 'a |
|
155 |
val fold_aterms: (zterm -> 'a -> 'a) -> zterm -> 'a -> 'a |
|
156 |
val fold_types: (ztyp -> 'a -> 'a) -> zterm -> 'a -> 'a |
|
157 |
val ztyp_ord: ztyp * ztyp -> order |
|
158 |
val aconv_zterm: zterm * zterm -> bool |
|
159 |
val ztyp_of: typ -> ztyp |
|
160 |
val typ_of: ztyp -> typ |
|
161 |
val zterm_of: Consts.T -> term -> zterm |
|
162 |
val term_of: Consts.T -> zterm -> term |
|
163 |
val global_zterm_of: theory -> term -> zterm |
|
164 |
val global_term_of: theory -> zterm -> term |
|
165 |
val dummy_proof: 'a -> zproof |
|
166 |
val todo_proof: 'a -> zproof |
|
79126 | 167 |
val axiom_proof: theory -> string -> term -> zproof |
168 |
val oracle_proof: theory -> string -> term -> zproof |
|
79124 | 169 |
val assume_proof: theory -> term -> zproof |
170 |
val trivial_proof: theory -> term -> zproof |
|
171 |
val implies_intr_proof: theory -> term -> zproof -> zproof |
|
172 |
val forall_intr_proof: theory -> typ -> string * term -> zproof -> zproof |
|
173 |
val forall_elim_proof: theory -> term -> zproof -> zproof |
|
79128 | 174 |
val of_class_proof: typ * class -> zproof |
79124 | 175 |
val reflexive_proof: theory -> typ -> term -> zproof |
176 |
val symmetric_proof: theory -> typ -> term -> term -> zproof -> zproof |
|
177 |
val transitive_proof: theory -> typ -> term -> term -> term -> zproof -> zproof -> zproof |
|
178 |
val equal_intr_proof: theory -> term -> term -> zproof -> zproof -> zproof |
|
179 |
val equal_elim_proof: theory -> term -> term -> zproof -> zproof -> zproof |
|
180 |
val abstract_rule_proof: theory -> typ -> typ -> string * term -> term -> term -> zproof -> zproof |
|
181 |
val combination_proof: theory -> typ -> typ -> term -> term -> term -> term -> |
|
182 |
zproof -> zproof -> zproof |
|
79133 | 183 |
val generalize_proof: Names.set * Names.set -> int -> zproof -> zproof |
79135 | 184 |
val varifyT_proof: ((string * sort) * (indexname * sort)) list -> zproof -> zproof |
79124 | 185 |
end; |
186 |
||
187 |
structure ZTerm: ZTERM = |
|
188 |
struct |
|
189 |
||
190 |
datatype ztyp = datatype ztyp; |
|
191 |
datatype zterm = datatype zterm; |
|
192 |
datatype zproof = datatype zproof; |
|
193 |
||
194 |
open ZTerm; |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
195 |
|
79119 | 196 |
fun aconv_zterm (tm1, tm2) = |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
197 |
pointer_eq (tm1, tm2) orelse |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
198 |
(case (tm1, tm2) of |
79119 | 199 |
(ZApp (t1, u1), ZApp (t2, u2)) => aconv_zterm (t1, t2) andalso aconv_zterm (u1, u2) |
200 |
| (ZAbs (_, T1, t1), ZAbs (_, T2, t2)) => aconv_zterm (t1, t2) andalso T1 = T2 |
|
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
201 |
| (a1, a2) => a1 = a2); |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
202 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
203 |
|
79132 | 204 |
(* map structure *) |
205 |
||
206 |
fun subst_type_same tvar = |
|
207 |
let |
|
208 |
fun typ (ZTVar x) = tvar x |
|
209 |
| typ (ZFun (T, U)) = (ZFun (typ T, Same.commit typ U) handle Same.SAME => ZFun (T, typ U)) |
|
210 |
| typ ZProp = raise Same.SAME |
|
211 |
| typ (ZItself T) = ZItself (typ T) |
|
212 |
| typ (ZType0 _) = raise Same.SAME |
|
213 |
| typ (ZType1 (a, T)) = ZType1 (a, typ T) |
|
214 |
| typ (ZType (a, Ts)) = ZType (a, Same.map typ Ts); |
|
215 |
in typ end; |
|
216 |
||
217 |
fun subst_term_same typ var = |
|
218 |
let |
|
219 |
fun term (ZVar (x, T)) = |
|
220 |
let val (T', same) = Same.commit_id typ T in |
|
221 |
(case Same.catch var (x, T') of |
|
222 |
NONE => if same then raise Same.SAME else ZVar (x, T') |
|
223 |
| SOME t' => t') |
|
224 |
end |
|
225 |
| term (ZBound _) = raise Same.SAME |
|
226 |
| term (ZConst0 _) = raise Same.SAME |
|
227 |
| term (ZConst1 (a, T)) = ZConst1 (a, typ T) |
|
228 |
| term (ZConst (a, Ts)) = ZConst (a, Same.map typ Ts) |
|
229 |
| term (ZAbs (a, T, t)) = |
|
230 |
(ZAbs (a, typ T, Same.commit term t) handle Same.SAME => ZAbs (a, T, term t)) |
|
231 |
| term (ZApp (t, u)) = |
|
232 |
(ZApp (term t, Same.commit term u) handle Same.SAME => ZApp (t, term u)) |
|
233 |
| term (ZClass (T, c)) = ZClass (typ T, c); |
|
234 |
in term end; |
|
235 |
||
236 |
fun map_proof_same typ term = |
|
237 |
let |
|
238 |
fun change_insts (instT, inst) = |
|
239 |
let |
|
240 |
val changed = Unsynchronized.ref false; |
|
241 |
val instT' = |
|
242 |
(instT, instT) |-> ZTVars.fold (fn (v, T) => |
|
243 |
(case Same.catch typ T of |
|
244 |
SOME U => (changed := true; ZTVars.update (v, U)) |
|
245 |
| NONE => I)); |
|
246 |
val inst' = |
|
247 |
if ! changed then |
|
248 |
ZVars.dest inst |
|
249 |
|> map (fn ((x, T), t) => ((x, Same.commit typ T), Same.commit term t)) |
|
250 |
|> ZVars.make |
|
251 |
else |
|
252 |
(inst, inst) |-> ZVars.fold (fn (v, t) => |
|
253 |
(case Same.catch term t of |
|
254 |
SOME u => (changed := true; ZVars.update (v, u)) |
|
255 |
| NONE => I)); |
|
256 |
in if ! changed then SOME (instT', inst') else NONE end; |
|
257 |
||
258 |
fun proof ZDummy = raise Same.SAME |
|
259 |
| proof (ZConstP (a, A, instT, inst)) = |
|
260 |
(case change_insts (instT, inst) of |
|
261 |
NONE => ZConstP (a, term A, instT, inst) |
|
262 |
| SOME (instT', inst') => ZConstP (a, Same.commit term A, instT', inst')) |
|
263 |
| proof (ZBoundP _) = raise Same.SAME |
|
264 |
| proof (ZHyp h) = ZHyp (term h) |
|
265 |
| proof (ZAbst (a, T, p)) = |
|
266 |
(ZAbst (a, typ T, Same.commit proof p) handle Same.SAME => ZAbst (a, T, proof p)) |
|
267 |
| proof (ZAbsP (a, t, p)) = |
|
268 |
(ZAbsP (a, term t, Same.commit proof p) handle Same.SAME => ZAbsP (a, t, proof p)) |
|
269 |
| proof (ZAppt (p, t)) = |
|
270 |
(ZAppt (proof p, Same.commit term t) handle Same.SAME => ZAppt (p, term t)) |
|
271 |
| proof (ZAppP (p, q)) = |
|
272 |
(ZAppP (proof p, Same.commit proof q) handle Same.SAME => ZAppP (p, proof q)) |
|
273 |
| proof (ZClassP (T, c)) = ZClassP (typ T, c); |
|
274 |
in proof end; |
|
275 |
||
276 |
||
79124 | 277 |
(* instantiation *) |
278 |
||
279 |
fun init_instT t = ZTVars.build (ZTVars.add_tvars t) |> ZTVars.map (fn v => fn _ => ZTVar v); |
|
280 |
fun init_inst t = ZVars.build (ZVars.add_vars t) |> ZVars.map (fn v => fn _ => ZVar v); |
|
79126 | 281 |
|
282 |
fun map_const_proof (f, g) prf = |
|
283 |
(case prf of |
|
284 |
ZConstP (a, A, instT, inst) => |
|
285 |
let |
|
286 |
val instT' = ZTVars.map (fn ((x, _), _) => fn y => the_default y (try f x)) instT; |
|
287 |
val inst' = ZVars.map (fn ((x, _), _) => fn y => the_default y (try g x)) inst; |
|
288 |
in ZConstP (a, A, instT', inst') end |
|
289 |
| _ => prf); |
|
79124 | 290 |
|
291 |
||
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
292 |
(* convert ztyp / zterm vs. regular typ / term *) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
293 |
|
79119 | 294 |
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
|
295 |
| ztyp_of (TVar v) = ZTVar v |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
296 |
| ztyp_of (Type ("fun", [T, U])) = ZFun (ztyp_of T, ztyp_of U) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
297 |
| ztyp_of (Type (c, [])) = if c = "prop" then ZProp else ZType0 c |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
298 |
| ztyp_of (Type (c, [T])) = if c = "itself" then ZItself (ztyp_of T) else ZType1 (c, ztyp_of T) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
299 |
| 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
|
300 |
|
79119 | 301 |
fun typ_of (ZTVar ((a, ~1), S)) = TFree (a, S) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
302 |
| typ_of (ZTVar v) = TVar v |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
303 |
| 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
|
304 |
| typ_of ZProp = propT |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
305 |
| typ_of (ZItself T) = Term.itselfT (typ_of T) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
306 |
| typ_of (ZType0 c) = Type (c, []) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
307 |
| typ_of (ZType1 (c, T)) = Type (c, [typ_of T]) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
308 |
| 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
|
309 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
310 |
fun zterm_of consts = |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
311 |
let |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
312 |
val typargs = Consts.typargs consts; |
79119 | 313 |
fun zterm (Free (x, T)) = ZVar ((x, ~1), ztyp_of T) |
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
314 |
| zterm (Var (xi, T)) = ZVar (xi, ztyp_of T) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
315 |
| zterm (Bound i) = ZBound i |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
316 |
| zterm (Const (c, T)) = |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
317 |
(case typargs (c, T) of |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
318 |
[] => ZConst0 c |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
319 |
| [T] => ZConst1 (c, ztyp_of T) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
320 |
| Ts => ZConst (c, map ztyp_of Ts)) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
321 |
| zterm (Abs (a, T, b)) = ZAbs (a, ztyp_of T, zterm b) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
322 |
| zterm ((t as Const (c, _)) $ (u as Const ("Pure.type", _))) = |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
323 |
if String.isSuffix Logic.class_suffix c then |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
324 |
ZClass (ztyp_of (Logic.dest_type u), Logic.class_of_const c) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
325 |
else ZApp (zterm t, zterm u) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
326 |
| zterm (t $ u) = ZApp (zterm t, zterm u); |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
327 |
in zterm end; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
328 |
|
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
329 |
fun term_of consts = |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
330 |
let |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
331 |
val instance = Consts.instance consts; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
332 |
fun const (c, Ts) = Const (c, instance (c, Ts)); |
79119 | 333 |
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
|
334 |
| term (ZVar (xi, T)) = Var (xi, typ_of T) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
335 |
| term (ZBound i) = Bound i |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
336 |
| term (ZConst0 c) = const (c, []) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
337 |
| term (ZConst1 (c, T)) = const (c, [typ_of T]) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
338 |
| term (ZConst (c, Ts)) = const (c, map typ_of Ts) |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
339 |
| 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
|
340 |
| term (ZApp (t, u)) = term t $ term u |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
341 |
| 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
|
342 |
in term end; |
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
343 |
|
79119 | 344 |
val global_zterm_of = zterm_of o Sign.consts_of; |
345 |
val global_term_of = term_of o Sign.consts_of; |
|
79113
5109e4b2a292
pro-forma support for optional zproof: no proper content yet;
wenzelm
parents:
79098
diff
changeset
|
346 |
|
79119 | 347 |
|
348 |
||
349 |
(** proof construction **) |
|
79113
5109e4b2a292
pro-forma support for optional zproof: no proper content yet;
wenzelm
parents:
79098
diff
changeset
|
350 |
|
5109e4b2a292
pro-forma support for optional zproof: no proper content yet;
wenzelm
parents:
79098
diff
changeset
|
351 |
fun dummy_proof _ = ZDummy; |
5109e4b2a292
pro-forma support for optional zproof: no proper content yet;
wenzelm
parents:
79098
diff
changeset
|
352 |
val todo_proof = dummy_proof; |
5109e4b2a292
pro-forma support for optional zproof: no proper content yet;
wenzelm
parents:
79098
diff
changeset
|
353 |
|
79124 | 354 |
|
355 |
(* basic logic *) |
|
356 |
||
79126 | 357 |
fun const_proof thy a A = |
79119 | 358 |
let |
359 |
val t = global_zterm_of thy A; |
|
79126 | 360 |
val instT = init_instT t; |
361 |
val inst = init_inst t; |
|
362 |
in ZConstP (a, t, instT, inst) end; |
|
363 |
||
364 |
fun axiom_proof thy name = const_proof thy (ZAxiom name); |
|
365 |
fun oracle_proof thy name = const_proof thy (ZOracle name); |
|
79119 | 366 |
|
367 |
fun assume_proof thy A = |
|
368 |
ZHyp (global_zterm_of thy A); |
|
369 |
||
370 |
fun trivial_proof thy A = |
|
371 |
ZAbsP ("H", global_zterm_of thy A, ZBoundP 0); |
|
372 |
||
373 |
fun implies_intr_proof thy A prf = |
|
374 |
let |
|
375 |
val h = global_zterm_of thy A; |
|
376 |
fun abs_hyp i (p as ZHyp t) = if aconv_zterm (h, t) then ZBoundP i else p |
|
377 |
| abs_hyp i (ZAbst (x, T, p)) = ZAbst (x, T, abs_hyp i p) |
|
378 |
| abs_hyp i (ZAbsP (x, t, p)) = ZAbsP (x, t, abs_hyp (i + 1) p) |
|
379 |
| abs_hyp i (ZAppt (p, t)) = ZAppt (abs_hyp i p, t) |
|
380 |
| abs_hyp i (ZAppP (p, q)) = ZAppP (abs_hyp i p, abs_hyp i q) |
|
381 |
| abs_hyp _ p = p; |
|
382 |
in ZAbsP ("H", h, abs_hyp 0 prf) end; |
|
383 |
||
79124 | 384 |
fun forall_intr_proof thy T (a, x) prf = |
79119 | 385 |
let |
79124 | 386 |
val Z = ztyp_of T; |
79119 | 387 |
val z = global_zterm_of thy x; |
388 |
||
389 |
fun abs_term i b = |
|
390 |
if aconv_zterm (b, z) then ZBound i |
|
391 |
else |
|
392 |
(case b of |
|
393 |
ZAbs (x, T, t) => ZAbs (x, T, abs_term (i + 1) t) |
|
394 |
| ZApp (t, u) => ZApp (abs_term i t, abs_term i u) |
|
395 |
| _ => b); |
|
396 |
||
79124 | 397 |
fun abs_proof i (ZAbst (x, T, prf)) = ZAbst (x, T, abs_proof (i + 1) prf) |
398 |
| abs_proof i (ZAbsP (x, t, prf)) = ZAbsP (x, abs_term i t, abs_proof i prf) |
|
399 |
| abs_proof i (ZAppt (p, t)) = ZAppt (abs_proof i p, abs_term i t) |
|
400 |
| abs_proof i (ZAppP (p, q)) = ZAppP (abs_proof i p, abs_proof i q) |
|
401 |
| abs_proof _ p = p; |
|
79119 | 402 |
|
79124 | 403 |
in ZAbst (a, Z, abs_proof 0 prf) end; |
79119 | 404 |
|
405 |
fun forall_elim_proof thy t p = ZAppt (p, global_zterm_of thy t); |
|
406 |
||
79128 | 407 |
fun of_class_proof (T, c) = ZClassP (ztyp_of T, c); |
408 |
||
79124 | 409 |
|
410 |
(* equality *) |
|
411 |
||
412 |
local |
|
413 |
||
414 |
val thy0 = |
|
415 |
Context.the_global_context () |
|
416 |
|> Sign.add_types_global [(Binding.name "fun", 2, NoSyn), (Binding.name "prop", 0, NoSyn)] |
|
417 |
|> Sign.local_path |
|
418 |
|> Sign.add_consts |
|
419 |
[(Binding.name "all", (Term.aT [] --> propT) --> propT, NoSyn), |
|
420 |
(Binding.name "imp", propT --> propT --> propT, NoSyn), |
|
421 |
(Binding.name "eq", Term.aT [] --> Term.aT [] --> propT, NoSyn)]; |
|
422 |
||
423 |
val [reflexive_axiom, symmetric_axiom, transitive_axiom, equal_intr_axiom, equal_elim_axiom, |
|
424 |
abstract_rule_axiom, combination_axiom] = |
|
79126 | 425 |
Theory.equality_axioms |> map (fn (b, t) => axiom_proof thy0 (Sign.full_name thy0 b) t); |
79124 | 426 |
|
427 |
in |
|
428 |
||
429 |
val is_reflexive_proof = |
|
79126 | 430 |
fn ZConstP (ZAxiom "Pure.reflexive", _, _, _) => true | _ => false; |
79124 | 431 |
|
432 |
fun reflexive_proof thy T t = |
|
433 |
let |
|
434 |
val A = ztyp_of T; |
|
435 |
val x = global_zterm_of thy t; |
|
79126 | 436 |
in map_const_proof (fn "'a" => A, fn "x" => x) reflexive_axiom end; |
79124 | 437 |
|
438 |
fun symmetric_proof thy T t u prf = |
|
439 |
if is_reflexive_proof prf then prf |
|
440 |
else |
|
441 |
let |
|
442 |
val A = ztyp_of T; |
|
443 |
val x = global_zterm_of thy t; |
|
444 |
val y = global_zterm_of thy u; |
|
79126 | 445 |
val ax = map_const_proof (fn "'a" => A, fn "x" => x | "y" => y) symmetric_axiom; |
79124 | 446 |
in ZAppP (ax, prf) end; |
447 |
||
448 |
fun transitive_proof thy T t u v prf1 prf2 = |
|
449 |
if is_reflexive_proof prf1 then prf2 |
|
450 |
else if is_reflexive_proof prf2 then prf1 |
|
451 |
else |
|
452 |
let |
|
453 |
val A = ztyp_of T; |
|
454 |
val x = global_zterm_of thy t; |
|
455 |
val y = global_zterm_of thy u; |
|
456 |
val z = global_zterm_of thy v; |
|
79126 | 457 |
val ax = map_const_proof (fn "'a" => A, fn "x" => x | "y" => y | "z" => z) transitive_axiom; |
79124 | 458 |
in ZAppP (ZAppP (ax, prf1), prf2) end; |
459 |
||
460 |
fun equal_intr_proof thy t u prf1 prf2 = |
|
461 |
let |
|
462 |
val A = global_zterm_of thy t; |
|
463 |
val B = global_zterm_of thy u; |
|
79126 | 464 |
val ax = map_const_proof (undefined, fn "A" => A | "B" => B) equal_intr_axiom; |
79124 | 465 |
in ZAppP (ZAppP (ax, prf1), prf2) end; |
466 |
||
467 |
fun equal_elim_proof thy t u prf1 prf2 = |
|
468 |
let |
|
469 |
val A = global_zterm_of thy t; |
|
470 |
val B = global_zterm_of thy u; |
|
79126 | 471 |
val ax = map_const_proof (undefined, fn "A" => A | "B" => B) equal_elim_axiom; |
79124 | 472 |
in ZAppP (ZAppP (ax, prf1), prf2) end; |
473 |
||
474 |
fun abstract_rule_proof thy T U x t u prf = |
|
475 |
let |
|
476 |
val A = ztyp_of T; |
|
477 |
val B = ztyp_of U; |
|
478 |
val f = global_zterm_of thy t; |
|
479 |
val g = global_zterm_of thy u; |
|
79126 | 480 |
val ax = |
481 |
map_const_proof (fn "'a" => A | "'b" => B, fn "f" => f | "g" => g) |
|
482 |
abstract_rule_axiom; |
|
79124 | 483 |
in ZAppP (ax, forall_intr_proof thy T x prf) end; |
484 |
||
485 |
fun combination_proof thy T U f g t u prf1 prf2 = |
|
486 |
let |
|
487 |
val A = ztyp_of T; |
|
488 |
val B = ztyp_of U; |
|
489 |
val f' = global_zterm_of thy f; |
|
490 |
val g' = global_zterm_of thy g; |
|
491 |
val x = global_zterm_of thy t; |
|
492 |
val y = global_zterm_of thy u; |
|
493 |
val ax = |
|
79126 | 494 |
map_const_proof (fn "'a" => A | "'b" => B, fn "f" => f' | "g" => g' | "x" => x | "y" => y) |
79124 | 495 |
combination_axiom; |
496 |
in ZAppP (ZAppP (ax, prf1), prf2) end; |
|
497 |
||
79098
d8940e5bbb25
tight representation of types / terms / proof terms (presently unused);
wenzelm
parents:
diff
changeset
|
498 |
end; |
79124 | 499 |
|
79133 | 500 |
|
501 |
(* substitution *) |
|
502 |
||
503 |
fun generalize_proof (tfrees, frees) idx prf = |
|
504 |
let |
|
505 |
val typ = |
|
506 |
if Names.is_empty tfrees then Same.same else |
|
507 |
subst_type_same (fn ((a, i), S) => |
|
508 |
if i = ~1 andalso Names.defined tfrees a then ZTVar ((a, idx), S) |
|
509 |
else raise Same.SAME); |
|
79136 | 510 |
val term = |
79133 | 511 |
if Names.is_empty frees then Same.same else |
79136 | 512 |
subst_term_same typ (fn ((x, i), T) => |
79133 | 513 |
if i = ~1 andalso Names.defined frees x then ZVar ((x, idx), T) |
79136 | 514 |
else raise Same.SAME); |
515 |
in Same.commit (map_proof_same typ term) prf end; |
|
79133 | 516 |
|
79135 | 517 |
fun varifyT_proof names prf = |
518 |
if null names then prf |
|
519 |
else |
|
520 |
let |
|
521 |
val tab = ZTVars.build (names |> fold (fn ((a, S), b) => ZTVars.add (((a, ~1), S), b))); |
|
79136 | 522 |
val typ = |
523 |
subst_type_same (fn v => |
|
524 |
(case ZTVars.lookup tab v of |
|
525 |
NONE => raise Same.SAME |
|
526 |
| SOME w => ZTVar w)); |
|
79135 | 527 |
val term = subst_term_same typ Same.same; |
528 |
in Same.commit (map_proof_same typ term) prf end; |
|
529 |
||
79124 | 530 |
end; |