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