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