| author | haftmann | 
| Wed, 28 Nov 2007 09:01:42 +0100 | |
| changeset 25485 | 33840a854e63 | 
| parent 25246 | 584d8f2e1fc9 | 
| child 26328 | b2d6f520172c | 
| permissions | -rw-r--r-- | 
| 11522 | 1 | (* Title: Pure/Proof/reconstruct.ML | 
| 2 | ID: $Id$ | |
| 11539 | 3 | Author: Stefan Berghofer, TU Muenchen | 
| 11522 | 4 | |
| 5 | Reconstruction of partial proof terms. | |
| 6 | *) | |
| 7 | ||
| 8 | signature RECONSTRUCT = | |
| 9 | sig | |
| 10 | val quiet_mode : bool ref | |
| 16862 | 11 | 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 | 12 | val prop_of' : term list -> Proofterm.proof -> term | 
| 13138 | 13 | val prop_of : Proofterm.proof -> term | 
| 16862 | 14 | 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 | 15 | Proofterm.proof -> Proofterm.proof | 
| 11522 | 16 | end; | 
| 17 | ||
| 18 | structure Reconstruct : RECONSTRUCT = | |
| 19 | struct | |
| 20 | ||
| 21 | open Proofterm; | |
| 22 | ||
| 23 | val quiet_mode = ref true; | |
| 24 | fun message s = if !quiet_mode then () else writeln s; | |
| 25 | ||
| 16787 | 26 | fun vars_of t = rev (fold_aterms | 
| 16862 | 27 | (fn v as Var _ => insert (op =) v | _ => I) t []); | 
| 11522 | 28 | |
| 23178 | 29 | fun forall_intr t prop = | 
| 11522 | 30 | let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p) | 
| 31 | in all T $ Abs (a, T, abstract_over (t, prop)) end; | |
| 32 | ||
| 23178 | 33 | fun forall_intr_vfs prop = fold_rev forall_intr | 
| 34 | (vars_of prop @ sort Term.term_ord (term_frees prop)) prop; | |
| 11522 | 35 | |
| 23178 | 36 | fun forall_intr_prf t prf = | 
| 12001 | 37 | let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p) | 
| 15531 | 38 | in Abst (a, SOME T, prf_abstract_over t prf) end; | 
| 12001 | 39 | |
| 23178 | 40 | fun forall_intr_vfs_prf prop prf = fold_rev forall_intr_prf | 
| 41 | (vars_of prop @ sort Term.term_ord (term_frees prop)) prf; | |
| 12001 | 42 | |
| 11522 | 43 | fun merge_envs (Envir.Envir {asol=asol1, iTs=iTs1, maxidx=maxidx1})
 | 
