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