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;
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)
wenzelm@33245
    43
  in (list_comb (v, map Bound (length Ts - 1 downto 0)), env') end;
berghofe@11522
    44
wenzelm@33245
    45
fun mk_tvar S (Envir.Envir {maxidx, tenv, tyenv}) =
wenzelm@33245
    46
  (TVar (("'t", maxidx + 1), S),
wenzelm@33245
    47
    Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv});
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 =>
wenzelm@33245
    76
            let val (T, env') = 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
wenzelm@33245
    83
        val (T', env') = if T = dummyT then mk_tvar [] env else (T, env);
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
        | _ =>
wenzelm@33245
    93
          let val (V, env3) = 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@33245
   102
fun decompose thy Ts (p as (t, u)) env =
wenzelm@33245
   103
  let
wenzelm@33245
   104
    fun rigrig (a, T) (b, U) uT ts us =
wenzelm@33245
   105
      if a <> b then cantunify thy p
wenzelm@33245
   106
      else apfst flat (fold_map (decompose thy Ts) (ts ~~ us) (uT env T U))
wenzelm@33245
   107
  in
wenzelm@33245
   108
    case pairself (strip_comb o Envir.head_norm env) p of
wenzelm@20312
   109
      ((Const c, ts), (Const d, us)) => rigrig c d (unifyT thy) ts us
wenzelm@20312
   110
    | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT thy) ts us
berghofe@13715
   111
    | ((Bound i, ts), (Bound j, us)) =>
berghofe@13715
   112
        rigrig (i, dummyT) (j, dummyT) (K o K) ts us
berghofe@13715
   113
    | ((Abs (_, T, t), []), (Abs (_, U, u), [])) =>
wenzelm@33245
   114
        decompose thy (T::Ts) (t, u) (unifyT thy env T U)
berghofe@13715
   115
    | ((Abs (_, T, t), []), _) =>
wenzelm@33245
   116
        decompose thy (T::Ts) (t, incr_boundvars 1 u $ Bound 0) env
berghofe@13715
   117
    | (_, (Abs (_, T, u), [])) =>
wenzelm@33245
   118
        decompose thy (T::Ts) (incr_boundvars 1 t $ Bound 0, u) env
wenzelm@33245
   119
    | _ => ([(mk_abs Ts t, mk_abs Ts u)], env)
berghofe@13715
   120
  end;
berghofe@11522
   121
wenzelm@20312
   122
fun make_constraints_cprf thy env cprf =
berghofe@11522
   123
  let
berghofe@13669
   124
    fun add_cnstrt Ts prop prf cs env vTs (t, u) =
berghofe@11522
   125
      let
berghofe@11522
   126
        val t' = mk_abs Ts t;
berghofe@12236
   127
        val u' = mk_abs Ts u
berghofe@11522
   128
      in
wenzelm@20312
   129
        (prop, prf, cs, Pattern.unify thy (t', u') env, vTs)
berghofe@12236
   130
        handle Pattern.Pattern =>
wenzelm@33245
   131
            let val (cs', env') = decompose thy [] (t', u') env
berghofe@13669
   132
            in (prop, prf, cs @ cs', env', vTs) end
berghofe@12236
   133
        | Pattern.Unif =>
wenzelm@20312
   134
            cantunify thy (Envir.norm_term env t', Envir.norm_term env u')
berghofe@11522
   135
      end;
berghofe@11522
   136
berghofe@13669
   137
    fun mk_cnstrts_atom env vTs prop opTs prf =
berghofe@11522
   138
          let
krauss@36042
   139
            val tvars = Term.add_tvars prop [] |> rev;
krauss@36042
   140
            val tfrees = Term.add_tfrees prop [] |> rev;
wenzelm@33245
   141
            val (Ts, env') =
wenzelm@31903
   142
              (case opTs of
wenzelm@33245
   143
                NONE => fold_map mk_tvar (map snd tvars @ map snd tfrees) env
wenzelm@33245
   144
              | SOME Ts => (Ts, env));
berghofe@28813
   145
            val prop' = subst_atomic_types (map TVar tvars @ map TFree tfrees ~~ Ts)
berghofe@28813
   146
              (forall_intr_vfs prop) handle Library.UnequalLengths =>
berghofe@13669
   147
                error ("Wrong number of type arguments for " ^
wenzelm@21646
   148
                  quote (get_name [] prop prf))
berghofe@28813
   149
          in (prop', change_type (SOME Ts) prf, [], env', vTs) end;
berghofe@11522
   150
berghofe@13669
   151
    fun head_norm (prop, prf, cnstrts, env, vTs) =
berghofe@13669
   152
      (Envir.head_norm env prop, prf, cnstrts, env, vTs);
wenzelm@23178
   153
wenzelm@30146
   154
    fun mk_cnstrts env _ Hs vTs (PBound i) = ((nth Hs i, PBound i, [], env, vTs)
berghofe@25246
   155
          handle Subscript => error ("mk_cnstrts: bad variable index " ^ string_of_int i))
berghofe@13669
   156
      | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) =
