src/Pure/Proof/reconstruct.ML
author wenzelm
Tue Sep 29 11:49:22 2009 +0200 (2009-09-29)
changeset 32738 15bb09ca0378
parent 32187 cca43ca13f4f
child 33037 b22e44496dc2
permissions -rw-r--r--
explicit indication of Unsynchronized.ref;
     1 (*  Title:      Pure/Proof/reconstruct.ML
     2     Author:     Stefan Berghofer, TU Muenchen
     3 
     4 Reconstruction of partial proof terms.
     5 *)
     6 
     7 signature RECONSTRUCT =
     8 sig
     9   val quiet_mode : bool Unsynchronized.ref
    10   val reconstruct_proof : theory -> term -> Proofterm.proof -> Proofterm.proof
    11   val prop_of' : term list -> Proofterm.proof -> term
    12   val prop_of : Proofterm.proof -> term
    13   val expand_proof : theory -> (string * term option) list ->
    14     Proofterm.proof -> Proofterm.proof
    15 end;
    16 
    17 structure Reconstruct : RECONSTRUCT =
    18 struct
    19 
    20 open Proofterm;
    21 
    22 val quiet_mode = Unsynchronized.ref true;
    23 fun message s = if !quiet_mode then () else writeln s;
    24 
    25 fun vars_of t = map Var (rev (Term.add_vars t []));
    26 fun frees_of t = map Free (rev (Term.add_frees t []));
    27 
    28 fun forall_intr_vfs prop = fold_rev Logic.all
    29   (vars_of prop @ frees_of prop) prop;
    30 
    31 fun forall_intr_prf t prf =
    32   let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p)
    33   in Abst (a, SOME T, prf_abstract_over t prf) end;
    34 
    35 fun forall_intr_vfs_prf prop prf = fold_rev forall_intr_prf
    36   (vars_of prop @ frees_of prop) prf;
    37 
    38 
    39 (**** generate constraints for proof term ****)
    40 
    41 fun mk_var env Ts T =
    42   let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T)
    43   in (env', list_comb (v, map Bound (length Ts - 1 downto 0))) end;
    44 
    45 fun mk_tvar (Envir.Envir {maxidx, tenv, tyenv}, s) =
    46   (Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv},
    47    TVar (("'t", maxidx + 1), s));
    48 
    49 val mk_abs = fold (fn T => fn u => Abs ("", T, u));
    50 
    51 fun unifyT thy env T U =
    52   let
    53     val Envir.Envir {maxidx, tenv, tyenv} = env;
    54     val (tyenv', maxidx') = Sign.typ_unify thy (T, U) (tyenv, maxidx);
    55   in Envir.Envir {maxidx = maxidx', tenv = tenv, tyenv = tyenv'} end;
    56 
    57 fun chaseT env (T as TVar v) =
    58       (case Type.lookup (Envir.type_env env) v of
    59         NONE => T
    60       | SOME T' => chaseT env T')
    61   | chaseT _ T = T;
    62 
    63 fun infer_type thy (env as Envir.Envir {maxidx, tenv, tyenv}) Ts vTs
    64       (t as Const (s, T)) = if T = dummyT then
    65         (case Sign.const_type thy s of
    66           NONE => error ("reconstruct_proof: No such constant: " ^ quote s)
    67         | SOME T =>
    68             let val T' = Type.strip_sorts (Logic.incr_tvar (maxidx + 1) T)
    69             in (Const (s, T'), T', vTs,
    70               Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv})
    71             end)
    72       else (t, T, vTs, env)
    73   | infer_type thy env Ts vTs (t as Free (s, T)) =
    74       if T = dummyT then (case Symtab.lookup vTs s of
    75           NONE =>
    76             let val (env', T) = mk_tvar (env, [])
    77             in (Free (s, T), T, Symtab.update_new (s, T) vTs, env') end
    78         | SOME T => (Free (s, T), T, vTs, env))
    79       else (t, T, vTs, env)
    80   | infer_type thy env Ts vTs (Var _) = error "reconstruct_proof: internal error"
    81   | infer_type thy env Ts vTs (Abs (s, T, t)) =
    82       let
    83         val (env', T') = if T = dummyT then mk_tvar (env, []) else (env, T);
    84         val (t', U, vTs', env'') = infer_type thy env' (T' :: Ts) vTs t
    85       in (Abs (s, T', t'), T' --> U, vTs', env'') end
    86   | infer_type thy env Ts vTs (t $ u) =
    87       let
    88         val (t', T, vTs1, env1) = infer_type thy env Ts vTs t;
    89         val (u', U, vTs2, env2) = infer_type thy env1 Ts vTs1 u;
    90       in (case chaseT env2 T of
    91           Type ("fun", [U', V]) => (t' $ u', V, vTs2, unifyT thy env2 U U')
    92         | _ =>
    93           let val (env3, V) = mk_tvar (env2, [])
    94           in (t' $ u', V, vTs2, unifyT thy env3 T (U --> V)) end)
    95       end
    96   | infer_type thy env Ts vTs (t as Bound i) = ((t, nth Ts i, vTs, env)
    97       handle Subscript => error ("infer_type: bad variable index " ^ string_of_int i));
    98 
    99 fun cantunify thy (t, u) = error ("Non-unifiable terms:\n" ^
   100   Syntax.string_of_term_global thy t ^ "\n\n" ^ Syntax.string_of_term_global thy u);
   101 
   102 fun decompose thy Ts (env, p as (t, u)) =
   103   let fun rigrig (a, T) (b, U) uT ts us = if a <> b then cantunify thy p
   104     else apsnd flat (Library.foldl_map (decompose thy Ts) (uT env T U, ts ~~ us))
   105   in case pairself (strip_comb o Envir.head_norm env) p of
   106       ((Const c, ts), (Const d, us)) => rigrig c d (unifyT thy) ts us
   107     | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT thy) ts us
   108     | ((Bound i, ts), (Bound j, us)) =>
   109         rigrig (i, dummyT) (j, dummyT) (K o K) ts us
   110     | ((Abs (_, T, t), []), (Abs (_, U, u), [])) =>
   111         decompose thy (T::Ts) (unifyT thy env T U, (t, u))
   112     | ((Abs (_, T, t), []), _) =>
   113         decompose thy (T::Ts) (env, (t, incr_boundvars 1 u $ Bound 0))
   114     | (_, (Abs (_, T, u), [])) =>
   115         decompose thy (T::Ts) (env, (incr_boundvars 1 t $ Bound 0, u))
   116     | _ => (env, [(mk_abs Ts t, mk_abs Ts u)])
   117   end;
   118 
   119 fun make_constraints_cprf thy env cprf =
   120   let
   121     fun add_cnstrt Ts prop prf cs env vTs (t, u) =
   122       let
   123         val t' = mk_abs Ts t;
   124         val u' = mk_abs Ts u
   125       in
   126         (prop, prf, cs, Pattern.unify thy (t', u') env, vTs)
   127         handle Pattern.Pattern =>
   128             let val (env', cs') = decompose thy [] (env, (t', u'))
   129             in (prop, prf, cs @ cs', env', vTs) end
   130         | Pattern.Unif =>
   131             cantunify thy (Envir.norm_term env t', Envir.norm_term env u')
   132       end;
   133 
   134     fun mk_cnstrts_atom env vTs prop opTs prf =
   135           let
   136             val tvars = OldTerm.term_tvars prop;
   137             val tfrees = OldTerm.term_tfrees prop;
   138             val (env', Ts) =
   139               (case opTs of
   140                 NONE => Library.foldl_map mk_tvar (env, map snd tvars @ map snd tfrees)
   141               | SOME Ts => (env, Ts));
   142             val prop' = subst_atomic_types (map TVar tvars @ map TFree tfrees ~~ Ts)
   143               (forall_intr_vfs prop) handle Library.UnequalLengths =>
   144                 error ("Wrong number of type arguments for " ^
   145                   quote (get_name [] prop prf))
   146           in (prop', change_type (SOME Ts) prf, [], env', vTs) end;
   147 
   148     fun head_norm (prop, prf, cnstrts, env, vTs) =
   149       (Envir.head_norm env prop, prf, cnstrts, env, vTs);
   150 
   151     fun mk_cnstrts env _ Hs vTs (PBound i) = ((nth Hs i, PBound i, [], env, vTs)
   152           handle Subscript => error ("mk_cnstrts: bad variable index " ^ string_of_int i))
   153       | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) =
   154           let
   155             val (env', T) = (case opT of
   156               NONE => mk_tvar (env, []) | SOME T => (env, T));
   157             val (t, prf, cnstrts, env'', vTs') =
   158               mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf;
   159           in (Const ("all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, SOME T, prf),
   160             cnstrts, env'', vTs')
   161           end
   162       | mk_cnstrts env Ts Hs vTs (AbsP (s, SOME t, cprf)) =
   163           let
   164             val (t', _, vTs', env') = infer_type thy env Ts vTs t;
   165             val (u, prf, cnstrts, env'', vTs'') = mk_cnstrts env' Ts (t'::Hs) vTs' cprf;
   166           in (Logic.mk_implies (t', u), AbsP (s, SOME t', prf), cnstrts, env'', vTs'')
   167           end
   168       | mk_cnstrts env Ts Hs vTs (AbsP (s, NONE, cprf)) =
   169           let
   170             val (env', t) = mk_var env Ts propT;
   171             val (u, prf, cnstrts, env'', vTs') = mk_cnstrts env' Ts (t::Hs) vTs cprf;
   172           in (Logic.mk_implies (t, u), AbsP (s, SOME t, prf), cnstrts, env'', vTs')
   173           end
   174       | mk_cnstrts env Ts Hs vTs (cprf1 %% cprf2) =
   175           let val (u, prf2, cnstrts, env', vTs') = mk_cnstrts env Ts Hs vTs cprf2
   176           in (case head_norm (mk_cnstrts env' Ts Hs vTs' cprf1) of
   177               (Const ("==>", _) $ u' $ t', prf1, cnstrts', env'', vTs'') =>
   178                 add_cnstrt Ts t' (prf1 %% prf2) (cnstrts' @ cnstrts)
   179                   env'' vTs'' (u, u')
   180             | (t, prf1, cnstrts', env'', vTs'') =>
   181                 let val (env''', v) = mk_var env'' Ts propT
   182                 in add_cnstrt Ts v (prf1 %% prf2) (cnstrts' @ cnstrts)
   183                   env''' vTs'' (t, Logic.mk_implies (u, v))
   184                 end)
   185           end
   186       | mk_cnstrts env Ts Hs vTs (cprf % SOME t) =
   187           let val (t', U, vTs1, env1) = infer_type thy env Ts vTs t
   188           in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of
   189              (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
   190                  prf, cnstrts, env2, vTs2) =>
   191                let val env3 = unifyT thy env2 T U
   192                in (betapply (f, t'), prf % SOME t', cnstrts, env3, vTs2)
   193                end
   194            | (u, prf, cnstrts, env2, vTs2) =>
   195                let val (env3, v) = mk_var env2 Ts (U --> propT);
   196                in
   197                  add_cnstrt Ts (v $ t') (prf % SOME t') cnstrts env3 vTs2
   198                    (u, Const ("all", (U --> propT) --> propT) $ v)
   199                end)
   200           end
   201       | mk_cnstrts env Ts Hs vTs (cprf % NONE) =
   202           (case head_norm (mk_cnstrts env Ts Hs vTs cprf) of
   203              (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
   204                  prf, cnstrts, env', vTs') =>
   205                let val (env'', t) = mk_var env' Ts T
   206                in (betapply (f, t), prf % SOME t, cnstrts, env'', vTs')
   207                end
   208            | (u, prf, cnstrts, env', vTs') =>
   209                let
   210                  val (env1, T) = mk_tvar (env', []);
   211                  val (env2, v) = mk_var env1 Ts (T --> propT);
   212                  val (env3, t) = mk_var env2 Ts T
   213                in
   214                  add_cnstrt Ts (v $ t) (prf % SOME t) cnstrts env3 vTs'
   215                    (u, Const ("all", (T --> propT) --> propT) $ v)
   216                end)
   217       | mk_cnstrts env _ _ vTs (prf as PThm (_, ((_, prop, opTs), _))) =
   218           mk_cnstrts_atom env vTs prop opTs prf
   219       | mk_cnstrts env _ _ vTs (prf as PAxm (_, prop, opTs)) =
   220           mk_cnstrts_atom env vTs prop opTs prf
   221       | mk_cnstrts env _ _ vTs (prf as OfClass (T, c)) =
   222           mk_cnstrts_atom env vTs (Logic.mk_of_class (T, c)) NONE prf
   223       | mk_cnstrts env _ _ vTs (prf as Oracle (_, prop, opTs)) =
   224           mk_cnstrts_atom env vTs prop opTs prf
   225       | mk_cnstrts env _ _ vTs (Hyp t) = (t, Hyp t, [], env, vTs)
   226       | mk_cnstrts _ _ _ _ _ = error "reconstruct_proof: minimal proof object"
   227   in mk_cnstrts env [] [] Symtab.empty cprf end;
   228 
   229 
   230 (**** update list of free variables of constraints ****)
   231 
   232 fun upd_constrs env cs =
   233   let
   234     val tenv = Envir.term_env env;
   235     val tyenv = Envir.type_env env;
   236     val dom = []
   237       |> Vartab.fold (cons o #1) tenv
   238       |> Vartab.fold (cons o #1) tyenv;
   239     val vran = []
   240       |> Vartab.fold (Term.add_var_names o #2 o #2) tenv
   241       |> Vartab.fold (Term.add_tvar_namesT o #2 o #2) tyenv;
   242     fun check_cs [] = []
   243       | check_cs ((u, p, vs) :: ps) =
   244           let val vs' = subtract (op =) dom vs in
   245             if vs = vs' then (u, p, vs) :: check_cs ps
   246             else (true, p, fold (insert op =) vs' vran) :: check_cs ps
   247           end;
   248   in check_cs cs end;
   249 
   250 
   251 (**** solution of constraints ****)
   252 
   253 fun solve _ [] bigenv = bigenv
   254   | solve thy cs bigenv =
   255       let
   256         fun search env [] = error ("Unsolvable constraints:\n" ^
   257               Pretty.string_of (Pretty.chunks (map (fn (_, p, _) =>
   258                 Goal_Display.pretty_flexpair (Syntax.init_pretty_global thy) (pairself
   259                   (Envir.norm_term bigenv) p)) cs)))
   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
   267                     (Pattern.unify thy (tn1, tn2) env, ps) handle Pattern.Unif =>
   268                        cantunify thy (tn1, tn2)
   269                   else
   270                     let val (env', cs') = decompose thy [] (env, (tn1, tn2))
   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
   280         solve thy (upd_constrs env cs') (Envir.merge (bigenv, env))
   281       end;
   282 
   283 
   284 (**** reconstruction of proofs ****)
   285 
   286 fun reconstruct_proof thy prop cprf =
   287   let
   288     val (cprf' % SOME prop', thawf) = freeze_thaw_prf (cprf % SOME prop);
   289     val _ = message "Collecting constraints...";
   290     val (t, prf, cs, env, _) = make_constraints_cprf thy
   291       (Envir.empty (maxidx_proof cprf ~1)) cprf';
   292     val cs' = map (fn p => (true, p, op union
   293         (pairself (map (fst o dest_Var) o OldTerm.term_vars) p)))
   294       (map (pairself (Envir.norm_term env)) ((t, prop')::cs));
   295     val _ = message ("Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
   296     val env' = solve thy cs' env
   297   in
   298     thawf (norm_proof env' prf)
   299   end;
   300 
   301 fun prop_of_atom prop Ts = subst_atomic_types
   302   (map TVar (OldTerm.term_tvars prop) @ map TFree (OldTerm.term_tfrees prop) ~~ Ts)
   303   (forall_intr_vfs prop);
   304 
   305 val head_norm = Envir.head_norm (Envir.empty 0);
   306 
   307 fun prop_of0 Hs (PBound i) = nth Hs i
   308   | prop_of0 Hs (Abst (s, SOME T, prf)) =
   309       Term.all T $ (Abs (s, T, prop_of0 Hs prf))
   310   | prop_of0 Hs (AbsP (s, SOME t, prf)) =
   311       Logic.mk_implies (t, prop_of0 (t :: Hs) prf)
   312   | prop_of0 Hs (prf % SOME t) = (case head_norm (prop_of0 Hs prf) of
   313       Const ("all", _) $ f => f $ t
   314     | _ => error "prop_of: all expected")
   315   | prop_of0 Hs (prf1 %% prf2) = (case head_norm (prop_of0 Hs prf1) of
   316       Const ("==>", _) $ P $ Q => Q
   317     | _ => error "prop_of: ==> expected")
   318   | prop_of0 Hs (Hyp t) = t
   319   | prop_of0 Hs (PThm (_, ((_, prop, SOME Ts), _))) = prop_of_atom prop Ts
   320   | prop_of0 Hs (PAxm (_, prop, SOME Ts)) = prop_of_atom prop Ts
   321   | prop_of0 Hs (OfClass (T, c)) = Logic.mk_of_class (T, c)
   322   | prop_of0 Hs (Oracle (_, prop, SOME Ts)) = prop_of_atom prop Ts
   323   | prop_of0 _ _ = error "prop_of: partial proof object";
   324 
   325 val prop_of' = Envir.beta_eta_contract oo prop_of0;
   326 val prop_of = prop_of' [];
   327 
   328 
   329 (**** expand and reconstruct subproofs ****)
   330 
   331 fun expand_proof thy thms prf =
   332   let
   333     fun expand maxidx prfs (AbsP (s, t, prf)) =
   334           let val (maxidx', prfs', prf') = expand maxidx prfs prf
   335           in (maxidx', prfs', AbsP (s, t, prf')) end
   336       | expand maxidx prfs (Abst (s, T, prf)) =
   337           let val (maxidx', prfs', prf') = expand maxidx prfs prf
   338           in (maxidx', prfs', Abst (s, T, prf')) end
   339       | expand maxidx prfs (prf1 %% prf2) =
   340           let
   341             val (maxidx', prfs', prf1') = expand maxidx prfs prf1;
   342             val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2;
   343           in (maxidx'', prfs'', prf1' %% prf2') end
   344       | expand maxidx prfs (prf % t) =
   345           let val (maxidx', prfs', prf') = expand maxidx prfs prf
   346           in (maxidx', prfs', prf' % t) end
   347       | expand maxidx prfs (prf as PThm (_, ((a, prop, SOME Ts), body))) =
   348           if not (exists
   349             (fn (b, NONE) => a = b
   350               | (b, SOME prop') => a = b andalso prop = prop') thms)
   351           then (maxidx, prfs, prf) else
   352           let
   353             val (maxidx', prf, prfs') =
   354               (case AList.lookup (op =) prfs (a, prop) of
   355                 NONE =>
   356                   let
   357                     val _ = message ("Reconstructing proof of " ^ a);
   358                     val _ = message (Syntax.string_of_term_global thy prop);
   359                     val prf' = forall_intr_vfs_prf prop
   360                       (reconstruct_proof thy prop (join_proof body));
   361                     val (maxidx', prfs', prf) = expand
   362                       (maxidx_proof prf' ~1) prfs prf'
   363                   in (maxidx' + maxidx + 1, incr_indexes (maxidx + 1) prf,
   364                     ((a, prop), (maxidx', prf)) :: prfs')
   365                   end
   366               | SOME (maxidx', prf) => (maxidx' + maxidx + 1,
   367                   incr_indexes (maxidx + 1) prf, prfs));
   368             val tfrees = OldTerm.term_tfrees prop;
   369             val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j))
   370               (OldTerm.term_tvars prop) @ map (rpair ~1 o fst) tfrees ~~ Ts;
   371             val varify = map_type_tfree (fn p as (a, S) =>
   372               if member (op =) tfrees p then TVar ((a, ~1), S) else TFree p)
   373           in
   374             (maxidx', prfs', map_proof_terms (subst_TVars tye o
   375                map_types varify) (typ_subst_TVars tye o varify) prf)
   376           end
   377       | expand maxidx prfs prf = (maxidx, prfs, prf);
   378 
   379   in #3 (expand (maxidx_proof prf ~1) [] prf) end;
   380 
   381 end;