| author | blanchet | 
| Wed, 15 Sep 2010 17:05:18 +0200 | |
| changeset 39420 | 0cf524fad3f5 | 
| parent 37310 | 96e2b9a6f074 | 
| child 40722 | 441260986b63 | 
| 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 | |
| 32738 | 9 | val quiet_mode : bool Unsynchronized.ref | 
| 16862 | 10 | val reconstruct_proof : theory -> term -> Proofterm.proof -> Proofterm.proof | 
| 13256 
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
 berghofe parents: 
13138diff
changeset | 11 | val prop_of' : term list -> Proofterm.proof -> term | 
| 13138 | 12 | val prop_of : Proofterm.proof -> term | 
| 16862 | 13 | val expand_proof : theory -> (string * term option) list -> | 
| 13342 
915d4d004643
expand_proof now also takes an optional term describing the proposition
 berghofe parents: 
13256diff
changeset | 14 | Proofterm.proof -> Proofterm.proof | 
| 11522 | 15 | end; | 
| 16 | ||
| 17 | structure Reconstruct : RECONSTRUCT = | |
| 18 | struct | |
| 19 | ||
| 32738 | 20 | val quiet_mode = Unsynchronized.ref true; | 
| 11522 | 21 | fun message s = if !quiet_mode then () else writeln s; | 
| 22 | ||
| 28813 | 23 | fun vars_of t = map Var (rev (Term.add_vars t [])); | 
| 24 | fun frees_of t = map Free (rev (Term.add_frees t [])); | |
| 11522 | 25 | |
| 27330 | 26 | fun forall_intr_vfs prop = fold_rev Logic.all | 
| 28813 | 27 | (vars_of prop @ frees_of prop) prop; | 
| 11522 | 28 | |
| 37310 | 29 | fun forall_intr_vfs_prf prop prf = fold_rev Proofterm.forall_intr_proof' | 
| 28813 | 30 | (vars_of prop @ frees_of prop) prf; | 
| 12001 | 31 | |
| 11522 | 32 | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 33 | (**** generate constraints for proof term ****) | 
| 11522 | 34 | |
| 23178 | 35 | fun mk_var env Ts T = | 
| 11522 | 36 | let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T) | 
| 33245 | 37 | in (list_comb (v, map Bound (length Ts - 1 downto 0)), env') end; | 
| 11522 | 38 | |
| 33245 | 39 | fun mk_tvar S (Envir.Envir {maxidx, tenv, tyenv}) =
 | 
| 40 |   (TVar (("'t", maxidx + 1), S),
 | |
| 41 |     Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv});
 | |
| 11522 | 42 | |
| 19473 | 43 | val mk_abs = fold (fn T => fn u => Abs ("", T, u));
 | 
| 11522 | 44 | |
| 20312 | 45 | fun unifyT thy env T U = | 
| 11522 | 46 | let | 
| 32032 | 47 |     val Envir.Envir {maxidx, tenv, tyenv} = env;
 | 
| 48 | val (tyenv', maxidx') = Sign.typ_unify thy (T, U) (tyenv, maxidx); | |
| 49 |   in Envir.Envir {maxidx = maxidx', tenv = tenv, tyenv = tyenv'} end;
 | |
| 11522 | 50 | |
| 32032 | 51 | fun chaseT env (T as TVar v) = | 
| 52 | (case Type.lookup (Envir.type_env env) v of | |
| 53 | NONE => T | |
| 54 | | SOME T' => chaseT env T') | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 55 | | chaseT _ T = T; | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 56 | |
| 32032 | 57 | fun infer_type thy (env as Envir.Envir {maxidx, tenv, tyenv}) Ts vTs
 | 
| 58 | (t as Const (s, T)) = if T = dummyT then | |
| 59 | (case Sign.const_type thy s of | |
| 15531 | 60 |           NONE => error ("reconstruct_proof: No such constant: " ^ quote s)
 | 
| 23178 | 61 | | SOME T => | 
| 19419 | 62 | 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: 
13610diff
changeset | 63 | in (Const (s, T'), T', vTs, | 
| 32032 | 64 |               Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv})
 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 65 | end) | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 66 | else (t, T, vTs, env) | 
