src/HOL/Nominal/nominal_thmdecls.ML
author haftmann
Sat Aug 28 16:14:32 2010 +0200 (2010-08-28)
changeset 38864 4abe644fcea5
parent 38808 89ae86205739
child 39557 fe5722fce758
permissions -rw-r--r--
formerly unnamed infix equality now named HOL.eq
     1 (*  Title:      HOL/Nominal/nominal_thmdecls.ML
     2     Author:     Julien Narboux, TU Muenchen
     3     Author:     Christian Urban, TU Muenchen
     4 
     5 Infrastructure for the lemma collection "eqvts".
     6 
     7 By attaching [eqvt] or [eqvt_force] to a lemma, it will get stored in
     8 a data-slot in the context. Possible modifiers are [... add] and
     9 [... del] for adding and deleting, respectively, the lemma from the
    10 data-slot.
    11 *)
    12 
    13 signature NOMINAL_THMDECLS =
    14 sig
    15   val eqvt_add: attribute
    16   val eqvt_del: attribute
    17   val eqvt_force_add: attribute
    18   val eqvt_force_del: attribute
    19   val setup: theory -> theory
    20   val get_eqvt_thms: Proof.context -> thm list
    21 end;
    22 
    23 structure NominalThmDecls: NOMINAL_THMDECLS =
    24 struct
    25 
    26 structure Data = Generic_Data
    27 (
    28   type T = thm list
    29   val empty = []
    30   val extend = I
    31   val merge = Thm.merge_thms
    32 )
    33 
    34 (* Exception for when a theorem does not conform with form of an equivariance lemma. *)
    35 (* There are two forms: one is an implication (for relations) and the other is an    *)
    36 (* equality (for functions). In the implication-case, say P ==> Q, Q must be equal   *)
    37 (* to P except that every free variable of Q, say x, is replaced by pi o x. In the   *)
    38 (* equality case, say lhs = rhs, the lhs must be of the form pi o t and the rhs must *)
    39 (* be equal to t except that every free variable, say x, is replaced by pi o x. In   *)
    40 (* the implicational case it is also checked that the variables and permutation fit  *)
    41 (* together, i.e. are of the right "pt_class", so that a stronger version of the     *)
    42 (* equality-lemma can be derived. *)
    43 exception EQVT_FORM of string
    44 
    45 val (nominal_eqvt_debug, setup_nominal_eqvt_debug) =
    46   Attrib.config_bool "nominal_eqvt_debug" (K false);
    47 
    48 fun tactic ctxt (msg, tac) =
    49   if Config.get ctxt nominal_eqvt_debug
    50   then tac THEN' (K (print_tac ("after " ^ msg)))
    51   else tac
    52 
    53 fun prove_eqvt_tac ctxt orig_thm pi pi' =
    54   let
    55     val thy = ProofContext.theory_of ctxt
    56     val mypi = Thm.cterm_of thy pi
    57     val T = fastype_of pi'
    58     val mypifree = Thm.cterm_of thy (Const (@{const_name "rev"}, T --> T) $ pi')
    59     val perm_pi_simp = PureThy.get_thms thy "perm_pi_simp"
    60   in
    61     EVERY1 [tactic ctxt ("iffI applied", rtac @{thm iffI}),
    62             tactic ctxt ("remove pi with perm_boolE", dtac @{thm perm_boolE}),
    63             tactic ctxt ("solve with orig_thm", etac orig_thm),
    64             tactic ctxt ("applies orig_thm instantiated with rev pi",
    65                        dtac (Drule.cterm_instantiate [(mypi,mypifree)] orig_thm)),
    66             tactic ctxt ("getting rid of the pi on the right", rtac @{thm perm_boolI}),
    67             tactic ctxt ("getting rid of all remaining perms",
    68                        full_simp_tac (HOL_basic_ss addsimps perm_pi_simp))]
    69   end;
    70 
    71 fun get_derived_thm ctxt hyp concl orig_thm pi typi =
    72   let
    73     val thy = ProofContext.theory_of ctxt;
    74     val pi' = Var (pi, typi);
    75     val lhs = Const (@{const_name "perm"}, typi --> HOLogic.boolT --> HOLogic.boolT) $ pi' $ hyp;
    76     val ([goal_term, pi''], ctxt') = Variable.import_terms false
    77       [HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, concl)), pi'] ctxt
    78     val _ = writeln (Syntax.string_of_term ctxt' goal_term);
    79   in
    80     Goal.prove ctxt' [] [] goal_term
    81       (fn _ => prove_eqvt_tac ctxt' orig_thm pi' pi'') |>
    82     singleton (ProofContext.export ctxt' ctxt)
    83   end
    84 
    85 (* replaces in t every variable, say x, with pi o x *)
    86 fun apply_pi trm (pi, typi) =
    87 let
    88   fun replace n ty =
    89   let 
    90     val c  = Const (@{const_name "perm"}, typi --> ty --> ty) 
    91     val v1 = Var (pi, typi)
    92     val v2 = Var (n, ty)
    93   in
    94     c $ v1 $ v2 
    95   end
    96 in
    97   map_aterms (fn Var (n, ty) => replace n ty | t => t) trm
    98 end
    99 
   100 (* returns *the* pi which is in front of all variables, provided there *)
   101 (* exists such a pi; otherwise raises EQVT_FORM                        *)
   102 fun get_pi t thy =
   103   let fun get_pi_aux s =
   104         (case s of
   105           (Const (@{const_name "perm"} ,typrm) $
   106              (Var (pi,typi as Type(@{type_name "list"}, [Type (@{type_name Product_Type.prod}, [Type (tyatm,[]),_])]))) $
   107                (Var (n,ty))) =>
   108              let
   109                 (* FIXME: this should be an operation the library *)
   110                 val class_name = (Long_Name.map_base_name (fn s => "pt_"^s) tyatm)
   111              in
   112                 if (Sign.of_sort thy (ty,[class_name]))
   113                 then [(pi,typi)]
   114                 else raise
   115                 EQVT_FORM ("Could not find any permutation or an argument is not an instance of "^class_name)
   116              end
   117         | Abs (_,_,t1) => get_pi_aux t1
   118         | (t1 $ t2) => get_pi_aux t1 @ get_pi_aux t2
   119         | _ => [])
   120   in
   121     (* collect first all pi's in front of variables in t and then use distinct *)
   122     (* to ensure that all pi's must have been the same, i.e. distinct returns  *)
   123     (* a singleton-list  *)
   124     (case (distinct (op =) (get_pi_aux t)) of
   125       [(pi,typi)] => (pi, typi)
   126     | _ => raise EQVT_FORM "All permutation should be the same")
   127   end;
   128 
   129 (* Either adds a theorem (orig_thm) to or deletes one from the equivariance *)
   130 (* lemma list depending on flag. To be added the lemma has to satisfy a     *)
   131 (* certain form. *)
   132 
   133 fun eqvt_add_del_aux flag orig_thm context = 
   134   let
   135     val thy = Context.theory_of context
   136     val thms_to_be_added = (case (prop_of orig_thm) of
   137         (* case: eqvt-lemma is of the implicational form *)
   138         (Const("==>", _) $ (Const (@{const_name Trueprop},_) $ hyp) $ (Const (@{const_name Trueprop},_) $ concl)) =>
   139           let
   140             val (pi,typi) = get_pi concl thy
   141           in
   142              if (apply_pi hyp (pi,typi) = concl)
   143              then
   144                (warning ("equivariance lemma of the relational form");
   145                 [orig_thm,
   146                  get_derived_thm (Context.proof_of context) hyp concl orig_thm pi typi])
   147              else raise EQVT_FORM "Type Implication"
   148           end
   149        (* case: eqvt-lemma is of the equational form *)
   150       | (Const (@{const_name Trueprop}, _) $ (Const (@{const_name HOL.eq}, _) $
   151             (Const (@{const_name "perm"},typrm) $ Var (pi,typi) $ lhs) $ rhs)) =>
   152            (if (apply_pi lhs (pi,typi)) = rhs
   153                then [orig_thm]
   154                else raise EQVT_FORM "Type Equality")
   155       | _ => raise EQVT_FORM "Type unknown")
   156   in
   157       fold (fn thm => Data.map (flag thm)) thms_to_be_added context
   158   end
   159   handle EQVT_FORM s =>
   160       error (Display.string_of_thm (Context.proof_of context) orig_thm ^ 
   161                " does not comply with the form of an equivariance lemma (" ^ s ^").")
   162 
   163 
   164 val eqvt_add = Thm.declaration_attribute (eqvt_add_del_aux (Thm.add_thm));
   165 val eqvt_del = Thm.declaration_attribute (eqvt_add_del_aux (Thm.del_thm));
   166 
   167 val eqvt_force_add  = Thm.declaration_attribute (Data.map o Thm.add_thm);
   168 val eqvt_force_del  = Thm.declaration_attribute (Data.map o Thm.del_thm);
   169 
   170 val get_eqvt_thms = Context.Proof #> Data.get;
   171 
   172 val setup =
   173   setup_nominal_eqvt_debug #>
   174   Attrib.setup @{binding eqvt} (Attrib.add_del eqvt_add eqvt_del) 
   175    "equivariance theorem declaration" #>
   176   Attrib.setup @{binding eqvt_force} (Attrib.add_del eqvt_force_add eqvt_force_del)
   177     "equivariance theorem declaration (without checking the form of the lemma)" #>
   178   PureThy.add_thms_dynamic (@{binding eqvts}, Data.get);
   179 
   180 
   181 end;