src/Pure/Proof/proofchecker.ML
author kleing
Mon Jun 21 10:25:57 2004 +0200 (2004-06-21)
changeset 14981 e73f8140af78
parent 13733 8ea7388f66d4
child 15001 fb2141a9f8c0
permissions -rw-r--r--
Merged in license change from Isabelle2004
berghofe@11522
     1
(*  Title:      Pure/Proof/proofchecker.ML
berghofe@11522
     2
    ID:         $Id$
wenzelm@11539
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@11522
     4
berghofe@11522
     5
Simple proof checker based only on the core inference rules
berghofe@11522
     6
of Isabelle/Pure.
berghofe@11522
     7
*)
berghofe@11522
     8
berghofe@11522
     9
signature PROOF_CHECKER =
berghofe@11522
    10
sig
berghofe@11522
    11
  val thm_of_proof : theory -> Proofterm.proof -> thm
berghofe@11522
    12
end;
berghofe@11522
    13
berghofe@11612
    14
structure ProofChecker : PROOF_CHECKER =
berghofe@11522
    15
struct
berghofe@11522
    16
berghofe@11522
    17
open Proofterm;
berghofe@11522
    18
berghofe@11522
    19
(***** construct a theorem out of a proof term *****)
berghofe@11522
    20
berghofe@11522
    21
fun lookup_thm thy =
berghofe@11522
    22
  let val tab = foldr Symtab.update
berghofe@11522
    23
    (flat (map thms_of (thy :: Theory.ancestors_of thy)), Symtab.empty)
berghofe@11522
    24
  in
berghofe@11522
    25
    (fn s => case Symtab.lookup (tab, s) of
berghofe@11522
    26
       None => error ("Unknown theorem " ^ quote s)
berghofe@11522
    27
     | Some thm => thm)
berghofe@11522
    28
  end;
berghofe@11522
    29
berghofe@13733
    30
val beta_eta_convert =
berghofe@13733
    31
  MetaSimplifier.fconv_rule MetaSimplifier.beta_eta_conversion;
berghofe@11522
    32
berghofe@11522
    33
fun thm_of_proof thy prf =
berghofe@11522
    34
  let
berghofe@11522
    35
    val names = add_prf_names ([], prf);
berghofe@11522
    36
    val sg = sign_of thy;
berghofe@11522
    37
    val lookup = lookup_thm thy;
berghofe@11522
    38
berghofe@13670
    39
    fun thm_of_atom thm Ts =
berghofe@13670
    40
      let
berghofe@13670
    41
        val tvars = term_tvars (prop_of thm);
berghofe@13670
    42
        val (thm', fmap) = Thm.varifyT' [] thm;
berghofe@13670
    43
        val ctye = map fst tvars @ map snd fmap ~~ map (Thm.ctyp_of sg) Ts
berghofe@13670
    44
      in
berghofe@13670
    45
        Thm.instantiate (ctye, []) (forall_intr_vars (forall_intr_frees thm'))
berghofe@13670
    46
      end;
berghofe@13670
    47
berghofe@11522
    48
    fun thm_of _ _ (PThm ((name, _), _, prop', Some Ts)) =
berghofe@11522
    49
          let
berghofe@13670
    50
            val thm = Thm.implies_intr_hyps (lookup name);
berghofe@11522
    51
            val {prop, ...} = rep_thm thm;
berghofe@13733
    52
            val _ = if prop aconv prop' then () else
berghofe@12238
    53
              error ("Duplicate use of theorem name " ^ quote name ^ "\n" ^
berghofe@12238
    54
                Sign.string_of_term sg prop ^ "\n\n" ^
berghofe@12238
    55
                Sign.string_of_term sg prop');
berghofe@13670
    56
          in thm_of_atom thm Ts end
berghofe@11522
    57
berghofe@11522
    58
      | thm_of _ _ (PAxm (name, _, Some Ts)) =
berghofe@13670
    59
          thm_of_atom (get_axiom thy name) Ts
berghofe@11522
    60
berghofe@11522
    61
      | thm_of _ Hs (PBound i) = nth_elem (i, Hs)
berghofe@11522
    62
berghofe@11522
    63
      | thm_of vs Hs (Abst (s, Some T, prf)) =
berghofe@11522
    64
          let
berghofe@11522
    65
            val x = variant (names @ map fst vs) s;
berghofe@11522
    66
            val thm = thm_of ((x, T) :: vs) Hs prf
berghofe@11522
    67
          in
berghofe@11522
    68
            Thm.forall_intr (Thm.cterm_of sg (Free (x, T))) thm
berghofe@11522
    69
          end
berghofe@11522
    70
berghofe@11612
    71
      | thm_of vs Hs (prf % Some t) =
berghofe@11522
    72
          let
berghofe@11522
    73
            val thm = thm_of vs Hs prf
berghofe@11522
    74
            val ct = Thm.cterm_of sg (Term.subst_bounds (map Free vs, t))
berghofe@11522
    75
          in Thm.forall_elim ct thm end
berghofe@11522
    76
berghofe@11522
    77
      | thm_of vs Hs (AbsP (s, Some t, prf)) =
berghofe@11522
    78
          let
berghofe@11522
    79
            val ct = Thm.cterm_of sg (Term.subst_bounds (map Free vs, t));
berghofe@11522
    80
            val thm = thm_of vs (Thm.assume ct :: Hs) prf
berghofe@11522
    81
          in
berghofe@11522
    82
            Thm.implies_intr ct thm
berghofe@11522
    83
          end
berghofe@11522
    84
berghofe@11612
    85
      | thm_of vs Hs (prf %% prf') =
berghofe@11522
    86
          let 
berghofe@11522
    87
            val thm = beta_eta_convert (thm_of vs Hs prf);
berghofe@11522
    88
            val thm' = beta_eta_convert (thm_of vs Hs prf')
berghofe@11522
    89
          in
berghofe@11522
    90
            Thm.implies_elim thm thm'
berghofe@11522
    91
          end
berghofe@11522
    92
berghofe@11522
    93
      | thm_of _ _ (Hyp t) = Thm.assume (Thm.cterm_of sg t)
berghofe@11522
    94
berghofe@11522
    95
      | thm_of _ _ _ = error "thm_of_proof: partial proof term";
berghofe@11522
    96
berghofe@13733
    97
  in beta_eta_convert (thm_of [] [] prf) end;
berghofe@11522
    98
berghofe@11522
    99
end;