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