author | wenzelm |
Mon, 29 Jul 2019 11:09:37 +0200 | |
changeset 70436 | 251f1fb44ccd |
parent 70435 | 52fbcf7a61f8 |
child 70443 | a21a96eda033 |
permissions | -rw-r--r-- |
11522 | 1 |
(* Title: Pure/Proof/reconstruct.ML |
11539 | 2 |
Author: Stefan Berghofer, TU Muenchen |
11522 | 3 |
|
4 |
Reconstruction of partial proof terms. |
|
5 |
*) |
|
6 |
||
7 |
signature RECONSTRUCT = |
|
8 |
sig |
|
62922 | 9 |
val quiet_mode : bool Config.T |
10 |
val reconstruct_proof : Proof.context -> term -> Proofterm.proof -> Proofterm.proof |
|
13256
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
berghofe
parents:
13138
diff
changeset
|
11 |
val prop_of' : term list -> Proofterm.proof -> term |
13138 | 12 |
val prop_of : Proofterm.proof -> term |
62922 | 13 |
val proof_of : Proof.context -> thm -> Proofterm.proof |
14 |
val expand_proof : Proof.context -> (string * term option) list -> |
|
13342
915d4d004643
expand_proof now also takes an optional term describing the proposition
berghofe
parents:
13256
diff
changeset
|
15 |
Proofterm.proof -> Proofterm.proof |
64986 | 16 |
val clean_proof_of : Proof.context -> bool -> thm -> Proofterm.proof |
11522 | 17 |
end; |
18 |
||
19 |
structure Reconstruct : RECONSTRUCT = |
|
20 |
struct |
|
21 |
||
69575 | 22 |
val quiet_mode = Config.declare_bool ("Reconstruct.quiet_mode", \<^here>) (K true); |
62922 | 23 |
|
24 |
fun message ctxt msg = |
|
25 |
if Config.get ctxt quiet_mode then () else writeln (msg ()); |
|
11522 | 26 |
|
27 |
||
70397 | 28 |
(* generate constraints for proof term *) |
11522 | 29 |
|
23178 | 30 |
fun mk_var env Ts T = |
11522 | 31 |
let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T) |
33245 | 32 |
in (list_comb (v, map Bound (length Ts - 1 downto 0)), env') end; |
11522 | 33 |
|
33245 | 34 |
fun mk_tvar S (Envir.Envir {maxidx, tenv, tyenv}) = |
35 |
(TVar (("'t", maxidx + 1), S), |
|
36 |
Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv}); |
|
11522 | 37 |
|
19473 | 38 |
val mk_abs = fold (fn T => fn u => Abs ("", T, u)); |
11522 | 39 |
|
62922 | 40 |
fun unifyT ctxt env T U = |
11522 | 41 |
let |
32032 | 42 |
val Envir.Envir {maxidx, tenv, tyenv} = env; |
62922 | 43 |
val (tyenv', maxidx') = Sign.typ_unify (Proof_Context.theory_of ctxt) (T, U) (tyenv, maxidx); |
32032 | 44 |
in Envir.Envir {maxidx = maxidx', tenv = tenv, tyenv = tyenv'} end; |
11522 | 45 |
|
32032 | 46 |
fun chaseT env (T as TVar v) = |
47 |
(case Type.lookup (Envir.type_env env) v of |
|
48 |
NONE => T |
|
49 |
| SOME T' => chaseT env T') |
|
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
50 |
| chaseT _ T = T; |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
51 |
|
62922 | 52 |
fun infer_type ctxt (env as Envir.Envir {maxidx, tenv, tyenv}) Ts vTs |
32032 | 53 |
(t as Const (s, T)) = if T = dummyT then |
62922 | 54 |
(case Sign.const_type (Proof_Context.theory_of ctxt) s of |
15531 | 55 |
NONE => error ("reconstruct_proof: No such constant: " ^ quote s) |
23178 | 56 |
| SOME T => |
19419 | 57 |
let val T' = Type.strip_sorts (Logic.incr_tvar (maxidx + 1) T) |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
58 |
in (Const (s, T'), T', vTs, |
32032 | 59 |
Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv}) |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
60 |
end) |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
61 |
else (t, T, vTs, env) |
62922 | 62 |
| infer_type ctxt env Ts vTs (t as Free (s, T)) = |
17412 | 63 |
if T = dummyT then (case Symtab.lookup vTs s of |
15531 | 64 |
NONE => |
33245 | 65 |
let val (T, env') = mk_tvar [] env |
17412 | 66 |
in (Free (s, T), T, Symtab.update_new (s, T) vTs, env') end |
15531 | 67 |
| SOME T => (Free (s, T), T, vTs, env)) |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
68 |
else (t, T, vTs, env) |
62922 | 69 |
| infer_type ctxt env Ts vTs (Var _) = error "reconstruct_proof: internal error" |
70 |
| infer_type ctxt env Ts vTs (Abs (s, T, t)) = |
|
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
71 |
let |
33245 | 72 |
val (T', env') = if T = dummyT then mk_tvar [] env else (T, env); |
62922 | 73 |
val (t', U, vTs', env'') = infer_type ctxt env' (T' :: Ts) vTs t |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
74 |
in (Abs (s, T', t'), T' --> U, vTs', env'') end |
62922 | 75 |
| infer_type ctxt env Ts vTs (t $ u) = |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
76 |
let |
62922 | 77 |
val (t', T, vTs1, env1) = infer_type ctxt env Ts vTs t; |
78 |
val (u', U, vTs2, env2) = infer_type ctxt env1 Ts vTs1 u; |
|
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
79 |
in (case chaseT env2 T of |
62922 | 80 |
Type ("fun", [U', V]) => (t' $ u', V, vTs2, unifyT ctxt env2 U U') |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
81 |
| _ => |
33245 | 82 |
let val (V, env3) = mk_tvar [] env2 |
62922 | 83 |
in (t' $ u', V, vTs2, unifyT ctxt env3 T (U --> V)) end) |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
84 |
end |
62922 | 85 |
| infer_type ctxt env Ts vTs (t as Bound i) = ((t, nth Ts i, vTs, env) |
43278 | 86 |
handle General.Subscript => error ("infer_type: bad variable index " ^ string_of_int i)); |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
87 |
|
62922 | 88 |
fun cantunify ctxt (t, u) = |
89 |
error ("Non-unifiable terms:\n" ^ |
|
90 |
Syntax.string_of_term ctxt t ^ "\n\n" ^ Syntax.string_of_term ctxt u); |
|
11522 | 91 |
|
62922 | 92 |
fun decompose ctxt Ts (p as (t, u)) env = |
33245 | 93 |
let |
94 |
fun rigrig (a, T) (b, U) uT ts us = |
|
62922 | 95 |
if a <> b then cantunify ctxt p |
96 |
else apfst flat (fold_map (decompose ctxt Ts) (ts ~~ us) (uT env T U)) |
|
33245 | 97 |
in |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58950
diff
changeset
|
98 |
case apply2 (strip_comb o Envir.head_norm env) p of |
62922 | 99 |
((Const c, ts), (Const d, us)) => rigrig c d (unifyT ctxt) ts us |
100 |
| ((Free c, ts), (Free d, us)) => rigrig c d (unifyT ctxt) ts us |
|
13715 | 101 |
| ((Bound i, ts), (Bound j, us)) => |
102 |
rigrig (i, dummyT) (j, dummyT) (K o K) ts us |
|
103 |
| ((Abs (_, T, t), []), (Abs (_, U, u), [])) => |
|
62922 | 104 |
decompose ctxt (T::Ts) (t, u) (unifyT ctxt env T U) |
13715 | 105 |
| ((Abs (_, T, t), []), _) => |
62922 | 106 |
decompose ctxt (T::Ts) (t, incr_boundvars 1 u $ Bound 0) env |
13715 | 107 |
| (_, (Abs (_, T, u), [])) => |
62922 | 108 |
decompose ctxt (T::Ts) (incr_boundvars 1 t $ Bound 0, u) env |
33245 | 109 |
| _ => ([(mk_abs Ts t, mk_abs Ts u)], env) |
13715 | 110 |
end; |
11522 | 111 |
|
62922 | 112 |
fun make_constraints_cprf ctxt env cprf = |
11522 | 113 |
let |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
114 |
fun add_cnstrt Ts prop prf cs env vTs (t, u) = |
11522 | 115 |
let |
116 |
val t' = mk_abs Ts t; |
|
12236 | 117 |
val u' = mk_abs Ts u |
11522 | 118 |
in |
62922 | 119 |
(prop, prf, cs, Pattern.unify (Context.Proof ctxt) (t', u') env, vTs) |
12236 | 120 |
handle Pattern.Pattern => |
62922 | 121 |
let val (cs', env') = decompose ctxt [] (t', u') env |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
122 |
in (prop, prf, cs @ cs', env', vTs) end |
12236 | 123 |
| Pattern.Unif => |
62922 | 124 |
cantunify ctxt (Envir.norm_term env t', Envir.norm_term env u') |
11522 | 125 |
end; |
126 |
||
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
127 |
fun mk_cnstrts_atom env vTs prop opTs prf = |
11522 | 128 |
let |
36042
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
krauss
parents:
33245
diff
changeset
|
129 |
val tvars = Term.add_tvars prop [] |> rev; |
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
krauss
parents:
33245
diff
changeset
|
130 |
val tfrees = Term.add_tfrees prop [] |> rev; |
33245 | 131 |
val (Ts, env') = |
31903 | 132 |
(case opTs of |
33245 | 133 |
NONE => fold_map mk_tvar (map snd tvars @ map snd tfrees) env |
134 |
| SOME Ts => (Ts, env)); |
|
28813 | 135 |
val prop' = subst_atomic_types (map TVar tvars @ map TFree tfrees ~~ Ts) |
70400
65bbef66e2ec
clarified treatment of unnamed PThm nodes (from close_derivation): retain full proof, publish when named;
wenzelm
parents:
70397
diff
changeset
|
136 |
(Proofterm.forall_intr_vfs prop) handle ListPair.UnequalLengths => |
37310 | 137 |
error ("Wrong number of type arguments for " ^ quote (Proofterm.guess_name prf)) |
70417 | 138 |
in (prop', Proofterm.change_types (SOME Ts) prf, [], env', vTs) end; |
11522 | 139 |
|
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
140 |
fun head_norm (prop, prf, cnstrts, env, vTs) = |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
141 |
(Envir.head_norm env prop, prf, cnstrts, env, vTs); |
23178 | 142 |
|
30146 | 143 |
fun mk_cnstrts env _ Hs vTs (PBound i) = ((nth Hs i, PBound i, [], env, vTs) |
43278 | 144 |
handle General.Subscript => error ("mk_cnstrts: bad variable index " ^ string_of_int i)) |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
145 |
| mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) = |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
146 |
let |
33245 | 147 |
val (T, env') = |
148 |
(case opT of |
|
149 |
NONE => mk_tvar [] env |
|
150 |
| SOME T => (T, env)); |
|
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
151 |
val (t, prf, cnstrts, env'', vTs') = |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
152 |
mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf; |
62922 | 153 |
in |
154 |
(Const ("Pure.all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, SOME T, prf), |
|
155 |
cnstrts, env'', vTs') |
|
11522 | 156 |
end |
15531 | 157 |
| mk_cnstrts env Ts Hs vTs (AbsP (s, SOME t, cprf)) = |
11522 | 158 |
let |
62922 | 159 |
val (t', _, vTs', env') = infer_type ctxt env Ts vTs t; |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
160 |
val (u, prf, cnstrts, env'', vTs'') = mk_cnstrts env' Ts (t'::Hs) vTs' cprf; |
15531 | 161 |
in (Logic.mk_implies (t', u), AbsP (s, SOME t', prf), cnstrts, env'', vTs'') |
11522 | 162 |
end |
15531 | 163 |
| mk_cnstrts env Ts Hs vTs (AbsP (s, NONE, cprf)) = |
11522 | 164 |
let |
33245 | 165 |
val (t, env') = mk_var env Ts propT; |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
166 |
val (u, prf, cnstrts, env'', vTs') = mk_cnstrts env' Ts (t::Hs) vTs cprf; |
15531 | 167 |
in (Logic.mk_implies (t, u), AbsP (s, SOME t, prf), cnstrts, env'', vTs') |
11522 | 168 |
end |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
169 |
| mk_cnstrts env Ts Hs vTs (cprf1 %% cprf2) = |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
170 |
let val (u, prf2, cnstrts, env', vTs') = mk_cnstrts env Ts Hs vTs cprf2 |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
171 |
in (case head_norm (mk_cnstrts env' Ts Hs vTs' cprf1) of |
56245 | 172 |
(Const ("Pure.imp", _) $ u' $ t', prf1, cnstrts', env'', vTs'') => |
11613 | 173 |
add_cnstrt Ts t' (prf1 %% prf2) (cnstrts' @ cnstrts) |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
174 |
env'' vTs'' (u, u') |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
175 |
| (t, prf1, cnstrts', env'', vTs'') => |
33245 | 176 |
let val (v, env''') = mk_var env'' Ts propT |
11613 | 177 |
in add_cnstrt Ts v (prf1 %% prf2) (cnstrts' @ cnstrts) |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
178 |
env''' vTs'' (t, Logic.mk_implies (u, v)) |
11522 | 179 |
end) |
180 |
end |
|
15531 | 181 |
| mk_cnstrts env Ts Hs vTs (cprf % SOME t) = |
62922 | 182 |
let val (t', U, vTs1, env1) = infer_type ctxt env Ts vTs t |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
183 |
in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of |
56245 | 184 |
(Const ("Pure.all", Type ("fun", [Type ("fun", [T, _]), _])) $ f, |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
185 |
prf, cnstrts, env2, vTs2) => |
62922 | 186 |
let val env3 = unifyT ctxt env2 T U |
15531 | 187 |
in (betapply (f, t'), prf % SOME t', cnstrts, env3, vTs2) |
11522 | 188 |
end |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
189 |
| (u, prf, cnstrts, env2, vTs2) => |
33245 | 190 |
let val (v, env3) = mk_var env2 Ts (U --> propT); |
11522 | 191 |
in |
15531 | 192 |
add_cnstrt Ts (v $ t') (prf % SOME t') cnstrts env3 vTs2 |
56245 | 193 |
(u, Const ("Pure.all", (U --> propT) --> propT) $ v) |
11522 | 194 |
end) |
195 |
end |
|
15531 | 196 |
| mk_cnstrts env Ts Hs vTs (cprf % NONE) = |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
197 |
(case head_norm (mk_cnstrts env Ts Hs vTs cprf) of |
56245 | 198 |
(Const ("Pure.all", Type ("fun", [Type ("fun", [T, _]), _])) $ f, |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
199 |
prf, cnstrts, env', vTs') => |
33245 | 200 |
let val (t, env'') = mk_var env' Ts T |
15531 | 201 |
in (betapply (f, t), prf % SOME t, cnstrts, env'', vTs') |
11522 | 202 |
end |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
203 |
| (u, prf, cnstrts, env', vTs') => |
11522 | 204 |
let |
33245 | 205 |
val (T, env1) = mk_tvar [] env'; |
206 |
val (v, env2) = mk_var env1 Ts (T --> propT); |
|
207 |
val (t, env3) = mk_var env2 Ts T |
|
11522 | 208 |
in |
15531 | 209 |
add_cnstrt Ts (v $ t) (prf % SOME t) cnstrts env3 vTs' |
56245 | 210 |
(u, Const ("Pure.all", (T --> propT) --> propT) $ v) |
11522 | 211 |
end) |
70412
64ead6de6212
defer rew_proof on unnamed PThm node as open_proof operation: significant performance improvement;
wenzelm
parents:
70400
diff
changeset
|
212 |
| mk_cnstrts env _ _ vTs (prf as PThm (_, ((_, prop, opTs, _), _))) = |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
213 |
mk_cnstrts_atom env vTs prop opTs prf |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
214 |
| mk_cnstrts env _ _ vTs (prf as PAxm (_, prop, opTs)) = |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
215 |
mk_cnstrts_atom env vTs prop opTs prf |
31943
5e960a0780a2
renamed inclass/Inclass to of_class/OfClass, in accordance to of_sort;
wenzelm
parents:
31903
diff
changeset
|
216 |
| mk_cnstrts env _ _ vTs (prf as OfClass (T, c)) = |
5e960a0780a2
renamed inclass/Inclass to of_class/OfClass, in accordance to of_sort;
wenzelm
parents:
31903
diff
changeset
|
217 |
mk_cnstrts_atom env vTs (Logic.mk_of_class (T, c)) NONE prf |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
218 |
| mk_cnstrts env _ _ vTs (prf as Oracle (_, prop, opTs)) = |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
219 |
mk_cnstrts_atom env vTs prop opTs prf |
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
220 |
| mk_cnstrts env _ _ vTs (Hyp t) = (t, Hyp t, [], env, vTs) |
11613 | 221 |
| mk_cnstrts _ _ _ _ _ = error "reconstruct_proof: minimal proof object" |
13669
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
berghofe
parents:
13610
diff
changeset
|
222 |
in mk_cnstrts env [] [] Symtab.empty cprf end; |
11522 | 223 |
|
224 |
||
70397 | 225 |
(* update list of free variables of constraints *) |
11522 | 226 |
|
227 |
fun upd_constrs env cs = |
|
228 |
let |
|
32032 | 229 |
val tenv = Envir.term_env env; |
230 |
val tyenv = Envir.type_env env; |
|
20675 | 231 |
val dom = [] |
32032 | 232 |
|> Vartab.fold (cons o #1) tenv |
233 |
|> Vartab.fold (cons o #1) tyenv; |
|
20675 | 234 |
val vran = [] |
32032 | 235 |
|> Vartab.fold (Term.add_var_names o #2 o #2) tenv |
236 |
|> Vartab.fold (Term.add_tvar_namesT o #2 o #2) tyenv; |
|
11522 | 237 |
fun check_cs [] = [] |
32032 | 238 |
| check_cs ((u, p, vs) :: ps) = |
239 |
let val vs' = subtract (op =) dom vs in |
|
240 |
if vs = vs' then (u, p, vs) :: check_cs ps |
|
241 |
else (true, p, fold (insert op =) vs' vran) :: check_cs ps |
|
242 |
end; |
|
11522 | 243 |
in check_cs cs end; |
244 |
||
32032 | 245 |
|
70397 | 246 |
(* solution of constraints *) |
11522 | 247 |
|
248 |
fun solve _ [] bigenv = bigenv |
|
62922 | 249 |
| solve ctxt cs bigenv = |
11522 | 250 |
let |
11660 | 251 |
fun search env [] = error ("Unsolvable constraints:\n" ^ |
252 |
Pretty.string_of (Pretty.chunks (map (fn (_, p, _) => |
|
62922 | 253 |
Thm.pretty_flexpair ctxt (apply2 (Envir.norm_term bigenv) p)) cs))) |
11522 | 254 |
| search env ((u, p as (t1, t2), vs)::ps) = |
255 |
if u then |
|
256 |
let |
|
257 |
val tn1 = Envir.norm_term bigenv t1; |
|
258 |
val tn2 = Envir.norm_term bigenv t2 |
|
259 |
in |
|
260 |
if Pattern.pattern tn1 andalso Pattern.pattern tn2 then |
|
62922 | 261 |
(Pattern.unify (Context.Proof ctxt) (tn1, tn2) env, ps) handle Pattern.Unif => |
262 |
cantunify ctxt (tn1, tn2) |
|
11522 | 263 |
else |
62922 | 264 |
let val (cs', env') = decompose ctxt [] (tn1, tn2) env |
11522 | 265 |
in if cs' = [(tn1, tn2)] then |
266 |
apsnd (cons (false, (tn1, tn2), vs)) (search env ps) |
|
267 |
else search env' (map (fn q => (true, q, vs)) cs' @ ps) |
|
268 |
end |
|
269 |
end |
|
270 |
else apsnd (cons (false, p, vs)) (search env ps); |
|
271 |
val Envir.Envir {maxidx, ...} = bigenv; |
|
272 |
val (env, cs') = search (Envir.empty maxidx) cs; |
|
273 |
in |
|
62922 | 274 |
solve ctxt (upd_constrs env cs') (Envir.merge (bigenv, env)) |
11522 | 275 |
end; |
276 |
||
277 |
||
70397 | 278 |
(* reconstruction of proofs *) |
11522 | 279 |
|
62922 | 280 |
fun reconstruct_proof ctxt prop cprf = |
11522 | 281 |
let |
37310 | 282 |
val (cprf' % SOME prop', thawf) = Proofterm.freeze_thaw_prf (cprf % SOME prop); |
62922 | 283 |
val _ = message ctxt (fn _ => "Collecting constraints ..."); |
284 |
val (t, prf, cs, env, _) = make_constraints_cprf ctxt |
|
37310 | 285 |
(Envir.empty (Proofterm.maxidx_proof cprf ~1)) cprf'; |
44119
caddb5264048
avoid OldTerm operations -- with subtle changes of semantics;
wenzelm
parents:
44060
diff
changeset
|
286 |
val cs' = |
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
58950
diff
changeset
|
287 |
map (apply2 (Envir.norm_term env)) ((t, prop') :: cs) |
44119
caddb5264048
avoid OldTerm operations -- with subtle changes of semantics;
wenzelm
parents:
44060
diff
changeset
|
288 |
|> map (fn p => (true, p, Term.add_var_names (#1 p) (Term.add_var_names (#2 p) []))); |
62922 | 289 |
val _ = |
290 |
message ctxt |
|
291 |
(fn () => "Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ..."); |
|
292 |
val env' = solve ctxt cs' env |
|
11522 | 293 |
in |
37310 | 294 |
thawf (Proofterm.norm_proof env' prf) |
11522 | 295 |
end; |
296 |
||
28813 | 297 |
fun prop_of_atom prop Ts = subst_atomic_types |
36042
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
krauss
parents:
33245
diff
changeset
|
298 |
(map TVar (Term.add_tvars prop [] |> rev) @ map TFree (Term.add_tfrees prop [] |> rev) ~~ Ts) |
70400
65bbef66e2ec
clarified treatment of unnamed PThm nodes (from close_derivation): retain full proof, publish when named;
wenzelm
parents:
70397
diff
changeset
|
299 |
(Proofterm.forall_intr_vfs prop); |
13138 | 300 |
|
63616 | 301 |
val head_norm = Envir.head_norm Envir.init; |
13734
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
berghofe
parents:
13715
diff
changeset
|
302 |
|
30146 | 303 |
fun prop_of0 Hs (PBound i) = nth Hs i |
15531 | 304 |
| prop_of0 Hs (Abst (s, SOME T, prf)) = |
46217
7b19666f0e3d
renamed Term.all to Logic.all_const, in accordance to HOLogic.all_const;
wenzelm
parents:
44119
diff
changeset
|
305 |
Logic.all_const T $ (Abs (s, T, prop_of0 Hs prf)) |
15531 | 306 |
| prop_of0 Hs (AbsP (s, SOME t, prf)) = |
13734
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
berghofe
parents:
13715
diff
changeset
|
307 |
Logic.mk_implies (t, prop_of0 (t :: Hs) prf) |
15531 | 308 |
| prop_of0 Hs (prf % SOME t) = (case head_norm (prop_of0 Hs prf) of |
56245 | 309 |
Const ("Pure.all", _) $ f => f $ t |
13256
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
berghofe
parents:
13138
diff
changeset
|
310 |
| _ => error "prop_of: all expected") |
13734
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
berghofe
parents:
13715
diff
changeset
|
311 |
| prop_of0 Hs (prf1 %% prf2) = (case head_norm (prop_of0 Hs prf1) of |
56245 | 312 |
Const ("Pure.imp", _) $ P $ Q => Q |
13256
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
berghofe
parents:
13138
diff
changeset
|
313 |
| _ => error "prop_of: ==> expected") |
13734
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
berghofe
parents:
13715
diff
changeset
|
314 |
| prop_of0 Hs (Hyp t) = t |
70412
64ead6de6212
defer rew_proof on unnamed PThm node as open_proof operation: significant performance improvement;
wenzelm
parents:
70400
diff
changeset
|
315 |
| prop_of0 Hs (PThm (_, ((_, prop, SOME Ts, _), _))) = prop_of_atom prop Ts |
15531 | 316 |
| prop_of0 Hs (PAxm (_, prop, SOME Ts)) = prop_of_atom prop Ts |
31943
5e960a0780a2
renamed inclass/Inclass to of_class/OfClass, in accordance to of_sort;
wenzelm
parents:
31903
diff
changeset
|
317 |
| prop_of0 Hs (OfClass (T, c)) = Logic.mk_of_class (T, c) |
15531 | 318 |
| prop_of0 Hs (Oracle (_, prop, SOME Ts)) = prop_of_atom prop Ts |
13734
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
berghofe
parents:
13715
diff
changeset
|
319 |
| prop_of0 _ _ = error "prop_of: partial proof object"; |
13138 | 320 |
|
18929 | 321 |
val prop_of' = Envir.beta_eta_contract oo prop_of0; |
13256
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
berghofe
parents:
13138
diff
changeset
|
322 |
val prop_of = prop_of' []; |
13138 | 323 |
|
62922 | 324 |
fun proof_of ctxt raw_thm = |
67649 | 325 |
let val thm = Thm.transfer' ctxt raw_thm |
62922 | 326 |
in reconstruct_proof ctxt (Thm.prop_of thm) (Thm.proof_of thm) end; |
44060 | 327 |
|
328 |
||
70397 | 329 |
(* expand and reconstruct subproofs *) |
11522 | 330 |
|
62922 | 331 |
fun expand_proof ctxt thms prf = |
11522 | 332 |
let |
23178 | 333 |
fun expand maxidx prfs (AbsP (s, t, prf)) = |
12870
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
334 |
let val (maxidx', prfs', prf') = expand maxidx prfs prf |
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
335 |
in (maxidx', prfs', AbsP (s, t, prf')) end |
23178 | 336 |
| expand maxidx prfs (Abst (s, T, prf)) = |
12870
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
337 |
let val (maxidx', prfs', prf') = expand maxidx prfs prf |
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
338 |
in (maxidx', prfs', Abst (s, T, prf')) end |
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
339 |
| expand maxidx prfs (prf1 %% prf2) = |
11522 | 340 |
let |
12870
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
341 |
val (maxidx', prfs', prf1') = expand maxidx prfs prf1; |
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
342 |
val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2; |
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
343 |
in (maxidx'', prfs'', prf1' %% prf2') end |
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
344 |
| expand maxidx prfs (prf % t) = |
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
345 |
let val (maxidx', prfs', prf') = expand maxidx prfs prf |
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
346 |
in (maxidx', prfs', prf' % t) end |
70412
64ead6de6212
defer rew_proof on unnamed PThm node as open_proof operation: significant performance improvement;
wenzelm
parents:
70400
diff
changeset
|
347 |
| expand maxidx prfs (prf as PThm (_, ((a, prop, SOME Ts, open_proof), body))) = |
13342
915d4d004643
expand_proof now also takes an optional term describing the proposition
berghofe
parents:
13256
diff
changeset
|
348 |
if not (exists |
15531 | 349 |
(fn (b, NONE) => a = b |
350 |
| (b, SOME prop') => a = b andalso prop = prop') thms) |
|
13342
915d4d004643
expand_proof now also takes an optional term describing the proposition
berghofe
parents:
13256
diff
changeset
|
351 |
then (maxidx, prfs, prf) else |
11522 | 352 |
let |
16862 | 353 |
val (maxidx', prf, prfs') = |
17232 | 354 |
(case AList.lookup (op =) prfs (a, prop) of |
15531 | 355 |
NONE => |
11522 | 356 |
let |
44059 | 357 |
val _ = |
62922 | 358 |
message ctxt (fn () => |
359 |
"Reconstructing proof of " ^ a ^ "\n" ^ Syntax.string_of_term ctxt prop); |
|
70412
64ead6de6212
defer rew_proof on unnamed PThm node as open_proof operation: significant performance improvement;
wenzelm
parents:
70400
diff
changeset
|
360 |
val prf' = |
64ead6de6212
defer rew_proof on unnamed PThm node as open_proof operation: significant performance improvement;
wenzelm
parents:
70400
diff
changeset
|
361 |
Proofterm.join_proof body |
64ead6de6212
defer rew_proof on unnamed PThm node as open_proof operation: significant performance improvement;
wenzelm
parents:
70400
diff
changeset
|
362 |
|> open_proof |
64ead6de6212
defer rew_proof on unnamed PThm node as open_proof operation: significant performance improvement;
wenzelm
parents:
70400
diff
changeset
|
363 |
|> reconstruct_proof ctxt prop |
64ead6de6212
defer rew_proof on unnamed PThm node as open_proof operation: significant performance improvement;
wenzelm
parents:
70400
diff
changeset
|
364 |
|> Proofterm.forall_intr_vfs_prf prop; |
70419 | 365 |
val (maxidx', prfs', prf) = expand (Proofterm.maxidx_proof prf' ~1) prfs prf' |
366 |
in |
|
367 |
(maxidx' + maxidx + 1, Proofterm.incr_indexes (maxidx + 1) prf, |
|
368 |
((a, prop), (maxidx', prf)) :: prfs') |
|
13610
d4a2ac255447
Fixed bug in expand_proof which caused indexes to be incremented incorrectly.
berghofe
parents:
13342
diff
changeset
|
369 |
end |
70419 | 370 |
| SOME (maxidx', prf) => |
371 |
(maxidx' + maxidx + 1, Proofterm.incr_indexes (maxidx + 1) prf, prfs)); |
|
372 |
in (maxidx', prfs', Proofterm.app_types (maxidx + 1) prop Ts prf) end |
|
12870
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
berghofe
parents:
12527
diff
changeset
|
373 |
| expand maxidx prfs prf = (maxidx, prfs, prf); |
11522 | 374 |
|
37310 | 375 |
in #3 (expand (Proofterm.maxidx_proof prf ~1) [] prf) end; |
11522 | 376 |
|
64986 | 377 |
|
378 |
(* cleanup for output etc. *) |
|
379 |
||
380 |
fun clean_proof_of ctxt full thm = |
|
381 |
let |
|
70436 | 382 |
val (_, prop) = |
64986 | 383 |
Logic.unconstrainT (Thm.shyps_of thm) |
384 |
(Logic.list_implies (Thm.hyps_of thm, Thm.prop_of thm)); |
|
385 |
in |
|
386 |
Proofterm.proof_of (Proofterm.strip_thm (Thm.proof_body_of thm)) |
|
387 |
|> reconstruct_proof ctxt prop |
|
388 |
|> expand_proof ctxt [("", NONE)] |
|
389 |
|> Proofterm.rew_proof (Proof_Context.theory_of ctxt) |
|
390 |
|> Proofterm.no_thm_proofs |
|
391 |
|> not full ? Proofterm.shrink_proof |
|
392 |
end; |
|
393 |
||
11522 | 394 |
end; |