src/Pure/Proof/reconstruct.ML
author wenzelm
Tue May 31 11:53:40 2005 +0200 (2005-05-31)
changeset 16149 d8cac577493c
parent 15798 016f3be5a5ec
child 16787 b6b6e2faaa41
permissions -rw-r--r--
Theory.restore_naming;
tuned fold;
berghofe@11522
     1
(*  Title:      Pure/Proof/reconstruct.ML
berghofe@11522
     2
    ID:         $Id$
wenzelm@11539
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@11522
     4
berghofe@11522
     5
Reconstruction of partial proof terms.
berghofe@11522
     6
*)
berghofe@11522
     7
berghofe@11522
     8
signature RECONSTRUCT =
berghofe@11522
     9
sig
berghofe@11522
    10
  val quiet_mode : bool ref
berghofe@11613
    11
  val reconstruct_proof : Sign.sg -> term -> Proofterm.proof -> Proofterm.proof
berghofe@13256
    12
  val prop_of' : term list -> Proofterm.proof -> term
berghofe@13138
    13
  val prop_of : Proofterm.proof -> term
berghofe@13342
    14
  val expand_proof : Sign.sg -> (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
berghofe@11522
    21
open Proofterm;
berghofe@11522
    22
berghofe@11522
    23
val quiet_mode = ref true;
berghofe@11522
    24
fun message s = if !quiet_mode then () else writeln s;
berghofe@11522
    25
berghofe@11522
    26
fun vars_of t = rev (foldl_aterms
berghofe@11522
    27
  (fn (vs, v as Var _) => v ins vs | (vs, _) => vs) ([], t));
berghofe@11522
    28
berghofe@11522
    29
fun forall_intr (t, prop) =
berghofe@11522
    30
  let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p)
berghofe@11522
    31
  in all T $ Abs (a, T, abstract_over (t, prop)) end;
berghofe@11522
    32
skalberg@15574
    33
fun forall_intr_vfs prop = foldr forall_intr prop
skalberg@15574
    34
  (vars_of prop @ sort (make_ord atless) (term_frees prop));
berghofe@11522
    35
berghofe@12001
    36
fun forall_intr_prf (t, prf) =
berghofe@12001
    37
  let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p)
skalberg@15531
    38
  in Abst (a, SOME T, prf_abstract_over t prf) end;
berghofe@12001
    39
skalberg@15574
    40
fun forall_intr_vfs_prf prop prf = foldr forall_intr_prf prf
skalberg@15574
    41
  (vars_of prop @ sort (make_ord atless) (term_frees prop));
berghofe@12001
    42
berghofe@11522
    43
fun merge_envs (Envir.Envir {asol=asol1, iTs=iTs1, maxidx=maxidx1})
berghofe@11522
    44
  (Envir.Envir {asol=asol2, iTs=iTs2, maxidx=maxidx2}) =
berghofe@15798
    45
    Envir.Envir {asol=Vartab.merge (op =) (asol1, asol2),
berghofe@11522
    46
                 iTs=Vartab.merge (op =) (iTs1, iTs2),
berghofe@11522
    47
                 maxidx=Int.max (maxidx1, maxidx2)};
berghofe@11522
    48
berghofe@11522
    49
berghofe@13669
    50
(**** generate constraints for proof term ****)
berghofe@11522
    51
berghofe@11522
    52
fun mk_var env Ts T = 
berghofe@11522
    53
  let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T)
