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