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