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