| 44 |   (Envir.Envir {asol=asol2, iTs=iTs2, maxidx=maxidx2}) =
 | |
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15574diff
changeset | 45 |     Envir.Envir {asol=Vartab.merge (op =) (asol1, asol2),
 | 
| 11522 | 46 | iTs=Vartab.merge (op =) (iTs1, iTs2), | 
| 47 | maxidx=Int.max (maxidx1, maxidx2)}; | |
| 48 | ||
| 49 | ||
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 50 | (**** generate constraints for proof term ****) | 
| 11522 | 51 | |
| 23178 | 52 | fun mk_var env Ts T = | 
| 11522 | 53 | let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T) | 
| 54 | in (env', list_comb (v, map Bound (length Ts - 1 downto 0))) end; | |
| 55 | ||
| 56 | fun mk_tvar (Envir.Envir {iTs, asol, maxidx}, s) =
 | |
| 57 |   (Envir.Envir {iTs = iTs, asol = asol, maxidx = maxidx+1},
 | |
| 58 |    TVar (("'t", maxidx+1), s));
 | |
| 59 | ||
| 19473 | 60 | val mk_abs = fold (fn T => fn u => Abs ("", T, u));
 | 
| 11522 | 61 | |
| 20312 | 62 | fun unifyT thy env T U = | 
| 11522 | 63 | let | 
| 64 |     val Envir.Envir {asol, iTs, maxidx} = env;
 | |
| 20312 | 65 | val (iTs', maxidx') = Sign.typ_unify thy (T, U) (iTs, maxidx) | 
| 11660 | 66 |   in Envir.Envir {asol=asol, iTs=iTs', maxidx=maxidx'} end
 | 
| 67 |   handle Type.TUNIFY => error ("Non-unifiable types:\n" ^
 | |
| 20312 | 68 | Sign.string_of_typ thy T ^ "\n\n" ^ Sign.string_of_typ thy U); | 
| 11522 | 69 | |
| 15798 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15574diff
changeset | 70 | fun chaseT (env as Envir.Envir {iTs, ...}) (T as TVar ixnS) =
 | 
| 
016f3be5a5ec
Adapted to new interface of instantiation and unification / matching functions.
 berghofe parents: 
15574diff
changeset | 71 | (case Type.lookup (iTs, ixnS) of NONE => T | SOME T' => chaseT env T') | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 72 | | chaseT _ T = T; | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 73 | |
| 20312 | 74 | fun infer_type thy (env as Envir.Envir {maxidx, asol, iTs}) Ts vTs
 | 
| 75 | (t as Const (s, T)) = if T = dummyT then (case Sign.const_type thy s of | |
| 15531 | 76 |           NONE => error ("reconstruct_proof: No such constant: " ^ quote s)
 | 
| 23178 | 77 | | SOME T => | 
| 19419 | 78 | 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 | 79 | in (Const (s, T'), T', vTs, | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 80 |               Envir.Envir {maxidx = maxidx + 1, asol = asol, iTs = iTs})
 | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 81 | end) | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 82 | else (t, T, vTs, env) | 
| 20312 | 83 | | infer_type thy env Ts vTs (t as Free (s, T)) = | 
| 17412 | 84 | if T = dummyT then (case Symtab.lookup vTs s of | 
| 15531 | 85 | NONE => | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 86 | let val (env', T) = mk_tvar (env, []) | 
| 17412 | 87 | in (Free (s, T), T, Symtab.update_new (s, T) vTs, env') end | 
| 15531 | 88 | | SOME T => (Free (s, T), T, vTs, env)) | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 89 | else (t, T, vTs, env) | 
| 20312 | 90 | | infer_type thy env Ts vTs (Var _) = error "reconstruct_proof: internal error" | 
| 91 | | 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 | 92 | let | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 93 | val (env', T') = if T = dummyT then mk_tvar (env, []) else (env, T); | 
| 20312 | 94 | 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 | 95 | in (Abs (s, T', t'), T' --> U, vTs', env'') end | 
| 20312 | 96 | | infer_type thy env Ts vTs (t $ u) = | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 97 | let | 
| 20312 | 98 | val (t', T, vTs1, env1) = infer_type thy env Ts vTs t; | 
| 99 | 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 | 100 | in (case chaseT env2 T of | 
| 20312 | 101 |           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 | 102 | | _ => | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 103 | let val (env3, V) = mk_tvar (env2, []) | 
| 20312 | 104 | 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 | 105 | end | 
| 25246 
584d8f2e1fc9
Handle Subscript exception when looking up bound variables.
 berghofe parents: 
23178diff
changeset | 106 | | infer_type thy env Ts vTs (t as Bound i) = ((t, List.nth (Ts, i), vTs, env) | 
| 
584d8f2e1fc9
Handle Subscript exception when looking up bound variables.
 berghofe parents: 
23178diff
changeset | 107 |       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 | 108 | |
| 20312 | 109 | fun cantunify thy (t, u) = error ("Non-unifiable terms:\n" ^
 | 
| 110 | Sign.string_of_term thy t ^ "\n\n" ^ Sign.string_of_term thy u); | |
| 11522 | 111 | |
| 20312 | 112 | fun decompose thy Ts (env, p as (t, u)) = | 
| 113 | let fun rigrig (a, T) (b, U) uT ts us = if a <> b then cantunify thy p | |
| 114 | else apsnd flat (foldl_map (decompose thy Ts) (uT env T U, ts ~~ us)) | |
| 13715 | 115 | in case pairself (strip_comb o Envir.head_norm env) p of | 
| 20312 | 116 | ((Const c, ts), (Const d, us)) => rigrig c d (unifyT thy) ts us | 
| 117 | | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT thy) ts us | |
| 13715 | 118 | | ((Bound i, ts), (Bound j, us)) => | 
| 119 | rigrig (i, dummyT) (j, dummyT) (K o K) ts us | |
| 120 | | ((Abs (_, T, t), []), (Abs (_, U, u), [])) => | |
| 20312 | 121 | decompose thy (T::Ts) (unifyT thy env T U, (t, u)) | 
| 13715 | 122 | | ((Abs (_, T, t), []), _) => | 
| 20312 | 123 | decompose thy (T::Ts) (env, (t, incr_boundvars 1 u $ Bound 0)) | 
| 13715 | 124 | | (_, (Abs (_, T, u), [])) => | 
| 20312 | 125 | decompose thy (T::Ts) (env, (incr_boundvars 1 t $ Bound 0, u)) | 
| 13715 | 126 | | _ => (env, [(mk_abs Ts t, mk_abs Ts u)]) | 
| 127 | end; | |
| 11522 | 128 | |
| 20312 | 129 | fun make_constraints_cprf thy env cprf = | 
| 11522 | 130 | let | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 131 | fun add_cnstrt Ts prop prf cs env vTs (t, u) = | 
| 11522 | 132 | let | 
| 133 | val t' = mk_abs Ts t; | |
| 12236 | 134 | val u' = mk_abs Ts u | 
| 11522 | 135 | in | 
| 20312 | 136 | (prop, prf, cs, Pattern.unify thy (t', u') env, vTs) | 
| 12236 | 137 | handle Pattern.Pattern => | 
| 20312 | 138 | let val (env', cs') = decompose thy [] (env, (t', u')) | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 139 | in (prop, prf, cs @ cs', env', vTs) end | 
| 12236 | 140 | | Pattern.Unif => | 
| 20312 | 141 | cantunify thy (Envir.norm_term env t', Envir.norm_term env u') | 
| 11522 | 142 | end; | 
| 143 | ||
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 144 | fun mk_cnstrts_atom env vTs prop opTs prf = | 
| 11522 | 145 | let | 
| 146 | val tvars = term_tvars prop; | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 147 | val tfrees = term_tfrees prop; | 
| 21116 | 148 | val (fmap, prop') = Type.varify [] prop; | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 149 | val (env', Ts) = (case opTs of | 
| 15531 | 150 | NONE => foldl_map mk_tvar (env, map snd tvars @ map snd tfrees) | 
| 151 | | SOME Ts => (env, Ts)); | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 152 | val prop'' = subst_TVars (map fst tvars @ map snd fmap ~~ Ts) | 
| 19841 | 153 | (forall_intr_vfs prop') handle Library.UnequalLengths => | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 154 |                 error ("Wrong number of type arguments for " ^
 | 
| 21646 
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
 wenzelm parents: 
21116diff
changeset | 155 | quote (get_name [] prop prf)) | 
| 15531 | 156 | in (prop'', change_type (SOME Ts) prf, [], env', vTs) end; | 
| 11522 | 157 | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 158 | fun head_norm (prop, prf, cnstrts, env, vTs) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 159 | (Envir.head_norm env prop, prf, cnstrts, env, vTs); | 
| 23178 | 160 | |
| 25246 
584d8f2e1fc9
Handle Subscript exception when looking up bound variables.
 berghofe parents: 
23178diff
changeset | 161 | fun mk_cnstrts env _ Hs vTs (PBound i) = ((List.nth (Hs, i), PBound i, [], env, vTs) | 
| 
584d8f2e1fc9
Handle Subscript exception when looking up bound variables.
 berghofe parents: 
23178diff
changeset | 162 |           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 | 163 | | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 164 | let | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 165 | val (env', T) = (case opT of | 
| 15531 | 166 | NONE => mk_tvar (env, []) | SOME T => (env, T)); | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 167 | val (t, prf, cnstrts, env'', vTs') = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 168 | mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf; | 
| 15531 | 169 |           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 | 170 | cnstrts, env'', vTs') | 
| 11522 | 171 | end | 
| 15531 | 172 | | mk_cnstrts env Ts Hs vTs (AbsP (s, SOME t, cprf)) = | 
| 11522 | 173 | let | 
| 20312 | 174 | 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 | 175 | val (u, prf, cnstrts, env'', vTs'') = mk_cnstrts env' Ts (t'::Hs) vTs' cprf; | 
| 15531 | 176 | in (Logic.mk_implies (t', u), AbsP (s, SOME t', prf), cnstrts, env'', vTs'') | 
| 11522 | 177 | end | 
| 15531 | 178 | | mk_cnstrts env Ts Hs vTs (AbsP (s, NONE, cprf)) = | 
| 11522 | 179 | let | 
| 180 | val (env', t) = mk_var env Ts propT; | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 181 | val (u, prf, cnstrts, env'', vTs') = mk_cnstrts env' Ts (t::Hs) vTs cprf; | 
| 15531 | 182 | in (Logic.mk_implies (t, u), AbsP (s, SOME t, prf), cnstrts, env'', vTs') | 
| 11522 | 183 | end | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 184 | | mk_cnstrts env Ts Hs vTs (cprf1 %% cprf2) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 185 | 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 | 186 | 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 | 187 |               (Const ("==>", _) $ u' $ t', prf1, cnstrts', env'', vTs'') =>
 | 
| 11613 | 188 | add_cnstrt Ts t' (prf1 %% prf2) (cnstrts' @ cnstrts) | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 189 | env'' vTs'' (u, u') | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 190 | | (t, prf1, cnstrts', env'', vTs'') => | 
| 11522 | 191 | let val (env''', v) = mk_var env'' Ts propT | 
| 11613 | 192 | in add_cnstrt Ts v (prf1 %% prf2) (cnstrts' @ cnstrts) | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 193 | env''' vTs'' (t, Logic.mk_implies (u, v)) | 
| 11522 | 194 | end) | 
| 195 | end | |
| 15531 | 196 | | mk_cnstrts env Ts Hs vTs (cprf % SOME t) = | 
| 20312 | 197 | 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 | 198 | in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of | 
| 11522 | 199 |              (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 200 | prf, cnstrts, env2, vTs2) => | 
| 20312 | 201 | let val env3 = unifyT thy env2 T U | 
| 15531 | 202 | in (betapply (f, t'), prf % SOME t', cnstrts, env3, vTs2) | 
| 11522 | 203 | end | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 204 | | (u, prf, cnstrts, env2, vTs2) => | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 205 | let val (env3, v) = mk_var env2 Ts (U --> propT); | 
| 11522 | 206 | in | 
| 15531 | 207 | 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 | 208 |                    (u, Const ("all", (U --> propT) --> propT) $ v)
 | 
| 11522 | 209 | end) | 
| 210 | end | |
| 15531 | 211 | | mk_cnstrts env Ts Hs vTs (cprf % NONE) = | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 212 | (case head_norm (mk_cnstrts env Ts Hs vTs cprf) of | 
| 11522 | 213 |              (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
 | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 214 | prf, cnstrts, env', vTs') => | 
| 11522 | 215 | let val (env'', t) = mk_var env' Ts T | 
| 15531 | 216 | in (betapply (f, t), prf % SOME t, cnstrts, env'', vTs') | 
| 11522 | 217 | end | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 218 | | (u, prf, cnstrts, env', vTs') => | 
| 11522 | 219 | let | 
| 14854 | 220 | val (env1, T) = mk_tvar (env', []); | 
| 11522 | 221 | val (env2, v) = mk_var env1 Ts (T --> propT); | 
| 222 | val (env3, t) = mk_var env2 Ts T | |
| 223 | in | |
| 15531 | 224 | add_cnstrt Ts (v $ t) (prf % SOME t) cnstrts env3 vTs' | 
| 11522 | 225 |                    (u, Const ("all", (T --> propT) --> propT) $ v)
 | 
| 226 | end) | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 227 | | mk_cnstrts env _ _ vTs (prf as PThm (_, _, prop, opTs)) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 228 | mk_cnstrts_atom env vTs prop opTs prf | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 229 | | mk_cnstrts env _ _ vTs (prf as PAxm (_, prop, opTs)) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 230 | mk_cnstrts_atom env vTs prop opTs prf | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 231 | | mk_cnstrts env _ _ vTs (prf as Oracle (_, prop, opTs)) = | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 232 | mk_cnstrts_atom env vTs prop opTs prf | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 233 | | mk_cnstrts env _ _ vTs (Hyp t) = (t, Hyp t, [], env, vTs) | 
| 11613 | 234 | | mk_cnstrts _ _ _ _ _ = error "reconstruct_proof: minimal proof object" | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 235 | in mk_cnstrts env [] [] Symtab.empty cprf end; | 
| 11522 | 236 | |
| 237 | ||
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 238 | (**** update list of free variables of constraints ****) | 
| 11522 | 239 | |
| 20675 | 240 | val add_term_ixns = fold_aterms (fn Var (xi, _) => insert (op =) xi | _ => I); | 
| 241 | val add_typ_ixns = fold_atyps (fn TVar (ai, _) => insert (op =) ai | _ => I); | |
| 242 | ||
| 11522 | 243 | fun upd_constrs env cs = | 
| 244 | let | |
| 245 |     val Envir.Envir {asol, iTs, ...} = env;
 | |
| 20675 | 246 | val dom = [] | 
| 247 | |> Vartab.fold (cons o #1) asol | |
| 248 | |> Vartab.fold (cons o #1) iTs; | |
| 249 | val vran = [] | |
| 250 | |> Vartab.fold (add_term_ixns o #2 o #2) asol | |
| 251 | |> Vartab.fold (add_typ_ixns o #2 o #2) iTs; | |
| 11522 | 252 | fun check_cs [] = [] | 
| 253 | | check_cs ((u, p, vs)::ps) = | |
| 19305 | 254 | let val vs' = subtract (op =) dom vs; | 
| 11522 | 255 | in if vs = vs' then (u, p, vs)::check_cs ps | 
| 20675 | 256 | else (true, p, fold (insert (op =)) vs' vran)::check_cs ps | 
| 11522 | 257 | end | 
| 258 | in check_cs cs end; | |
| 259 | ||
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 260 | (**** solution of constraints ****) | 
| 11522 | 261 | |
| 262 | fun solve _ [] bigenv = bigenv | |
| 20312 | 263 | | solve thy cs bigenv = | 
| 11522 | 264 | let | 
| 11660 | 265 |         fun search env [] = error ("Unsolvable constraints:\n" ^
 | 
| 266 | Pretty.string_of (Pretty.chunks (map (fn (_, p, _) => | |
| 20312 | 267 | Display.pretty_flexpair (Sign.pp thy) (pairself | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 268 | (Envir.norm_term bigenv) p)) cs))) | 
| 11522 | 269 | | search env ((u, p as (t1, t2), vs)::ps) = | 
| 270 | if u then | |
| 271 | let | |
| 272 | val tn1 = Envir.norm_term bigenv t1; | |
| 273 | val tn2 = Envir.norm_term bigenv t2 | |
| 274 | in | |
| 275 | if Pattern.pattern tn1 andalso Pattern.pattern tn2 then | |
| 20312 | 276 | (Pattern.unify thy (tn1, tn2) env, ps) handle Pattern.Unif => | 
| 277 | cantunify thy (tn1, tn2) | |
| 11522 | 278 | else | 
| 20312 | 279 | let val (env', cs') = decompose thy [] (env, (tn1, tn2)) | 
| 11522 | 280 | in if cs' = [(tn1, tn2)] then | 
| 281 | apsnd (cons (false, (tn1, tn2), vs)) (search env ps) | |
| 282 | else search env' (map (fn q => (true, q, vs)) cs' @ ps) | |
| 283 | end | |
| 284 | end | |
| 285 | else apsnd (cons (false, p, vs)) (search env ps); | |
| 286 |         val Envir.Envir {maxidx, ...} = bigenv;
 | |
| 287 | val (env, cs') = search (Envir.empty maxidx) cs; | |
| 288 | in | |
| 20312 | 289 | solve thy (upd_constrs env cs') (merge_envs bigenv env) | 
| 11522 | 290 | end; | 
| 291 | ||
| 292 | ||
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 293 | (**** reconstruction of proofs ****) | 
| 11522 | 294 | |
| 20312 | 295 | fun reconstruct_proof thy prop cprf = | 
| 11522 | 296 | let | 
| 15531 | 297 | val (cprf' % SOME prop', thawf) = freeze_thaw_prf (cprf % SOME prop); | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 298 | val _ = message "Collecting constraints..."; | 
| 20312 | 299 | val (t, prf, cs, env, _) = make_constraints_cprf thy | 
| 300 | (Envir.empty (maxidx_proof cprf ~1)) cprf'; | |
| 11522 | 301 | val cs' = map (fn p => (true, p, op union | 
| 20312 | 302 | (pairself (map (fst o dest_Var) o term_vars) p))) | 
| 303 | (map (pairself (Envir.norm_term env)) ((t, prop')::cs)); | |
| 11522 | 304 |     val _ = message ("Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
 | 
| 20312 | 305 | val env' = solve thy cs' env | 
| 11522 | 306 | in | 
| 307 | thawf (norm_proof env' prf) | |
| 308 | end; | |
| 309 | ||
| 13256 
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
 berghofe parents: 
13138diff
changeset | 310 | fun prop_of_atom prop Ts = | 
| 21116 | 311 | let val (fmap, prop') = Type.varify [] prop; | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 312 | in subst_TVars (map fst (term_tvars prop) @ map snd fmap ~~ Ts) | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 313 | (forall_intr_vfs prop') | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 314 | end; | 
| 13138 | 315 | |
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 316 | val head_norm = Envir.head_norm (Envir.empty 0); | 
| 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 317 | |
| 15570 | 318 | fun prop_of0 Hs (PBound i) = List.nth (Hs, i) | 
| 15531 | 319 | | prop_of0 Hs (Abst (s, SOME T, prf)) = | 
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 320 | all T $ (Abs (s, T, prop_of0 Hs prf)) | 
| 15531 | 321 | | prop_of0 Hs (AbsP (s, SOME t, prf)) = | 
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 322 | Logic.mk_implies (t, prop_of0 (t :: Hs) prf) | 
| 15531 | 323 | | 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 | 324 |       Const ("all", _) $ f => f $ t
 | 
| 13256 
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
 berghofe parents: 
13138diff
changeset | 325 | | _ => error "prop_of: all expected") | 
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 326 | | 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 | 327 |       Const ("==>", _) $ P $ Q => Q
 | 
| 
cf85c4f7dcf2
Added function prop_of' taking assumption context as an argument.
 berghofe parents: 
13138diff
changeset | 328 | | _ => error "prop_of: ==> expected") | 
| 13734 
50dcee1c509e
prop_of now returns proposition in beta-eta normal form.
 berghofe parents: 
13715diff
changeset | 329 | | prop_of0 Hs (Hyp t) = t | 
| 15531 | 330 | | prop_of0 Hs (PThm (_, _, prop, SOME Ts)) = prop_of_atom prop Ts | 
| 331 | | prop_of0 Hs (PAxm (_, prop, SOME Ts)) = prop_of_atom prop Ts | |
| 332 | | 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 | 333 | | prop_of0 _ _ = error "prop_of: partial proof object"; | 
| 13138 | 334 | |
| 18929 | 335 | 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 | 336 | val prop_of = prop_of' []; | 
| 13138 | 337 | |
| 11522 | 338 | |
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 339 | (**** expand and reconstruct subproofs ****) | 
| 11522 | 340 | |
| 20312 | 341 | fun expand_proof thy thms prf = | 
| 11522 | 342 | let | 
| 23178 | 343 | 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 | 344 | 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 | 345 | in (maxidx', prfs', AbsP (s, t, prf')) end | 
| 23178 | 346 | | 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 | 347 | 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 | 348 | 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 | 349 | | expand maxidx prfs (prf1 %% prf2) = | 
| 11522 | 350 | let | 
| 12870 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 351 | 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 | 352 | 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 | 353 | in (maxidx'', prfs'', prf1' %% prf2') end | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 354 | | expand maxidx prfs (prf % t) = | 
| 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 355 | 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 | 356 | in (maxidx', prfs', prf' % t) end | 
| 21646 
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
 wenzelm parents: 
21116diff
changeset | 357 | | expand maxidx prfs (prf as PThm (a, cprf, prop, SOME Ts)) = | 
| 13342 
915d4d004643
expand_proof now also takes an optional term describing the proposition
 berghofe parents: 
13256diff
changeset | 358 | if not (exists | 
| 15531 | 359 | (fn (b, NONE) => a = b | 
| 360 | | (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 | 361 | then (maxidx, prfs, prf) else | 
| 11522 | 362 | let | 
| 13610 
d4a2ac255447
Fixed bug in expand_proof which caused indexes to be incremented incorrectly.
 berghofe parents: 
13342diff
changeset | 363 | fun inc i = | 
| 16876 | 364 | map_proof_terms (Logic.incr_indexes ([], i)) (Logic.incr_tvar i); | 
| 16862 | 365 | val (maxidx', prf, prfs') = | 
| 17232 | 366 | (case AList.lookup (op =) prfs (a, prop) of | 
| 15531 | 367 | NONE => | 
| 11522 | 368 | let | 
| 369 |                     val _ = message ("Reconstructing proof of " ^ a);
 | |
| 20312 | 370 | val _ = message (Sign.string_of_term thy prop); | 
| 13610 
d4a2ac255447
Fixed bug in expand_proof which caused indexes to be incremented incorrectly.
 berghofe parents: 
13342diff
changeset | 371 | val prf' = forall_intr_vfs_prf prop | 
| 20312 | 372 | (reconstruct_proof thy prop cprf); | 
| 12870 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 373 | val (maxidx', prfs', prf) = expand | 
| 20312 | 374 | (maxidx_proof prf' ~1) prfs prf' | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 375 | in (maxidx' + maxidx + 1, inc (maxidx + 1) prf, | 
| 13610 
d4a2ac255447
Fixed bug in expand_proof which caused indexes to be incremented incorrectly.
 berghofe parents: 
13342diff
changeset | 376 | ((a, prop), (maxidx', prf)) :: prfs') | 
| 
d4a2ac255447
Fixed bug in expand_proof which caused indexes to be incremented incorrectly.
 berghofe parents: 
13342diff
changeset | 377 | end | 
| 15531 | 378 | | SOME (maxidx', prf) => (maxidx' + maxidx + 1, | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 379 | inc (maxidx + 1) prf, prfs)); | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 380 | val tfrees = term_tfrees prop; | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 381 | val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j)) | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 382 | (term_tvars prop) @ map (rpair ~1 o fst) tfrees ~~ Ts; | 
| 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 383 | val varify = map_type_tfree (fn p as (a, S) => | 
| 20675 | 384 | if member (op =) tfrees p then TVar ((a, ~1), S) else TFree p) | 
| 11522 | 385 | in | 
| 13669 
a9f229eafba7
- reconstruct_proof no longer relies on TypeInfer.infer_types
 berghofe parents: 
13610diff
changeset | 386 | (maxidx', prfs', map_proof_terms (subst_TVars tye o | 
| 20548 
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
 wenzelm parents: 
20312diff
changeset | 387 | map_types varify) (typ_subst_TVars tye o varify) prf) | 
| 11522 | 388 | end | 
| 12870 
3905bc0e9002
Indexes of variables in expanded proofs are now incremented to avoid clashes.
 berghofe parents: 
12527diff
changeset | 389 | | expand maxidx prfs prf = (maxidx, prfs, prf); | 
| 11522 | 390 | |
| 20312 | 391 | in #3 (expand (maxidx_proof prf ~1) [] prf) end; | 
| 11522 | 392 | |
| 393 | end; |