src/Pure/Proof/proofchecker.ML
author wenzelm
Thu, 03 Jun 2010 23:56:05 +0200
changeset 37310 96e2b9a6f074
parent 37229 47eb565796f4
child 39814 fe5722fce758
permissions -rw-r--r--
do not open Proofterm, which is very ould style;
berghofe@11522
     1
(*  Title:      Pure/Proof/proofchecker.ML
wenzelm@11539
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@11522
     3
berghofe@11522
     4
Simple proof checker based only on the core inference rules
berghofe@11522
     5
of Isabelle/Pure.
berghofe@11522
     6
*)
berghofe@11522
     7
berghofe@11522
     8
signature PROOF_CHECKER =
berghofe@11522
     9
sig
berghofe@11522
    10
  val thm_of_proof : theory -> Proofterm.proof -> thm
berghofe@11522
    11
end;
berghofe@11522
    12
berghofe@11612
    13
structure ProofChecker : PROOF_CHECKER =
berghofe@11522
    14
struct
berghofe@11522
    15
berghofe@11522
    16
(***** construct a theorem out of a proof term *****)
berghofe@11522
    17
berghofe@11522
    18
fun lookup_thm thy =
wenzelm@17412
    19
  let val tab = fold_rev Symtab.update (PureThy.all_thms_of thy) Symtab.empty
berghofe@11522
    20
  in
wenzelm@17412
    21
    (fn s => case Symtab.lookup tab s of
skalberg@15531
    22
       NONE => error ("Unknown theorem " ^ quote s)
skalberg@15531
    23
     | SOME thm => thm)
berghofe@11522
    24
  end;
berghofe@11522
    25
berghofe@13733
    26
val beta_eta_convert =
wenzelm@22910
    27
  Conv.fconv_rule Drule.beta_eta_conversion;
berghofe@11522
    28
berghofe@37229
    29
(* equality modulo renaming of type variables *)
berghofe@37229
    30
fun is_equal t t' =
berghofe@37229
    31
  let
berghofe@37229
    32
    val atoms = fold_types (fold_atyps (insert (op =))) t [];
berghofe@37229
    33
    val atoms' = fold_types (fold_atyps (insert (op =))) t' []
berghofe@37229
    34
  in
berghofe@37229
    35
    length atoms = length atoms' andalso
