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