src/Pure/Proof/reconstruct.ML
author wenzelm
Thu May 31 23:47:36 2007 +0200 (2007-05-31 ago)
changeset 23178 07ba6b58b3d2
parent 21646 c07b5b0e8492
child 25246 584d8f2e1fc9
permissions -rw-r--r--
simplified/unified list fold;
     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 = fold_rev forall_intr
    34   (vars_of prop @ sort Term.term_ord (term_frees prop)) 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 = fold_rev forall_intr_prf
    41   (vars_of prop @ sort Term.term_ord (term_frees prop)) prf;
    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 val mk_abs = fold (fn T => fn u => Abs ("", T, u));
    61 
    62 fun unifyT thy env T U =
    63   let
    64     val Envir.Envir {asol, iTs, maxidx} = env;
    65     val (iTs', maxidx') = Sign.typ_unify thy (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 thy T ^ "\n\n" ^ Sign.string_of_typ thy 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 thy (env as Envir.Envir {maxidx, asol, iTs}) Ts vTs
    75       (t as Const (s, T)) = if T = dummyT then (case Sign.const_type thy s of
    76           NONE => error ("reconstruct_proof: No such constant: " ^ quote s)
    77         | SOME T =>
    78             let val T' = Type.strip_sorts (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 thy 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 thy env Ts vTs (Var _) = error "reconstruct_proof: internal error"
    91   | infer_type thy 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 thy env' (T' :: Ts) vTs t
    95       in (Abs (s, T', t'), T' --> U, vTs', env'') end
    96   | infer_type thy env Ts vTs (t $ u) =
    97       let
    98         val (t', T, vTs1, env1) = infer_type thy env Ts vTs t;
    99         val (u', U, vTs2, env2) = infer_type thy env1 Ts vTs1 u;
   100       in (case chaseT env2 T of
   101           Type ("fun", [U', V]) => (t' $ u', V, vTs2, unifyT thy env2 U U')
   102         | _ =>
   103           let val (env3, V) = mk_tvar (env2, [])
   104           in (t' $ u', V, vTs2, unifyT thy env3 T (U --> V)) end)
   105       end
   106   | infer_type thy env Ts vTs (t as Bound i) = (t, List.nth (Ts, i), vTs, env);
   107 
   108 fun cantunify thy (t, u) = error ("Non-unifiable terms:\n" ^
   109   Sign.string_of_term thy t ^ "\n\n" ^ Sign.string_of_term thy u);
   110 
   111 fun decompose thy Ts (env, p as (t, u)) =
   112   let fun rigrig (a, T) (b, U) uT ts us = if a <> b then cantunify thy p
   113     else apsnd flat (foldl_map (decompose thy 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 thy) ts us
   116     | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT thy) 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 thy (T::Ts) (unifyT thy env T U, (t, u))
   121     | ((Abs (_, T, t), []), _) =>
   122         decompose thy (T::Ts) (env, (t, incr_boundvars 1 u $ Bound 0))
   123     | (_, (Abs (_, T, u), [])) =>
   124         decompose thy (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 thy 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 thy (t', u') env, vTs)
   136         handle Pattern.Pattern =>
   137             let val (env', cs') = decompose thy [] (env, (t', u'))
   138             in (prop, prf, cs @ cs', env', vTs) end
   139         | Pattern.Unif =>
   140             cantunify thy (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 (fmap, prop') = 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 Library.UnequalLengths =>
   153                 error ("Wrong number of type arguments for " ^
   154                   quote (get_name [] 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 thy 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 thy 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 thy 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 
   236 (**** update list of free variables of constraints ****)
   237 
   238 val add_term_ixns = fold_aterms (fn Var (xi, _) => insert (op =) xi | _ => I);
   239 val add_typ_ixns = fold_atyps (fn TVar (ai, _) => insert (op =) ai | _ => I);
   240 
   241 fun upd_constrs env cs =
   242   let
   243     val Envir.Envir {asol, iTs, ...} = env;
   244     val dom = []
   245       |> Vartab.fold (cons o #1) asol
   246       |> Vartab.fold (cons o #1) iTs;
   247     val vran = []
   248       |> Vartab.fold (add_term_ixns o #2 o #2) asol
   249       |> Vartab.fold (add_typ_ixns o #2 o #2) iTs;
   250     fun check_cs [] = []
   251       | check_cs ((u, p, vs)::ps) =
   252           let val vs' = subtract (op =) dom vs;
   253           in if vs = vs' then (u, p, vs)::check_cs ps
   254              else (true, p, fold (insert (op =)) vs' vran)::check_cs ps
   255           end
   256   in check_cs cs end;
   257 
   258 (**** solution of constraints ****)
   259 
   260 fun solve _ [] bigenv = bigenv
   261   | solve thy cs bigenv =
   262       let
   263         fun search env [] = error ("Unsolvable constraints:\n" ^
   264               Pretty.string_of (Pretty.chunks (map (fn (_, p, _) =>
   265                 Display.pretty_flexpair (Sign.pp thy) (pairself
   266                   (Envir.norm_term bigenv) p)) cs)))
   267           | search env ((u, p as (t1, t2), vs)::ps) =
   268               if u then
   269                 let
   270                   val tn1 = Envir.norm_term bigenv t1;
   271                   val tn2 = Envir.norm_term bigenv t2
   272                 in
   273                   if Pattern.pattern tn1 andalso Pattern.pattern tn2 then
   274                     (Pattern.unify thy (tn1, tn2) env, ps) handle Pattern.Unif =>
   275                        cantunify thy (tn1, tn2)
   276                   else
   277                     let val (env', cs') = decompose thy [] (env, (tn1, tn2))
   278                     in if cs' = [(tn1, tn2)] then
   279                          apsnd (cons (false, (tn1, tn2), vs)) (search env ps)
   280                        else search env' (map (fn q => (true, q, vs)) cs' @ ps)
   281                     end
   282                 end
   283               else apsnd (cons (false, p, vs)) (search env ps);
   284         val Envir.Envir {maxidx, ...} = bigenv;
   285         val (env, cs') = search (Envir.empty maxidx) cs;
   286       in
   287         solve thy (upd_constrs env cs') (merge_envs bigenv env)
   288       end;
   289 
   290 
   291 (**** reconstruction of proofs ****)
   292 
   293 fun reconstruct_proof thy prop cprf =
   294   let
   295     val (cprf' % SOME prop', thawf) = freeze_thaw_prf (cprf % SOME prop);
   296     val _ = message "Collecting constraints...";
   297     val (t, prf, cs, env, _) = make_constraints_cprf thy
   298       (Envir.empty (maxidx_proof cprf ~1)) cprf';
   299     val cs' = map (fn p => (true, p, op union
   300         (pairself (map (fst o dest_Var) o term_vars) p)))
   301       (map (pairself (Envir.norm_term env)) ((t, prop')::cs));
   302     val _ = message ("Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
   303     val env' = solve thy cs' env
   304   in
   305     thawf (norm_proof env' prf)
   306   end;
   307 
   308 fun prop_of_atom prop Ts =
   309   let val (fmap, prop') = Type.varify [] prop;
   310   in subst_TVars (map fst (term_tvars prop) @ map snd fmap ~~ Ts)
   311     (forall_intr_vfs prop')
   312   end;
   313 
   314 val head_norm = Envir.head_norm (Envir.empty 0);
   315 
   316 fun prop_of0 Hs (PBound i) = List.nth (Hs, i)
   317   | prop_of0 Hs (Abst (s, SOME T, prf)) =
   318       all T $ (Abs (s, T, prop_of0 Hs prf))
   319   | prop_of0 Hs (AbsP (s, SOME t, prf)) =
   320       Logic.mk_implies (t, prop_of0 (t :: Hs) prf)
   321   | prop_of0 Hs (prf % SOME t) = (case head_norm (prop_of0 Hs prf) of
   322       Const ("all", _) $ f => f $ t
   323     | _ => error "prop_of: all expected")
   324   | prop_of0 Hs (prf1 %% prf2) = (case head_norm (prop_of0 Hs prf1) of
   325       Const ("==>", _) $ P $ Q => Q
   326     | _ => error "prop_of: ==> expected")
   327   | prop_of0 Hs (Hyp t) = t
   328   | prop_of0 Hs (PThm (_, _, prop, SOME Ts)) = prop_of_atom prop Ts
   329   | prop_of0 Hs (PAxm (_, prop, SOME Ts)) = prop_of_atom prop Ts
   330   | prop_of0 Hs (Oracle (_, prop, SOME Ts)) = prop_of_atom prop Ts
   331   | prop_of0 _ _ = error "prop_of: partial proof object";
   332 
   333 val prop_of' = Envir.beta_eta_contract oo prop_of0;
   334 val prop_of = prop_of' [];
   335 
   336 
   337 (**** expand and reconstruct subproofs ****)
   338 
   339 fun expand_proof thy thms prf =
   340   let
   341     fun expand maxidx prfs (AbsP (s, t, prf)) =
   342           let val (maxidx', prfs', prf') = expand maxidx prfs prf
   343           in (maxidx', prfs', AbsP (s, t, prf')) end
   344       | expand maxidx prfs (Abst (s, T, prf)) =
   345           let val (maxidx', prfs', prf') = expand maxidx prfs prf
   346           in (maxidx', prfs', Abst (s, T, prf')) end
   347       | expand maxidx prfs (prf1 %% prf2) =
   348           let
   349             val (maxidx', prfs', prf1') = expand maxidx prfs prf1;
   350             val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2;
   351           in (maxidx'', prfs'', prf1' %% prf2') end
   352       | expand maxidx prfs (prf % t) =
   353           let val (maxidx', prfs', prf') = expand maxidx prfs prf
   354           in (maxidx', prfs', prf' % t) end
   355       | expand maxidx prfs (prf as PThm (a, cprf, prop, SOME Ts)) =
   356           if not (exists
   357             (fn (b, NONE) => a = b
   358               | (b, SOME prop') => a = b andalso prop = prop') thms)
   359           then (maxidx, prfs, prf) else
   360           let
   361             fun inc i =
   362               map_proof_terms (Logic.incr_indexes ([], i)) (Logic.incr_tvar i);
   363             val (maxidx', prf, prfs') =
   364               (case AList.lookup (op =) prfs (a, prop) of
   365                 NONE =>
   366                   let
   367                     val _ = message ("Reconstructing proof of " ^ a);
   368                     val _ = message (Sign.string_of_term thy prop);
   369                     val prf' = forall_intr_vfs_prf prop
   370                       (reconstruct_proof thy prop cprf);
   371                     val (maxidx', prfs', prf) = expand
   372                       (maxidx_proof prf' ~1) prfs prf'
   373                   in (maxidx' + maxidx + 1, inc (maxidx + 1) prf,
   374                     ((a, prop), (maxidx', prf)) :: prfs')
   375                   end
   376               | SOME (maxidx', prf) => (maxidx' + maxidx + 1,
   377                   inc (maxidx + 1) prf, prfs));
   378             val tfrees = term_tfrees prop;
   379             val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j))
   380               (term_tvars prop) @ map (rpair ~1 o fst) tfrees ~~ Ts;
   381             val varify = map_type_tfree (fn p as (a, S) =>
   382               if member (op =) tfrees p then TVar ((a, ~1), S) else TFree p)
   383           in
   384             (maxidx', prfs', map_proof_terms (subst_TVars tye o
   385                map_types varify) (typ_subst_TVars tye o varify) prf)
   386           end
   387       | expand maxidx prfs prf = (maxidx, prfs, prf);
   388 
   389   in #3 (expand (maxidx_proof prf ~1) [] prf) end;
   390 
   391 end;