src/Pure/Proof/proofchecker.ML
author wenzelm
Sat, 13 Dec 2008 15:00:39 +0100
changeset 29091 b81fe045e799
parent 28808 7925199a0226
child 29270 0eade173f77e
permissions -rw-r--r--
Context.display_names;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     1
(*  Title:      Pure/Proof/proofchecker.ML
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
11539
0f17da240450 tuned headers;
wenzelm
parents: 11522
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     4
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     5
Simple proof checker based only on the core inference rules
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     6
of Isabelle/Pure.
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     7
*)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     8
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     9
signature PROOF_CHECKER =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    10
sig
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    11
  val thm_of_proof : theory -> Proofterm.proof -> thm
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    12
end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    13
11612
ae8450657bf0 Exchanged % and %%.
berghofe
parents: 11539
diff changeset
    14
structure ProofChecker : PROOF_CHECKER =
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    15
struct
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    16
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    17
open Proofterm;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    18
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    19
(***** construct a theorem out of a proof term *****)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    20
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    21
fun lookup_thm thy =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
    22
  let val tab = fold_rev Symtab.update (PureThy.all_thms_of thy) Symtab.empty
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    23
  in
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17223
diff changeset
    24
    (fn s => case Symtab.lookup tab s of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15001
diff changeset
    25
       NONE => error ("Unknown theorem " ^ quote s)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15001
diff changeset
    26
     | SOME thm => thm)
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    27
  end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    28
13733
8ea7388f66d4 - tuned beta_eta_convert
berghofe
parents: 13670
diff changeset
    29
val beta_eta_convert =
22910
54d231cbc19a moved conversions to structure Conv;
wenzelm
parents: 21646
diff changeset
    30
  Conv.fconv_rule Drule.beta_eta_conversion;
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    31
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    32
fun thm_of_proof thy prf =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    33
  let
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    34
    val prf_names = Proofterm.fold_proof_terms
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    35
      (fold_aterms (fn Free (x, _) => Name.declare x | _ => I)) (K I) prf Name.context;
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    36
    val lookup = lookup_thm thy;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    37
13670
c71b905a852a Fixed problem with theorems containing TFrees.
berghofe
parents: 12238
diff changeset
    38
    fun thm_of_atom thm Ts =
c71b905a852a Fixed problem with theorems containing TFrees.
berghofe
parents: 12238
diff changeset
    39
      let
16862
wenzelm
parents: 16608
diff changeset
    40
        val tvars = term_tvars (Thm.full_prop_of thm);