| 20312 | 67 | | infer_type thy env Ts vTs (t as Free (s, T)) = | 
| 17412 | 68 | if T = dummyT then (case Symtab.lookup vTs s of | 
| 15531 | 69 | NONE => | 
| 33245 | 70 | let val (T, env') = mk_tvar [] env | 
| 17412 | 71 | in (Free (s, T), T, Symtab.update_new (s, T) vTs, env') end | 
| 15531 | 72 | | SOME T => (Free (s, T), T, vTs, env)) | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 73 | else (t, T, vTs, env) | 
| 20312 | 74 | | infer_type thy env Ts vTs (Var _) = error "reconstruct_proof: internal error" | 
| 75 | | infer_type thy env Ts vTs (Abs (s, T, t)) = | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 76 | let | 
| 33245 | 77 | val (T', env') = if T = dummyT then mk_tvar [] env else (T, env); | 
| 20312 | 78 | val (t', U, vTs', env'') = infer_type thy env' (T' :: Ts) vTs t | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 79 | in (Abs (s, T', t'), T' --> U, vTs', env'') end | 
| 20312 | 80 | | infer_type thy env Ts vTs (t $ u) = | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 81 | let | 
| 20312 | 82 | val (t', T, vTs1, env1) = infer_type thy env Ts vTs t; | 
| 83 | val (u', U, vTs2, env2) = infer_type thy env1 Ts vTs1 u; | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 84 | in (case chaseT env2 T of | 
| 20312 | 85 |           Type ("fun", [U', V]) => (t' $ u', V, vTs2, unifyT thy env2 U U')
 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 86 | | _ => | 
| 33245 | 87 | let val (V, env3) = mk_tvar [] env2 | 
| 20312 | 88 | in (t' $ u', V, vTs2, unifyT thy env3 T (U --> V)) end) | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 89 | end | 
| 30146 | 90 | | infer_type thy env Ts vTs (t as Bound i) = ((t, nth Ts i, vTs, env) | 
| 25246 
584d8f2e1fc9
Handle Subscript exception when looking up bound variables.
 berghofe parents: 
23178diff
changeset | 91 |       handle Subscript => error ("infer_type: bad variable index " ^ string_of_int i));
 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 92 | |
| 20312 | 93 | fun cantunify thy (t, u) = error ("Non-unifiable terms:\n" ^
 | 
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26328diff
changeset | 94 | Syntax.string_of_term_global thy t ^ "\n\n" ^ Syntax.string_of_term_global thy u); | 
| 11522 | 95 | |
| 33245 | 96 | fun decompose thy Ts (p as (t, u)) env = | 
| 97 | let | |
| 98 | fun rigrig (a, T) (b, U) uT ts us = | |
| 99 | if a <> b then cantunify thy p | |
| 100 | else apfst flat (fold_map (decompose thy Ts) (ts ~~ us) (uT env T U)) | |
| 101 | in | |
| 102 | case pairself (strip_comb o Envir.head_norm env) p of | |
| 20312 | 103 | ((Const c, ts), (Const d, us)) => rigrig c d (unifyT thy) ts us | 
| 104 | | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT thy) ts us | |
| 13715 | 105 | | ((Bound i, ts), (Bound j, us)) => | 
| 106 | rigrig (i, dummyT) (j, dummyT) (K o K) ts us | |
| 107 | | ((Abs (_, T, t), []), (Abs (_, U, u), [])) => | |
| 33245 | 108 | decompose thy (T::Ts) (t, u) (unifyT thy env T U) | 
| 13715 | 109 | | ((Abs (_, T, t), []), _) => | 
| 33245 | 110 | decompose thy (T::Ts) (t, incr_boundvars 1 u $ Bound 0) env | 
| 13715 | 111 | | (_, (Abs (_, T, u), [])) => | 
| 33245 | 112 | decompose thy (T::Ts) (incr_boundvars 1 t $ Bound 0, u) env | 
| 113 | | _ => ([(mk_abs Ts t, mk_abs Ts u)], env) | |
| 13715 | 114 | end; | 
| 11522 | 115 | |
| 20312 | 116 | fun make_constraints_cprf thy env cprf = | 
| 11522 | 117 | let | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 118 | fun add_cnstrt Ts prop prf cs env vTs (t, u) = | 
| 11522 | 119 | let | 
| 120 | val t' = mk_abs Ts t; | |
| 12236 | 121 | val u' = mk_abs Ts u | 
| 11522 | 122 | in | 
| 20312 | 123 | (prop, prf, cs, Pattern.unify thy (t', u') env, vTs) | 
| 12236 | 124 | handle Pattern.Pattern => | 
| 33245 | 125 | let val (cs', env') = decompose thy [] (t', u') env | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 126 | in (prop, prf, cs @ cs', env', vTs) end | 
| 12236 | 127 | | Pattern.Unif => | 
| 20312 | 128 | cantunify thy (Envir.norm_term env t', Envir.norm_term env u') | 
| 11522 | 129 | end; | 
| 130 | ||
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 131 | fun mk_cnstrts_atom env vTs prop opTs prf = | 
| 11522 | 132 | let | 
| 36042 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
33245diff
changeset | 133 | val tvars = Term.add_tvars prop [] |> rev; | 
| 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
33245diff
changeset | 134 | val tfrees = Term.add_tfrees prop [] |> rev; | 
| 33245 | 135 | val (Ts, env') = | 
| 31903 | 136 | (case opTs of | 
| 33245 | 137 | NONE => fold_map mk_tvar (map snd tvars @ map snd tfrees) env | 
| 138 | | SOME Ts => (Ts, env)); | |
| 28813 | 139 | val prop' = subst_atomic_types (map TVar tvars @ map TFree tfrees ~~ Ts) | 
| 140 | (forall_intr_vfs prop) handle Library.UnequalLengths => | |
| 37310 | 141 |                 error ("Wrong number of type arguments for " ^ quote (Proofterm.guess_name prf))
 | 
| 142 | in (prop', Proofterm.change_type (SOME Ts) prf, [], env', vTs) end; | |
| 11522 | 143 | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 144 | fun head_norm (prop, prf, cnstrts, env, vTs) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 145 | (Envir.head_norm env prop, prf, cnstrts, env, vTs); | 
| 23178 | 146 | |
| 30146 | 147 | fun mk_cnstrts env _ Hs vTs (PBound i) = ((nth Hs i, PBound i, [], env, vTs) | 
| 25246 
584d8f2e1fc9
Handle Subscript exception when looking up bound variables.
 berghofe parents: 
23178diff
changeset | 148 |           handle Subscript => error ("mk_cnstrts: bad variable index " ^ string_of_int i))
 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 149 | | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 150 | let | 
| 33245 | 151 | val (T, env') = | 
| 152 | (case opT of | |
| 153 | NONE => mk_tvar [] env | |
| 154 | | SOME T => (T, env)); | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 155 | val (t, prf, cnstrts, env'', vTs') = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 156 | mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf; | 
| 15531 | 157 |           in (Const ("all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, SOME T, prf),
 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 158 | cnstrts, env'', vTs') | 
| 11522 | 159 | end | 
| 15531 | 160 | | mk_cnstrts env Ts Hs vTs (AbsP (s, SOME t, cprf)) = | 
| 11522 | 161 | let | 
| 20312 | 162 | val (t', _, vTs', env') = infer_type thy env Ts vTs t; | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 163 | val (u, prf, cnstrts, env'', vTs'') = mk_cnstrts env' Ts (t'::Hs) vTs' cprf; | 
| 15531 | 164 | in (Logic.mk_implies (t', u), AbsP (s, SOME t', prf), cnstrts, env'', vTs'') | 
| 11522 | 165 | end | 
| 15531 | 166 | | mk_cnstrts env Ts Hs vTs (AbsP (s, NONE, cprf)) = | 
| 11522 | 167 | let | 
| 33245 | 168 | val (t, env') = mk_var env Ts propT; | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 169 | val (u, prf, cnstrts, env'', vTs') = mk_cnstrts env' Ts (t::Hs) vTs cprf; | 
| 15531 | 170 | in (Logic.mk_implies (t, u), AbsP (s, SOME t, prf), cnstrts, env'', vTs') | 
| 11522 | 171 | end | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 172 | | mk_cnstrts env Ts Hs vTs (cprf1 %% cprf2) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 173 | 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: 
13610diff
changeset | 174 | in (case head_norm (mk_cnstrts env' Ts Hs vTs' cprf1) of | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 175 |               (Const ("==>", _) $ u' $ t', prf1, cnstrts', env'', vTs'') =>
 | 
| 11613 | 176 | add_cnstrt Ts t' (prf1 %% prf2) (cnstrts' @ cnstrts) | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 177 | env'' vTs'' (u, u') | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 178 | | (t, prf1, cnstrts', env'', vTs'') => | 
| 33245 | 179 | let val (v, env''') = mk_var env'' Ts propT | 
| 11613 | 180 | in add_cnstrt Ts v (prf1 %% prf2) (cnstrts' @ cnstrts) | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 181 | env''' vTs'' (t, Logic.mk_implies (u, v)) | 
| 11522 | 182 | end) | 
| 183 | end | |
| 15531 | 184 | | mk_cnstrts env Ts Hs vTs (cprf % SOME t) = | 
| 20312 | 185 | let val (t', U, vTs1, env1) = infer_type thy env Ts vTs t | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 186 | in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of | 
| 11522 | 187 |              (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 188 | prf, cnstrts, env2, vTs2) => | 
| 20312 | 189 | let val env3 = unifyT thy env2 T U | 
| 15531 | 190 | in (betapply (f, t'), prf % SOME t', cnstrts, env3, vTs2) | 
| 11522 | 191 | end | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 192 | | (u, prf, cnstrts, env2, vTs2) => | 
| 33245 | 193 | let val (v, env3) = mk_var env2 Ts (U --> propT); | 
| 11522 | 194 | in | 
| 15531 | 195 | add_cnstrt Ts (v $ t') (prf % SOME t') cnstrts env3 vTs2 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 196 |                    (u, Const ("all", (U --> propT) --> propT) $ v)
 | 
| 11522 | 197 | end) | 
| 198 | end | |
| 15531 | 199 | | mk_cnstrts env Ts Hs vTs (cprf % NONE) = | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 200 | (case head_norm (mk_cnstrts env Ts Hs vTs cprf) of | 
| 11522 | 201 |              (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 202 | prf, cnstrts, env', vTs') => | 
| 33245 | 203 | let val (t, env'') = mk_var env' Ts T | 
| 15531 | 204 | in (betapply (f, t), prf % SOME t, cnstrts, env'', vTs') | 
| 11522 | 205 | end | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 206 | | (u, prf, cnstrts, env', vTs') => | 
| 11522 | 207 | let | 
| 33245 | 208 | val (T, env1) = mk_tvar [] env'; | 
| 209 | val (v, env2) = mk_var env1 Ts (T --> propT); | |
| 210 | val (t, env3) = mk_var env2 Ts T | |
| 11522 | 211 | in | 
| 15531 | 212 | add_cnstrt Ts (v $ t) (prf % SOME t) cnstrts env3 vTs' | 
| 11522 | 213 |                    (u, Const ("all", (T --> propT) --> propT) $ v)
 | 
| 214 | end) | |
| 28808 | 215 | | mk_cnstrts env _ _ vTs (prf as PThm (_, ((_, prop, opTs), _))) = | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 216 | mk_cnstrts_atom env vTs prop opTs prf | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 217 | | mk_cnstrts env _ _ vTs (prf as PAxm (_, prop, opTs)) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 218 | mk_cnstrts_atom env vTs prop opTs prf | 
| 31943 
5e960a0780a2
renamed inclass/Inclass to of_class/OfClass, in accordance to of_sort;
 wenzelm parents: 
31903diff
changeset | 219 | | mk_cnstrts env _ _ vTs (prf as OfClass (T, c)) = | 
| 
5e960a0780a2
renamed inclass/Inclass to of_class/OfClass, in accordance to of_sort;
 wenzelm parents: 
31903diff
changeset | 220 | 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: 
13610diff
changeset | 221 | | mk_cnstrts env _ _ vTs (prf as Oracle (_, prop, opTs)) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 222 | mk_cnstrts_atom env vTs prop opTs prf | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 223 | | mk_cnstrts env _ _ vTs (Hyp t) = (t, Hyp t, [], env, vTs) | 
| 11613 | 224 | | mk_cnstrts _ _ _ _ _ = error "reconstruct_proof: minimal proof object" | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 225 | in mk_cnstrts env [] [] Symtab.empty cprf end; | 
| 11522 | 226 | |
| 227 | ||
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 228 | (**** update list of free variables of constraints ****) | 
| 11522 | 229 | |
| 230 | fun upd_constrs env cs = | |
| 231 | let | |
| 32032 | 232 | val tenv = Envir.term_env env; | 
| 233 | val tyenv = Envir.type_env env; | |
| 20675 | 234 | val dom = [] | 
| 32032 | 235 | |> Vartab.fold (cons o #1) tenv | 
| 236 | |> Vartab.fold (cons o #1) tyenv; | |
| 20675 | 237 | val vran = [] | 
| 32032 | 238 | |> Vartab.fold (Term.add_var_names o #2 o #2) tenv | 
| 239 | |> Vartab.fold (Term.add_tvar_namesT o #2 o #2) tyenv; | |
| 11522 | 240 | fun check_cs [] = [] | 
| 32032 | 241 | | check_cs ((u, p, vs) :: ps) = | 
| 242 | let val vs' = subtract (op =) dom vs in | |
| 243 | if vs = vs' then (u, p, vs) :: check_cs ps | |
| 244 | else (true, p, fold (insert op =) vs' vran) :: check_cs ps | |
| 245 | end; | |
| 11522 | 246 | in check_cs cs end; | 
| 247 | ||
| 32032 | 248 | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 249 | (**** solution of constraints ****) | 
| 11522 | 250 | |
| 251 | fun solve _ [] bigenv = bigenv | |
| 20312 | 252 | | solve thy cs bigenv = | 
| 11522 | 253 | let | 
| 11660 | 254 |         fun search env [] = error ("Unsolvable constraints:\n" ^
 | 
| 255 | Pretty.string_of (Pretty.chunks (map (fn (_, p, _) => | |
| 32187 | 256 | Goal_Display.pretty_flexpair (Syntax.init_pretty_global thy) (pairself | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 257 | (Envir.norm_term bigenv) p)) cs))) | 
| 11522 | 258 | | search env ((u, p as (t1, t2), vs)::ps) = | 
| 259 | if u then | |
| 260 | let | |
| 261 | val tn1 = Envir.norm_term bigenv t1; | |
| 262 | val tn2 = Envir.norm_term bigenv t2 | |
| 263 | in | |
| 264 | if Pattern.pattern tn1 andalso Pattern.pattern tn2 then | |
| 20312 | 265 | (Pattern.unify thy (tn1, tn2) env, ps) handle Pattern.Unif => | 
| 266 | cantunify thy (tn1, tn2) | |
| 11522 | 267 | else | 
| 33245 | 268 | let val (cs', env') = decompose thy [] (tn1, tn2) env | 
| 11522 | 269 | in if cs' = [(tn1, tn2)] then | 
| 270 | apsnd (cons (false, (tn1, tn2), vs)) (search env ps) | |
| 271 | else search env' (map (fn q => (true, q, vs)) cs' @ ps) | |
| 272 | end | |
| 273 | end | |
| 274 | else apsnd (cons (false, p, vs)) (search env ps); | |
| 275 |         val Envir.Envir {maxidx, ...} = bigenv;
 | |
| 276 | val (env, cs') = search (Envir.empty maxidx) cs; | |
| 277 | in | |
| 32032 | 278 | solve thy (upd_constrs env cs') (Envir.merge (bigenv, env)) | 
| 11522 | 279 | end; | 
| 280 | ||
| 281 | ||
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 282 | (**** reconstruction of proofs ****) | 
| 11522 | 283 | |
| 20312 | 284 | fun reconstruct_proof thy prop cprf = | 
| 11522 | 285 | let | 
| 37310 | 286 | val (cprf' % SOME prop', thawf) = Proofterm.freeze_thaw_prf (cprf % SOME prop); | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 287 | val _ = message "Collecting constraints..."; | 
| 20312 | 288 | val (t, prf, cs, env, _) = make_constraints_cprf thy | 
| 37310 | 289 | (Envir.empty (Proofterm.maxidx_proof cprf ~1)) cprf'; | 
| 33042 | 290 | val cs' = map (fn p => (true, p, uncurry (union (op =)) | 
| 29265 
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
 wenzelm parents: 
29259diff
changeset | 291 | (pairself (map (fst o dest_Var) o OldTerm.term_vars) p))) | 
| 20312 | 292 | (map (pairself (Envir.norm_term env)) ((t, prop')::cs)); | 
| 11522 | 293 |     val _ = message ("Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
 | 
| 20312 | 294 | val env' = solve thy cs' env | 
| 11522 | 295 | in | 
| 37310 | 296 | thawf (Proofterm.norm_proof env' prf) | 
| 11522 | 297 | end; | 
| 298 | ||
| 28813 | 299 | 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: 
33245diff
changeset | 300 | (map TVar (Term.add_tvars prop [] |> rev) @ map TFree (Term.add_tfrees prop [] |> rev) ~~ Ts) | 
| 28813 | 301 | (forall_intr_vfs prop); | 
| 13138 | 302 | |
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 303 | val head_norm = Envir.head_norm (Envir.empty 0); | 
| 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 304 | |
| 30146 | 305 | fun prop_of0 Hs (PBound i) = nth Hs i | 
| 15531 | 306 | | prop_of0 Hs (Abst (s, SOME T, prf)) = | 
| 27330 | 307 | Term.all T $ (Abs (s, T, prop_of0 Hs prf)) | 
| 15531 | 308 | | prop_of0 Hs (AbsP (s, SOME t, prf)) = | 
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 309 | Logic.mk_implies (t, prop_of0 (t :: Hs) prf) | 
| 15531 | 310 | | prop_of0 Hs (prf % SOME t) = (case head_norm (prop_of0 Hs prf) of | 
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 311 |       Const ("all", _) $ f => f $ t
 | 
| 13256 
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
 berghofe parents: 
13138diff
changeset | 312 | | _ => error "prop_of: all expected") | 
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 313 | | prop_of0 Hs (prf1 %% prf2) = (case head_norm (prop_of0 Hs prf1) of | 
| 13256 
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
 berghofe parents: 
13138diff
changeset | 314 |       Const ("==>", _) $ P $ Q => Q
 | 
| 
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
 berghofe parents: 
13138diff
changeset | 315 | | _ => error "prop_of: ==> expected") | 
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 316 | | prop_of0 Hs (Hyp t) = t | 
| 28808 | 317 | | prop_of0 Hs (PThm (_, ((_, prop, SOME Ts), _))) = prop_of_atom prop Ts | 
| 15531 | 318 | | 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: 
31903diff
changeset | 319 | | prop_of0 Hs (OfClass (T, c)) = Logic.mk_of_class (T, c) | 
| 15531 | 320 | | 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: 
13715diff
changeset | 321 | | prop_of0 _ _ = error "prop_of: partial proof object"; | 
| 13138 | 322 | |
| 18929 | 323 | val prop_of' = Envir.beta_eta_contract oo prop_of0; | 
| 13256 
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
 berghofe parents: 
13138diff
changeset | 324 | val prop_of = prop_of' []; | 
| 13138 | 325 | |
| 11522 | 326 | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 327 | (**** expand and reconstruct subproofs ****) | 
| 11522 | 328 | |
| 20312 | 329 | fun expand_proof thy thms prf = | 
| 11522 | 330 | let | 
| 23178 | 331 | fun expand maxidx prfs (AbsP (s, t, prf)) = | 
| 12870 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 332 | let val (maxidx', prfs', prf') = expand maxidx prfs prf | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 333 | in (maxidx', prfs', AbsP (s, t, prf')) end | 
| 23178 | 334 | | expand maxidx prfs (Abst (s, T, prf)) = | 
| 12870 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 335 | let val (maxidx', prfs', prf') = expand maxidx prfs prf | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 336 | in (maxidx', prfs', Abst (s, T, prf')) end | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 337 | | expand maxidx prfs (prf1 %% prf2) = | 
| 11522 | 338 | let | 
| 12870 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 339 | val (maxidx', prfs', prf1') = expand maxidx prfs prf1; | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 340 | val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2; | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 341 | in (maxidx'', prfs'', prf1' %% prf2') end | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 342 | | expand maxidx prfs (prf % t) = | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 343 | let val (maxidx', prfs', prf') = expand maxidx prfs prf | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 344 | in (maxidx', prfs', prf' % t) end | 
| 28808 | 345 | | expand maxidx prfs (prf as PThm (_, ((a, prop, SOME Ts), body))) = | 
| 13342 
915d4d004643
expand_proof now also takes an optional term describing the proposition
 berghofe parents: 
13256diff
changeset | 346 | if not (exists | 
| 15531 | 347 | (fn (b, NONE) => a = b | 
| 348 | | (b, SOME prop') => a = b andalso prop = prop') thms) | |
| 13342 
915d4d004643
expand_proof now also takes an optional term describing the proposition
 berghofe parents: 
13256diff
changeset | 349 | then (maxidx, prfs, prf) else | 
| 11522 | 350 | let | 
| 16862 | 351 | val (maxidx', prf, prfs') = | 
| 17232 | 352 | (case AList.lookup (op =) prfs (a, prop) of | 
| 15531 | 353 | NONE => | 
| 11522 | 354 | let | 
| 355 |                     val _ = message ("Reconstructing proof of " ^ a);
 | |
| 26939 
1035c89b4c02
moved global pretty/string_of functions from Sign to Syntax;
 wenzelm parents: 
26328diff
changeset | 356 | val _ = message (Syntax.string_of_term_global thy prop); | 
| 13610 
d4a2ac255447
Fixed bug in expand_proof which caused indexes to be incremented incorrectly.
 berghofe parents: 
13342diff
changeset | 357 | val prf' = forall_intr_vfs_prf prop | 
| 37310 | 358 | (reconstruct_proof thy prop (Proofterm.join_proof body)); | 
| 12870 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 359 | val (maxidx', prfs', prf) = expand | 
| 37310 | 360 | (Proofterm.maxidx_proof prf' ~1) prfs prf' | 
| 361 | in (maxidx' + maxidx + 1, Proofterm.incr_indexes (maxidx + 1) prf, | |
| 13610 
d4a2ac255447
Fixed bug in expand_proof which caused indexes to be incremented incorrectly.
 berghofe parents: 
13342diff
changeset | 362 | ((a, prop), (maxidx', prf)) :: prfs') | 
| 
d4a2ac255447
Fixed bug in expand_proof which caused indexes to be incremented incorrectly.
 berghofe parents: 
13342diff
changeset | 363 | end | 
| 15531 | 364 | | SOME (maxidx', prf) => (maxidx' + maxidx + 1, | 
| 37310 | 365 | Proofterm.incr_indexes (maxidx + 1) prf, prfs)); | 
| 36042 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
33245diff
changeset | 366 | val tfrees = Term.add_tfrees prop [] |> rev; | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 367 | val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j)) | 
| 36042 
85efdadee8ae
switched PThm/PAxm etc. to use canonical order of type variables (term variables unchanged)
 krauss parents: 
33245diff
changeset | 368 | (Term.add_tvars prop [] |> rev) @ map (rpair ~1 o fst) tfrees ~~ Ts; | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 369 | val varify = map_type_tfree (fn p as (a, S) => | 
| 20675 | 370 | if member (op =) tfrees p then TVar ((a, ~1), S) else TFree p) | 
| 11522 | 371 | in | 
| 37310 | 372 | (maxidx', prfs', Proofterm.map_proof_types (typ_subst_TVars tye o varify) prf) | 
| 11522 | 373 | end | 
| 12870 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 374 | | expand maxidx prfs prf = (maxidx, prfs, prf); | 
| 11522 | 375 | |
| 37310 | 376 | in #3 (expand (Proofterm.maxidx_proof prf ~1) [] prf) end; | 
| 11522 | 377 | |
| 378 | end; |