berghofe@13669
   157
          let
wenzelm@33245
   158
            val (T, env') =
wenzelm@33245
   159
              (case opT of
wenzelm@33245
   160
                NONE => mk_tvar [] env
wenzelm@33245
   161
              | SOME T => (T, env));
berghofe@13669
   162
            val (t, prf, cnstrts, env'', vTs') =
berghofe@13669
   163
              mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf;
skalberg@15531
   164
          in (Const ("all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, SOME T, prf),
berghofe@13669
   165
            cnstrts, env'', vTs')
berghofe@11522
   166
          end
skalberg@15531
   167
      | mk_cnstrts env Ts Hs vTs (AbsP (s, SOME t, cprf)) =
berghofe@11522
   168
          let
wenzelm@20312
   169
            val (t', _, vTs', env') = infer_type thy env Ts vTs t;
berghofe@13669
   170
            val (u, prf, cnstrts, env'', vTs'') = mk_cnstrts env' Ts (t'::Hs) vTs' cprf;
skalberg@15531
   171
          in (Logic.mk_implies (t', u), AbsP (s, SOME t', prf), cnstrts, env'', vTs'')
berghofe@11522
   172
          end
skalberg@15531
   173
      | mk_cnstrts env Ts Hs vTs (AbsP (s, NONE, cprf)) =
berghofe@11522
   174
          let
wenzelm@33245
   175
            val (t, env') = mk_var env Ts propT;
berghofe@13669
   176
            val (u, prf, cnstrts, env'', vTs') = mk_cnstrts env' Ts (t::Hs) vTs cprf;
skalberg@15531
   177
          in (Logic.mk_implies (t, u), AbsP (s, SOME t, prf), cnstrts, env'', vTs')
berghofe@11522
   178
          end
berghofe@13669
   179
      | mk_cnstrts env Ts Hs vTs (cprf1 %% cprf2) =
berghofe@13669
   180
          let val (u, prf2, cnstrts, env', vTs') = mk_cnstrts env Ts Hs vTs cprf2
berghofe@13669
   181
          in (case head_norm (mk_cnstrts env' Ts Hs vTs' cprf1) of
berghofe@13669
   182
              (Const ("==>", _) $ u' $ t', prf1, cnstrts', env'', vTs'') =>
berghofe@11613
   183
                add_cnstrt Ts t' (prf1 %% prf2) (cnstrts' @ cnstrts)
berghofe@13669
   184
                  env'' vTs'' (u, u')
berghofe@13669
   185
            | (t, prf1, cnstrts', env'', vTs'') =>
wenzelm@33245
   186
                let val (v, env''') = mk_var env'' Ts propT
berghofe@11613
   187
                in add_cnstrt Ts v (prf1 %% prf2) (cnstrts' @ cnstrts)
berghofe@13669
   188
                  env''' vTs'' (t, Logic.mk_implies (u, v))
berghofe@11522
   189
                end)
berghofe@11522
   190
          end
skalberg@15531
   191
      | mk_cnstrts env Ts Hs vTs (cprf % SOME t) =
wenzelm@20312
   192
          let val (t', U, vTs1, env1) = infer_type thy env Ts vTs t
berghofe@13669
   193
          in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of
berghofe@11522
   194
             (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
berghofe@13669
   195
                 prf, cnstrts, env2, vTs2) =>
wenzelm@20312
   196
               let val env3 = unifyT thy env2 T U
skalberg@15531
   197
               in (betapply (f, t'), prf % SOME t', cnstrts, env3, vTs2)
berghofe@11522
   198
               end
berghofe@13669
   199
           | (u, prf, cnstrts, env2, vTs2) =>
wenzelm@33245
   200
               let val (v, env3) = mk_var env2 Ts (U --> propT);
berghofe@11522
   201
               in
skalberg@15531
   202
                 add_cnstrt Ts (v $ t') (prf % SOME t') cnstrts env3 vTs2
berghofe@13669
   203
                   (u, Const ("all", (U --> propT) --> propT) $ v)
berghofe@11522
   204
               end)
berghofe@11522
   205
          end
skalberg@15531
   206
      | mk_cnstrts env Ts Hs vTs (cprf % NONE) =
berghofe@13669
   207
          (case head_norm (mk_cnstrts env Ts Hs vTs cprf) of
berghofe@11522
   208
             (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
berghofe@13669
   209
                 prf, cnstrts, env', vTs') =>
wenzelm@33245
   210
               let val (t, env'') = mk_var env' Ts T
skalberg@15531
   211
               in (betapply (f, t), prf % SOME t, cnstrts, env'', vTs')
berghofe@11522
   212
               end
berghofe@13669
   213
           | (u, prf, cnstrts, env', vTs') =>
berghofe@11522
   214
               let
wenzelm@33245
   215
                 val (T, env1) = mk_tvar [] env';
wenzelm@33245
   216
                 val (v, env2) = mk_var env1 Ts (T --> propT);
wenzelm@33245
   217
                 val (t, env3) = mk_var env2 Ts T
berghofe@11522
   218
               in
skalberg@15531
   219
                 add_cnstrt Ts (v $ t) (prf % SOME t) cnstrts env3 vTs'
berghofe@11522
   220
                   (u, Const ("all", (T --> propT) --> propT) $ v)
berghofe@11522
   221
               end)
wenzelm@28808
   222
      | mk_cnstrts env _ _ vTs (prf as PThm (_, ((_, prop, opTs), _))) =
berghofe@13669
   223
          mk_cnstrts_atom env vTs prop opTs prf
berghofe@13669
   224
      | mk_cnstrts env _ _ vTs (prf as PAxm (_, prop, opTs)) =
berghofe@13669
   225
          mk_cnstrts_atom env vTs prop opTs prf
wenzelm@31943
   226
      | mk_cnstrts env _ _ vTs (prf as OfClass (T, c)) =
wenzelm@31943
   227
          mk_cnstrts_atom env vTs (Logic.mk_of_class (T, c)) NONE prf
berghofe@13669
   228
      | mk_cnstrts env _ _ vTs (prf as Oracle (_, prop, opTs)) =
berghofe@13669
   229
          mk_cnstrts_atom env vTs prop opTs prf
berghofe@13669
   230
      | mk_cnstrts env _ _ vTs (Hyp t) = (t, Hyp t, [], env, vTs)
berghofe@11613
   231
      | mk_cnstrts _ _ _ _ _ = error "reconstruct_proof: minimal proof object"
berghofe@13669
   232
  in mk_cnstrts env [] [] Symtab.empty cprf end;
berghofe@11522
   233
berghofe@11522
   234
berghofe@13669
   235
(**** update list of free variables of constraints ****)
berghofe@11522
   236
berghofe@11522
   237
fun upd_constrs env cs =
berghofe@11522
   238
  let
wenzelm@32032
   239
    val tenv = Envir.term_env env;
wenzelm@32032
   240
    val tyenv = Envir.type_env env;
wenzelm@20675
   241
    val dom = []
wenzelm@32032
   242
      |> Vartab.fold (cons o #1) tenv
wenzelm@32032
   243
      |> Vartab.fold (cons o #1) tyenv;
wenzelm@20675
   244
    val vran = []
wenzelm@32032
   245
      |> Vartab.fold (Term.add_var_names o #2 o #2) tenv
wenzelm@32032
   246
      |> Vartab.fold (Term.add_tvar_namesT o #2 o #2) tyenv;
berghofe@11522
   247
    fun check_cs [] = []
wenzelm@32032
   248
      | check_cs ((u, p, vs) :: ps) =
wenzelm@32032
   249
          let val vs' = subtract (op =) dom vs in
wenzelm@32032
   250
            if vs = vs' then (u, p, vs) :: check_cs ps
wenzelm@32032
   251
            else (true, p, fold (insert op =) vs' vran) :: check_cs ps
wenzelm@32032
   252
          end;
berghofe@11522
   253
  in check_cs cs end;
berghofe@11522
   254
wenzelm@32032
   255
berghofe@13669
   256
(**** solution of constraints ****)
berghofe@11522
   257
berghofe@11522
   258
fun solve _ [] bigenv = bigenv
wenzelm@20312
   259
  | solve thy cs bigenv =
berghofe@11522
   260
      let
berghofe@11660
   261
        fun search env [] = error ("Unsolvable constraints:\n" ^
berghofe@11660
   262
              Pretty.string_of (Pretty.chunks (map (fn (_, p, _) =>
wenzelm@32187
   263
                Goal_Display.pretty_flexpair (Syntax.init_pretty_global thy) (pairself
berghofe@13669
   264
                  (Envir.norm_term bigenv) p)) cs)))
berghofe@11522
   265
          | search env ((u, p as (t1, t2), vs)::ps) =
berghofe@11522
   266
              if u then
berghofe@11522
   267
                let
berghofe@11522
   268
                  val tn1 = Envir.norm_term bigenv t1;
berghofe@11522
   269
                  val tn2 = Envir.norm_term bigenv t2
berghofe@11522
   270
                in
berghofe@11522
   271
                  if Pattern.pattern tn1 andalso Pattern.pattern tn2 then
wenzelm@20312
   272
                    (Pattern.unify thy (tn1, tn2) env, ps) handle Pattern.Unif =>
wenzelm@20312
   273
                       cantunify thy (tn1, tn2)
berghofe@11522
   274
                  else
wenzelm@33245
   275
                    let val (cs', env') = decompose thy [] (tn1, tn2) env
berghofe@11522
   276
                    in if cs' = [(tn1, tn2)] then
berghofe@11522
   277
                         apsnd (cons (false, (tn1, tn2), vs)) (search env ps)
berghofe@11522
   278
                       else search env' (map (fn q => (true, q, vs)) cs' @ ps)
berghofe@11522
   279
                    end
berghofe@11522
   280
                end
berghofe@11522
   281
              else apsnd (cons (false, p, vs)) (search env ps);
berghofe@11522
   282
        val Envir.Envir {maxidx, ...} = bigenv;
berghofe@11522
   283
        val (env, cs') = search (Envir.empty maxidx) cs;
berghofe@11522
   284
      in
wenzelm@32032
   285
        solve thy (upd_constrs env cs') (Envir.merge (bigenv, env))
berghofe@11522
   286
      end;
berghofe@11522
   287
berghofe@11522
   288
berghofe@13669
   289
(**** reconstruction of proofs ****)
berghofe@11522
   290
wenzelm@20312
   291
fun reconstruct_proof thy prop cprf =
berghofe@11522
   292
  let
skalberg@15531
   293
    val (cprf' % SOME prop', thawf) = freeze_thaw_prf (cprf % SOME prop);
berghofe@13669
   294
    val _ = message "Collecting constraints...";
wenzelm@20312
   295
    val (t, prf, cs, env, _) = make_constraints_cprf thy
wenzelm@20312
   296
      (Envir.empty (maxidx_proof cprf ~1)) cprf';
haftmann@33042
   297
    val cs' = map (fn p => (true, p, uncurry (union (op =)) 
wenzelm@29265
   298
        (pairself (map (fst o dest_Var) o OldTerm.term_vars) p)))
wenzelm@20312
   299
      (map (pairself (Envir.norm_term env)) ((t, prop')::cs));
berghofe@11522
   300
    val _ = message ("Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
wenzelm@20312
   301
    val env' = solve thy cs' env
berghofe@11522
   302
  in
berghofe@11522
   303
    thawf (norm_proof env' prf)
berghofe@11522
   304
  end;
berghofe@11522
   305
berghofe@28813
   306
fun prop_of_atom prop Ts = subst_atomic_types
krauss@36042
   307
  (map TVar (Term.add_tvars prop [] |> rev) @ map TFree (Term.add_tfrees prop [] |> rev) ~~ Ts)
berghofe@28813
   308
  (forall_intr_vfs prop);
berghofe@13138
   309
berghofe@13734
   310
val head_norm = Envir.head_norm (Envir.empty 0);
berghofe@13734
   311
wenzelm@30146
   312
fun prop_of0 Hs (PBound i) = nth Hs i
skalberg@15531
   313
  | prop_of0 Hs (Abst (s, SOME T, prf)) =
wenzelm@27330
   314
      Term.all T $ (Abs (s, T, prop_of0 Hs prf))
skalberg@15531
   315
  | prop_of0 Hs (AbsP (s, SOME t, prf)) =
berghofe@13734
   316
      Logic.mk_implies (t, prop_of0 (t :: Hs) prf)
skalberg@15531
   317
  | prop_of0 Hs (prf % SOME t) = (case head_norm (prop_of0 Hs prf) of
berghofe@13734
   318
      Const ("all", _) $ f => f $ t
berghofe@13256
   319
    | _ => error "prop_of: all expected")
berghofe@13734
   320
  | prop_of0 Hs (prf1 %% prf2) = (case head_norm (prop_of0 Hs prf1) of
berghofe@13256
   321
      Const ("==>", _) $ P $ Q => Q
berghofe@13256
   322
    | _ => error "prop_of: ==> expected")
berghofe@13734
   323
  | prop_of0 Hs (Hyp t) = t
wenzelm@28808
   324
  | prop_of0 Hs (PThm (_, ((_, prop, SOME Ts), _))) = prop_of_atom prop Ts
skalberg@15531
   325
  | prop_of0 Hs (PAxm (_, prop, SOME Ts)) = prop_of_atom prop Ts
wenzelm@31943
   326
  | prop_of0 Hs (OfClass (T, c)) = Logic.mk_of_class (T, c)
skalberg@15531
   327
  | prop_of0 Hs (Oracle (_, prop, SOME Ts)) = prop_of_atom prop Ts
berghofe@13734
   328
  | prop_of0 _ _ = error "prop_of: partial proof object";
berghofe@13138
   329
wenzelm@18929
   330
val prop_of' = Envir.beta_eta_contract oo prop_of0;
berghofe@13256
   331
val prop_of = prop_of' [];
berghofe@13138
   332
berghofe@11522
   333
berghofe@13669
   334
(**** expand and reconstruct subproofs ****)
berghofe@11522
   335
wenzelm@20312
   336
fun expand_proof thy thms prf =
berghofe@11522
   337
  let
wenzelm@23178
   338
    fun expand maxidx prfs (AbsP (s, t, prf)) =
berghofe@12870
   339
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   340
          in (maxidx', prfs', AbsP (s, t, prf')) end
wenzelm@23178
   341
      | expand maxidx prfs (Abst (s, T, prf)) =
berghofe@12870
   342
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   343
          in (maxidx', prfs', Abst (s, T, prf')) end
berghofe@12870
   344
      | expand maxidx prfs (prf1 %% prf2) =
berghofe@11522
   345
          let
berghofe@12870
   346
            val (maxidx', prfs', prf1') = expand maxidx prfs prf1;
berghofe@12870
   347
            val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2;
berghofe@12870
   348
          in (maxidx'', prfs'', prf1' %% prf2') end
berghofe@12870
   349
      | expand maxidx prfs (prf % t) =
berghofe@12870
   350
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   351
          in (maxidx', prfs', prf' % t) end
wenzelm@28808
   352
      | expand maxidx prfs (prf as PThm (_, ((a, prop, SOME Ts), body))) =
berghofe@13342
   353
          if not (exists
skalberg@15531
   354
            (fn (b, NONE) => a = b
skalberg@15531
   355
              | (b, SOME prop') => a = b andalso prop = prop') thms)
berghofe@13342
   356
          then (maxidx, prfs, prf) else
berghofe@11522
   357
          let
wenzelm@16862
   358
            val (maxidx', prf, prfs') =
haftmann@17232
   359
              (case AList.lookup (op =) prfs (a, prop) of
skalberg@15531
   360
                NONE =>
berghofe@11522
   361
                  let
berghofe@11522
   362
                    val _ = message ("Reconstructing proof of " ^ a);
wenzelm@26939
   363
                    val _ = message (Syntax.string_of_term_global thy prop);
berghofe@13610
   364
                    val prf' = forall_intr_vfs_prf prop
wenzelm@29635
   365
                      (reconstruct_proof thy prop (join_proof body));
berghofe@12870
   366
                    val (maxidx', prfs', prf) = expand
wenzelm@20312
   367
                      (maxidx_proof prf' ~1) prfs prf'
wenzelm@32028
   368
                  in (maxidx' + maxidx + 1, incr_indexes (maxidx + 1) prf,
berghofe@13610
   369
                    ((a, prop), (maxidx', prf)) :: prfs')
berghofe@13610
   370
                  end
skalberg@15531
   371
              | SOME (maxidx', prf) => (maxidx' + maxidx + 1,
wenzelm@32028
   372
                  incr_indexes (maxidx + 1) prf, prfs));
krauss@36042
   373
            val tfrees = Term.add_tfrees prop [] |> rev;
berghofe@13669
   374
            val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j))
krauss@36042
   375
              (Term.add_tvars prop [] |> rev) @ map (rpair ~1 o fst) tfrees ~~ Ts;
berghofe@13669
   376
            val varify = map_type_tfree (fn p as (a, S) =>
wenzelm@20675
   377
              if member (op =) tfrees p then TVar ((a, ~1), S) else TFree p)
berghofe@11522
   378
          in
wenzelm@36620
   379
            (maxidx', prfs', map_proof_types (typ_subst_TVars tye o varify) prf)
berghofe@11522
   380
          end
berghofe@12870
   381
      | expand maxidx prfs prf = (maxidx, prfs, prf);
berghofe@11522
   382
wenzelm@20312
   383
  in #3 (expand (maxidx_proof prf ~1) [] prf) end;
berghofe@11522
   384
berghofe@11522
   385
end;