berghofe@11522
    54
  in (env', list_comb (v, map Bound (length Ts - 1 downto 0))) end;
berghofe@11522
    55
berghofe@11522
    56
fun mk_tvar (Envir.Envir {iTs, asol, maxidx}, s) =
berghofe@11522
    57
  (Envir.Envir {iTs = iTs, asol = asol, maxidx = maxidx+1},
berghofe@11522
    58
   TVar (("'t", maxidx+1), s));
berghofe@11522
    59
skalberg@15570
    60
fun mk_abs Ts t = Library.foldl (fn (u, T) => Abs ("", T, u)) (t, Ts);
berghofe@11522
    61
berghofe@11522
    62
fun unifyT sg env T U =
berghofe@11522
    63
  let
berghofe@11522
    64
    val Envir.Envir {asol, iTs, maxidx} = env;
wenzelm@12527
    65
    val (iTs', maxidx') = Type.unify (Sign.tsig_of sg) (iTs, maxidx) (T, U)
berghofe@11660
    66
  in Envir.Envir {asol=asol, iTs=iTs', maxidx=maxidx'} end
berghofe@11660
    67
  handle Type.TUNIFY => error ("Non-unifiable types:\n" ^
berghofe@11660
    68
    Sign.string_of_typ sg T ^ "\n\n" ^ Sign.string_of_typ sg U);
berghofe@11522
    69
berghofe@15798
    70
fun chaseT (env as Envir.Envir {iTs, ...}) (T as TVar ixnS) =
berghofe@15798
    71
      (case Type.lookup (iTs, ixnS) of NONE => T | SOME T' => chaseT env T')
berghofe@13669
    72
  | chaseT _ T = T;
berghofe@13669
    73
berghofe@13669
    74
fun infer_type sg (env as Envir.Envir {maxidx, asol, iTs}) Ts vTs
berghofe@13669
    75
      (t as Const (s, T)) = if T = dummyT then (case Sign.const_type sg s of
skalberg@15531
    76
          NONE => error ("reconstruct_proof: No such constant: " ^ quote s)
skalberg@15531
    77
        | SOME T => 
berghofe@13669
    78
            let val T' = incr_tvar (maxidx + 1) T
berghofe@13669
    79
            in (Const (s, T'), T', vTs,
berghofe@13669
    80
              Envir.Envir {maxidx = maxidx + 1, asol = asol, iTs = iTs})
berghofe@13669
    81
            end)
berghofe@13669
    82
      else (t, T, vTs, env)
berghofe@13669
    83
  | infer_type sg env Ts vTs (t as Free (s, T)) =
berghofe@13669
    84
      if T = dummyT then (case Symtab.lookup (vTs, s) of
skalberg@15531
    85
          NONE =>
berghofe@13669
    86
            let val (env', T) = mk_tvar (env, [])
berghofe@13669
    87
            in (Free (s, T), T, Symtab.update_new ((s, T), vTs), env') end
skalberg@15531
    88
        | SOME T => (Free (s, T), T, vTs, env))
berghofe@13669
    89
      else (t, T, vTs, env)
berghofe@13669
    90
  | infer_type sg env Ts vTs (Var _) = error "reconstruct_proof: internal error"
berghofe@13669
    91
  | infer_type sg env Ts vTs (Abs (s, T, t)) =
berghofe@13669
    92
      let
berghofe@13669
    93
        val (env', T') = if T = dummyT then mk_tvar (env, []) else (env, T);
berghofe@13669
    94
        val (t', U, vTs', env'') = infer_type sg env' (T' :: Ts) vTs t
berghofe@13669
    95
      in (Abs (s, T', t'), T' --> U, vTs', env'') end
berghofe@13669
    96
  | infer_type sg env Ts vTs (t $ u) =
berghofe@13669
    97
      let
berghofe@13669
    98
        val (t', T, vTs1, env1) = infer_type sg env Ts vTs t;
berghofe@13669
    99
        val (u', U, vTs2, env2) = infer_type sg env1 Ts vTs1 u;
berghofe@13669
   100
      in (case chaseT env2 T of
berghofe@13669
   101
          Type ("fun", [U', V]) => (t' $ u', V, vTs2, unifyT sg env2 U U')
berghofe@13669
   102
        | _ =>
berghofe@13669
   103
          let val (env3, V) = mk_tvar (env2, [])
berghofe@13669
   104
          in (t' $ u', V, vTs2, unifyT sg env3 T (U --> V)) end)
berghofe@13669
   105
      end
skalberg@15570
   106
  | infer_type sg env Ts vTs (t as Bound i) = (t, List.nth (Ts, i), vTs, env);
berghofe@13669
   107
berghofe@13715
   108
fun cantunify sg (t, u) = error ("Non-unifiable terms:\n" ^
berghofe@13715
   109
  Sign.string_of_term sg t ^ "\n\n" ^ Sign.string_of_term sg u);
berghofe@11522
   110
berghofe@13715
   111
fun decompose sg Ts (env, p as (t, u)) =
berghofe@13715
   112
  let fun rigrig (a, T) (b, U) uT ts us = if a <> b then cantunify sg p
skalberg@15570
   113
    else apsnd List.concat (foldl_map (decompose sg Ts) (uT env T U, ts ~~ us))
berghofe@13715
   114
  in case pairself (strip_comb o Envir.head_norm env) p of
berghofe@13715
   115
      ((Const c, ts), (Const d, us)) => rigrig c d (unifyT sg) ts us
berghofe@13715
   116
    | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT sg) ts us
berghofe@13715
   117
    | ((Bound i, ts), (Bound j, us)) =>
berghofe@13715
   118
        rigrig (i, dummyT) (j, dummyT) (K o K) ts us
berghofe@13715
   119
    | ((Abs (_, T, t), []), (Abs (_, U, u), [])) =>
berghofe@13715
   120
        decompose sg (T::Ts) (unifyT sg env T U, (t, u))
berghofe@13715
   121
    | ((Abs (_, T, t), []), _) =>
berghofe@13715
   122
        decompose sg (T::Ts) (env, (t, incr_boundvars 1 u $ Bound 0))
berghofe@13715
   123
    | (_, (Abs (_, T, u), [])) =>
berghofe@13715
   124
        decompose sg (T::Ts) (env, (incr_boundvars 1 t $ Bound 0, u))
berghofe@13715
   125
    | _ => (env, [(mk_abs Ts t, mk_abs Ts u)])
berghofe@13715
   126
  end;
berghofe@11522
   127
berghofe@13669
   128
fun make_constraints_cprf sg env cprf =
berghofe@11522
   129
  let
berghofe@13669
   130
    fun add_cnstrt Ts prop prf cs env vTs (t, u) =
berghofe@11522
   131
      let
berghofe@11522
   132
        val t' = mk_abs Ts t;
berghofe@12236
   133
        val u' = mk_abs Ts u
berghofe@11522
   134
      in
berghofe@13669
   135
        (prop, prf, cs, Pattern.unify (sg, env, [(t', u')]), vTs)
berghofe@12236
   136
        handle Pattern.Pattern =>
berghofe@13715
   137
            let val (env', cs') = decompose sg [] (env, (t', u'))
berghofe@13669
   138
            in (prop, prf, cs @ cs', env', vTs) end
berghofe@12236
   139
        | Pattern.Unif =>
berghofe@13715
   140
            cantunify sg (Envir.norm_term env t', Envir.norm_term env u')
berghofe@11522
   141
      end;
berghofe@11522
   142
berghofe@13669
   143
    fun mk_cnstrts_atom env vTs prop opTs prf =
berghofe@11522
   144
          let
berghofe@11522
   145
            val tvars = term_tvars prop;
berghofe@13669
   146
            val tfrees = term_tfrees prop;
berghofe@13669
   147
            val (prop', fmap) = Type.varify (prop, []);
berghofe@13669
   148
            val (env', Ts) = (case opTs of
skalberg@15531
   149
                NONE => foldl_map mk_tvar (env, map snd tvars @ map snd tfrees)
skalberg@15531
   150
              | SOME Ts => (env, Ts));
berghofe@13669
   151
            val prop'' = subst_TVars (map fst tvars @ map snd fmap ~~ Ts)
skalberg@15570
   152
              (forall_intr_vfs prop') handle UnequalLengths =>
berghofe@13669
   153
                error ("Wrong number of type arguments for " ^
berghofe@13669
   154
                  quote (fst (get_name_tags [] prop prf)))
skalberg@15531
   155
          in (prop'', change_type (SOME Ts) prf, [], env', vTs) end;
berghofe@11522
   156
berghofe@13669
   157
    fun head_norm (prop, prf, cnstrts, env, vTs) =
berghofe@13669
   158
      (Envir.head_norm env prop, prf, cnstrts, env, vTs);
berghofe@13669
   159
 
skalberg@15570
   160
    fun mk_cnstrts env _ Hs vTs (PBound i) = (List.nth (Hs, i), PBound i, [], env, vTs)
berghofe@13669
   161
      | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) =
berghofe@13669
   162
          let
berghofe@13669
   163
            val (env', T) = (case opT of
skalberg@15531
   164
              NONE => mk_tvar (env, []) | SOME T => (env, T));
berghofe@13669
   165
            val (t, prf, cnstrts, env'', vTs') =
berghofe@13669
   166
              mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf;
skalberg@15531
   167
          in (Const ("all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, SOME T, prf),
berghofe@13669
   168
            cnstrts, env'', vTs')
berghofe@11522
   169
          end
skalberg@15531
   170
      | mk_cnstrts env Ts Hs vTs (AbsP (s, SOME t, cprf)) =
berghofe@11522
   171
          let
berghofe@13669
   172
            val (t', _, vTs', env') = infer_type sg env Ts vTs t;
berghofe@13669
   173
            val (u, prf, cnstrts, env'', vTs'') = mk_cnstrts env' Ts (t'::Hs) vTs' cprf;
skalberg@15531
   174
          in (Logic.mk_implies (t', u), AbsP (s, SOME t', prf), cnstrts, env'', vTs'')
berghofe@11522
   175
          end
skalberg@15531
   176
      | mk_cnstrts env Ts Hs vTs (AbsP (s, NONE, cprf)) =
berghofe@11522
   177
          let
berghofe@11522
   178
            val (env', t) = mk_var env Ts propT;
berghofe@13669
   179
            val (u, prf, cnstrts, env'', vTs') = mk_cnstrts env' Ts (t::Hs) vTs cprf;
skalberg@15531
   180
          in (Logic.mk_implies (t, u), AbsP (s, SOME t, prf), cnstrts, env'', vTs')
berghofe@11522
   181
          end
berghofe@13669
   182
      | mk_cnstrts env Ts Hs vTs (cprf1 %% cprf2) =
berghofe@13669
   183
          let val (u, prf2, cnstrts, env', vTs') = mk_cnstrts env Ts Hs vTs cprf2
berghofe@13669
   184
          in (case head_norm (mk_cnstrts env' Ts Hs vTs' cprf1) of
berghofe@13669
   185
              (Const ("==>", _) $ u' $ t', prf1, cnstrts', env'', vTs'') =>
berghofe@11613
   186
                add_cnstrt Ts t' (prf1 %% prf2) (cnstrts' @ cnstrts)
berghofe@13669
   187
                  env'' vTs'' (u, u')
berghofe@13669
   188
            | (t, prf1, cnstrts', env'', vTs'') =>
berghofe@11522
   189
                let val (env''', v) = mk_var env'' Ts propT
berghofe@11613
   190
                in add_cnstrt Ts v (prf1 %% prf2) (cnstrts' @ cnstrts)
berghofe@13669
   191
                  env''' vTs'' (t, Logic.mk_implies (u, v))
berghofe@11522
   192
                end)
berghofe@11522
   193
          end
skalberg@15531
   194
      | mk_cnstrts env Ts Hs vTs (cprf % SOME t) =
berghofe@13669
   195
          let val (t', U, vTs1, env1) = infer_type sg env Ts vTs t
berghofe@13669
   196
          in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of
berghofe@11522
   197
             (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
berghofe@13669
   198
                 prf, cnstrts, env2, vTs2) =>
berghofe@13669
   199
               let val env3 = unifyT sg env2 T U
skalberg@15531
   200
               in (betapply (f, t'), prf % SOME t', cnstrts, env3, vTs2)
berghofe@11522
   201
               end
berghofe@13669
   202
           | (u, prf, cnstrts, env2, vTs2) =>
berghofe@13669
   203
               let val (env3, v) = mk_var env2 Ts (U --> propT);
berghofe@11522
   204
               in
skalberg@15531
   205
                 add_cnstrt Ts (v $ t') (prf % SOME t') cnstrts env3 vTs2
berghofe@13669
   206
                   (u, Const ("all", (U --> propT) --> propT) $ v)
berghofe@11522
   207
               end)
berghofe@11522
   208
          end
skalberg@15531
   209
      | mk_cnstrts env Ts Hs vTs (cprf % NONE) =
berghofe@13669
   210
          (case head_norm (mk_cnstrts env Ts Hs vTs cprf) of
berghofe@11522
   211
             (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
berghofe@13669
   212
                 prf, cnstrts, env', vTs') =>
berghofe@11522
   213
               let val (env'', t) = mk_var env' Ts T
skalberg@15531
   214
               in (betapply (f, t), prf % SOME t, cnstrts, env'', vTs')
berghofe@11522
   215
               end
berghofe@13669
   216
           | (u, prf, cnstrts, env', vTs') =>
berghofe@11522
   217
               let
wenzelm@14854
   218
                 val (env1, T) = mk_tvar (env', []);
berghofe@11522
   219
                 val (env2, v) = mk_var env1 Ts (T --> propT);
berghofe@11522
   220
                 val (env3, t) = mk_var env2 Ts T
berghofe@11522
   221
               in
skalberg@15531
   222
                 add_cnstrt Ts (v $ t) (prf % SOME t) cnstrts env3 vTs'
berghofe@11522
   223
                   (u, Const ("all", (T --> propT) --> propT) $ v)
berghofe@11522
   224
               end)
berghofe@13669
   225
      | mk_cnstrts env _ _ vTs (prf as PThm (_, _, prop, opTs)) =
berghofe@13669
   226
          mk_cnstrts_atom env vTs prop opTs prf
berghofe@13669
   227
      | mk_cnstrts env _ _ vTs (prf as PAxm (_, prop, opTs)) =
berghofe@13669
   228
          mk_cnstrts_atom env vTs prop opTs prf
berghofe@13669
   229
      | mk_cnstrts env _ _ vTs (prf as Oracle (_, prop, opTs)) =
berghofe@13669
   230
          mk_cnstrts_atom env vTs prop opTs prf
berghofe@13669
   231
      | mk_cnstrts env _ _ vTs (Hyp t) = (t, Hyp t, [], env, vTs)
berghofe@11613
   232
      | mk_cnstrts _ _ _ _ _ = error "reconstruct_proof: minimal proof object"
berghofe@13669
   233
  in mk_cnstrts env [] [] Symtab.empty cprf end;
berghofe@11522
   234
berghofe@11522
   235
fun add_term_ixns (is, Var (i, T)) = add_typ_ixns (i ins is, T)
berghofe@11522
   236
  | add_term_ixns (is, Free (_, T)) = add_typ_ixns (is, T)
berghofe@11522
   237
  | add_term_ixns (is, Const (_, T)) = add_typ_ixns (is, T)
berghofe@11522
   238
  | add_term_ixns (is, t1 $ t2) = add_term_ixns (add_term_ixns (is, t1), t2)
berghofe@11522
   239
  | add_term_ixns (is, Abs (_, T, t)) = add_term_ixns (add_typ_ixns (is, T), t)
berghofe@11522
   240
  | add_term_ixns (is, _) = is;
berghofe@11522
   241
berghofe@11522
   242
berghofe@13669
   243
(**** update list of free variables of constraints ****)
berghofe@11522
   244
berghofe@11522
   245
fun upd_constrs env cs =
berghofe@11522
   246
  let
berghofe@11522
   247
    val Envir.Envir {asol, iTs, ...} = env;
berghofe@11522
   248
    val dom = Vartab.foldl (uncurry (cons o fst) o Library.swap)
berghofe@11522
   249
      (Vartab.foldl (uncurry (cons o fst) o Library.swap) ([], asol), iTs); 
berghofe@15798
   250
    val vran = Vartab.foldl (add_typ_ixns o apsnd (snd o snd))
berghofe@15798
   251
      (Vartab.foldl (add_term_ixns o apsnd (snd o snd)) ([], asol), iTs);
berghofe@11522
   252
    fun check_cs [] = []
berghofe@11522
   253
      | check_cs ((u, p, vs)::ps) =
berghofe@11522
   254
          let val vs' = vs \\ dom;
berghofe@11522
   255
          in if vs = vs' then (u, p, vs)::check_cs ps
berghofe@11522
   256
             else (true, p, vs' union vran)::check_cs ps
berghofe@11522
   257
          end
berghofe@11522
   258
  in check_cs cs end;
berghofe@11522
   259
berghofe@13669
   260
(**** solution of constraints ****)
berghofe@11522
   261
berghofe@11522
   262
fun solve _ [] bigenv = bigenv
berghofe@11522
   263
  | solve sg cs bigenv =
berghofe@11522
   264
      let
berghofe@11660
   265
        fun search env [] = error ("Unsolvable constraints:\n" ^
berghofe@11660
   266
              Pretty.string_of (Pretty.chunks (map (fn (_, p, _) =>
wenzelm@14876
   267
                Display.pretty_flexpair (Sign.pp sg) (pairself
berghofe@13669
   268
                  (Envir.norm_term bigenv) p)) cs)))
berghofe@11522
   269
          | search env ((u, p as (t1, t2), vs)::ps) =
berghofe@11522
   270
              if u then
berghofe@11522
   271
                let
berghofe@11522
   272
                  val tn1 = Envir.norm_term bigenv t1;
berghofe@11522
   273
                  val tn2 = Envir.norm_term bigenv t2
berghofe@11522
   274
                in
berghofe@11522
   275
                  if Pattern.pattern tn1 andalso Pattern.pattern tn2 then
berghofe@11522
   276
                    ((Pattern.unify (sg, env, [(tn1, tn2)]), ps) handle Pattern.Unif =>
berghofe@13715
   277
                       cantunify sg (tn1, tn2))
berghofe@11522
   278
                  else
berghofe@13715
   279
                    let val (env', cs') = decompose sg [] (env, (tn1, tn2))
berghofe@11522
   280
                    in if cs' = [(tn1, tn2)] then
berghofe@11522
   281
                         apsnd (cons (false, (tn1, tn2), vs)) (search env ps)
berghofe@11522
   282
                       else search env' (map (fn q => (true, q, vs)) cs' @ ps)
berghofe@11522
   283
                    end
berghofe@11522
   284
                end
berghofe@11522
   285
              else apsnd (cons (false, p, vs)) (search env ps);
berghofe@11522
   286
        val Envir.Envir {maxidx, ...} = bigenv;
berghofe@11522
   287
        val (env, cs') = search (Envir.empty maxidx) cs;
berghofe@11522
   288
      in
berghofe@11522
   289
        solve sg (upd_constrs env cs') (merge_envs bigenv env)
berghofe@11522
   290
      end;
berghofe@11522
   291
berghofe@11522
   292
berghofe@13669
   293
(**** reconstruction of proofs ****)
berghofe@11522
   294
berghofe@11613
   295
fun reconstruct_proof sg prop cprf =
berghofe@11522
   296
  let
skalberg@15531
   297
    val (cprf' % SOME prop', thawf) = freeze_thaw_prf (cprf % SOME prop);
berghofe@13669
   298
    val _ = message "Collecting constraints...";
berghofe@13669
   299
    val (t, prf, cs, env, _) = make_constraints_cprf sg
berghofe@13669
   300
      (Envir.empty (maxidx_of_proof cprf)) cprf';
berghofe@11522
   301
    val cs' = map (fn p => (true, p, op union
berghofe@11522
   302
      (pairself (map (fst o dest_Var) o term_vars) p))) (map (pairself (Envir.norm_term env)) ((t, prop')::cs));
berghofe@11522
   303
    val _ = message ("Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
berghofe@11522
   304
    val env' = solve sg cs' env
berghofe@11522
   305
  in
berghofe@11522
   306
    thawf (norm_proof env' prf)
berghofe@11522
   307
  end;
berghofe@11522
   308
berghofe@13256
   309
fun prop_of_atom prop Ts =
berghofe@13669
   310
  let val (prop', fmap) = Type.varify (prop, []);
berghofe@13669
   311
  in subst_TVars (map fst (term_tvars prop) @ map snd fmap ~~ Ts)
berghofe@13669
   312
    (forall_intr_vfs prop')
berghofe@13669
   313
  end;
berghofe@13138
   314
berghofe@13734
   315
val head_norm = Envir.head_norm (Envir.empty 0);
berghofe@13734
   316
skalberg@15570
   317
fun prop_of0 Hs (PBound i) = List.nth (Hs, i)
skalberg@15531
   318
  | prop_of0 Hs (Abst (s, SOME T, prf)) =
berghofe@13734
   319
      all T $ (Abs (s, T, prop_of0 Hs prf))
skalberg@15531
   320
  | prop_of0 Hs (AbsP (s, SOME t, prf)) =
berghofe@13734
   321
      Logic.mk_implies (t, prop_of0 (t :: Hs) prf)
skalberg@15531
   322
  | prop_of0 Hs (prf % SOME t) = (case head_norm (prop_of0 Hs prf) of
berghofe@13734
   323
      Const ("all", _) $ f => f $ t
berghofe@13256
   324
    | _ => error "prop_of: all expected")
berghofe@13734
   325
  | prop_of0 Hs (prf1 %% prf2) = (case head_norm (prop_of0 Hs prf1) of
berghofe@13256
   326
      Const ("==>", _) $ P $ Q => Q
berghofe@13256
   327
    | _ => error "prop_of: ==> expected")
berghofe@13734
   328
  | prop_of0 Hs (Hyp t) = t
skalberg@15531
   329
  | prop_of0 Hs (PThm (_, _, prop, SOME Ts)) = prop_of_atom prop Ts
skalberg@15531
   330
  | prop_of0 Hs (PAxm (_, prop, SOME Ts)) = prop_of_atom prop Ts
skalberg@15531
   331
  | prop_of0 Hs (Oracle (_, prop, SOME Ts)) = prop_of_atom prop Ts
berghofe@13734
   332
  | prop_of0 _ _ = error "prop_of: partial proof object";
berghofe@13138
   333
berghofe@13734
   334
val prop_of' = Pattern.eta_contract oo (Envir.beta_norm oo prop_of0);
berghofe@13256
   335
val prop_of = prop_of' [];
berghofe@13138
   336
berghofe@11522
   337
berghofe@13669
   338
(**** expand and reconstruct subproofs ****)
berghofe@11522
   339
berghofe@13342
   340
fun expand_proof sg thms prf =
berghofe@11522
   341
  let
berghofe@12870
   342
    fun expand maxidx prfs (AbsP (s, t, prf)) = 
berghofe@12870
   343
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   344
          in (maxidx', prfs', AbsP (s, t, prf')) end
berghofe@12870
   345
      | expand maxidx prfs (Abst (s, T, prf)) = 
berghofe@12870
   346
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   347
          in (maxidx', prfs', Abst (s, T, prf')) end
berghofe@12870
   348
      | expand maxidx prfs (prf1 %% prf2) =
berghofe@11522
   349
          let
berghofe@12870
   350
            val (maxidx', prfs', prf1') = expand maxidx prfs prf1;
berghofe@12870
   351
            val (maxidx'', prfs'', prf2') = expand maxidx' prfs' prf2;
berghofe@12870
   352
          in (maxidx'', prfs'', prf1' %% prf2') end
berghofe@12870
   353
      | expand maxidx prfs (prf % t) =
berghofe@12870
   354
          let val (maxidx', prfs', prf') = expand maxidx prfs prf
berghofe@12870
   355
          in (maxidx', prfs', prf' % t) end
skalberg@15531
   356
      | expand maxidx prfs (prf as PThm ((a, _), cprf, prop, SOME Ts)) =
berghofe@13342
   357
          if not (exists
skalberg@15531
   358
            (fn (b, NONE) => a = b
skalberg@15531
   359
              | (b, SOME prop') => a = b andalso prop = prop') thms)
berghofe@13342
   360
          then (maxidx, prfs, prf) else
berghofe@11522
   361
          let
berghofe@13610
   362
            fun inc i =
berghofe@13610
   363
              map_proof_terms (Logic.incr_indexes ([], i)) (incr_tvar i);
berghofe@13669
   364
            val (maxidx', prf, prfs') = (case assoc (prfs, (a, prop)) of
skalberg@15531
   365
                NONE =>
berghofe@11522
   366
                  let
berghofe@11522
   367
                    val _ = message ("Reconstructing proof of " ^ a);
berghofe@11522
   368
                    val _ = message (Sign.string_of_term sg prop);
berghofe@13610
   369
                    val prf' = forall_intr_vfs_prf prop
berghofe@13610
   370
                      (reconstruct_proof sg prop cprf);
berghofe@12870
   371
                    val (maxidx', prfs', prf) = expand
berghofe@12870
   372
                      (maxidx_of_proof prf') prfs prf'
berghofe@13669
   373
                  in (maxidx' + maxidx + 1, inc (maxidx + 1) prf,
berghofe@13610
   374
                    ((a, prop), (maxidx', prf)) :: prfs')
berghofe@13610
   375
                  end
skalberg@15531
   376
              | SOME (maxidx', prf) => (maxidx' + maxidx + 1,
berghofe@13669
   377
                  inc (maxidx + 1) prf, prfs));
berghofe@13669
   378
            val tfrees = term_tfrees prop;
berghofe@13669
   379
            val tye = map (fn ((s, j), _) => (s, maxidx + 1 + j))
berghofe@13669
   380
              (term_tvars prop) @ map (rpair ~1 o fst) tfrees ~~ Ts;
berghofe@13669
   381
            val varify = map_type_tfree (fn p as (a, S) =>
berghofe@13669
   382
              if p mem tfrees then TVar ((a, ~1), S) else TFree p)
berghofe@11522
   383
          in
berghofe@13669
   384
            (maxidx', prfs', map_proof_terms (subst_TVars tye o
berghofe@13669
   385
               map_term_types varify) (typ_subst_TVars tye o varify) prf)
berghofe@11522
   386
          end
berghofe@12870
   387
      | expand maxidx prfs prf = (maxidx, prfs, prf);
berghofe@11522
   388
berghofe@12870
   389
  in #3 (expand (maxidx_of_proof prf) [] prf) end;
berghofe@11522
   390
berghofe@11522
   391
end;