18127
9f03d8a9a81b Thm.varifyT': natural argument order;
wenzelm
parents: 17412
diff changeset
    41
        val (fmap, thm') = Thm.varifyT' [] thm;
20151
b22c14181eb7 thm_of_proof: tuned Name operations;
wenzelm
parents: 20071
diff changeset
    42
        val ctye = map (pairself (Thm.ctyp_of thy))
15798
016f3be5a5ec Adapted to new interface of instantiation and unification / matching functions.
berghofe
parents: 15574
diff changeset
    43
          (map TVar tvars @ map (fn ((_, S), ixn) => TVar (ixn, S)) fmap ~~ Ts)
13670
c71b905a852a Fixed problem with theorems containing TFrees.
berghofe
parents: 12238
diff changeset
    44
      in
c71b905a852a Fixed problem with theorems containing TFrees.
berghofe
parents: 12238
diff changeset
    45
        Thm.instantiate (ctye, []) (forall_intr_vars (forall_intr_frees thm'))
c71b905a852a Fixed problem with theorems containing TFrees.
berghofe
parents: 12238
diff changeset
    46
      end;
c71b905a852a Fixed problem with theorems containing TFrees.
berghofe
parents: 12238
diff changeset
    47
28808
7925199a0226 adapted PThm;
wenzelm
parents: 28674
diff changeset
    48
    fun thm_of _ _ (PThm (_, ((name, prop', SOME Ts), _))) =
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    49
          let
16608
4f8d7b83c7e2 Drule.implies_intr_hyps;
wenzelm
parents: 16351
diff changeset
    50
            val thm = Drule.implies_intr_hyps (lookup name);
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    51
            val {prop, ...} = rep_thm thm;
13733
8ea7388f66d4 - tuned beta_eta_convert
berghofe
parents: 13670
diff changeset
    52
            val _ = if prop aconv prop' then () else
12238
09966ccbc84c Improved error message.
berghofe
parents: 11612
diff changeset
    53
              error ("Duplicate use of theorem name " ^ quote name ^ "\n" ^
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 22910
diff changeset
    54
                Syntax.string_of_term_global thy prop ^ "\n\n" ^
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 22910
diff changeset
    55
                Syntax.string_of_term_global thy prop');
13670
c71b905a852a Fixed problem with theorems containing TFrees.
berghofe
parents: 12238
diff changeset
    56
          in thm_of_atom thm Ts end
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    57
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15001
diff changeset
    58
      | thm_of _ _ (PAxm (name, _, SOME Ts)) =
28674
08a77c495dc1 renamed Thm.get_axiom_i to Thm.axiom;
wenzelm
parents: 26939
diff changeset
    59
          thm_of_atom (Thm.axiom thy name) Ts
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    60
15570
8d8c70b41bab Move towards standard functions.
skalberg
parents: 15531
diff changeset
    61
      | thm_of _ Hs (PBound i) = List.nth (Hs, i)
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    62
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    63
      | thm_of (vs, names) Hs (Abst (s, SOME T, prf)) =
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    64
          let
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    65
            val ([x], names') = Name.variants [s] names;
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    66
            val thm = thm_of ((x, T) :: vs, names') Hs prf
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    67
          in
20151
b22c14181eb7 thm_of_proof: tuned Name operations;
wenzelm
parents: 20071
diff changeset
    68
            Thm.forall_intr (Thm.cterm_of thy (Free (x, T))) thm
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    69
          end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    70
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    71
      | thm_of (vs, names) Hs (prf % SOME t) =
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    72
          let
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    73
            val thm = thm_of (vs, names) Hs prf;
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    74
            val ct = Thm.cterm_of thy (Term.subst_bounds (map Free vs, t));
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    75
          in Thm.forall_elim ct thm end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    76
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    77
      | thm_of (vs, names) Hs (AbsP (s, SOME t, prf)) =
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    78
          let
20151
b22c14181eb7 thm_of_proof: tuned Name operations;
wenzelm
parents: 20071
diff changeset
    79
            val ct = Thm.cterm_of thy (Term.subst_bounds (map Free vs, t));
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    80
            val thm = thm_of (vs, names) (Thm.assume ct :: Hs) prf;
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    81
          in
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    82
            Thm.implies_intr ct thm
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    83
          end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    84
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    85
      | thm_of vars Hs (prf %% prf') =
20151
b22c14181eb7 thm_of_proof: tuned Name operations;
wenzelm
parents: 20071
diff changeset
    86
          let
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    87
            val thm = beta_eta_convert (thm_of vars Hs prf);
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    88
            val thm' = beta_eta_convert (thm_of vars Hs prf');
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    89
          in
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    90
            Thm.implies_elim thm thm'
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    91
          end
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    92
20151
b22c14181eb7 thm_of_proof: tuned Name operations;
wenzelm
parents: 20071
diff changeset
    93
      | thm_of _ _ (Hyp t) = Thm.assume (Thm.cterm_of thy t)
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    94
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    95
      | thm_of _ _ _ = error "thm_of_proof: partial proof term";
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    96
20164
928c8dc07216 thm_of_proof: improved generation of variables;
wenzelm
parents: 20151
diff changeset
    97
  in beta_eta_convert (thm_of ([], prf_names) [] prf) end;
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    98
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    99
end;