src/Pure/Proof/reconstruct.ML
author wenzelm
Tue Sep 29 11:49:22 2009 +0200 (2009-09-29)
changeset 32738 15bb09ca0378
parent 32187 cca43ca13f4f
child 33037 b22e44496dc2
permissions -rw-r--r--
explicit indication of Unsynchronized.ref;
berghofe@11522
     1
(*  Title:      Pure/Proof/reconstruct.ML
wenzelm@11539
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@11522
     3
berghofe@11522
     4
Reconstruction of partial proof terms.
berghofe@11522
     5
*)
berghofe@11522
     6
berghofe@11522
     7
signature RECONSTRUCT =
berghofe@11522
     8
sig
wenzelm@32738
     9
  val quiet_mode : bool Unsynchronized.ref
wenzelm@16862
    10
  val reconstruct_proof : theory -> term -> Proofterm.proof -> Proofterm.proof
berghofe@13256
    11
  val prop_of' : term list -> Proofterm.proof -> term
berghofe@13138
    12
  val prop_of : Proofterm.proof -> term
wenzelm@16862
    13
  val expand_proof : theory -> (string * term option) list ->
berghofe@13342
    14
    Proofterm.proof -> Proofterm.proof
berghofe@11522
    15
end;
berghofe@11522
    16
berghofe@11522
    17
structure Reconstruct : RECONSTRUCT =
berghofe@11522
    18
struct
berghofe@11522
    19
berghofe@11522
    20
open Proofterm;
berghofe@11522
    21
wenzelm@32738
    22
val quiet_mode = Unsynchronized.ref true;
berghofe@11522
    23
fun message s = if !quiet_mode then () else writeln s;
berghofe@11522
    24
berghofe@28813
    25
fun vars_of t = map Var (rev (Term.add_vars t []));
berghofe@28813
    26
fun frees_of t = map Free (rev (Term.add_frees t []));
berghofe@11522
    27
wenzelm@27330
    28
fun forall_intr_vfs prop = fold_rev Logic.all
berghofe@28813
    29
  (vars_of prop @ frees_of prop) prop;
berghofe@11522
    30
wenzelm@23178
    31
fun forall_intr_prf t prf =
berghofe@12001
    32
  let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p)
skalberg@15531
    33
  in Abst (a, SOME T, prf_abstract_over t prf) end;
berghofe@12001
    34
wenzelm@23178
    35
fun forall_intr_vfs_prf prop prf = fold_rev forall_intr_prf
berghofe@28813
    36
  (vars_of prop @ frees_of prop) prf;
berghofe@12001
    37
berghofe@11522
    38
berghofe@13669
    39
(**** generate constraints for proof term ****)
berghofe@11522
    40
wenzelm@23178
    41
fun mk_var env Ts T =
berghofe@11522
    42
  let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T)
berghofe@11522
    43
  in (env', list_comb (v, map Bound (length Ts - 1 downto 0))) end;
berghofe@11522
    44
wenzelm@32032
    45
fun mk_tvar (Envir.Envir {maxidx, tenv, tyenv}, s) =
wenzelm@32032
    46
  (Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv},
wenzelm@32032
    47
   TVar (("'t", maxidx + 1), s));
berghofe@11522
    48
wenzelm@19473
    49
val mk_abs = fold (fn T => fn u => Abs ("", T, u));
berghofe@11522
    50
wenzelm@20312
    51
fun unifyT thy env T U =
berghofe@11522
    52
  let
wenzelm@32032
    53
    val Envir.Envir {maxidx, tenv, tyenv} = env;
wenzelm@32032
    54
    val (tyenv', maxidx') = Sign.typ_unify thy (T, U) (tyenv, maxidx);
wenzelm@32032
    55
  in Envir.Envir {maxidx = maxidx', tenv = tenv, tyenv = tyenv'} end;
berghofe@11522
    56
wenzelm@32032
    57
fun chaseT env (T as TVar v) =
wenzelm@32032
    58
      (case Type.lookup (Envir.type_env env) v of
wenzelm@32032
    59
        NONE => T
wenzelm@32032
    60
      | SOME T' => chaseT env T')
berghofe@13669
    61
  | chaseT _ T = T;
berghofe@13669
    62
wenzelm@32032
    63
