src/Pure/Proof/reconstruct.ML
author wenzelm
Fri Oct 07 21:16:48 2016 +0200 (2016-10-07)
changeset 64092 95469c544b82
parent 63616 ff66974e31be
child 64556 851ae0e7b09c
permissions -rw-r--r--
accept obscure timezone used in 2011;
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@62922
     9
  val quiet_mode : bool Config.T
wenzelm@62922
    10
  val reconstruct_proof : Proof.context -> 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@62922
    13
  val proof_of : Proof.context -> thm -> Proofterm.proof
wenzelm@62922
    14
  val expand_proof : Proof.context -> (string * term option) list ->
berghofe@13342
    15
    Proofterm.proof -> Proofterm.proof
berghofe@11522
    16
end;
berghofe@11522
    17
berghofe@11522
    18
structure Reconstruct : RECONSTRUCT =
berghofe@11522
    19
struct
berghofe@11522
    20
wenzelm@62922
    21
val quiet_mode =
wenzelm@62922
    22
  Config.bool (Config.declare ("Reconstruct.quiet_mode", @{here}) (K (Config.Bool true)));
wenzelm@62922
    23
wenzelm@62922
    24
fun message ctxt msg =
wenzelm@62922
    25
  if Config.get ctxt quiet_mode then () else writeln (msg ());
berghofe@11522
    26
berghofe@28813
    27
fun vars_of t = map Var (rev (Term.add_vars t []));
berghofe@28813
    28
fun frees_of t = map Free (rev (Term.add_frees t []));
berghofe@11522
    29
wenzelm@27330
    30
fun forall_intr_vfs prop = fold_rev Logic.all
berghofe@28813
    31
  (vars_of prop @ frees_of prop) prop;
berghofe@11522
    32
wenzelm@37310
    33
fun forall_intr_vfs_prf prop prf = fold_rev Proofterm.forall_intr_proof'
berghofe@28813
    34
  (vars_of prop @ frees_of prop) prf;
berghofe@12001
    35
berghofe@11522
    36
berghofe@13669
    37
(**** generate constraints for proof term ****)
berghofe@11522
    38
wenzelm@23178
    39
fun mk_var env Ts T =
berghofe@11522
    40
  let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T)
wenzelm@33245
    41
  in (list_comb (v, map Bound (length Ts - 1 downto 0)), env') end;
berghofe@11522
    42
wenzelm@33245
    43
fun mk_tvar S (Envir.Envir {maxidx, tenv, tyenv}) =
wenzelm@33245
    44
  (TVar (("'t", maxidx + 1), S),
wenzelm@33245
    45
    Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv});
berghofe@11522
    46
wenzelm@19473
    47
val mk_abs = fold (fn T => fn u => Abs ("", T, u));
berghofe@11522
    48
wenzelm@62922
    49
fun unifyT ctxt env T U =
berghofe@11522
    50
  let
wenzelm@32032
    51
    val Envir.Envir {maxidx, tenv, tyenv} = env;
wenzelm@62922
    52
    val (tyenv', maxidx') = Sign.typ_unify (Proof_Context.theory_of ctxt) (T, U) (tyenv, maxidx);
wenzelm@32032
    53
  in Envir.Envir {maxidx = maxidx', tenv = tenv, tyenv = tyenv'} end;
berghofe@11522
    54
wenzelm@32032
    55
fun chaseT env (T as TVar v) =
wenzelm@32032
    56
      (case Type.lookup (Envir.type_env env) v of
wenzelm@32032
    57
        NONE => T
wenzelm@32032
    58
      | SOME T' => chaseT env T')
berghofe@13669
    59
  | chaseT _ T = T;
berghofe@13669
    60
wenzelm@62922
    61
fun infer_type ctxt (env as Envir.Envir {maxidx, tenv, tyenv}) Ts vTs
wenzelm@32032
    62
      (t as Const (s, T)) = if T = dummyT then