berghofe@37229
    36
    map_types (map_atyps (the o AList.lookup (op =) (atoms ~~ atoms'))) t aconv t'
berghofe@37229
    37
  end;
berghofe@37229
    38
berghofe@37229
    39
fun pretty_prf thy vs Hs prf =
wenzelm@37310
    40
  let val prf' = prf |> Proofterm.prf_subst_bounds (map Free vs) |>
wenzelm@37310
    41
    Proofterm.prf_subst_pbounds (map (Hyp o prop_of) Hs)
berghofe@37229
    42
  in
berghofe@37229
    43
    (Proof_Syntax.pretty_proof (Syntax.init_pretty_global thy) prf',
berghofe@37229
    44
     Syntax.pretty_term_global thy (Reconstruct.prop_of prf'))
berghofe@37229
    45
  end;
berghofe@37229
    46
berghofe@37229
    47
fun pretty_term thy vs _ t =
berghofe@37229
    48
  let val t' = subst_bounds (map Free vs, t)
berghofe@37229
    49
  in
berghofe@37229
    50
    (Syntax.pretty_term_global thy t',
berghofe@37229
    51
     Syntax.pretty_typ_global thy (fastype_of t'))
berghofe@37229
    52
  end;
berghofe@37229
    53
berghofe@37229
    54
fun appl_error thy prt vs Hs s f a =
berghofe@37229
    55
  let
berghofe@37229
    56
    val (pp_f, pp_fT) = pretty_prf thy vs Hs f;
berghofe@37229
    57
    val (pp_a, pp_aT) = prt thy vs Hs a
berghofe@37229
    58
  in
berghofe@37229
    59
    error (cat_lines
berghofe@37229
    60
      [s,
berghofe@37229
    61
       "",
berghofe@37229
    62
       Pretty.string_of (Pretty.block
berghofe@37229
    63
         [Pretty.str "Operator:", Pretty.brk 2, pp_f,
berghofe@37229
    64
           Pretty.str " ::", Pretty.brk 1, pp_fT]),
berghofe@37229
    65
       Pretty.string_of (Pretty.block
berghofe@37229
    66
         [Pretty.str "Operand:", Pretty.brk 3, pp_a,
berghofe@37229
    67
           Pretty.str " ::", Pretty.brk 1, pp_aT]),
berghofe@37229
    68
       ""])
berghofe@37229
    69
  end;
berghofe@37229
    70
berghofe@11522
    71
fun thm_of_proof thy prf =
berghofe@11522
    72
  let
wenzelm@29277
    73
    val prf_names = Proofterm.fold_proof_terms Term.declare_term_frees (K I) prf Name.context;
berghofe@11522
    74
    val lookup = lookup_thm thy;
berghofe@11522
    75
berghofe@13670
    76
    fun thm_of_atom thm Ts =
berghofe@13670
    77
      let
krauss@36042
    78
        val tvars = Term.add_tvars (Thm.full_prop_of thm) [] |> rev;
wenzelm@35845
    79
        val (fmap, thm') = Thm.varifyT_global' [] thm;
wenzelm@20151
    80
        val ctye = map (pairself (Thm.ctyp_of thy))
berghofe@15798
    81
          (map TVar tvars @ map (fn ((_, S), ixn) => TVar (ixn, S)) fmap ~~ Ts)
berghofe@13670
    82
      in
wenzelm@35986
    83
        Thm.instantiate (ctye, []) (forall_intr_vars (Thm.forall_intr_frees thm'))
berghofe@13670
    84
      end;
berghofe@13670
    85
wenzelm@28808
    86
    fun thm_of _ _ (PThm (_, ((name, prop', SOME Ts), _))) =
berghofe@11522
    87
          let
berghofe@37229
    88
            val thm = Thm.unconstrainT (Drule.implies_intr_hyps (lookup name));
berghofe@11522
    89
            val {prop, ...} = rep_thm thm;
berghofe@37229
    90
            val _ = if is_equal prop prop' then () else
berghofe@12238
    91
              error ("Duplicate use of theorem name " ^ quote name ^ "\n" ^
wenzelm@26939
    92
                Syntax.string_of_term_global thy prop ^ "\n\n" ^
wenzelm@26939
    93
                Syntax.string_of_term_global thy prop');
berghofe@13670
    94
          in thm_of_atom thm Ts end
berghofe@11522
    95
skalberg@15531
    96
      | thm_of _ _ (PAxm (name, _, SOME Ts)) =
wenzelm@28674
    97
          thm_of_atom (Thm.axiom thy name) Ts
berghofe@11522
    98
wenzelm@30146
    99
      | thm_of _ Hs (PBound i) = nth Hs i
berghofe@11522
   100
wenzelm@20164
   101
      | thm_of (vs, names) Hs (Abst (s, SOME T, prf)) =
berghofe@11522
   102
          let
wenzelm@20164
   103
            val ([x], names') = Name.variants [s] names;
wenzelm@20164
   104
            val thm = thm_of ((x, T) :: vs, names') Hs prf
berghofe@11522
   105
          in
wenzelm@20151
   106
            Thm.forall_intr (Thm.cterm_of thy (Free (x, T))) thm
berghofe@11522
   107
          end
berghofe@11522
   108
wenzelm@20164
   109
      | thm_of (vs, names) Hs (prf % SOME t) =
berghofe@11522
   110
          let
wenzelm@20164
   111
            val thm = thm_of (vs, names) Hs prf;
wenzelm@20164
   112
            val ct = Thm.cterm_of thy (Term.subst_bounds (map Free vs, t));
berghofe@37229
   113
          in
berghofe@37229
   114
            Thm.forall_elim ct thm
berghofe@37229
   115
            handle THM (s, _, _) => appl_error thy pretty_term vs Hs s prf t
berghofe@37229
   116
          end
berghofe@11522
   117
wenzelm@20164
   118
      | thm_of (vs, names) Hs (AbsP (s, SOME t, prf)) =
berghofe@11522
   119
          let
wenzelm@20151
   120
            val ct = Thm.cterm_of thy (Term.subst_bounds (map Free vs, t));
wenzelm@20164
   121
            val thm = thm_of (vs, names) (Thm.assume ct :: Hs) prf;
berghofe@11522
   122
          in
berghofe@11522
   123
            Thm.implies_intr ct thm
berghofe@11522
   124
          end
berghofe@11522
   125
wenzelm@20164
   126
      | thm_of vars Hs (prf %% prf') =
wenzelm@20151
   127
          let
wenzelm@20164
   128
            val thm = beta_eta_convert (thm_of vars Hs prf);
wenzelm@20164
   129
            val thm' = beta_eta_convert (thm_of vars Hs prf');
berghofe@11522
   130
          in
berghofe@11522
   131
            Thm.implies_elim thm thm'
berghofe@37229
   132
            handle THM (s, _, _) => appl_error thy pretty_prf (fst vars) Hs s prf prf'
berghofe@11522
   133
          end
berghofe@11522
   134
wenzelm@20151
   135
      | thm_of _ _ (Hyp t) = Thm.assume (Thm.cterm_of thy t)
berghofe@11522
   136
berghofe@11522
   137
      | thm_of _ _ _ = error "thm_of_proof: partial proof term";
berghofe@11522
   138
wenzelm@20164
   139
  in beta_eta_convert (thm_of ([], prf_names) [] prf) end;
berghofe@11522
   140
berghofe@11522
   141
end;