src/Pure/Proof/reconstruct.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (23 months ago)
changeset 66695 91500c024c7f
parent 64986 b81a048960a3
child 67649 1e1782c1aedf
permissions -rw-r--r--
tuned;
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
wenzelm@64986
    16
  val clean_proof_of : Proof.context -> bool -> thm -> Proofterm.proof
berghofe@11522
    17
end;
berghofe@11522
    18
berghofe@11522
    19
structure Reconstruct : RECONSTRUCT =
berghofe@11522
    20
struct
berghofe@11522
    21
wenzelm@62922
    22
val quiet_mode =
wenzelm@64556
    23
  Config.bool (Config.declare ("Reconstruct.quiet_mode", \<^here>) (K (Config.Bool true)));
wenzelm@62922
    24
wenzelm@62922
    25
fun message ctxt msg =
wenzelm@62922
    26
  if Config.get ctxt quiet_mode then () else writeln (msg ());
berghofe@11522
    27
berghofe@28813
    28
fun vars_of t = map Var (rev (Term.add_vars t []));
berghofe@28813
    29
fun frees_of t = map Free (rev (Term.add_frees t []));
berghofe@11522
    30
wenzelm@27330
    31
fun forall_intr_vfs prop = fold_rev Logic.all
berghofe@28813
    32
  (vars_of prop @ frees_of prop) prop;
berghofe@11522
    33
wenzelm@37310
    34
fun forall_intr_vfs_prf prop prf = fold_rev Proofterm.forall_intr_proof'
berghofe@28813
    35
  (vars_of prop @ frees_of prop) prf;
berghofe@12001
    36
berghofe@11522
    37
berghofe@13669
    38
(**** generate constraints for proof term ****)
berghofe@11522
    39
wenzelm@23178
    40
fun mk_var env Ts T =
berghofe@11522
    41
  let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T)
wenzelm@33245
    42
  in (list_comb (v, map Bound (length Ts - 1 downto 0)), env') end;
berghofe@11522
    43
wenzelm@33245
    44
fun mk_tvar S (Envir.Envir {maxidx, tenv, tyenv}) =
wenzelm@33245
    45
  (TVar (("'t", maxidx + 1), S),
wenzelm@33245
    46
    Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv});
berghofe@11522
    47
wenzelm@19473
    48
val mk_abs = fold (fn T => fn u => Abs ("", T, u));
berghofe@11522
    49
wenzelm@62922
    50
fun unifyT ctxt env T U =
berghofe@11522
    51
  let
wenzelm@32032
    52
    val Envir.Envir {maxidx, tenv, tyenv} = env;
wenzelm@62922
    53
    val (tyenv', maxidx') = Sign.typ_unify (Proof_Context.theory_of ctxt) (T, U) (tyenv, maxidx);
wenzelm@32032
    54
  in Envir.Envir {maxidx = maxidx', tenv = tenv, tyenv = tyenv'} end;
berghofe@11522
    55
wenzelm@32032
    56
fun chaseT env (T as TVar v) =
wenzelm@32032
    57
      (case Type.lookup (Envir.type_env env) v of
wenzelm@32032
    58
        NONE => T
wenzelm@32032
    59
      | SOME T' => chaseT env T')
berghofe@13669
    60
  | chaseT _ T = T;
berghofe@13669
    61
wenzelm@62922
    62
fun infer_type ctxt (env as Envir.Envir {maxidx, tenv, tyenv}) Ts vTs
wenzelm@32032
    63
      (t as Const (s, T)) = if T = dummyT then
wenzelm@62922
    64
        (case Sign.const_type (Proof_Context.theory_of ctxt) s of
skalberg@15531
    65
          NONE => error ("reconstruct_proof: No such constant: " ^ quote s)
wenzelm@23178
    66
        | SOME T =>
wenzelm@19419
    67
            let val T' = Type.strip_sorts (Logic.incr_tvar (maxidx + 1) T)
berghofe@13669
    68
            in (Const (s, T'), T', vTs,
wenzelm@32032
    69
              Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv})
berghofe@13669
    70
            end)
berghofe@13669
    71
      else (t, T, vTs, env)
wenzelm@62922
    72
  | infer_type ctxt env Ts vTs (t as Free (s, T)) =
