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