fun infer_type thy (env as Envir.Envir {maxidx, tenv, tyenv}) Ts vTs
wenzelm@32032
    64
      (t as Const (s, T)) = if T = dummyT then
wenzelm@32032
    65
        (case Sign.const_type thy s of
skalberg@15531
    66
          NONE => error ("reconstruct_proof: No such constant: " ^ quote s)
wenzelm@23178
    67
        | SOME T =>
wenzelm@19419
    68
            let val T' = Type.strip_sorts (Logic.incr_tvar (maxidx + 1) T)
berghofe@13669
    69
            in (Const (s, T'), T', vTs,
wenzelm@32032
    70
              Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv})
berghofe@13669
    71
            end)
berghofe@13669
    72
      else (t, T, vTs, env)
wenzelm@20312
    73
  | infer_type thy env Ts vTs (t as Free (s, T)) =
wenzelm@17412
    74
      if T = dummyT then (case Symtab.lookup vTs s of
skalberg@15531
    75
          NONE =>
berghofe@13669
    76
            let val (env', T) = mk_tvar (env, [])
wenzelm@17412
    77
            in (Free (s, T), T, Symtab.update_new (s, T) vTs, env') end
skalberg@15531
    78
        | SOME T => (Free (s, T), T, vTs, env))
berghofe@13669
    79
      else (t, T, vTs, env)
wenzelm@20312
    80
  | infer_type thy env Ts vTs (Var _) = error "reconstruct_proof: internal error"
wenzelm@20312
    81
  | infer_type thy env Ts vTs (Abs (s, T, t)) =
berghofe@13669
    82
      let
berghofe@13669
    83
        val (env', T') = if T = dummyT then mk_tvar (env, []) else (env, T);
wenzelm@20312
    84
        val (t', U, vTs', env'') = infer_type thy env' (T' :: Ts) vTs t
berghofe@13669
    85
      in (Abs (s, T', t'), T' --> U, vTs', env'') end
wenzelm@20312
    86
  | infer_type thy env Ts vTs (t $ u) =
berghofe@13669
    87
      let
wenzelm@20312
    88
        val (t', T, vTs1, env1) = infer_type thy env Ts vTs t;
wenzelm@20312
    89
        val (u', U, vTs2, env2) = infer_type thy env1 Ts vTs1 u;
berghofe@13669
    90
      in (case chaseT env2 T of
wenzelm@20312
    91
          Type ("fun", [U', V]) => (t' $ u', V, vTs2, unifyT thy env2 U U')
berghofe@13669
    92
        | _ =>
berghofe@13669
    93
          let val (env3, V) = mk_tvar (env2, [])
wenzelm@20312
    94
          in (t' $ u', V, vTs2, unifyT thy env3 T (U --> V)) end)
berghofe@13669
    95
      end
wenzelm@30146
    96
  | infer_type thy env Ts vTs (t as Bound i) = ((t, nth Ts i, vTs, env)
berghofe@25246
    97
      handle Subscript => error ("infer_type: bad variable index " ^ string_of_int i));
berghofe@13669
    98
wenzelm@20312
    99
fun cantunify thy (t, u) = error ("Non-unifiable terms:\n" ^
wenzelm@26939
   100
  Syntax.string_of_term_global thy t ^ "\n\n" ^ Syntax.string_of_term_global thy u);
berghofe@11522
   101
wenzelm@20312
   102
fun decompose thy Ts (env, p as (t, u)) =
wenzelm@20312
   103
  let fun rigrig (a, T) (b, U) uT ts us = if a <> b then cantunify thy p
wenzelm@30190
   104
    else apsnd flat (Library.foldl_map (decompose thy Ts) (uT env T U, ts ~~ us))
berghofe@13715
   105
  in case pairself (strip_comb o Envir.head_norm env) p of
wenzelm@20312
   106
      ((Const c, ts), (Const d, us)) => rigrig c d (unifyT thy) ts us
wenzelm@20312
   107
    | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT thy) ts us
berghofe@13715
   108
    | ((Bound i, ts), (Bound j, us)) =>
berghofe@13715
   109
        rigrig (i, dummyT) (j, dummyT) (K o K) ts us
berghofe@13715
   110
    | ((Abs (_, T, t), []), (Abs (_, U, u), [])) =>
wenzelm@20312
   111
        decompose thy (T::Ts) (unifyT thy env T U, (t, u))
berghofe@13715
   112
    | ((Abs (_, T, t), []), _) =>
wenzelm@20312
   113
        decompose thy (T::Ts) (env, (t, incr_boundvars 1 u $ Bound 0))
berghofe@13715
   114
    | (_, (Abs (_, T, u), [])) =>
wenzelm@20312
   115
        decompose thy (T::Ts) (env, (incr_boundvars 1 t $ Bound 0, u))
berghofe@13715
   116
    | _ => (env, [(mk_abs Ts t, mk_abs Ts u)])
berghofe@13715
   117
  end;
berghofe@11522
   118
wenzelm@20312
   119
fun make_constraints_cprf thy env cprf =
berghofe@11522
   120
  let
berghofe@13669
   121
    fun add_cnstrt Ts prop prf cs env vTs (t, u) =
berghofe@11522
   122
      let
berghofe@11522
   123
        val t' = mk_abs Ts t;
berghofe@12236
   124
        val u' = mk_abs Ts u
berghofe@11522
   125
      in
wenzelm@20312
   126
        (prop, prf, cs, Pattern.unify thy (t', u') env, vTs)
berghofe@12236
   127
        handle Pattern.Pattern =>
wenzelm@20312
   128
            let val (env', cs') = decompose thy [] (env, (t', u'))
berghofe@13669
   129
            in (prop, prf, cs @ cs', env', vTs) end
berghofe@12236
   130
        | Pattern.Unif =>
wenzelm@20312
   131
            cantunify thy (Envir.norm_term env t', Envir.norm_term env u')
berghofe@11522
   132
      end;
berghofe@11522
   133
berghofe@13669
   134
    fun mk_cnstrts_atom env vTs prop opTs prf =
berghofe@11522
   135
          let
wenzelm@29270
   136
            val tvars = OldTerm.term_tvars prop;
wenzelm@29270
   137
            val tfrees = OldTerm.term_tfrees prop;
wenzelm@31903
   138
            val (env', Ts) =
wenzelm@31903
   139
              (case opTs of
wenzelm@30190
   140
                NONE => Library.foldl_map mk_tvar (env, map snd tvars @ map snd tfrees)
skalberg@15531
   141
              | SOME Ts => (env, Ts));
berghofe@28813
   142
            val prop' = subst_atomic_types (map TVar tvars @ map TFree tfrees ~~ Ts)
berghofe@28813
   143
              (forall_intr_vfs prop) handle Library.UnequalLengths =>
berghofe@13669
   144
                error ("Wrong number of type arguments for " ^
wenzelm@21646
   145
                  quote (get_name [] prop prf))
berghofe@28813
   146
          in (prop', change_type (SOME Ts) prf, [], env', vTs) end;
berghofe@11522
   147
berghofe@13669
   148
    fun head_norm (prop, prf, cnstrts, env, vTs) =
berghofe@13669
   149
      (Envir.head_norm env prop, prf, cnstrts, env, vTs);
wenzelm@23178
   150
wenzelm@30146
   151
    fun mk_cnstrts env _ Hs vTs (PBound i) = ((nth Hs i, PBound i, [], env, vTs)
berghofe@25246
   152
          handle Subscript => error ("mk_cnstrts: bad variable index " ^ string_of_int i))
berghofe@13669
   153
      | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) =
berghofe@13669
   154
          let
berghofe@13669
   155
            val (env', T) = (case opT of
skalberg@15531
   156
              NONE => mk_tvar (env, []) | SOME T => (env, T));
berghofe@13669
   157
            val (t, prf, cnstrts, env'', vTs') =
berghofe@13669
   158
              mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf;
skalberg@15531
   159
          in (Const ("all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, SOME T, prf),
berghofe@13669
   160
            cnstrts, env'', vTs')
berghofe@11522
   161
          end
skalberg@15531
   162
      | mk_cnstrts env Ts Hs vTs (AbsP (s, SOME t, cprf)) =
berghofe@11522
   163
          let
wenzelm@20312
   164
            val (t', _, vTs', env') = infer_type thy env Ts vTs t;
berghofe@13669
   165
            val (u, prf, cnstrts, env'', vTs'') = mk_cnstrts env' Ts (t'::Hs) vTs' cprf;
skalberg@15531
   166
          in (Logic.mk_implies (t', u), AbsP (s, SOME t', prf), cnstrts, env'', vTs'')
berghofe@11522
   167
          end
skalberg@15531
   168
      | mk_cnstrts env Ts Hs vTs (AbsP (s, NONE, cprf)) =
berghofe@11522
   169
          let
berghofe@11522
   170
            val (env', t) = mk_var env Ts propT;
berghofe@13669
   171
            val (u, prf, cnstrts, env'', vTs') = mk_cnstrts env' Ts (t::Hs) vTs cprf;
skalberg@15531
   172
          in (Logic.mk_implies (t, u), AbsP (s, SOME t, prf), cnstrts, env'', vTs')
berghofe@11522
   173
          end
berghofe@13669
   174
      | mk_cnstrts env Ts Hs vTs (cprf1 %% cprf2) =
berghofe@13669
   175
          let val (u, prf2, cnstrts, env', vTs') = mk_cnstrts env Ts Hs vTs cprf2
berghofe@13669
   176
          in (case head_norm (mk_cnstrts env' Ts Hs vTs' cprf1) of
berghofe@13669
   177
              (Const ("==>", _) $ u' $ t', prf1, cnstrts', env'', vTs'') =>
berghofe@11613
   178
                add_cnstrt Ts t' (prf1 %% prf2) (cnstrts' @ cnstrts)
berghofe@13669
   179
                  env'' vTs'' (u, u')
berghofe@13669
   180
            | (t, prf1, cnstrts', env'', vTs'') =>
berghofe@11522
   181
                let val (env''', v) = mk_var env'' Ts propT
berghofe@11613
   182
                in add_cnstrt Ts v (prf1 %% prf2) (cnstrts' @ cnstrts)
berghofe@13669
   183
                  env''' vTs'' (t, Logic.mk_implies (u, v))
berghofe@11522
   184
                end)
berghofe@11522
   185
          end
skalberg@15531
   186
      | mk_cnstrts env Ts Hs vTs (cprf % SOME t) =
wenzelm@20312
   187
          let val (t', U, vTs1, env1) = infer_type thy env Ts vTs t
berghofe@13669
   188
          in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of
berghofe@11522
   189
             (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
berghofe@13669
   190
                 prf, cnstrts, env2, vTs2) =>
wenzelm@20312
   191
               let val env3 = unifyT thy env2 T U
skalberg@15531
   192
               in (betapply (f, t'), prf % SOME t', cnstrts, env3, vTs2)
berghofe@11522
   193
               end
berghofe@13669
   194
           | (u, prf, cnstrts, env2, vTs2) =>
berghofe@13669
   195
               let val (env3, v) = mk_var env2 Ts (U --> propT);
berghofe@11522
   196
               in
skalberg@15531
   197
                 add_cnstrt Ts (v $ t') (prf % SOME t') cnstrts env3 vTs2
berghofe@13669
   198
                   (u, Const ("all", (U --> propT) --> propT) $ v)
berghofe@11522
   199
               end)
berghofe@11522
   200
          end
skalberg@15531
   201
      | mk_cnstrts env Ts Hs vTs (cprf % NONE) =
berghofe@13669
   202
          (case head_norm (mk_cnstrts env Ts Hs vTs cprf) of
berghofe@11522
   203
             (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
berghofe@13669
   204
                 prf, cnstrts, env', vTs') =>
berghofe@11522
   205
               let val (env'', t) = mk_var env' Ts T
skalberg@15531
   206
               in (betapply (f, t), prf % SOME t, cnstrts, env'', vTs')
berghofe@11522
   207
               end
berghofe@13669
   208
           | (u, prf, cnstrts, env', vTs') =>
berghofe@11522
   209
               let
wenzelm@14854
   210
                 val (env1, T) = mk_tvar (env', []);
berghofe@11522
   211
                 val (env2, v) = mk_var env1 Ts (T --> propT);
berghofe@11522
   212
                 val (env3, t) = mk_var env2 Ts T
berghofe@11522
   213
               in
skalberg@15531
   214
                 add_cnstrt Ts (v $ t) (prf % SOME t) cnstrts env3 vTs'
berghofe@11522
   215
                   (u, Const ("all", (T --> propT) --> propT) $ v)
berghofe@11522
   216
               end)
wenzelm@28808
   217
      | mk_cnstrts env _ _ vTs (prf as PThm (_, ((_, prop, opTs), _))) =
berghofe@13669
   218
          mk_cnstrts_atom env vTs prop opTs prf
berghofe@13669
   219
      | mk_cnstrts env _ _ vTs (prf as PAxm (_, prop, opTs)) =
berghofe@13669
   220
          mk_cnstrts_atom env vTs prop opTs prf
wenzelm@31943
   221
      | mk_cnstrts env _ _ vTs (prf as OfClass (T, c)) =
wenzelm@31943
   222
          mk_cnstrts_atom env vTs (Logic.mk_of_class (T, c)) NONE prf
berghofe@13669
   223
      | mk_cnstrts env _ _ vTs (prf as Oracle (_, prop, opTs)) =
berghofe@13669
   224
          mk_cnstrts_atom env vTs prop opTs prf
berghofe@13669
   225
      | mk_cnstrts env _ _ vTs (Hyp t) = (t, Hyp t, [], env, vTs)
berghofe@11613
   226
      | mk_cnstrts _ _ _ _ _ = error "reconstruct_proof: minimal proof object"
berghofe@13669
   227
  in mk_cnstrts env [] [] Symtab.empty cprf end;
berghofe@11522
   228
berghofe@11522
   229
berghofe@13669
   230
(**** update list of free variables of constraints ****)
berghofe@11522
   231
berghofe@11522
   232
fun upd_constrs env cs =
berghofe@11522
   233
  let
wenzelm@32032
   234
    val tenv = Envir.term_env env;
wenzelm@32032
   235
    val tyenv = Envir.type_env env;
wenzelm@20675
   236
    val dom = []
wenzelm@32032
   237
      |> Vartab.fold (cons o #1) tenv
wenzelm@32032
   238
      |> Vartab.fold (cons o #1) tyenv;
wenzelm@20675
   239
    val vran = []
wenzelm@32032
   240
      |> Vartab.fold (Term.add_var_names o #2 o #2) tenv
wenzelm@32032
   241
      |> Vartab.fold (Term.add_tvar_namesT o #2 o #2) tyenv;
berghofe@11522
   242
    fun check_cs [] = []
wenzelm@32032
   243
      | check_cs ((u, p, vs) :: ps) =
wenzelm@32032
   244
          let val vs' = subtract (op =) dom vs in
wenzelm@32032
   245
            if vs = vs' then (u, p, vs) :: check_cs ps
wenzelm@32032
   246
            else (true, p, fold (insert op =) vs' vran) :: check_cs ps
wenzelm@32032
   247
          end;
berghofe@11522
   248
  in check_cs cs end;
berghofe@11522
   249
wenzelm@32032
   250
berghofe@13669
   251
(**** solution of constraints ****)
berghofe@11522
   252
berghofe@11522
   253
fun solve _ [] bigenv = bigenv
wenzelm@20312
   254
  | solve thy cs bigenv =
berghofe@11522
   255
      let
berghofe@11660
   256
        fun search env [] = error ("Unsolvable constraints:\n" ^
berghofe@11660
   257
              Pretty.string_of (Pretty.chunks (map (fn (_, p, _) =>
wenzelm@32187
   258
                Goal_Display.pretty_flexpair (Syntax.init_pretty_global thy) (pairself
berghofe@13669
   259
                  (Envir.norm_term bigenv) p)) cs)))
berghofe@11522
   260
          | search env ((u, p as (t1, t2), vs)::ps) =
berghofe@11522
   261
              if u then
berghofe@11522
   262
                let
berghofe@11522
   263
                  val tn1 = Envir.norm_term bigenv t1;
berghofe@11522
   264
                  val tn2 = Envir.norm_term bigenv t2
berghofe@11522
   265
                in
berghofe@11522
   266
                  if Pattern.pattern tn1 andalso Pattern.pattern tn2 then
wenzelm@20312
   267
                    (Pattern.unify thy (tn1, tn2) env, ps) handle Pattern.Unif =>
wenzelm@20312
   268
                       cantunify thy (tn1, tn2)
berghofe@11522
   269
                  else
wenzelm@20312
   270
                    let val (env', cs') = decompose thy [] (env, (tn1, tn2))
berghofe@11522
   271
                    in if cs' = [(tn1, tn2)] then
berghofe@11522
   272
                         apsnd (cons (false, (tn1, tn2), vs)) (search env ps)
berghofe@11522
   273
                       else search env' (map (fn q => (true, q, vs)) cs' @ ps)
berghofe@11522
   274
                    end
berghofe@11522
   275
                end
berghofe@11522
   276
              else apsnd (cons (false, p, vs)) (search env ps);
berghofe@11522
   277
        val Envir.Envir {maxidx, ...} = bigenv;
berghofe@11522
   278
        val (env, cs') = search (Envir.empty maxidx) cs;
berghofe@11522
   279
      in
wenzelm@32032
   280
        solve thy (upd_constrs env cs') (Envir.merge (bigenv, env))
berghofe@11522
   281
      end;
berghofe@11522
   282
berghofe@11522
   283
berghofe@13669
   284
(**** reconstruction of proofs ****)
berghofe@11522
   285
wenzelm@20312
   286
fun reconstruct_proof thy prop cprf =
berghofe@11522
   287
  let
skalberg@15531
   288
    val (cprf' % SOME prop', thawf) = freeze_thaw_prf (cprf % SOME prop);
berghofe@13669
   289
    val _ = message "Collecting constraints...";
wenzelm@20312
   290
    val (t, prf, cs, env, _) = make_constraints_cprf thy
wenzelm@20312
   291
      (Envir.empty (maxidx_proof cprf ~1)) cprf';
berghofe@11522
   292
    val cs' = map (fn p => (true, p, op union
wenzelm@29265
   293
        (pairself (map (fst o dest_Var) o OldTerm.term_vars) p)))
wenzelm@20312
   294
      (map (pairself (Envir.norm_term env)) ((t, prop')::cs));
berghofe@11522
   295
    val _ = message ("Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
wenzelm@20312
   296
    val env' = solve thy cs' env
berghofe@11522
   297
  in
berghofe@11522
   298
    thawf (norm_proof env' prf)
berghofe@11522
   299
  end;
berghofe@11522
   300
berghofe@28813
   301
fun prop_of_atom prop Ts = subst_atomic_types
wenzelm@29270
   302
  (map TVar (OldTerm.term_tvars prop) @ map TFree (OldTerm.term_tfrees prop) ~~ Ts)
berghofe@28813
   303
  (forall_intr_vfs prop);
berghofe@13138
   304
berghofe@13734
   305
val head_norm = Envir.head_norm (Envir.empty 0);
berghofe@13734
   306
wenzelm@30146
   307
fun prop_of0 Hs (PBound i) = nth Hs i
skalberg@15531
   308
  | prop_of0 Hs (Abst (s, SOME T, prf)) =
wenzelm@27330
   309
      Term.all T $ (Abs (s, T, prop_of0 Hs prf))
skalberg@15531
   310
  | prop_of0 Hs (AbsP (s, SOME t, prf)) =
berghofe@13734
   311
      Logic.mk_implies (t, prop_of0 (t :: Hs) prf)
skalberg@15531
   312
  | prop_of0 Hs (prf % SOME t) = (case head_norm (prop_of0 Hs prf) of
berghofe@13734
   313
      Const ("all", _) $ f => f $ t
berghofe@13256
   314
    | _ => error "prop_of: all expected")
berghofe@13734
   315
  | prop_of0 Hs (prf1 %% prf2) = (case head_norm (prop_of0 Hs prf1) of
berghofe@13256
   316
      Const ("==>", _) $ P $ Q => Q
berghofe@13256
   317
    | _ => error "prop_of: ==> expected")
berghofe@13734
   318
  | prop_of0 Hs (Hyp t) = t
wenzelm@28808
   319
  | prop_of0 Hs (PThm (_, ((_, prop, SOME Ts), _))) = prop_of_atom prop Ts
skalberg@15531
   320
  | prop_of0 Hs (PAxm (_, prop, SOME Ts)) = prop_of_atom prop Ts
wenzelm@31943
   321
  | prop_of0 Hs (OfClass (T, c)) = Logic.mk_of_class (T, c)
skalberg@15531
   322
  | prop_of0 Hs (Oracle (_, prop, SOME Ts)) = prop_of_atom prop Ts
berghofe@13734
   323
  | prop_of0 _ _ = error "prop_of: partial proof object";
berghofe@13138
   324
wenzelm@18929
   325
val prop_of' = Envir.beta_eta_contract oo prop_of0;
berghofe@13256
   326
val prop_of = prop_of' [];
berghofe@13138
   327
berghofe@11522
   328
berghofe@13669
   329
(**** expand and reconstruct subproofs ****)
berghofe@11522
   330
wenzelm@20312
   331
fun expand_proof thy thms prf =
berghofe@11522
   332
  let
wenzelm@23178
   333
    fun expand maxidx prfs (AbsP (s, t, prf)) =
berghofe@12870
   334
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   335
          in (maxidx', prfs', AbsP (s, t, prf')) end
wenzelm@23178
   336
      | expand maxidx prfs (Abst (s, T, prf)) =
berghofe@12870
   337
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   338
          in (maxidx', prfs', Abst (s, T, prf')) end
berghofe@12870
   339
      | expand maxidx prfs (prf1 %% prf2) =
berghofe@11522
   340
          let
berghofe@12870
   341
            val (maxidx', prfs', prf1') = expand maxidx prfs prf1;
berghofe@12870
   342
            val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2;
berghofe@12870
   343
          in (maxidx'', prfs'', prf1' %% prf2') end
berghofe@12870
   344
      | expand maxidx prfs (prf % t) =
berghofe@12870
   345
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   346
          in (maxidx', prfs', prf' % t) end
wenzelm@28808
   347
      | expand maxidx prfs (prf as PThm (_, ((a, prop, SOME Ts), body))) =
berghofe@13342
   348
          if not (exists
skalberg@15531
   349
            (fn (b, NONE) => a = b
skalberg@15531
   350
              | (b, SOME prop') => a = b andalso prop = prop') thms)
berghofe@13342
   351
          then (maxidx, prfs, prf) else
berghofe@11522
   352
          let
wenzelm@16862
   353
            val (maxidx', prf, prfs') =
haftmann@17232
   354
              (case AList.lookup (op =) prfs (a, prop) of
skalberg@15531
   355
                NONE =>
berghofe@11522
   356
                  let
berghofe@11522
   357
                    val _ = message ("Reconstructing proof of " ^ a);
wenzelm@26939
   358
                    val _ = message (Syntax.string_of_term_global thy prop);
berghofe@13610
   359
                    val prf' = forall_intr_vfs_prf prop
wenzelm@29635
   360
                      (reconstruct_proof thy prop (join_proof body));
berghofe@12870
   361
                    val (maxidx', prfs', prf) = expand
wenzelm@20312
   362
                      (maxidx_proof prf' ~1) prfs prf'
wenzelm@32028
   363
                  in (maxidx' + maxidx + 1, incr_indexes (maxidx + 1) prf,
berghofe@13610
   364
                    ((a, prop), (maxidx', prf)) :: prfs')
berghofe@13610
   365
                  end
skalberg@15531
   366
              | SOME (maxidx', prf) => (maxidx' + maxidx + 1,
wenzelm@32028
   367
                  incr_indexes (maxidx + 1) prf, prfs));
wenzelm@29270
   368
            val tfrees = OldTerm.term_tfrees prop;
berghofe@13669
   369
            val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j))
wenzelm@29270
   370
              (OldTerm.term_tvars prop) @ map (rpair ~1 o fst) tfrees ~~ Ts;
berghofe@13669
   371
            val varify = map_type_tfree (fn p as (a, S) =>
wenzelm@20675
   372
              if member (op =) tfrees p then TVar ((a, ~1), S) else TFree p)
berghofe@11522
   373
          in
berghofe@13669
   374
            (maxidx', prfs', map_proof_terms (subst_TVars tye o
wenzelm@20548
   375
               map_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
wenzelm@20312
   379
  in #3 (expand (maxidx_proof prf ~1) [] prf) end;
berghofe@11522
   380
berghofe@11522
   381
end;