| 11522 |      1 | (*  Title:      Pure/Proof/proofchecker.ML
 | 
|  |      2 |     ID:         $Id$
 | 
| 11539 |      3 |     Author:     Stefan Berghofer, TU Muenchen
 | 
|  |      4 |     License:    GPL (GNU GENERAL PUBLIC LICENSE)
 | 
| 11522 |      5 | 
 | 
|  |      6 | Simple proof checker based only on the core inference rules
 | 
|  |      7 | of Isabelle/Pure.
 | 
|  |      8 | *)
 | 
|  |      9 | 
 | 
|  |     10 | signature PROOF_CHECKER =
 | 
|  |     11 | sig
 | 
|  |     12 |   val thm_of_proof : theory -> Proofterm.proof -> thm
 | 
|  |     13 | end;
 | 
|  |     14 | 
 | 
| 11612 |     15 | structure ProofChecker : PROOF_CHECKER =
 | 
| 11522 |     16 | struct
 | 
|  |     17 | 
 | 
|  |     18 | open Proofterm;
 | 
|  |     19 | 
 | 
|  |     20 | (***** construct a theorem out of a proof term *****)
 | 
|  |     21 | 
 | 
|  |     22 | fun lookup_thm thy =
 | 
|  |     23 |   let val tab = foldr Symtab.update
 | 
|  |     24 |     (flat (map thms_of (thy :: Theory.ancestors_of thy)), Symtab.empty)
 | 
|  |     25 |   in
 | 
|  |     26 |     (fn s => case Symtab.lookup (tab, s) of
 | 
|  |     27 |        None => error ("Unknown theorem " ^ quote s)
 | 
|  |     28 |      | Some thm => thm)
 | 
|  |     29 |   end;
 | 
|  |     30 | 
 | 
|  |     31 | fun beta_eta_convert thm =
 | 
|  |     32 |   let
 | 
|  |     33 |     val beta_thm = beta_conversion true (cprop_of thm);
 | 
|  |     34 |     val (_, rhs) = Drule.dest_equals (cprop_of beta_thm);
 | 
|  |     35 |   in Thm.equal_elim (Thm.transitive beta_thm (eta_conversion rhs)) thm end;
 | 
|  |     36 | 
 | 
|  |     37 | fun thm_of_proof thy prf =
 | 
|  |     38 |   let
 | 
|  |     39 |     val names = add_prf_names ([], prf);
 | 
|  |     40 |     val sg = sign_of thy;
 | 
|  |     41 |     val lookup = lookup_thm thy;
 | 
|  |     42 | 
 | 
|  |     43 |     fun thm_of _ _ (PThm ((name, _), _, prop', Some Ts)) =
 | 
|  |     44 |           let
 | 
|  |     45 |             val thm = lookup name;
 | 
|  |     46 |             val {prop, ...} = rep_thm thm;
 | 
|  |     47 |             val _ = if prop=prop' then () else
 | 
| 12238 |     48 |               error ("Duplicate use of theorem name " ^ quote name ^ "\n" ^
 | 
|  |     49 |                 Sign.string_of_term sg prop ^ "\n\n" ^
 | 
|  |     50 |                 Sign.string_of_term sg prop');
 | 
| 11522 |     51 |             val tvars = term_tvars prop;
 | 
|  |     52 |             val ctye = map fst tvars ~~ map (Thm.ctyp_of sg) Ts
 | 
|  |     53 |           in
 | 
|  |     54 |             Thm.instantiate (ctye, []) (forall_intr_vars thm)
 | 
|  |     55 |           end
 | 
|  |     56 | 
 | 
|  |     57 |       | thm_of _ _ (PAxm (name, _, Some Ts)) =
 | 
|  |     58 |           let
 | 
|  |     59 |             val thm = get_axiom thy name;
 | 
|  |     60 |             val {prop, ...} = rep_thm thm;
 | 
|  |     61 |             val tvars = term_tvars prop;
 | 
|  |     62 |             val ctye = map fst tvars ~~ map (Thm.ctyp_of sg) Ts
 | 
|  |     63 |           in
 | 
|  |     64 |             Thm.instantiate (ctye, []) (forall_intr_vars thm)
 | 
|  |     65 |           end
 | 
|  |     66 | 
 | 
|  |     67 |       | thm_of _ Hs (PBound i) = nth_elem (i, Hs)
 | 
|  |     68 | 
 | 
|  |     69 |       | thm_of vs Hs (Abst (s, Some T, prf)) =
 | 
|  |     70 |           let
 | 
|  |     71 |             val x = variant (names @ map fst vs) s;
 | 
|  |     72 |             val thm = thm_of ((x, T) :: vs) Hs prf
 | 
|  |     73 |           in
 | 
|  |     74 |             Thm.forall_intr (Thm.cterm_of sg (Free (x, T))) thm
 | 
|  |     75 |           end
 | 
|  |     76 | 
 | 
| 11612 |     77 |       | thm_of vs Hs (prf % Some t) =
 | 
| 11522 |     78 |           let
 | 
|  |     79 |             val thm = thm_of vs Hs prf
 | 
|  |     80 |             val ct = Thm.cterm_of sg (Term.subst_bounds (map Free vs, t))
 | 
|  |     81 |           in Thm.forall_elim ct thm end
 | 
|  |     82 | 
 | 
|  |     83 |       | thm_of vs Hs (AbsP (s, Some t, prf)) =
 | 
|  |     84 |           let
 | 
|  |     85 |             val ct = Thm.cterm_of sg (Term.subst_bounds (map Free vs, t));
 | 
|  |     86 |             val thm = thm_of vs (Thm.assume ct :: Hs) prf
 | 
|  |     87 |           in
 | 
|  |     88 |             Thm.implies_intr ct thm
 | 
|  |     89 |           end
 | 
|  |     90 | 
 | 
| 11612 |     91 |       | thm_of vs Hs (prf %% prf') =
 | 
| 11522 |     92 |           let 
 | 
|  |     93 |             val thm = beta_eta_convert (thm_of vs Hs prf);
 | 
|  |     94 |             val thm' = beta_eta_convert (thm_of vs Hs prf')
 | 
|  |     95 |           in
 | 
|  |     96 |             Thm.implies_elim thm thm'
 | 
|  |     97 |           end
 | 
|  |     98 | 
 | 
|  |     99 |       | thm_of _ _ (Hyp t) = Thm.assume (Thm.cterm_of sg t)
 | 
|  |    100 | 
 | 
|  |    101 |       | thm_of _ _ _ = error "thm_of_proof: partial proof term";
 | 
|  |    102 | 
 | 
|  |    103 |   in thm_of [] [] prf end;
 | 
|  |    104 | 
 | 
|  |    105 | end;
 | 
|  |    106 | 
 |