wenzelm@17412
    73
      if T = dummyT then (case Symtab.lookup vTs s of
skalberg@15531
    74
          NONE =>
wenzelm@33245
    75
            let val (T, env') = mk_tvar [] env
wenzelm@17412
    76
            in (Free (s, T), T, Symtab.update_new (s, T) vTs, env') end
skalberg@15531
    77
        | SOME T => (Free (s, T), T, vTs, env))
berghofe@13669
    78
      else (t, T, vTs, env)
wenzelm@62922
    79
  | infer_type ctxt env Ts vTs (Var _) = error "reconstruct_proof: internal error"
wenzelm@62922
    80
  | infer_type ctxt env Ts vTs (Abs (s, T, t)) =
berghofe@13669
    81
      let
wenzelm@33245
    82
        val (T', env') = if T = dummyT then mk_tvar [] env else (T, env);
wenzelm@62922
    83
        val (t', U, vTs', env'') = infer_type ctxt env' (T' :: Ts) vTs t
berghofe@13669
    84
      in (Abs (s, T', t'), T' --> U, vTs', env'') end
wenzelm@62922
    85
  | infer_type ctxt env Ts vTs (t $ u) =
berghofe@13669
    86
      let
wenzelm@62922
    87
        val (t', T, vTs1, env1) = infer_type ctxt env Ts vTs t;
wenzelm@62922
    88
        val (u', U, vTs2, env2) = infer_type ctxt env1 Ts vTs1 u;
berghofe@13669
    89
      in (case chaseT env2 T of
wenzelm@62922
    90
          Type ("fun", [U', V]) => (t' $ u', V, vTs2, unifyT ctxt env2 U U')
berghofe@13669
    91
        | _ =>
wenzelm@33245
    92
          let val (V, env3) = mk_tvar [] env2
wenzelm@62922
    93
          in (t' $ u', V, vTs2, unifyT ctxt env3 T (U --> V)) end)
berghofe@13669
    94
      end
wenzelm@62922
    95
  | infer_type ctxt env Ts vTs (t as Bound i) = ((t, nth Ts i, vTs, env)
wenzelm@43278
    96
      handle General.Subscript => error ("infer_type: bad variable index " ^ string_of_int i));
berghofe@13669
    97
wenzelm@62922
    98
fun cantunify ctxt (t, u) =
wenzelm@62922
    99
  error ("Non-unifiable terms:\n" ^
wenzelm@62922
   100
    Syntax.string_of_term ctxt t ^ "\n\n" ^ Syntax.string_of_term ctxt u);
berghofe@11522
   101
wenzelm@62922
   102
fun decompose ctxt Ts (p as (t, u)) env =
wenzelm@33245
   103
  let
wenzelm@33245
   104
    fun rigrig (a, T) (b, U) uT ts us =
wenzelm@62922
   105
      if a <> b then cantunify ctxt p
wenzelm@62922
   106
      else apfst flat (fold_map (decompose ctxt Ts) (ts ~~ us) (uT env T U))
wenzelm@33245
   107
  in
wenzelm@59058
   108
    case apply2 (strip_comb o Envir.head_norm env) p of
wenzelm@62922
   109
      ((Const c, ts), (Const d, us)) => rigrig c d (unifyT ctxt) ts us
wenzelm@62922
   110
    | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT ctxt) 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@62922
   114
        decompose ctxt (T::Ts) (t, u) (unifyT ctxt env T U)
berghofe@13715
   115
    | ((Abs (_, T, t), []), _) =>
wenzelm@62922
   116
        decompose ctxt (T::Ts) (t, incr_boundvars 1 u $ Bound 0) env
berghofe@13715
   117
    | (_, (Abs (_, T, u), [])) =>
wenzelm@62922
   118
        decompose ctxt (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@62922
   122
fun make_constraints_cprf ctxt 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@62922
   129
        (prop, prf, cs, Pattern.unify (Context.Proof ctxt) (t', u') env, vTs)
berghofe@12236
   130
        handle Pattern.Pattern =>
wenzelm@62922
   131
            let val (cs', env') = decompose ctxt [] (t', u') env
berghofe@13669
   132
            in (prop, prf, cs @ cs', env', vTs) end
berghofe@12236
   133
        | Pattern.Unif =>
wenzelm@62922
   134
            cantunify ctxt (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)
wenzelm@40722
   146
              (forall_intr_vfs prop) handle ListPair.UnequalLengths =>
wenzelm@37310
   147
                error ("Wrong number of type arguments for " ^ quote (Proofterm.guess_name prf))
wenzelm@37310
   148
          in (prop', Proofterm.change_type (SOME Ts) prf, [], env', vTs) end;
berghofe@11522
   149
berghofe@13669
   150
    fun head_norm (prop, prf, cnstrts, env, vTs) =
berghofe@13669
   151
      (Envir.head_norm env prop, prf, cnstrts, env, vTs);
wenzelm@23178
   152
wenzelm@30146
   153
    fun mk_cnstrts env _ Hs vTs (PBound i) = ((nth Hs i, PBound i, [], env, vTs)
wenzelm@43278
   154
          handle General.Subscript => error ("mk_cnstrts: bad variable index " ^ string_of_int i))
berghofe@13669
   155
      | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) =
berghofe@13669
   156
          let
wenzelm@33245
   157
            val (T, env') =
wenzelm@33245
   158
              (case opT of
wenzelm@33245
   159
                NONE => mk_tvar [] env
wenzelm@33245
   160
              | SOME T => (T, env));
berghofe@13669
   161
            val (t, prf, cnstrts, env'', vTs') =
berghofe@13669
   162
              mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf;
wenzelm@62922
   163
          in
wenzelm@62922
   164
            (Const ("Pure.all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, SOME T, prf),
wenzelm@62922
   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@62922
   169
            val (t', _, vTs', env') = infer_type ctxt 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
wenzelm@56245
   182
              (Const ("Pure.imp", _) $ 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@62922
   192
          let val (t', U, vTs1, env1) = infer_type ctxt env Ts vTs t
berghofe@13669
   193
          in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of
wenzelm@56245
   194
             (Const ("Pure.all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
berghofe@13669
   195
                 prf, cnstrts, env2, vTs2) =>
wenzelm@62922
   196
               let val env3 = unifyT ctxt 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
wenzelm@56245
   203
                   (u, Const ("Pure.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
wenzelm@56245
   208
             (Const ("Pure.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'
wenzelm@56245
   220
                   (u, Const ("Pure.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@62922
   259
  | solve ctxt 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@62922
   263
                Thm.pretty_flexpair ctxt (apply2 (Envir.norm_term bigenv) p)) cs)))
berghofe@11522
   264
          | search env ((u, p as (t1, t2), vs)::ps) =
berghofe@11522
   265
              if u then
berghofe@11522
   266
                let
berghofe@11522
   267
                  val tn1 = Envir.norm_term bigenv t1;
berghofe@11522
   268
                  val tn2 = Envir.norm_term bigenv t2
berghofe@11522
   269
                in
berghofe@11522
   270
                  if Pattern.pattern tn1 andalso Pattern.pattern tn2 then
wenzelm@62922
   271
                    (Pattern.unify (Context.Proof ctxt) (tn1, tn2) env, ps) handle Pattern.Unif =>
wenzelm@62922
   272
                       cantunify ctxt (tn1, tn2)
berghofe@11522
   273
                  else
wenzelm@62922
   274
                    let val (cs', env') = decompose ctxt [] (tn1, tn2) env
berghofe@11522
   275
                    in if cs' = [(tn1, tn2)] then
berghofe@11522
   276
                         apsnd (cons (false, (tn1, tn2), vs)) (search env ps)
berghofe@11522
   277
                       else search env' (map (fn q => (true, q, vs)) cs' @ ps)
berghofe@11522
   278
                    end
berghofe@11522
   279
                end
berghofe@11522
   280
              else apsnd (cons (false, p, vs)) (search env ps);
berghofe@11522
   281
        val Envir.Envir {maxidx, ...} = bigenv;
berghofe@11522
   282
        val (env, cs') = search (Envir.empty maxidx) cs;
berghofe@11522
   283
      in
wenzelm@62922
   284
        solve ctxt (upd_constrs env cs') (Envir.merge (bigenv, env))
berghofe@11522
   285
      end;
berghofe@11522
   286
berghofe@11522
   287
berghofe@13669
   288
(**** reconstruction of proofs ****)
berghofe@11522
   289
wenzelm@62922
   290
fun reconstruct_proof ctxt prop cprf =
berghofe@11522
   291
  let
wenzelm@37310
   292
    val (cprf' % SOME prop', thawf) = Proofterm.freeze_thaw_prf (cprf % SOME prop);
wenzelm@62922
   293
    val _ = message ctxt (fn _ => "Collecting constraints ...");
wenzelm@62922
   294
    val (t, prf, cs, env, _) = make_constraints_cprf ctxt
wenzelm@37310
   295
      (Envir.empty (Proofterm.maxidx_proof cprf ~1)) cprf';
wenzelm@44119
   296
    val cs' =
wenzelm@59058
   297
      map (apply2 (Envir.norm_term env)) ((t, prop') :: cs)
wenzelm@44119
   298
      |> map (fn p => (true, p, Term.add_var_names (#1 p) (Term.add_var_names (#2 p) [])));
wenzelm@62922
   299
    val _ =
wenzelm@62922
   300
      message ctxt
wenzelm@62922
   301
        (fn () => "Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
wenzelm@62922
   302
    val env' = solve ctxt cs' env
berghofe@11522
   303
  in
wenzelm@37310
   304
    thawf (Proofterm.norm_proof env' prf)
berghofe@11522
   305
  end;
berghofe@11522
   306
berghofe@28813
   307
fun prop_of_atom prop Ts = subst_atomic_types
krauss@36042
   308
  (map TVar (Term.add_tvars prop [] |> rev) @ map TFree (Term.add_tfrees prop [] |> rev) ~~ Ts)
berghofe@28813
   309
  (forall_intr_vfs prop);
berghofe@13138
   310
wenzelm@63616
   311
val head_norm = Envir.head_norm Envir.init;
berghofe@13734
   312
wenzelm@30146
   313
fun prop_of0 Hs (PBound i) = nth Hs i
skalberg@15531
   314
  | prop_of0 Hs (Abst (s, SOME T, prf)) =
wenzelm@46217
   315
      Logic.all_const T $ (Abs (s, T, prop_of0 Hs prf))
skalberg@15531
   316
  | prop_of0 Hs (AbsP (s, SOME t, prf)) =
berghofe@13734
   317
      Logic.mk_implies (t, prop_of0 (t :: Hs) prf)
skalberg@15531
   318
  | prop_of0 Hs (prf % SOME t) = (case head_norm (prop_of0 Hs prf) of
wenzelm@56245
   319
      Const ("Pure.all", _) $ f => f $ t
berghofe@13256
   320
    | _ => error "prop_of: all expected")
berghofe@13734
   321
  | prop_of0 Hs (prf1 %% prf2) = (case head_norm (prop_of0 Hs prf1) of
wenzelm@56245
   322
      Const ("Pure.imp", _) $ P $ Q => Q
berghofe@13256
   323
    | _ => error "prop_of: ==> expected")
berghofe@13734
   324
  | prop_of0 Hs (Hyp t) = t
wenzelm@28808
   325
  | prop_of0 Hs (PThm (_, ((_, prop, SOME Ts), _))) = prop_of_atom prop Ts
skalberg@15531
   326
  | prop_of0 Hs (PAxm (_, prop, SOME Ts)) = prop_of_atom prop Ts
wenzelm@31943
   327
  | prop_of0 Hs (OfClass (T, c)) = Logic.mk_of_class (T, c)
skalberg@15531
   328
  | prop_of0 Hs (Oracle (_, prop, SOME Ts)) = prop_of_atom prop Ts
berghofe@13734
   329
  | prop_of0 _ _ = error "prop_of: partial proof object";
berghofe@13138
   330
wenzelm@18929
   331
val prop_of' = Envir.beta_eta_contract oo prop_of0;
berghofe@13256
   332
val prop_of = prop_of' [];
berghofe@13138
   333
wenzelm@62922
   334
fun proof_of ctxt raw_thm =
wenzelm@62922
   335
  let val thm = Thm.transfer (Proof_Context.theory_of ctxt) raw_thm
wenzelm@62922
   336
  in reconstruct_proof ctxt (Thm.prop_of thm) (Thm.proof_of thm) end;
wenzelm@44060
   337
wenzelm@44060
   338
berghofe@11522
   339
berghofe@13669
   340
(**** expand and reconstruct subproofs ****)
berghofe@11522
   341
wenzelm@62922
   342
fun expand_proof ctxt thms prf =
berghofe@11522
   343
  let
wenzelm@23178
   344
    fun expand maxidx prfs (AbsP (s, t, prf)) =
berghofe@12870
   345
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   346
          in (maxidx', prfs', AbsP (s, t, prf')) end
wenzelm@23178
   347
      | expand maxidx prfs (Abst (s, T, prf)) =
berghofe@12870
   348
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   349
          in (maxidx', prfs', Abst (s, T, prf')) end
berghofe@12870
   350
      | expand maxidx prfs (prf1 %% prf2) =
berghofe@11522
   351
          let
berghofe@12870
   352
            val (maxidx', prfs', prf1') = expand maxidx prfs prf1;
berghofe@12870
   353
            val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2;
berghofe@12870
   354
          in (maxidx'', prfs'', prf1' %% prf2') end
berghofe@12870
   355
      | expand maxidx prfs (prf % t) =
berghofe@12870
   356
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   357
          in (maxidx', prfs', prf' % t) end
wenzelm@28808
   358
      | expand maxidx prfs (prf as PThm (_, ((a, prop, SOME Ts), body))) =
berghofe@13342
   359
          if not (exists
skalberg@15531
   360
            (fn (b, NONE) => a = b
skalberg@15531
   361
              | (b, SOME prop') => a = b andalso prop = prop') thms)
berghofe@13342
   362
          then (maxidx, prfs, prf) else
berghofe@11522
   363
          let
wenzelm@16862
   364
            val (maxidx', prf, prfs') =
haftmann@17232
   365
              (case AList.lookup (op =) prfs (a, prop) of
skalberg@15531
   366
                NONE =>
berghofe@11522
   367
                  let
wenzelm@44059
   368
                    val _ =
wenzelm@62922
   369
                      message ctxt (fn () =>
wenzelm@62922
   370
                        "Reconstructing proof of " ^ a ^ "\n" ^ Syntax.string_of_term ctxt prop);
berghofe@13610
   371
                    val prf' = forall_intr_vfs_prf prop
wenzelm@62922
   372
                      (reconstruct_proof ctxt prop (Proofterm.join_proof body));
berghofe@12870
   373
                    val (maxidx', prfs', prf) = expand
wenzelm@37310
   374
                      (Proofterm.maxidx_proof prf' ~1) prfs prf'
wenzelm@37310
   375
                  in (maxidx' + maxidx + 1, Proofterm.incr_indexes (maxidx + 1) prf,
berghofe@13610
   376
                    ((a, prop), (maxidx', prf)) :: prfs')
berghofe@13610
   377
                  end
skalberg@15531
   378
              | SOME (maxidx', prf) => (maxidx' + maxidx + 1,
wenzelm@37310
   379
                  Proofterm.incr_indexes (maxidx + 1) prf, prfs));
krauss@36042
   380
            val tfrees = Term.add_tfrees prop [] |> rev;
berghofe@13669
   381
            val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j))
krauss@36042
   382
              (Term.add_tvars prop [] |> rev) @ map (rpair ~1 o fst) tfrees ~~ Ts;
berghofe@13669
   383
            val varify = map_type_tfree (fn p as (a, S) =>
wenzelm@20675
   384
              if member (op =) tfrees p then TVar ((a, ~1), S) else TFree p)
berghofe@11522
   385
          in
wenzelm@37310
   386
            (maxidx', prfs', Proofterm.map_proof_types (typ_subst_TVars tye o varify) prf)
berghofe@11522
   387
          end
berghofe@12870
   388
      | expand maxidx prfs prf = (maxidx, prfs, prf);
berghofe@11522
   389
wenzelm@37310
   390
  in #3 (expand (Proofterm.maxidx_proof prf ~1) [] prf) end;
berghofe@11522
   391
wenzelm@64986
   392
wenzelm@64986
   393
(* cleanup for output etc. *)
wenzelm@64986
   394
wenzelm@64986
   395
fun clean_proof_of ctxt full thm =
wenzelm@64986
   396
  let
wenzelm@64986
   397
    val (_, prop) =
wenzelm@64986
   398
      Logic.unconstrainT (Thm.shyps_of thm)
wenzelm@64986
   399
        (Logic.list_implies (Thm.hyps_of thm, Thm.prop_of thm));
wenzelm@64986
   400
  in
wenzelm@64986
   401
    Proofterm.proof_of (Proofterm.strip_thm (Thm.proof_body_of thm))
wenzelm@64986
   402
    |> reconstruct_proof ctxt prop
wenzelm@64986
   403
    |> expand_proof ctxt [("", NONE)]
wenzelm@64986
   404
    |> Proofterm.rew_proof (Proof_Context.theory_of ctxt)
wenzelm@64986
   405
    |> Proofterm.no_thm_proofs
wenzelm@64986
   406
    |> not full ? Proofterm.shrink_proof
wenzelm@64986
   407
  end;
wenzelm@64986
   408
berghofe@11522
   409
end;