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