wenzelm@62922
    63
        (case Sign.const_type (Proof_Context.theory_of ctxt) s of
skalberg@15531
    64
          NONE => error ("reconstruct_proof: No such constant: " ^ quote s)
wenzelm@23178
    65
        | SOME T =>
wenzelm@19419
    66
            let val T' = Type.strip_sorts (Logic.incr_tvar (maxidx + 1) T)
berghofe@13669
    67
            in (Const (s, T'), T', vTs,
wenzelm@32032
    68
              Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv})
berghofe@13669
    69
            end)
berghofe@13669
    70
      else (t, T, vTs, env)
wenzelm@62922
    71
  | infer_type ctxt env Ts vTs (t as Free (s, T)) =
wenzelm@17412
    72
      if T = dummyT then (case Symtab.lookup vTs s of
skalberg@15531
    73
          NONE =>
wenzelm@33245
    74
            let val (T, env') = mk_tvar [] env
wenzelm@17412
    75
            in (Free (s, T), T, Symtab.update_new (s, T) vTs, env') end
skalberg@15531
    76
        | SOME T => (Free (s, T), T, vTs, env))
berghofe@13669
    77
      else (t, T, vTs, env)
wenzelm@62922
    78
  | infer_type ctxt env Ts vTs (Var _) = error "reconstruct_proof: internal error"
wenzelm@62922
    79
  | infer_type ctxt env Ts vTs (Abs (s, T, t)) =
berghofe@13669
    80
      let
wenzelm@33245
    81
        val (T', env') = if T = dummyT then mk_tvar [] env else (T, env);
wenzelm@62922
    82
        val (t', U, vTs', env'') = infer_type ctxt env' (T' :: Ts) vTs t
berghofe@13669
    83
      in (Abs (s, T', t'), T' --> U, vTs', env'') end
wenzelm@62922
    84
  | infer_type ctxt env Ts vTs (t $ u) =
berghofe@13669
    85
      let
wenzelm@62922
    86
        val (t', T, vTs1, env1) = infer_type ctxt env Ts vTs t;
wenzelm@62922
    87
        val (u', U, vTs2, env2) = infer_type ctxt env1 Ts vTs1 u;
berghofe@13669
    88
      in (case chaseT env2 T of
wenzelm@62922
    89
          Type ("fun", [U', V]) => (t' $ u', V, vTs2, unifyT ctxt env2 U U')
berghofe@13669
    90
        | _ =>
wenzelm@33245
    91
          let val (V, env3) = mk_tvar [] env2
wenzelm@62922
    92
          in (t' $ u', V, vTs2, unifyT ctxt env3 T (U --> V)) end)
berghofe@13669
    93
      end
wenzelm@62922
    94
  | infer_type ctxt env Ts vTs (t as Bound i) = ((t, nth Ts i, vTs, env)
wenzelm@43278
    95
      handle General.Subscript => error ("infer_type: bad variable index " ^ string_of_int i));
berghofe@13669
    96
wenzelm@62922
    97
fun cantunify ctxt (t, u) =
wenzelm@62922
    98
  error ("Non-unifiable terms:\n" ^
wenzelm@62922
    99
    Syntax.string_of_term ctxt t ^ "\n\n" ^ Syntax.string_of_term ctxt u);
berghofe@11522
   100
wenzelm@62922
   101
fun decompose ctxt Ts (p as (t, u)) env =
wenzelm@33245
   102
  let
wenzelm@33245
   103
    fun rigrig (a, T) (b, U) uT ts us =
wenzelm@62922
   104
      if a <> b then cantunify ctxt p
wenzelm@62922
   105
      else apfst flat (fold_map (decompose ctxt Ts) (ts ~~ us) (uT env T U))
wenzelm@33245
   106
  in
wenzelm@59058
   107
    case apply2 (strip_comb o Envir.head_norm env) p of
wenzelm@62922
   108
      ((Const c, ts), (Const d, us)) => rigrig c d (unifyT ctxt) ts us
wenzelm@62922
   109
    | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT ctxt) ts us
berghofe@13715
   110
    | ((Bound i, ts), (Bound j, us)) =>
berghofe@13715
   111
        rigrig (i, dummyT) (j, dummyT) (K o K) ts us
berghofe@13715
   112
    | ((Abs (_, T, t), []), (Abs (_, U, u), [])) =>
wenzelm@62922
   113
        decompose ctxt (T::Ts) (t, u) (unifyT ctxt env T U)
berghofe@13715
   114
    | ((Abs (_, T, t), []), _) =>
wenzelm@62922
   115
        decompose ctxt (T::Ts) (t, incr_boundvars 1 u $ Bound 0) env
berghofe@13715
   116
    | (_, (Abs (_, T, u), [])) =>
wenzelm@62922
   117
        decompose ctxt (T::Ts) (incr_boundvars 1 t $ Bound 0, u) env
wenzelm@33245
   118
    | _ => ([(mk_abs Ts t, mk_abs Ts u)], env)
berghofe@13715
   119
  end;
berghofe@11522
   120
wenzelm@62922
   121
fun make_constraints_cprf ctxt 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
wenzelm@62922
   128
        (prop, prf, cs, Pattern.unify (Context.Proof ctxt) (t', u') env, vTs)
berghofe@12236
   129
        handle Pattern.Pattern =>
wenzelm@62922
   130
            let val (cs', env') = decompose ctxt [] (t', u') env
berghofe@13669
   131
            in (prop, prf, cs @ cs', env', vTs) end
berghofe@12236
   132
        | Pattern.Unif =>
wenzelm@62922
   133
            cantunify ctxt (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
krauss@36042
   138
            val tvars = Term.add_tvars prop [] |> rev;
krauss@36042
   139
            val tfrees = Term.add_tfrees prop [] |> rev;
wenzelm@33245
   140
            val (Ts, env') =
wenzelm@31903
   141
              (case opTs of
wenzelm@33245
   142
                NONE => fold_map mk_tvar (map snd tvars @ map snd tfrees) env
wenzelm@33245
   143
              | SOME Ts => (Ts, env));
berghofe@28813
   144
            val prop' = subst_atomic_types (map TVar tvars @ map TFree tfrees ~~ Ts)
wenzelm@40722
   145
              (forall_intr_vfs prop) handle ListPair.UnequalLengths =>
wenzelm@37310
   146
                error ("Wrong number of type arguments for " ^ quote (Proofterm.guess_name prf))
wenzelm@37310
   147
          in (prop', Proofterm.change_type (SOME Ts) prf, [], env', vTs) end;
berghofe@11522
   148
berghofe@13669
   149
    fun head_norm (prop, prf, cnstrts, env, vTs) =
berghofe@13669
   150
      (Envir.head_norm env prop, prf, cnstrts, env, vTs);
wenzelm@23178
   151
wenzelm@30146
   152
    fun mk_cnstrts env _ Hs vTs (PBound i) = ((nth Hs i, PBound i, [], env, vTs)
wenzelm@43278
   153
          handle General.Subscript => error ("mk_cnstrts: bad variable index " ^ string_of_int i))
berghofe@13669
   154
      | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) =
berghofe@13669
   155
          let
wenzelm@33245
   156
            val (T, env') =
wenzelm@33245
   157
              (case opT of
wenzelm@33245
   158
                NONE => mk_tvar [] env
wenzelm@33245
   159
              | SOME T => (T, env));
berghofe@13669
   160
            val (t, prf, cnstrts, env'', vTs') =
berghofe@13669
   161
              mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf;
wenzelm@62922
   162
          in
wenzelm@62922
   163
            (Const ("Pure.all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, SOME T, prf),
wenzelm@62922
   164
              cnstrts, env'', vTs')
berghofe@11522
   165
          end
skalberg@15531
   166
      | mk_cnstrts env Ts Hs vTs (AbsP (s, SOME t, cprf)) =
berghofe@11522
   167
          let
wenzelm@62922
   168
            val (t', _, vTs', env') = infer_type ctxt env Ts vTs t;
berghofe@13669
   169
            val (u, prf, cnstrts, env'', vTs'') = mk_cnstrts env' Ts (t'::Hs) vTs' cprf;
skalberg@15531
   170
          in (Logic.mk_implies (t', u), AbsP (s, SOME t', prf), cnstrts, env'', vTs'')
berghofe@11522
   171
          end
skalberg@15531
   172
      | mk_cnstrts env Ts Hs vTs (AbsP (s, NONE, cprf)) =
berghofe@11522
   173
          let
wenzelm@33245
   174
            val (t, env') = mk_var env Ts propT;
berghofe@13669
   175
            val (u, prf, cnstrts, env'', vTs') = mk_cnstrts env' Ts (t::Hs) vTs cprf;
skalberg@15531
   176
          in (Logic.mk_implies (t, u), AbsP (s, SOME t, prf), cnstrts, env'', vTs')
berghofe@11522
   177
          end
berghofe@13669
   178
      | mk_cnstrts env Ts Hs vTs (cprf1 %% cprf2) =
berghofe@13669
   179
          let val (u, prf2, cnstrts, env', vTs') = mk_cnstrts env Ts Hs vTs cprf2
berghofe@13669
   180
          in (case head_norm (mk_cnstrts env' Ts Hs vTs' cprf1) of
wenzelm@56245
   181
              (Const ("Pure.imp", _) $ u' $ t', prf1, cnstrts', env'', vTs'') =>
berghofe@11613
   182
                add_cnstrt Ts t' (prf1 %% prf2) (cnstrts' @ cnstrts)
berghofe@13669
   183
                  env'' vTs'' (u, u')
berghofe@13669
   184
            | (t, prf1, cnstrts', env'', vTs'') =>
wenzelm@33245
   185
                let val (v, env''') = mk_var env'' Ts propT
berghofe@11613
   186
                in add_cnstrt Ts v (prf1 %% prf2) (cnstrts' @ cnstrts)
berghofe@13669
   187
                  env''' vTs'' (t, Logic.mk_implies (u, v))
berghofe@11522
   188
                end)
berghofe@11522
   189
          end
skalberg@15531
   190
      | mk_cnstrts env Ts Hs vTs (cprf % SOME t) =
wenzelm@62922
   191
          let val (t', U, vTs1, env1) = infer_type ctxt env Ts vTs t
berghofe@13669
   192
          in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of
wenzelm@56245
   193
             (Const ("Pure.all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
berghofe@13669
   194
                 prf, cnstrts, env2, vTs2) =>
wenzelm@62922
   195
               let val env3 = unifyT ctxt env2 T U
skalberg@15531
   196
               in (betapply (f, t'), prf % SOME t', cnstrts, env3, vTs2)
berghofe@11522
   197
               end
berghofe@13669
   198
           | (u, prf, cnstrts, env2, vTs2) =>
wenzelm@33245
   199
               let val (v, env3) = mk_var env2 Ts (U --> propT);
berghofe@11522
   200
               in
skalberg@15531
   201
                 add_cnstrt Ts (v $ t') (prf % SOME t') cnstrts env3 vTs2
wenzelm@56245
   202
                   (u, Const ("Pure.all", (U --> propT) --> propT) $ v)
berghofe@11522
   203
               end)
berghofe@11522
   204
          end
skalberg@15531
   205
      | mk_cnstrts env Ts Hs vTs (cprf % NONE) =
berghofe@13669
   206
          (case head_norm (mk_cnstrts env Ts Hs vTs cprf) of
wenzelm@56245
   207
             (Const ("Pure.all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
berghofe@13669
   208
                 prf, cnstrts, env', vTs') =>
wenzelm@33245
   209
               let val (t, env'') = mk_var env' Ts T
skalberg@15531
   210
               in (betapply (f, t), prf % SOME t, cnstrts, env'', vTs')
berghofe@11522
   211
               end
berghofe@13669
   212
           | (u, prf, cnstrts, env', vTs') =>
berghofe@11522
   213
               let
wenzelm@33245
   214
                 val (T, env1) = mk_tvar [] env';
wenzelm@33245
   215
                 val (v, env2) = mk_var env1 Ts (T --> propT);
wenzelm@33245
   216
                 val (t, env3) = mk_var env2 Ts T
berghofe@11522
   217
               in
skalberg@15531
   218
                 add_cnstrt Ts (v $ t) (prf % SOME t) cnstrts env3 vTs'
wenzelm@56245
   219
                   (u, Const ("Pure.all", (T --> propT) --> propT) $ v)
berghofe@11522
   220
               end)
wenzelm@28808
   221
      | mk_cnstrts env _ _ vTs (prf as PThm (_, ((_, prop, opTs), _))) =
berghofe@13669
   222
          mk_cnstrts_atom env vTs prop opTs prf
berghofe@13669
   223
      | mk_cnstrts env _ _ vTs (prf as PAxm (_, prop, opTs)) =
berghofe@13669
   224
          mk_cnstrts_atom env vTs prop opTs prf
wenzelm@31943
   225
      | mk_cnstrts env _ _ vTs (prf as OfClass (T, c)) =
wenzelm@31943
   226
          mk_cnstrts_atom env vTs (Logic.mk_of_class (T, c)) NONE prf
berghofe@13669
   227
      | mk_cnstrts env _ _ vTs (prf as Oracle (_, prop, opTs)) =
berghofe@13669
   228
          mk_cnstrts_atom env vTs prop opTs prf
berghofe@13669
   229
      | mk_cnstrts env _ _ vTs (Hyp t) = (t, Hyp t, [], env, vTs)
berghofe@11613
   230
      | mk_cnstrts _ _ _ _ _ = error "reconstruct_proof: minimal proof object"
berghofe@13669
   231
  in mk_cnstrts env [] [] Symtab.empty cprf end;
berghofe@11522
   232
berghofe@11522
   233
berghofe@13669
   234
(**** update list of free variables of constraints ****)
berghofe@11522
   235
berghofe@11522
   236
fun upd_constrs env cs =
berghofe@11522
   237
  let
wenzelm@32032
   238
    val tenv = Envir.term_env env;
wenzelm@32032
   239
    val tyenv = Envir.type_env env;
wenzelm@20675
   240
    val dom = []
wenzelm@32032
   241
      |> Vartab.fold (cons o #1) tenv
wenzelm@32032
   242
      |> Vartab.fold (cons o #1) tyenv;
wenzelm@20675
   243
    val vran = []
wenzelm@32032
   244
      |> Vartab.fold (Term.add_var_names o #2 o #2) tenv
wenzelm@32032
   245
      |> Vartab.fold (Term.add_tvar_namesT o #2 o #2) tyenv;
berghofe@11522
   246
    fun check_cs [] = []
wenzelm@32032
   247
      | check_cs ((u, p, vs) :: ps) =
wenzelm@32032
   248
          let val vs' = subtract (op =) dom vs in
wenzelm@32032
   249
            if vs = vs' then (u, p, vs) :: check_cs ps
wenzelm@32032
   250
            else (true, p, fold (insert op =) vs' vran) :: check_cs ps
wenzelm@32032
   251
          end;
berghofe@11522
   252
  in check_cs cs end;
berghofe@11522
   253
wenzelm@32032
   254
berghofe@13669
   255
(**** solution of constraints ****)
berghofe@11522
   256
berghofe@11522
   257
fun solve _ [] bigenv = bigenv
wenzelm@62922
   258
  | solve ctxt cs bigenv =
berghofe@11522
   259
      let
berghofe@11660
   260
        fun search env [] = error ("Unsolvable constraints:\n" ^
berghofe@11660
   261
              Pretty.string_of (Pretty.chunks (map (fn (_, p, _) =>
wenzelm@62922
   262
                Thm.pretty_flexpair ctxt (apply2 (Envir.norm_term bigenv) p)) cs)))
berghofe@11522
   263
          | search env ((u, p as (t1, t2), vs)::ps) =
berghofe@11522
   264
              if u then
berghofe@11522
   265
                let
berghofe@11522
   266
                  val tn1 = Envir.norm_term bigenv t1;
berghofe@11522
   267
                  val tn2 = Envir.norm_term bigenv t2
berghofe@11522
   268
                in
berghofe@11522
   269
                  if Pattern.pattern tn1 andalso Pattern.pattern tn2 then
wenzelm@62922
   270
                    (Pattern.unify (Context.Proof ctxt) (tn1, tn2) env, ps) handle Pattern.Unif =>
wenzelm@62922
   271
                       cantunify ctxt (tn1, tn2)
berghofe@11522
   272
                  else
wenzelm@62922
   273
                    let val (cs', env') = decompose ctxt [] (tn1, tn2) env
berghofe@11522
   274
                    in if cs' = [(tn1, tn2)] then
berghofe@11522
   275
                         apsnd (cons (false, (tn1, tn2), vs)) (search env ps)
berghofe@11522
   276
                       else search env' (map (fn q => (true, q, vs)) cs' @ ps)
berghofe@11522
   277
                    end
berghofe@11522
   278
                end
berghofe@11522
   279
              else apsnd (cons (false, p, vs)) (search env ps);
berghofe@11522
   280
        val Envir.Envir {maxidx, ...} = bigenv;
berghofe@11522
   281
        val (env, cs') = search (Envir.empty maxidx) cs;
berghofe@11522
   282
      in
wenzelm@62922
   283
        solve ctxt (upd_constrs env cs') (Envir.merge (bigenv, env))
berghofe@11522
   284
      end;
berghofe@11522
   285
berghofe@11522
   286
berghofe@13669
   287
(**** reconstruction of proofs ****)
berghofe@11522
   288
wenzelm@62922
   289
fun reconstruct_proof ctxt prop cprf =
berghofe@11522
   290
  let
wenzelm@37310
   291
    val (cprf' % SOME prop', thawf) = Proofterm.freeze_thaw_prf (cprf % SOME prop);
wenzelm@62922
   292
    val _ = message ctxt (fn _ => "Collecting constraints ...");
wenzelm@62922
   293
    val (t, prf, cs, env, _) = make_constraints_cprf ctxt
wenzelm@37310
   294
      (Envir.empty (Proofterm.maxidx_proof cprf ~1)) cprf';
wenzelm@44119
   295
    val cs' =
wenzelm@59058
   296
      map (apply2 (Envir.norm_term env)) ((t, prop') :: cs)
wenzelm@44119
   297
      |> map (fn p => (true, p, Term.add_var_names (#1 p) (Term.add_var_names (#2 p) [])));
wenzelm@62922
   298
    val _ =
wenzelm@62922
   299
      message ctxt
wenzelm@62922
   300
        (fn () => "Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
wenzelm@62922
   301
    val env' = solve ctxt cs' env
berghofe@11522
   302
  in
wenzelm@37310
   303
    thawf (Proofterm.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
wenzelm@63616
   310
val head_norm = Envir.head_norm Envir.init;
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@46217
   314
      Logic.all_const 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
wenzelm@56245
   318
      Const ("Pure.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
wenzelm@56245
   321
      Const ("Pure.imp", _) $ 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
wenzelm@62922
   333
fun proof_of ctxt raw_thm =
wenzelm@62922
   334
  let val thm = Thm.transfer (Proof_Context.theory_of ctxt) raw_thm
wenzelm@62922
   335
  in reconstruct_proof ctxt (Thm.prop_of thm) (Thm.proof_of thm) end;
wenzelm@44060
   336
wenzelm@44060
   337
berghofe@11522
   338
berghofe@13669
   339
(**** expand and reconstruct subproofs ****)
berghofe@11522
   340
wenzelm@62922
   341
fun expand_proof ctxt thms prf =
berghofe@11522
   342
  let
wenzelm@23178
   343
    fun expand maxidx prfs (AbsP (s, t, prf)) =
berghofe@12870
   344
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   345
          in (maxidx', prfs', AbsP (s, t, prf')) end
wenzelm@23178
   346
      | expand maxidx prfs (Abst (s, T, prf)) =
berghofe@12870
   347
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   348
          in (maxidx', prfs', Abst (s, T, prf')) end
berghofe@12870
   349
      | expand maxidx prfs (prf1 %% prf2) =
berghofe@11522
   350
          let
berghofe@12870
   351
            val (maxidx', prfs', prf1') = expand maxidx prfs prf1;
berghofe@12870
   352
            val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2;
berghofe@12870
   353
          in (maxidx'', prfs'', prf1' %% prf2') end
berghofe@12870
   354
      | expand maxidx prfs (prf % t) =
berghofe@12870
   355
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   356
          in (maxidx', prfs', prf' % t) end
wenzelm@28808
   357
      | expand maxidx prfs (prf as PThm (_, ((a, prop, SOME Ts), body))) =
berghofe@13342
   358
          if not (exists
skalberg@15531
   359
            (fn (b, NONE) => a = b
skalberg@15531
   360
              | (b, SOME prop') => a = b andalso prop = prop') thms)
berghofe@13342
   361
          then (maxidx, prfs, prf) else
berghofe@11522
   362
          let
wenzelm@16862
   363
            val (maxidx', prf, prfs') =
haftmann@17232
   364
              (case AList.lookup (op =) prfs (a, prop) of
skalberg@15531
   365
                NONE =>
berghofe@11522
   366
                  let
wenzelm@44059
   367
                    val _ =
wenzelm@62922
   368
                      message ctxt (fn () =>
wenzelm@62922
   369
                        "Reconstructing proof of " ^ a ^ "\n" ^ Syntax.string_of_term ctxt prop);
berghofe@13610
   370
                    val prf' = forall_intr_vfs_prf prop
wenzelm@62922
   371
                      (reconstruct_proof ctxt prop (Proofterm.join_proof body));
berghofe@12870
   372
                    val (maxidx', prfs', prf) = expand
wenzelm@37310
   373
                      (Proofterm.maxidx_proof prf' ~1) prfs prf'
wenzelm@37310
   374
                  in (maxidx' + maxidx + 1, Proofterm.incr_indexes (maxidx + 1) prf,
berghofe@13610
   375
                    ((a, prop), (maxidx', prf)) :: prfs')
berghofe@13610
   376
                  end
skalberg@15531
   377
              | SOME (maxidx', prf) => (maxidx' + maxidx + 1,
wenzelm@37310
   378
                  Proofterm.incr_indexes (maxidx + 1) prf, prfs));
krauss@36042
   379
            val tfrees = Term.add_tfrees prop [] |> rev;
berghofe@13669
   380
            val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j))
krauss@36042
   381
              (Term.add_tvars prop [] |> rev) @ map (rpair ~1 o fst) tfrees ~~ Ts;
berghofe@13669
   382
            val varify = map_type_tfree (fn p as (a, S) =>
wenzelm@20675
   383
              if member (op =) tfrees p then TVar ((a, ~1), S) else TFree p)
berghofe@11522
   384
          in
wenzelm@37310
   385
            (maxidx', prfs', Proofterm.map_proof_types (typ_subst_TVars tye o varify) prf)
berghofe@11522
   386
          end
berghofe@12870
   387
      | expand maxidx prfs prf = (maxidx, prfs, prf);
berghofe@11522
   388
wenzelm@37310
   389
  in #3 (expand (Proofterm.maxidx_proof prf ~1) [] prf) end;
berghofe@11522
   390
berghofe@11522
   391
end;