src/Pure/Proof/reconstruct.ML
author wenzelm
Sat May 15 23:23:45 2010 +0200 (2010-05-15)
changeset 36951 985c197f2fe9
parent 36620 e6bb250402b5
child 37228 4bbda9fc26db
permissions -rw-r--r--
renamed structure ValueParse to Parse_Value;
eliminated old-style structure alias V;
     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 (list_comb (v, map Bound (length Ts - 1 downto 0)), env') end;
    44 
    45 fun mk_tvar S (Envir.Envir {maxidx, tenv, tyenv}) =
    46   (TVar (("'t", maxidx + 1), S),
    47     Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv});
    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 (T, env') = 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 (T', env') = if T = dummyT then mk_tvar [] env else (T, env);
    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 (V, env3) = 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 (p as (t, u)) env =
   103   let
   104     fun rigrig (a, T) (b, U) uT ts us =
   105       if a <> b then cantunify thy p
   106       else apfst flat (fold_map (decompose thy Ts) (ts ~~ us) (uT env T U))
   107   in
   108     case pairself (strip_comb o Envir.head_norm env) p of
   109       ((Const c, ts), (Const d, us)) => rigrig c d (unifyT thy) ts us
   110     | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT thy) 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 thy (T::Ts) (t, u) (unifyT thy env T U)
   115     | ((Abs (_, T, t), []), _) =>
   116         decompose thy (T::Ts) (t, incr_boundvars 1 u $ Bound 0) env
   117     | (_, (Abs (_, T, u), [])) =>
   118         decompose thy (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 thy 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 thy (t', u') env, vTs)
   130         handle Pattern.Pattern =>
   131             let val (cs', env') = decompose thy [] (t', u') env
   132             in (prop, prf, cs @ cs', env', vTs) end
   133         | Pattern.Unif =>
   134             cantunify thy (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 Library.UnequalLengths =>
   147                 error ("Wrong number of type arguments for " ^
   148                   quote (get_name [] prop prf))
   149           in (prop', change_type (SOME Ts) prf, [], env', vTs) end;
   150 
   151     fun head_norm (prop, prf, cnstrts, env, vTs) =
   152       (Envir.head_norm env prop, prf, cnstrts, env, vTs);
   153 
   154     fun mk_cnstrts env _ Hs vTs (PBound i) = ((nth Hs i, PBound i, [], env, vTs)
   155           handle Subscript => error ("mk_cnstrts: bad variable index " ^ string_of_int i))
   156       | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) =
   157           let
   158             val (T, env') =
   159               (case opT of
   160                 NONE => mk_tvar [] env
   161               | SOME T => (T, env));
   162             val (t, prf, cnstrts, env'', vTs') =
   163               mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf;
   164           in (Const ("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 thy 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 ("==>", _) $ 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 thy env Ts vTs t
   193           in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of
   194              (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
   195                  prf, cnstrts, env2, vTs2) =>
   196                let val env3 = unifyT thy 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 ("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 ("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 ("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 thy cs bigenv =
   260       let
   261         fun search env [] = error ("Unsolvable constraints:\n" ^
   262               Pretty.string_of (Pretty.chunks (map (fn (_, p, _) =>
   263                 Goal_Display.pretty_flexpair (Syntax.init_pretty_global thy) (pairself
   264                   (Envir.norm_term bigenv) p)) cs)))
   265           | search env ((u, p as (t1, t2), vs)::ps) =
   266               if u then
   267                 let
   268                   val tn1 = Envir.norm_term bigenv t1;
   269                   val tn2 = Envir.norm_term bigenv t2
   270                 in
   271                   if Pattern.pattern tn1 andalso Pattern.pattern tn2 then
   272                     (Pattern.unify thy (tn1, tn2) env, ps) handle Pattern.Unif =>
   273                        cantunify thy (tn1, tn2)
   274                   else
   275                     let val (cs', env') = decompose thy [] (tn1, tn2) env
   276                     in if cs' = [(tn1, tn2)] then
   277                          apsnd (cons (false, (tn1, tn2), vs)) (search env ps)
   278                        else search env' (map (fn q => (true, q, vs)) cs' @ ps)
   279                     end
   280                 end
   281               else apsnd (cons (false, p, vs)) (search env ps);
   282         val Envir.Envir {maxidx, ...} = bigenv;
   283         val (env, cs') = search (Envir.empty maxidx) cs;
   284       in
   285         solve thy (upd_constrs env cs') (Envir.merge (bigenv, env))
   286       end;
   287 
   288 
   289 (**** reconstruction of proofs ****)
   290 
   291 fun reconstruct_proof thy prop cprf =
   292   let
   293     val (cprf' % SOME prop', thawf) = freeze_thaw_prf (cprf % SOME prop);
   294     val _ = message "Collecting constraints...";
   295     val (t, prf, cs, env, _) = make_constraints_cprf thy
   296       (Envir.empty (maxidx_proof cprf ~1)) cprf';
   297     val cs' = map (fn p => (true, p, uncurry (union (op =)) 
   298         (pairself (map (fst o dest_Var) o OldTerm.term_vars) p)))
   299       (map (pairself (Envir.norm_term env)) ((t, prop')::cs));
   300     val _ = message ("Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
   301     val env' = solve thy cs' env
   302   in
   303     thawf (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.empty 0);
   311 
   312 fun prop_of0 Hs (PBound i) = nth Hs i
   313   | prop_of0 Hs (Abst (s, SOME T, prf)) =
   314       Term.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 (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 
   334 (**** expand and reconstruct subproofs ****)
   335 
   336 fun expand_proof thy thms prf =
   337   let
   338     fun expand maxidx prfs (AbsP (s, t, prf)) =
   339           let val (maxidx', prfs', prf') = expand maxidx prfs prf
   340           in (maxidx', prfs', AbsP (s, t, prf')) end
   341       | expand maxidx prfs (Abst (s, T, prf)) =
   342           let val (maxidx', prfs', prf') = expand maxidx prfs prf
   343           in (maxidx', prfs', Abst (s, T, prf')) end
   344       | expand maxidx prfs (prf1 %% prf2) =
   345           let
   346             val (maxidx', prfs', prf1') = expand maxidx prfs prf1;
   347             val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2;
   348           in (maxidx'', prfs'', prf1' %% prf2') end
   349       | expand maxidx prfs (prf % t) =
   350           let val (maxidx', prfs', prf') = expand maxidx prfs prf
   351           in (maxidx', prfs', prf' % t) end
   352       | expand maxidx prfs (prf as PThm (_, ((a, prop, SOME Ts), body))) =
   353           if not (exists
   354             (fn (b, NONE) => a = b
   355               | (b, SOME prop') => a = b andalso prop = prop') thms)
   356           then (maxidx, prfs, prf) else
   357           let
   358             val (maxidx', prf, prfs') =
   359               (case AList.lookup (op =) prfs (a, prop) of
   360                 NONE =>
   361                   let
   362                     val _ = message ("Reconstructing proof of " ^ a);
   363                     val _ = message (Syntax.string_of_term_global thy prop);
   364                     val prf' = forall_intr_vfs_prf prop
   365                       (reconstruct_proof thy prop (join_proof body));
   366                     val (maxidx', prfs', prf) = expand
   367                       (maxidx_proof prf' ~1) prfs prf'
   368                   in (maxidx' + maxidx + 1, incr_indexes (maxidx + 1) prf,
   369                     ((a, prop), (maxidx', prf)) :: prfs')
   370                   end
   371               | SOME (maxidx', prf) => (maxidx' + maxidx + 1,
   372                   incr_indexes (maxidx + 1) prf, prfs));
   373             val tfrees = Term.add_tfrees prop [] |> rev;
   374             val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j))
   375               (Term.add_tvars prop [] |> rev) @ map (rpair ~1 o fst) tfrees ~~ Ts;
   376             val varify = map_type_tfree (fn p as (a, S) =>
   377               if member (op =) tfrees p then TVar ((a, ~1), S) else TFree p)
   378           in
   379             (maxidx', prfs', map_proof_types (typ_subst_TVars tye o varify) prf)
   380           end
   381       | expand maxidx prfs prf = (maxidx, prfs, prf);
   382 
   383   in #3 (expand (maxidx_proof prf ~1) [] prf) end;
   384 
   385 end;