Initial revision of tools for proof terms.
authorberghofe
Fri Aug 31 16:17:05 2001 +0200 (2001-08-31 ago)
changeset 1152242fbb6abed5a
parent 11521 80acc6ce26c3
child 11523 9a658fe20107
Initial revision of tools for proof terms.
src/Pure/Proof/proof_rewrite_rules.ML
src/Pure/Proof/proof_syntax.ML
src/Pure/Proof/proofchecker.ML
src/Pure/Proof/reconstruct.ML
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Pure/Proof/proof_rewrite_rules.ML	Fri Aug 31 16:17:05 2001 +0200
     1.3 @@ -0,0 +1,103 @@
     1.4 +(*  Title:      Pure/Proof/proof_rewrite_rules.ML
     1.5 +    ID:         $Id$
     1.6 +    Author:     Stefan Berghofer
     1.7 +    Copyright   2000  TU Muenchen
     1.8 +
     1.9 +Simplification function for partial proof terms involving
    1.10 +meta level rules.
    1.11 +*)
    1.12 +
    1.13 +signature PROOF_REWRITE_RULES =
    1.14 +sig
    1.15 +  val rprocs : (string * (typ list -> Proofterm.proof -> Proofterm.proof option)) list
    1.16 +end;
    1.17 +
    1.18 +structure ProofRewriteRules : PROOF_REWRITE_RULES =
    1.19 +struct
    1.20 +
    1.21 +open Proofterm;
    1.22 +
    1.23 +fun rew _ (PThm (("ProtoPure.rev_triv_goal", _), _, _, _) %% _ %
    1.24 +      (PThm (("ProtoPure.triv_goal", _), _, _, _) %% _ % prf)) = Some prf
    1.25 +  | rew _ (PAxm ("ProtoPure.equal_elim", _, _) %% _ %% _ %
    1.26 +      (PAxm ("ProtoPure.equal_intr", _, _) %% _ %% _ % prf % _)) = Some prf
    1.27 +  | rew _ (PAxm ("ProtoPure.symmetric", _, _) %% _ %% _ %
    1.28 +      (PAxm ("ProtoPure.equal_intr", _, _) %% A %% B % prf1 % prf2)) =
    1.29 +          Some (equal_intr_axm %% B %% A % prf2 % prf1)
    1.30 +
    1.31 +  | rew _ (PAxm ("ProtoPure.equal_elim", _, _) %% Some X %% Some Y %
    1.32 +      (PAxm ("ProtoPure.combination", _, _) %% _ %% _ %% _ %% _ %
    1.33 +        (PAxm ("ProtoPure.combination", _, _) %% Some (Const ("==>", _)) %% _ %% _ %% _ %
    1.34 +           (PAxm ("ProtoPure.reflexive", _, _) %% _) % prf1) % prf2)) =
    1.35 +      let
    1.36 +        val _ $ A $ C = Envir.beta_norm X;
    1.37 +        val _ $ B $ D = Envir.beta_norm Y
    1.38 +      in Some (AbsP ("H1", None, AbsP ("H2", None,
    1.39 +        equal_elim_axm %%% C %%% D % incr_pboundvars 2 0 prf2 %
    1.40 +          (PBound 1 % (equal_elim_axm %%% B %%% A %
    1.41 +            (symmetric_axm %% None %% None % incr_pboundvars 2 0 prf1) % PBound 0)))))
    1.42 +      end
    1.43 +
    1.44 +  | rew _ (PAxm ("ProtoPure.equal_elim", _, _) %% Some X %% Some Y %
    1.45 +      (PAxm ("ProtoPure.symmetric", _, _) %% _ %% _ %
    1.46 +        (PAxm ("ProtoPure.combination", _, _) %% _ %% _ %% _ %% _ %
    1.47 +          (PAxm ("ProtoPure.combination", _, _) %% Some (Const ("==>", _)) %% _ %% _ %% _ %
    1.48 +             (PAxm ("ProtoPure.reflexive", _, _) %% _) % prf1) % prf2))) =
    1.49 +      let
    1.50 +        val _ $ A $ C = Envir.beta_norm Y;
    1.51 +        val _ $ B $ D = Envir.beta_norm X
    1.52 +      in Some (AbsP ("H1", None, AbsP ("H2", None,
    1.53 +        equal_elim_axm %%% D %%% C %
    1.54 +          (symmetric_axm %% None %% None % incr_pboundvars 2 0 prf2)
    1.55 +            % (PBound 1 % (equal_elim_axm %%% A %%% B % incr_pboundvars 2 0 prf1 % PBound 0)))))
    1.56 +      end
    1.57 +
    1.58 +  | rew _ (PAxm ("ProtoPure.equal_elim", _, _) %% Some X %% Some Y %
    1.59 +      (PAxm ("ProtoPure.combination", _, _) %% Some (Const ("all", _)) %% _ %% _ %% _ %
    1.60 +        (PAxm ("ProtoPure.reflexive", _, _) %% _) %
    1.61 +          (PAxm ("ProtoPure.abstract_rule", _, _) %% _ %% _ % prf))) =
    1.62 +      let
    1.63 +        val _ $ P = Envir.beta_norm X;
    1.64 +        val _ $ Q = Envir.beta_norm Y;
    1.65 +      in Some (AbsP ("H", None, Abst ("x", None,
    1.66 +          equal_elim_axm %%% incr_boundvars 1 P $ Bound 0 %%% incr_boundvars 1 Q $ Bound 0 %
    1.67 +            (incr_pboundvars 1 1 prf %%% Bound 0) % (PBound 0 %%% Bound 0))))
    1.68 +      end
    1.69 +
    1.70 +  | rew _ (PAxm ("ProtoPure.equal_elim", _, _) %% Some X %% Some Y %
    1.71 +      (PAxm ("ProtoPure.symmetric", _, _) %% _ %% _ %        
    1.72 +        (PAxm ("ProtoPure.combination", _, _) %% Some (Const ("all", _)) %% _ %% _ %% _ %
    1.73 +          (PAxm ("ProtoPure.reflexive", _, _) %% _) %
    1.74 +            (PAxm ("ProtoPure.abstract_rule", _, _) %% _ %% _ % prf)))) =
    1.75 +      let
    1.76 +        val _ $ P = Envir.beta_norm X;
    1.77 +        val _ $ Q = Envir.beta_norm Y;
    1.78 +      in Some (AbsP ("H", None, Abst ("x", None,
    1.79 +        equal_elim_axm %%% incr_boundvars 1 P $ Bound 0 %%% incr_boundvars 1 Q $ Bound 0 %
    1.80 +          (symmetric_axm %% None %% None % (incr_pboundvars 1 1 prf %%% Bound 0))
    1.81 +            % (PBound 0 %%% Bound 0))))
    1.82 +      end
    1.83 +
    1.84 +  | rew _ (PAxm ("ProtoPure.equal_elim", _, _) %% Some A %% Some C %
    1.85 +      (PAxm ("ProtoPure.transitive", _, _) %% _ %% Some B %% _ % prf1 % prf2) % prf3) =
    1.86 +         Some (equal_elim_axm %%% B %%% C % prf2 %
    1.87 +           (equal_elim_axm %%% A %%% B % prf1 % prf3))
    1.88 +  | rew _ (PAxm ("ProtoPure.equal_elim", _, _) %% Some A %% Some C %
    1.89 +      (PAxm ("ProtoPure.symmetric", _, _) %% _ %% _ %
    1.90 +        (PAxm ("ProtoPure.transitive", _, _) %% _ %% Some B %% _ % prf1 % prf2)) % prf3) =
    1.91 +         Some (equal_elim_axm %%% B %%% C % (symmetric_axm %% None %% None % prf1) %
    1.92 +           (equal_elim_axm %%% A %%% B % (symmetric_axm %% None %% None % prf2) % prf3))
    1.93 +
    1.94 +  | rew _ (PAxm ("ProtoPure.equal_elim", _, _) %% _ %% _ %
    1.95 +      (PAxm ("ProtoPure.reflexive", _, _) %% _) % prf) = Some prf
    1.96 +  | rew _ (PAxm ("ProtoPure.equal_elim", _, _) %% _ %% _ %
    1.97 +      (PAxm ("ProtoPure.symmetric", _, _) %% _ %% _ %
    1.98 +        (PAxm ("ProtoPure.reflexive", _, _) %% _)) % prf) = Some prf
    1.99 +
   1.100 +  | rew _ _ = None;
   1.101 +
   1.102 +val rprocs = [("Pure/meta_equality", rew)];
   1.103 +
   1.104 +end;
   1.105 +
   1.106 +Proofterm.add_prf_rprocs ProtoPure.thy ProofRewriteRules.rprocs;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/Pure/Proof/proof_syntax.ML	Fri Aug 31 16:17:05 2001 +0200
     2.3 @@ -0,0 +1,267 @@
     2.4 +(*  Title:      Pure/Proof/proof_syntax.ML
     2.5 +    ID:         $Id$
     2.6 +    Author:     Stefan Berghofer
     2.7 +    Copyright   2000  TU Muenchen
     2.8 +
     2.9 +Function for parsing and printing proof terms.
    2.10 +*)
    2.11 +
    2.12 +signature PROOF_SYNTAX =
    2.13 +sig
    2.14 +  val proofT : typ
    2.15 +  val add_proof_syntax : Sign.sg -> Sign.sg
    2.16 +  val disambiguate_names : theory -> Proofterm.proof ->
    2.17 +    Proofterm.proof * Proofterm.proof Symtab.table
    2.18 +  val proof_of_term : theory -> Proofterm.proof Symtab.table ->
    2.19 +    bool -> term -> Proofterm.proof
    2.20 +  val term_of_proof : Proofterm.proof -> term
    2.21 +  val cterm_of_proof : theory -> Proofterm.proof -> cterm * (cterm -> Proofterm.proof)
    2.22 +  val read_term : theory -> typ -> string -> term
    2.23 +  val read_proof : theory -> bool -> string -> Proofterm.proof
    2.24 +  val pretty_proof : Sign.sg -> Proofterm.proof -> Pretty.T
    2.25 +  val pretty_proof_of : bool -> thm -> Pretty.T
    2.26 +  val print_proof_of : bool -> thm -> unit
    2.27 +end;
    2.28 +
    2.29 +structure ProofSyntax : PROOF_SYNTAX =
    2.30 +struct
    2.31 +
    2.32 +open Proofterm;
    2.33 +
    2.34 +(**** add special syntax for embedding proof terms ****)
    2.35 +
    2.36 +val proofT = Type ("proof", []);
    2.37 +val lamT = Type ("lam_syn", []);
    2.38 +val idtT = Type ("idt", []);
    2.39 +val aT = TFree ("'a", ["logic"]);
    2.40 +
    2.41 +(** constants for theorems and axioms **)
    2.42 +
    2.43 +fun add_prefix a b = NameSpace.pack (a :: NameSpace.unpack b);
    2.44 +
    2.45 +fun add_proof_atom_consts names sg = Sign.add_consts_i
    2.46 +  (map (fn name => (name, proofT, NoSyn)) names) (Sign.add_path "//" sg);
    2.47 +
    2.48 +(** constants for application and abstraction **)
    2.49 +  
    2.50 +fun add_proof_syntax sg =
    2.51 +  sg
    2.52 +  |> Sign.copy
    2.53 +  |> Sign.add_path "/"
    2.54 +  |> Sign.add_defsort_i ["logic"]
    2.55 +  |> Sign.add_types [("proof", 0, NoSyn)]
    2.56 +  |> Sign.add_arities [("proof", [], "logic")]
    2.57 +  |> Sign.add_consts_i
    2.58 +      [("Appt", [proofT, aT] ---> proofT, Mixfix ("(1_ %%/ _)", [4, 5], 4)),
    2.59 +       ("AppP", [proofT, proofT] ---> proofT, Mixfix ("(1_ %/ _)", [4, 5], 4)),
    2.60 +       ("Abst", (aT --> proofT) --> proofT, NoSyn),
    2.61 +       ("AbsP", [propT, proofT --> proofT] ---> proofT, NoSyn)]
    2.62 +  |> Sign.add_nonterminals ["lam_syn"]
    2.63 +  |> Sign.add_syntax_i
    2.64 +      [("_Lam", [lamT, proofT] ---> proofT, Mixfix ("(3Lam _./ _)", [0,0], 1)),
    2.65 +       ("_Lam0", [lamT, lamT] ---> lamT, Mixfix ("_,/ _", [1, 0], 0)),
    2.66 +       ("_Lam1", [idtT, propT] ---> lamT, Mixfix ("_ : _", [0, 0], 1)),
    2.67 +       ("_Lam2", idtT --> lamT, Mixfix ("_", [0], 1))]
    2.68 +  |> Sign.add_modesyntax_i (("xsymbols", true),
    2.69 +      [("_Lam", [lamT, proofT] ---> proofT, Mixfix ("(3\\<Lambda>_./ _)", [0,0], 1)),
    2.70 +       ("Appt", [proofT, aT] ---> proofT, Mixfix ("(1_ \\<cdot>/ _)", [4, 5], 4)),
    2.71 +       ("AppP", [proofT, proofT] ---> proofT, Mixfix ("(1_ \\<bullet>/ _)", [4, 5], 4))])
    2.72 +  |> Sign.add_trrules_i (map Syntax.ParsePrintRule
    2.73 +      [(Syntax.mk_appl (Constant "_Lam")
    2.74 +          [Syntax.mk_appl (Constant "_Lam1") [Variable "x", Variable "A"], Variable "B"],
    2.75 +        Syntax.mk_appl (Constant "AbsP") [Variable "A",
    2.76 +          (Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "B"])]),
    2.77 +       (Syntax.mk_appl (Constant "_Lam")
    2.78 +          [Syntax.mk_appl (Constant "_Lam2") [Variable "x"], Variable "A"],
    2.79 +        Syntax.mk_appl (Constant "Abst")
    2.80 +          [(Syntax.mk_appl (Constant "_abs") [Variable "x", Variable "A"])]),
    2.81 +       (Syntax.mk_appl (Constant "_Lam")
    2.82 +          [Syntax.mk_appl (Constant "_Lam0") [Variable "l", Variable "m"], Variable "A"],
    2.83 +        Syntax.mk_appl (Constant "_Lam")
    2.84 +          [Variable "l", Syntax.mk_appl (Constant "_Lam") [Variable "m", Variable "A"]])]);
    2.85 +
    2.86 +
    2.87 +(**** create unambiguous theorem names ****)
    2.88 +
    2.89 +fun disambiguate_names thy prf =
    2.90 +  let
    2.91 +    val thms = thms_of_proof Symtab.empty prf;
    2.92 +    val thms' = map (apsnd (#prop o rep_thm)) (flat
    2.93 +      (map PureThy.thms_of (thy :: Theory.ancestors_of thy)));
    2.94 +
    2.95 +    val tab = Symtab.foldl (fn (tab, (key, ps)) =>
    2.96 +      let val prop = if_none (assoc (thms', key)) (Bound 0)
    2.97 +      in fst (foldr (fn ((prop', prf), x as (tab, i)) =>
    2.98 +        if prop <> prop' then
    2.99 +          (Symtab.update ((key ^ "_" ^ string_of_int i, prf), tab), i+1)
   2.100 +        else x) (ps, (tab, 1)))
   2.101 +      end) (Symtab.empty, thms);
   2.102 +
   2.103 +    fun rename (Abst (s, T, prf)) = Abst (s, T, rename prf)
   2.104 +      | rename (AbsP (s, t, prf)) = AbsP (s, t, rename prf)
   2.105 +      | rename (prf1 % prf2) = rename prf1 % rename prf2
   2.106 +      | rename (prf %% t) = rename prf %% t
   2.107 +      | rename (prf' as PThm ((s, tags), prf, prop, Ts)) =
   2.108 +          let
   2.109 +            val prop' = if_none (assoc (thms', s)) (Bound 0);
   2.110 +            val ps = map fst (the (Symtab.lookup (thms, s))) \ prop'
   2.111 +          in if prop = prop' then prf' else
   2.112 +            PThm ((s ^ "_" ^ string_of_int (length ps - find_index_eq prop ps), tags),
   2.113 +              prf, prop, Ts)
   2.114 +          end
   2.115 +      | rename prf = prf
   2.116 +
   2.117 +  in (rename prf, tab) end;
   2.118 +
   2.119 +
   2.120 +(**** translation between proof terms and pure terms ****)
   2.121 +
   2.122 +fun change_type T (PThm (name, prf, prop, _)) = PThm (name, prf, prop, T)
   2.123 +  | change_type T (PAxm (name, prop, _)) = PAxm (name, prop, T)
   2.124 +  | change_type _ _ = error "Not a proper theorem";
   2.125 +
   2.126 +fun proof_of_term thy tab ty =
   2.127 +  let
   2.128 +    val thys = thy :: Theory.ancestors_of thy;
   2.129 +    val thms = flat (map thms_of thys);
   2.130 +    val axms = flat (map (Symtab.dest o #axioms o rep_theory) thys);
   2.131 +
   2.132 +    fun prf_of [] (Bound i) = PBound i
   2.133 +      | prf_of Ts (Const (s, Type ("proof", _))) =
   2.134 +          change_type (if ty then Some Ts else None)
   2.135 +            (case NameSpace.unpack s of
   2.136 +               "Axm" :: xs =>
   2.137 +                 let
   2.138 +                   val name = NameSpace.pack xs;
   2.139 +                   val prop = (case assoc (axms, name) of
   2.140 +                       Some prop => prop
   2.141 +                     | None => error ("Unknown axiom " ^ quote name))
   2.142 +                 in PAxm (name, prop, None) end
   2.143 +             | "Thm" :: xs =>
   2.144 +                 let val name = NameSpace.pack xs;
   2.145 +                 in (case assoc (thms, name) of
   2.146 +                     Some thm => fst (strip_combt (#2 (#der (rep_thm thm))))
   2.147 +                   | None => (case Symtab.lookup (tab, name) of
   2.148 +                         Some prf => prf
   2.149 +                       | None => error ("Unknown theorem " ^ quote name)))
   2.150 +                 end
   2.151 +             | _ => error ("Illegal proof constant name: " ^ quote s))
   2.152 +      | prf_of Ts (v as Var ((_, Type ("proof", _)))) = Hyp v
   2.153 +      | prf_of [] (Const ("Abst", _) $ Abs (s, T, prf)) =
   2.154 +          Abst (s, if ty then Some T else None,
   2.155 +            incr_pboundvars (~1) 0 (prf_of [] prf))
   2.156 +      | prf_of [] (Const ("AbsP", _) $ t $ Abs (s, _, prf)) =
   2.157 +          AbsP (s, case t of Const ("dummy_pattern", _) => None | _ => Some t,
   2.158 +            incr_pboundvars 0 (~1) (prf_of [] prf))
   2.159 +      | prf_of [] (Const ("AppP", _) $ prf1 $ prf2) =
   2.160 +          prf_of [] prf1 % prf_of [] prf2
   2.161 +      | prf_of Ts (Const ("Appt", _) $ prf $ Const ("TYPE", Type (_, [T]))) =
   2.162 +          prf_of (T::Ts) prf
   2.163 +      | prf_of [] (Const ("Appt", _) $ prf $ t) = prf_of [] prf %%
   2.164 +          (case t of Const ("dummy_pattern", _) => None | _ => Some t)
   2.165 +      | prf_of _ t = error ("Not a proof term:\n" ^
   2.166 +          Sign.string_of_term (sign_of thy) t)
   2.167 +
   2.168 +  in prf_of [] end;
   2.169 +
   2.170 +
   2.171 +val AbsPt = Const ("AbsP", [propT, proofT --> proofT] ---> proofT);
   2.172 +val AppPt = Const ("AppP", [proofT, proofT] ---> proofT);
   2.173 +val Hypt = Free ("Hyp", propT --> proofT);
   2.174 +val Oraclet = Free ("Oracle", propT --> proofT);
   2.175 +val MinProoft = Free ("?", proofT);
   2.176 +
   2.177 +val mk_tyapp = foldl (fn (prf, T) => Const ("Appt",
   2.178 +  [proofT, itselfT T] ---> proofT) $ prf $ Logic.mk_type T);
   2.179 +
   2.180 +fun term_of _ (PThm ((name, _), _, _, None)) =
   2.181 +      Const (add_prefix "Thm" name, proofT)
   2.182 +  | term_of _ (PThm ((name, _), _, _, Some Ts)) =
   2.183 +      mk_tyapp (Const (add_prefix "Thm" name, proofT), Ts)
   2.184 +  | term_of _ (PAxm (name, _, None)) = Const (add_prefix "Axm" name, proofT)
   2.185 +  | term_of _ (PAxm (name, _, Some Ts)) =
   2.186 +      mk_tyapp (Const (add_prefix "Axm" name, proofT), Ts)
   2.187 +  | term_of _ (PBound i) = Bound i
   2.188 +  | term_of Ts (Abst (s, opT, prf)) = 
   2.189 +      let val T = if_none opT dummyT
   2.190 +      in Const ("Abst", (T --> proofT) --> proofT) $
   2.191 +        Abs (s, T, term_of (T::Ts) (incr_pboundvars 1 0 prf))
   2.192 +      end
   2.193 +  | term_of Ts (AbsP (s, t, prf)) =
   2.194 +      AbsPt $ if_none t (Const ("dummy_pattern", propT)) $
   2.195 +        Abs (s, proofT, term_of (proofT::Ts) (incr_pboundvars 0 1 prf))
   2.196 +  | term_of Ts (prf1 % prf2) =
   2.197 +      AppPt $ term_of Ts prf1 $ term_of Ts prf2
   2.198 +  | term_of Ts (prf %% opt) = 
   2.199 +      let val t = if_none opt (Const ("dummy_pattern", dummyT))
   2.200 +      in Const ("Appt",
   2.201 +        [proofT, fastype_of1 (Ts, t) handle TERM _ => dummyT] ---> proofT) $
   2.202 +          term_of Ts prf $ t
   2.203 +      end
   2.204 +  | term_of Ts (Hyp t) = Hypt $ t
   2.205 +  | term_of Ts (Oracle (_, t, _)) = Oraclet $ t
   2.206 +  | term_of Ts (MinProof _) = MinProoft;
   2.207 +
   2.208 +val term_of_proof = term_of [];
   2.209 +
   2.210 +fun cterm_of_proof thy prf =
   2.211 +  let
   2.212 +    val (prf', tab) = disambiguate_names thy prf;
   2.213 +    val thys = thy :: Theory.ancestors_of thy;
   2.214 +    val thm_names = filter_out (equal "") (map fst (flat (map thms_of thys))) @
   2.215 +      map fst (Symtab.dest tab);
   2.216 +    val axm_names = map fst (flat (map (Symtab.dest o #axioms o rep_theory) thys));
   2.217 +    val sg = sign_of thy |>
   2.218 +      add_proof_syntax |>
   2.219 +      add_proof_atom_consts
   2.220 +        (map (add_prefix "Thm") thm_names @ map (add_prefix "Axm") axm_names)
   2.221 +  in
   2.222 +    (cterm_of sg (term_of_proof prf'),
   2.223 +     proof_of_term thy tab true o Thm.term_of)
   2.224 +  end;
   2.225 +
   2.226 +fun read_term thy =
   2.227 +  let
   2.228 +    val thys = thy :: Theory.ancestors_of thy;
   2.229 +    val thm_names = filter_out (equal "") (map fst (flat (map thms_of thys)));
   2.230 +    val axm_names = map fst (flat (map (Symtab.dest o #axioms o rep_theory) thys));
   2.231 +    val sg = sign_of thy |>
   2.232 +      add_proof_syntax |>
   2.233 +      add_proof_atom_consts
   2.234 +        (map (add_prefix "Thm") thm_names @ map (add_prefix "Axm") axm_names)
   2.235 +  in
   2.236 +    (fn T => fn s => Thm.term_of (read_cterm sg (s, T)))
   2.237 +  end;
   2.238 +
   2.239 +fun read_proof thy =
   2.240 +  let val rd = read_term thy proofT
   2.241 +  in
   2.242 +    (fn ty => fn s => proof_of_term thy Symtab.empty ty (Logic.varify (rd s)))
   2.243 +  end;
   2.244 +
   2.245 +fun pretty_proof sg prf =
   2.246 +  let
   2.247 +    val thm_names = map fst (Symtab.dest (thms_of_proof Symtab.empty prf)) \ "";
   2.248 +    val axm_names = map fst (Symtab.dest (axms_of_proof Symtab.empty prf));
   2.249 +    val sg' = sg |>
   2.250 +      add_proof_syntax |>
   2.251 +      add_proof_atom_consts
   2.252 +        (map (add_prefix "Thm") thm_names @ map (add_prefix "Axm") axm_names)
   2.253 +  in
   2.254 +    Sign.pretty_term sg' (term_of_proof prf)
   2.255 +  end;
   2.256 +
   2.257 +fun pretty_proof_of full thm =
   2.258 +  let
   2.259 +    val {sign, der = (_, prf), prop, ...} = rep_thm thm;
   2.260 +    val prf' = (case strip_combt (fst (strip_combP prf)) of
   2.261 +        (PThm (_, prf', prop', _), _) => if prop=prop' then prf' else prf
   2.262 +      | _ => prf)
   2.263 +  in
   2.264 +    pretty_proof sign
   2.265 +      (if full then Reconstruct.reconstruct_prf sign prop prf' else prf')
   2.266 +  end;
   2.267 +
   2.268 +val print_proof_of = Pretty.writeln oo pretty_proof_of;
   2.269 +
   2.270 +end;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/Pure/Proof/proofchecker.ML	Fri Aug 31 16:17:05 2001 +0200
     3.3 @@ -0,0 +1,104 @@
     3.4 +(*  Title:      Pure/Proof/proofchecker.ML
     3.5 +    ID:         $Id$
     3.6 +    Author:     Stefan Berghofer
     3.7 +    Copyright   2000  TU Muenchen
     3.8 +
     3.9 +Simple proof checker based only on the core inference rules
    3.10 +of Isabelle/Pure.
    3.11 +*)
    3.12 +
    3.13 +signature PROOF_CHECKER =
    3.14 +sig
    3.15 +  val thm_of_proof : theory -> Proofterm.proof -> thm
    3.16 +end;
    3.17 +
    3.18 +structure ProofChecker =
    3.19 +struct
    3.20 +
    3.21 +open Proofterm;
    3.22 +
    3.23 +(***** construct a theorem out of a proof term *****)
    3.24 +
    3.25 +fun lookup_thm thy =
    3.26 +  let val tab = foldr Symtab.update
    3.27 +    (flat (map thms_of (thy :: Theory.ancestors_of thy)), Symtab.empty)
    3.28 +  in
    3.29 +    (fn s => case Symtab.lookup (tab, s) of
    3.30 +       None => error ("Unknown theorem " ^ quote s)
    3.31 +     | Some thm => thm)
    3.32 +  end;
    3.33 +
    3.34 +fun beta_eta_convert thm =
    3.35 +  let
    3.36 +    val beta_thm = beta_conversion true (cprop_of thm);
    3.37 +    val (_, rhs) = Drule.dest_equals (cprop_of beta_thm);
    3.38 +  in Thm.equal_elim (Thm.transitive beta_thm (eta_conversion rhs)) thm end;
    3.39 +
    3.40 +fun thm_of_proof thy prf =
    3.41 +  let
    3.42 +    val names = add_prf_names ([], prf);
    3.43 +    val sg = sign_of thy;
    3.44 +    val lookup = lookup_thm thy;
    3.45 +
    3.46 +    fun thm_of _ _ (PThm ((name, _), _, prop', Some Ts)) =
    3.47 +          let
    3.48 +            val thm = lookup name;
    3.49 +            val {prop, ...} = rep_thm thm;
    3.50 +            val _ = if prop=prop' then () else
    3.51 +              error ("Duplicate use of theorem name " ^ quote name);
    3.52 +            val tvars = term_tvars prop;
    3.53 +            val ctye = map fst tvars ~~ map (Thm.ctyp_of sg) Ts
    3.54 +          in
    3.55 +            Thm.instantiate (ctye, []) (forall_intr_vars thm)
    3.56 +          end
    3.57 +
    3.58 +      | thm_of _ _ (PAxm (name, _, Some Ts)) =
    3.59 +          let
    3.60 +            val thm = get_axiom thy name;
    3.61 +            val {prop, ...} = rep_thm thm;
    3.62 +            val tvars = term_tvars prop;
    3.63 +            val ctye = map fst tvars ~~ map (Thm.ctyp_of sg) Ts
    3.64 +          in
    3.65 +            Thm.instantiate (ctye, []) (forall_intr_vars thm)
    3.66 +          end
    3.67 +
    3.68 +      | thm_of _ Hs (PBound i) = nth_elem (i, Hs)
    3.69 +
    3.70 +      | thm_of vs Hs (Abst (s, Some T, prf)) =
    3.71 +          let
    3.72 +            val x = variant (names @ map fst vs) s;
    3.73 +            val thm = thm_of ((x, T) :: vs) Hs prf
    3.74 +          in
    3.75 +            Thm.forall_intr (Thm.cterm_of sg (Free (x, T))) thm
    3.76 +          end
    3.77 +
    3.78 +      | thm_of vs Hs (prf %% Some t) =
    3.79 +          let
    3.80 +            val thm = thm_of vs Hs prf
    3.81 +            val ct = Thm.cterm_of sg (Term.subst_bounds (map Free vs, t))
    3.82 +          in Thm.forall_elim ct thm end
    3.83 +
    3.84 +      | thm_of vs Hs (AbsP (s, Some t, prf)) =
    3.85 +          let
    3.86 +            val ct = Thm.cterm_of sg (Term.subst_bounds (map Free vs, t));
    3.87 +            val thm = thm_of vs (Thm.assume ct :: Hs) prf
    3.88 +          in
    3.89 +            Thm.implies_intr ct thm
    3.90 +          end
    3.91 +
    3.92 +      | thm_of vs Hs (prf % prf') =
    3.93 +          let 
    3.94 +            val thm = beta_eta_convert (thm_of vs Hs prf);
    3.95 +            val thm' = beta_eta_convert (thm_of vs Hs prf')
    3.96 +          in
    3.97 +            Thm.implies_elim thm thm'
    3.98 +          end
    3.99 +
   3.100 +      | thm_of _ _ (Hyp t) = Thm.assume (Thm.cterm_of sg t)
   3.101 +
   3.102 +      | thm_of _ _ _ = error "thm_of_proof: partial proof term";
   3.103 +
   3.104 +  in thm_of [] [] prf end;
   3.105 +
   3.106 +end;
   3.107 +
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/Pure/Proof/reconstruct.ML	Fri Aug 31 16:17:05 2001 +0200
     4.3 @@ -0,0 +1,346 @@
     4.4 +(*  Title:      Pure/Proof/reconstruct.ML
     4.5 +    ID:         $Id$
     4.6 +    Author:     Stefan Berghofer
     4.7 +    Copyright   2000  TU Muenchen
     4.8 +
     4.9 +Reconstruction of partial proof terms.
    4.10 +*)
    4.11 +
    4.12 +signature RECONSTRUCT =
    4.13 +sig
    4.14 +  val quiet_mode : bool ref
    4.15 +  val reconstruct_prf : Sign.sg -> term -> Proofterm.proof -> Proofterm.proof
    4.16 +  val expand_proof : Sign.sg -> string list -> Proofterm.proof -> Proofterm.proof
    4.17 +end;
    4.18 +
    4.19 +structure Reconstruct : RECONSTRUCT =
    4.20 +struct
    4.21 +
    4.22 +open Proofterm;
    4.23 +
    4.24 +val quiet_mode = ref true;
    4.25 +fun message s = if !quiet_mode then () else writeln s;
    4.26 +
    4.27 +fun vars_of t = rev (foldl_aterms
    4.28 +  (fn (vs, v as Var _) => v ins vs | (vs, _) => vs) ([], t));
    4.29 +
    4.30 +fun forall_intr (t, prop) =
    4.31 +  let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p)
    4.32 +  in all T $ Abs (a, T, abstract_over (t, prop)) end;
    4.33 +
    4.34 +fun forall_intr_vfs prop = foldr forall_intr
    4.35 +  (vars_of prop @ sort (make_ord atless) (term_frees prop), prop);
    4.36 +
    4.37 +fun merge_envs (Envir.Envir {asol=asol1, iTs=iTs1, maxidx=maxidx1})
    4.38 +  (Envir.Envir {asol=asol2, iTs=iTs2, maxidx=maxidx2}) =
    4.39 +    Envir.Envir {asol=Vartab.merge (op aconv) (asol1, asol2),
    4.40 +                 iTs=Vartab.merge (op =) (iTs1, iTs2),
    4.41 +                 maxidx=Int.max (maxidx1, maxidx2)};
    4.42 +
    4.43 +fun strip_abs (_::Ts) (Abs (_, _, t)) = strip_abs Ts t
    4.44 +  | strip_abs _ t = t;
    4.45 +
    4.46 +
    4.47 +(********************************************************************************
    4.48 +  generate constraints for proof term
    4.49 +*********************************************************************************)
    4.50 +
    4.51 +fun mk_var env Ts T = 
    4.52 +  let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T)
    4.53 +  in (env', list_comb (v, map Bound (length Ts - 1 downto 0))) end;
    4.54 +
    4.55 +fun mk_tvar (Envir.Envir {iTs, asol, maxidx}, s) =
    4.56 +  (Envir.Envir {iTs = iTs, asol = asol, maxidx = maxidx+1},
    4.57 +   TVar (("'t", maxidx+1), s));
    4.58 +
    4.59 +fun mk_abs Ts t = foldl (fn (u, T) => Abs ("", T, u)) (t, Ts);
    4.60 +
    4.61 +fun make_Tconstraints_cprf maxidx cprf =
    4.62 +  let
    4.63 +    fun mk_Tcnstrts maxidx Ts (Abst (s, Some T, cprf)) =
    4.64 +          let val (cs, cprf', maxidx') = mk_Tcnstrts maxidx (T::Ts) cprf;
    4.65 +          in (cs, Abst (s, Some T, cprf'), maxidx') end
    4.66 +      | mk_Tcnstrts maxidx Ts (Abst (s, None, cprf)) =
    4.67 +          let
    4.68 +            val T' = TVar (("'t", maxidx+1), ["logic"]);
    4.69 +            val (cs, cprf', maxidx') = mk_Tcnstrts (maxidx+1) (T'::Ts) cprf;
    4.70 +          in (cs, Abst (s, Some T', cprf'), maxidx') end
    4.71 +      | mk_Tcnstrts maxidx Ts (AbsP (s, Some t, cprf)) =
    4.72 +          let val (cs, cprf', maxidx') = mk_Tcnstrts maxidx Ts cprf;
    4.73 +          in ((mk_abs Ts t, rev Ts ---> propT)::cs, AbsP (s, Some t, cprf'), maxidx') end
    4.74 +      | mk_Tcnstrts maxidx Ts (AbsP (s, None, cprf)) =
    4.75 +          let val (cs, cprf', maxidx') = mk_Tcnstrts maxidx Ts cprf;
    4.76 +          in (cs, AbsP (s, None, cprf'), maxidx') end
    4.77 +      | mk_Tcnstrts maxidx Ts (cprf1 % cprf2) =
    4.78 +          let
    4.79 +            val (cs, cprf1', maxidx') = mk_Tcnstrts maxidx Ts cprf1;
    4.80 +            val (cs', cprf2', maxidx'') = mk_Tcnstrts maxidx' Ts cprf2;
    4.81 +          in (cs' @ cs, cprf1' % cprf2', maxidx'') end
    4.82 +      | mk_Tcnstrts maxidx Ts (cprf %% Some t) =
    4.83 +          let val (cs, cprf', maxidx') = mk_Tcnstrts maxidx Ts cprf;
    4.84 +          in ((mk_abs Ts t, rev Ts ---> TypeInfer.logicT)::cs,
    4.85 +            cprf' %% Some t, maxidx')
    4.86 +          end
    4.87 +      | mk_Tcnstrts maxidx Ts (cprf %% None) =
    4.88 +          let val (cs, cprf', maxidx') = mk_Tcnstrts maxidx Ts cprf;
    4.89 +          in (cs, cprf %% None, maxidx') end
    4.90 +      | mk_Tcnstrts maxidx _ cprf = ([], cprf, maxidx);
    4.91 +  in mk_Tcnstrts maxidx [] cprf end;
    4.92 +
    4.93 +fun unifyT sg env T U =
    4.94 +  let
    4.95 +    val Envir.Envir {asol, iTs, maxidx} = env;
    4.96 +    val (iTs', maxidx') = Type.unify (Sign.tsig_of sg) maxidx iTs (T, U)
    4.97 +  in Envir.Envir {asol=asol, iTs=iTs', maxidx=maxidx'} end;
    4.98 +
    4.99 +fun decompose sg env Ts
   4.100 +    (Const ("all", _) $ Abs (_, T, t)) (Const ("all", _) $ Abs (_, U, u)) =
   4.101 +      decompose sg (unifyT sg env T U) (T::Ts) t u
   4.102 +  | decompose sg env Ts
   4.103 +    (Const ("==>", _) $ t1 $ t2) (Const ("==>", _) $ u1 $ u2) =
   4.104 +      apsnd (cons (mk_abs Ts t1, mk_abs Ts u1)) (decompose sg env Ts t2 u2)
   4.105 +  | decompose sg env Ts t u = (env, [(mk_abs Ts t, mk_abs Ts u)]);
   4.106 +
   4.107 +fun cantunify sg t u = error ("Cannot unify:\n" ^
   4.108 +  Sign.string_of_term sg t ^ "\n\n" ^ Sign.string_of_term sg u);
   4.109 +
   4.110 +fun make_constraints_cprf sg env ts cprf =
   4.111 +  let
   4.112 +    fun add_cnstrt Ts prop prf cs env ts (t, u) =
   4.113 +      let
   4.114 +        val t' = mk_abs Ts t;
   4.115 +        val u' = mk_abs Ts u;
   4.116 +        val nt = Envir.norm_term env t';
   4.117 +        val nu = Envir.norm_term env u'
   4.118 +      in
   4.119 +        if Pattern.pattern nt andalso Pattern.pattern nu then
   4.120 +          let
   4.121 +            val env' = (Pattern.unify (sg, env, [(nt, nu)]) handle Pattern.Unif =>
   4.122 +                       cantunify sg nt nu);
   4.123 +          in (Envir.norm_term env' prop, prf, cs, env', ts) end
   4.124 +        else
   4.125 +          let val (env', cs') = decompose sg env [] nt nu
   4.126 +          in (Envir.norm_term env' prop, prf, cs @ cs', env', ts) end
   4.127 +      end;
   4.128 +
   4.129 +    fun mk_cnstrts_atom env ts prop opTs mk_prf =
   4.130 +          let
   4.131 +            val tvars = term_tvars prop;
   4.132 +            val (env', Ts) = if_none (apsome (pair env) opTs)
   4.133 +              (foldl_map (mk_tvar o apsnd snd) (env, tvars));
   4.134 +            val prop' = subst_TVars (map fst tvars ~~ Ts) (forall_intr_vfs prop);
   4.135 +          in (prop', mk_prf (Some Ts), [], env', ts) end;
   4.136 +
   4.137 +    fun mk_cnstrts env _ Hs ts (PBound i) = (nth_elem (i, Hs), PBound i, [], env, ts)
   4.138 +      | mk_cnstrts env Ts Hs ts (Abst (s, Some T, cprf)) =
   4.139 +          let val (t, prf, cnstrts, env', ts') =
   4.140 +              mk_cnstrts env (T::Ts) (map (incr_boundvars 1) Hs) ts cprf;
   4.141 +          in (Const ("all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, Some T, prf),
   4.142 +            cnstrts, env', ts')
   4.143 +          end
   4.144 +      | mk_cnstrts env Ts Hs (t::ts) (AbsP (s, Some _, cprf)) =
   4.145 +          let
   4.146 +            val (u, prf, cnstrts, env', ts') = mk_cnstrts env Ts (t::Hs) ts cprf;
   4.147 +            val t' = strip_abs Ts t;
   4.148 +          in (Logic.mk_implies (t', u), AbsP (s, Some t', prf), cnstrts, env', ts')
   4.149 +          end
   4.150 +      | mk_cnstrts env Ts Hs ts (AbsP (s, None, cprf)) =
   4.151 +          let
   4.152 +            val (env', t) = mk_var env Ts propT;
   4.153 +            val (u, prf, cnstrts, env'', ts') = mk_cnstrts env' Ts (t::Hs) ts cprf;
   4.154 +          in (Logic.mk_implies (t, u), AbsP (s, Some t, prf), cnstrts, env'', ts')
   4.155 +          end
   4.156 +      | mk_cnstrts env Ts Hs ts (cprf1 % cprf2) =
   4.157 +          let val (u, prf2, cnstrts, env', ts') = mk_cnstrts env Ts Hs ts cprf2
   4.158 +          in (case mk_cnstrts env' Ts Hs ts' cprf1 of
   4.159 +              (Const ("==>", _) $ u' $ t', prf1, cnstrts', env'', ts'') =>
   4.160 +                add_cnstrt Ts t' (prf1 % prf2) (cnstrts' @ cnstrts)
   4.161 +                  env'' ts'' (u, u')
   4.162 +            | (t, prf1, cnstrts', env'', ts'') =>
   4.163 +                let val (env''', v) = mk_var env'' Ts propT
   4.164 +                in add_cnstrt Ts v (prf1 % prf2) (cnstrts' @ cnstrts)
   4.165 +                  env''' ts'' (t, Logic.mk_implies (u, v))
   4.166 +                end)
   4.167 +          end
   4.168 +      | mk_cnstrts env Ts Hs (t::ts) (cprf %% Some _) =
   4.169 +          let val t' = strip_abs Ts t
   4.170 +          in (case mk_cnstrts env Ts Hs ts cprf of
   4.171 +             (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
   4.172 +                 prf, cnstrts, env', ts') =>
   4.173 +               let val env'' = unifyT sg env' T
   4.174 +                 (fastype_of1 (map (Envir.norm_type env') Ts, t'))
   4.175 +               in (betapply (f, t'), prf %% Some t', cnstrts, env'', ts')
   4.176 +               end
   4.177 +           | (u, prf, cnstrts, env', ts') =>
   4.178 +               let
   4.179 +                 val T = fastype_of1 (map (Envir.norm_type env') Ts, t');
   4.180 +                 val (env'', v) = mk_var env' Ts (T --> propT);
   4.181 +               in
   4.182 +                 add_cnstrt Ts (v $ t') (prf %% Some t') cnstrts env'' ts'
   4.183 +                   (u, Const ("all", (T --> propT) --> propT) $ v)
   4.184 +               end)
   4.185 +          end
   4.186 +      | mk_cnstrts env Ts Hs ts (cprf %% None) =
   4.187 +          (case mk_cnstrts env Ts Hs ts cprf of
   4.188 +             (Const ("all", Type ("fun", [Type ("fun", [T, _]), _])) $ f,
   4.189 +                 prf, cnstrts, env', ts') =>
   4.190 +               let val (env'', t) = mk_var env' Ts T
   4.191 +               in (betapply (f, t), prf %% Some t, cnstrts, env'', ts')
   4.192 +               end
   4.193 +           | (u, prf, cnstrts, env', ts') =>
   4.194 +               let
   4.195 +                 val (env1, T) = mk_tvar (env', ["logic"]);
   4.196 +                 val (env2, v) = mk_var env1 Ts (T --> propT);
   4.197 +                 val (env3, t) = mk_var env2 Ts T
   4.198 +               in
   4.199 +                 add_cnstrt Ts (v $ t) (prf %% Some t) cnstrts env3 ts'
   4.200 +                   (u, Const ("all", (T --> propT) --> propT) $ v)
   4.201 +               end)
   4.202 +      | mk_cnstrts env _ _ ts (PThm (name, prf, prop, opTs)) =
   4.203 +          mk_cnstrts_atom env ts prop opTs (fn x => PThm (name, prf, prop, x))
   4.204 +      | mk_cnstrts env _ _ ts (PAxm (name, prop, opTs)) =
   4.205 +          mk_cnstrts_atom env ts prop opTs (fn x => PAxm (name, prop, x))
   4.206 +      | mk_cnstrts env _ _ ts (Oracle (name, prop, opTs)) =
   4.207 +          mk_cnstrts_atom env ts prop opTs (fn x => Oracle (name, prop, x))
   4.208 +      | mk_cnstrts env _ _ ts (Hyp t) = (t, Hyp t, [], env, ts)
   4.209 +      | mk_cnstrts _ _ _ _ _ = error "reconstruct_prf: minimal proof object"
   4.210 +  in mk_cnstrts env [] [] ts cprf end;
   4.211 +
   4.212 +fun add_term_ixns (is, Var (i, T)) = add_typ_ixns (i ins is, T)
   4.213 +  | add_term_ixns (is, Free (_, T)) = add_typ_ixns (is, T)
   4.214 +  | add_term_ixns (is, Const (_, T)) = add_typ_ixns (is, T)
   4.215 +  | add_term_ixns (is, t1 $ t2) = add_term_ixns (add_term_ixns (is, t1), t2)
   4.216 +  | add_term_ixns (is, Abs (_, T, t)) = add_term_ixns (add_typ_ixns (is, T), t)
   4.217 +  | add_term_ixns (is, _) = is;
   4.218 +
   4.219 +
   4.220 +(********************************************************************************
   4.221 +  update list of free variables of constraints
   4.222 +*********************************************************************************)
   4.223 +
   4.224 +fun upd_constrs env cs =
   4.225 +  let
   4.226 +    val Envir.Envir {asol, iTs, ...} = env;
   4.227 +    val dom = Vartab.foldl (uncurry (cons o fst) o Library.swap)
   4.228 +      (Vartab.foldl (uncurry (cons o fst) o Library.swap) ([], asol), iTs); 
   4.229 +    val vran = Vartab.foldl (add_typ_ixns o apsnd snd)
   4.230 +      (Vartab.foldl (add_term_ixns o apsnd snd) ([], asol), iTs);
   4.231 +    fun check_cs [] = []
   4.232 +      | check_cs ((u, p, vs)::ps) =
   4.233 +          let val vs' = vs \\ dom;
   4.234 +          in if vs = vs' then (u, p, vs)::check_cs ps
   4.235 +             else (true, p, vs' union vran)::check_cs ps
   4.236 +          end
   4.237 +  in check_cs cs end;
   4.238 +
   4.239 +(********************************************************************************
   4.240 +  solution of constraints
   4.241 +*********************************************************************************)
   4.242 +
   4.243 +exception IMPOSS;
   4.244 +
   4.245 +fun solve _ [] bigenv = bigenv
   4.246 +  | solve sg cs bigenv =
   4.247 +      let
   4.248 +        fun search env [] = raise IMPOSS
   4.249 +          | search env ((u, p as (t1, t2), vs)::ps) =
   4.250 +              if u then
   4.251 +                let
   4.252 +                  val tn1 = Envir.norm_term bigenv t1;
   4.253 +                  val tn2 = Envir.norm_term bigenv t2
   4.254 +                in
   4.255 +                  if Pattern.pattern tn1 andalso Pattern.pattern tn2 then
   4.256 +                    ((Pattern.unify (sg, env, [(tn1, tn2)]), ps) handle Pattern.Unif =>
   4.257 +                       cantunify sg tn1 tn2)
   4.258 +                  else
   4.259 +                    let val (env', cs') = decompose sg env [] tn1 tn2
   4.260 +                    in if cs' = [(tn1, tn2)] then
   4.261 +                         apsnd (cons (false, (tn1, tn2), vs)) (search env ps)
   4.262 +                       else search env' (map (fn q => (true, q, vs)) cs' @ ps)
   4.263 +                    end
   4.264 +                end
   4.265 +              else apsnd (cons (false, p, vs)) (search env ps);
   4.266 +        val Envir.Envir {maxidx, ...} = bigenv;
   4.267 +        val (env, cs') = search (Envir.empty maxidx) cs;
   4.268 +      in
   4.269 +        solve sg (upd_constrs env cs') (merge_envs bigenv env)
   4.270 +      end;
   4.271 +
   4.272 +
   4.273 +(********************************************************************************
   4.274 +  reconstruction of proofs
   4.275 +*********************************************************************************)
   4.276 +
   4.277 +fun reconstruct_prf sg prop cprf =
   4.278 +  let
   4.279 +    val (cprf' %% Some prop', thawf) = freeze_thaw_prf (cprf %% Some prop);
   4.280 +    val _ = message "Collecting type constraints...";
   4.281 +    val (Tcs, cprf'', maxidx) = make_Tconstraints_cprf 0 cprf';
   4.282 +    val (ts, Ts) = ListPair.unzip Tcs;
   4.283 +    val tsig = Sign.tsig_of sg;
   4.284 +    val {classrel, arities, ...} = Type.rep_tsig tsig;
   4.285 +    val _ = message "Solving type constraints...";
   4.286 +    val (ts', _, unifier) = TypeInfer.infer_types (Sign.pretty_term sg) (Sign.pretty_typ sg)
   4.287 +      (Sign.const_type sg) classrel arities [] false (K true) ts Ts;
   4.288 +    val env = Envir.Envir {asol = Vartab.empty, iTs = Vartab.make unifier, maxidx = maxidx};
   4.289 +    val _ = message "Collecting term constraints...";
   4.290 +    val (t, prf, cs, env, _) = make_constraints_cprf sg env ts' cprf'';
   4.291 +    val cs' = map (fn p => (true, p, op union
   4.292 +      (pairself (map (fst o dest_Var) o term_vars) p))) (map (pairself (Envir.norm_term env)) ((t, prop')::cs));
   4.293 +    val _ = message ("Solving remaining constraints (" ^ string_of_int (length cs') ^ ") ...");
   4.294 +    val env' = solve sg cs' env
   4.295 +  in
   4.296 +    thawf (norm_proof env' prf)
   4.297 +  end;
   4.298 +
   4.299 +fun full_prf_of thm =
   4.300 +  let val {prop, der = (_, prf), sign, ...} = rep_thm thm
   4.301 +  in reconstruct_prf sign prop prf end;
   4.302 +
   4.303 +
   4.304 +(********************************************************************************
   4.305 +  expand and reconstruct subproofs
   4.306 +*********************************************************************************)
   4.307 +
   4.308 +fun full_forall_intr_proof prf x a T = Abst (a, Some T, prf_abstract_over x prf);
   4.309 +
   4.310 +fun expand_proof sg names prf =
   4.311 +  let
   4.312 +    fun expand prfs (AbsP (s, t, prf)) = 
   4.313 +          let val (prfs', prf') = expand prfs prf
   4.314 +          in (prfs', AbsP (s, t, prf')) end
   4.315 +      | expand prfs (Abst (s, T, prf)) = 
   4.316 +          let val (prfs', prf') = expand prfs prf
   4.317 +          in (prfs', Abst (s, T, prf')) end
   4.318 +      | expand prfs (prf1 % prf2) =
   4.319 +          let
   4.320 +            val (prfs', prf1') = expand prfs prf1;
   4.321 +            val (prfs'', prf2') = expand prfs' prf2;
   4.322 +          in (prfs'', prf1' % prf2') end
   4.323 +      | expand prfs (prf %% t) =
   4.324 +          let val (prfs', prf') = expand prfs prf
   4.325 +          in (prfs', prf' %% t) end
   4.326 +      | expand prfs (prf as PThm ((a, _), cprf, prop, Some Ts)) =
   4.327 +          if not (a mem names) then (prfs, prf) else
   4.328 +          let
   4.329 +            val (prf, prfs') = (case assoc (prfs, (a, prop)) of
   4.330 +                None =>
   4.331 +                  let
   4.332 +                    val _ = message ("Reconstructing proof of " ^ a);
   4.333 +                    val _ = message (Sign.string_of_term sg prop);
   4.334 +                    val prf = reconstruct_prf sg prop cprf
   4.335 +                  in (prf, ((a, prop), prf)::prfs) end
   4.336 +              | Some prf => (prf, prfs));
   4.337 +            val tvars = term_tvars prop;
   4.338 +            val vars = vars_of prop;
   4.339 +            val tye = map fst tvars ~~ Ts;
   4.340 +            fun abst (t as Var ((s, _), T), prf) = full_forall_intr_proof prf t s T;
   4.341 +            val prf' = map_proof_terms (subst_TVars tye) (typ_subst_TVars tye) prf
   4.342 +          in
   4.343 +            expand prfs' (foldr abst (map (subst_TVars tye) vars, prf'))
   4.344 +          end
   4.345 +      | expand prfs prf = (prfs, prf);
   4.346 +
   4.347 +  in snd (expand [] prf) end;
   4.348 +
   4.349 +end;