src/HOL/Nominal/nominal_permeq.ML
author berghofe
Fri Apr 28 15:54:34 2006 +0200 (2006-04-28)
changeset 19494 2e909d5309f4
parent 19477 a95176d0f0dd
child 19857 a0c36e0fc897
permissions -rw-r--r--
Renamed "nominal" theory to "Nominal".
berghofe@19494
     1
(*  Title:      HOL/Nominal/nominal_permeq.ML
berghofe@19494
     2
    ID:         $Id$
berghofe@19494
     3
    Author:     Christian Urban, TU Muenchen
berghofe@17870
     4
berghofe@19494
     5
Methods for simplifying permutations and
berghofe@19494
     6
for analysing equations involving permutations.
berghofe@19494
     7
*)
berghofe@17870
     8
berghofe@17870
     9
local
berghofe@17870
    10
urbanc@18012
    11
(* pulls out dynamically a thm via the simpset *)
urbanc@19477
    12
fun dynamic_thms ss name = ProofContext.get_thms (Simplifier.the_context ss) (Name name);
urbanc@19477
    13
fun dynamic_thm ss name = ProofContext.get_thm (Simplifier.the_context ss) (Name name);
urbanc@18012
    14
urbanc@19477
    15
(* a tactic simplyfying permutations *)
berghofe@19494
    16
val perm_fun_def = thm "Nominal.perm_fun_def"
berghofe@19494
    17
val perm_eq_app = thm "Nominal.pt_fun_app_eq"
urbanc@19477
    18
urbanc@19139
    19
fun perm_eval_tac ss i =
urbanc@18012
    20
    let
urbanc@19139
    21
        fun perm_eval_simproc sg ss redex =
urbanc@19169
    22
        let 
urbanc@19477
    23
	   (* the "application" case below is only applicable when the head   *)
urbanc@19169
    24
           (* of f is not a constant  or when it is a permuattion with two or *) 
urbanc@19169
    25
           (* more arguments                                                  *)
urbanc@19169
    26
           fun applicable t = 
urbanc@19169
    27
	       (case (strip_comb t) of
berghofe@19494
    28
		  (Const ("Nominal.perm",_),ts) => (length ts) >= 2
urbanc@19169
    29
		| (Const _,_) => false
urbanc@19169
    30
		| _ => true)
urbanc@19169
    31
urbanc@19169
    32
	in
urbanc@19139
    33
        (case redex of 
urbanc@19169
    34
        (* case pi o (f x) == (pi o f) (pi o x)          *)
urbanc@19169
    35
        (* special treatment according to the head of f  *)
berghofe@19494
    36
        (Const("Nominal.perm",
urbanc@19169
    37
          Type("fun",[Type("List.list",[Type("*",[Type(n,_),_])]),_])) $ pi $ (f $ x)) => 
urbanc@19169
    38
	   (case (applicable f) of
urbanc@19169
    39
                false => NONE  
urbanc@19163
    40
              | _ => 
urbanc@19163
    41
		let
urbanc@19163
    42
		    val name = Sign.base_name n
urbanc@19163
    43
		    val at_inst     = dynamic_thm ss ("at_"^name^"_inst")
urbanc@19163
    44
		    val pt_inst     = dynamic_thm ss ("pt_"^name^"_inst")  
urbanc@19163
    45
		in SOME ((at_inst RS (pt_inst RS perm_eq_app)) RS eq_reflection) end)
urbanc@19139
    46
urbanc@19139
    47
        (* case pi o (%x. f x) == (%x. pi o (f ((rev pi)o x))) *)
berghofe@19494
    48
        | (Const("Nominal.perm",_) $ pi $ (Abs _)) => SOME (perm_fun_def)
urbanc@19139
    49
urbanc@19139
    50
        (* no redex otherwise *) 
urbanc@19169
    51
        | _ => NONE) end
urbanc@19139
    52
urbanc@19139
    53
	val perm_eval =
urbanc@19139
    54
	    Simplifier.simproc (Theory.sign_of (the_context ())) "perm_eval" 
berghofe@19494
    55
	    ["Nominal.perm pi x"] perm_eval_simproc;
urbanc@19139
    56
urbanc@19477
    57
      (* these lemmas are created dynamically according to the atom types *) 
urbanc@19477
    58
      val perm_swap        = dynamic_thms ss "perm_swap"
urbanc@19477
    59
      val perm_fresh       = dynamic_thms ss "perm_fresh_fresh"
urbanc@19477
    60
      val perm_bij         = dynamic_thms ss "perm_bij"
urbanc@19477
    61
      val perm_pi_simp     = dynamic_thms ss "perm_pi_simp"
urbanc@19477
    62
      val ss' = ss addsimps (perm_swap@perm_fresh@perm_bij@perm_pi_simp)
urbanc@19477
    63
    in
urbanc@19477
    64
	("general simplification step", asm_full_simp_tac (ss' addsimprocs [perm_eval]) i)
urbanc@19477
    65
    end;
urbanc@19477
    66
urbanc@19477
    67
(* applies the perm_compose rule such that                             *)
urbanc@19477
    68
(*                                                                     *)
urbanc@19477
    69
(*   pi o (pi' o lhs) = rhs                                            *)
urbanc@19477
    70
(*                                                                     *)
urbanc@19477
    71
(* is transformed to                                                   *) 
urbanc@19477
    72
(*                                                                     *)
urbanc@19477
    73
(*  (pi o pi') o (pi' o lhs) = rhs                                     *)
urbanc@19477
    74
(*                                                                     *)
urbanc@19477
    75
(* this rule would loop in the simplifier, so some trick is used with  *)
urbanc@19477
    76
(* generating perm_aux'es for the outermost permutation and then un-   *)
urbanc@19477
    77
(* folding the definition                                              *)
urbanc@19477
    78
val pt_perm_compose_aux = thm "pt_perm_compose_aux";
urbanc@19477
    79
val cp1_aux             = thm "cp1_aux";
urbanc@19477
    80
val perm_aux_fold       = thm "perm_aux_fold"; 
urbanc@19477
    81
urbanc@19477
    82
fun perm_compose_tac ss i = 
urbanc@19477
    83
    let
urbanc@19477
    84
	fun perm_compose_simproc sg ss redex =
urbanc@19477
    85
	(case redex of
berghofe@19494
    86
           (Const ("Nominal.perm", Type ("fun", [Type ("List.list", 
berghofe@19494
    87
             [Type ("*", [T as Type (tname,_),_])]),_])) $ pi1 $ (Const ("Nominal.perm", 
urbanc@19477
    88
               Type ("fun", [Type ("List.list", [Type ("*", [U as Type (uname,_),_])]),_])) $ 
urbanc@19477
    89
                pi2 $ t)) =>
urbanc@19350
    90
        let
urbanc@19350
    91
	    val tname' = Sign.base_name tname
urbanc@19477
    92
            val uname' = Sign.base_name uname
urbanc@19350
    93
        in
urbanc@19477
    94
            if pi1 <> pi2 then  (* only apply the composition rule in this case *)
urbanc@19477
    95
               if T = U then    
urbanc@19350
    96
                SOME (Drule.instantiate'
urbanc@19350
    97
	              [SOME (ctyp_of sg (fastype_of t))]
urbanc@19350
    98
		      [SOME (cterm_of sg pi1), SOME (cterm_of sg pi2), SOME (cterm_of sg t)]
urbanc@19350
    99
		      (mk_meta_eq ([PureThy.get_thm sg (Name ("pt_"^tname'^"_inst")),
urbanc@19477
   100
	               PureThy.get_thm sg (Name ("at_"^tname'^"_inst"))] MRS pt_perm_compose_aux)))
urbanc@19477
   101
               else
urbanc@19477
   102
                SOME (Drule.instantiate'
urbanc@19477
   103
	              [SOME (ctyp_of sg (fastype_of t))]
urbanc@19477
   104
		      [SOME (cterm_of sg pi1), SOME (cterm_of sg pi2), SOME (cterm_of sg t)]
urbanc@19477
   105
		      (mk_meta_eq (PureThy.get_thm sg (Name ("cp_"^tname'^"_"^uname'^"_inst")) RS 
urbanc@19477
   106
                       cp1_aux)))
urbanc@19350
   107
            else NONE
urbanc@19350
   108
        end
urbanc@19350
   109
       | _ => NONE);
urbanc@19477
   110
	  
urbanc@19477
   111
      val perm_compose  =
urbanc@19350
   112
	Simplifier.simproc (the_context()) "perm_compose" 
berghofe@19494
   113
	["Nominal.perm pi1 (Nominal.perm pi2 t)"] perm_compose_simproc;
urbanc@19477
   114
urbanc@19477
   115
      val ss' = Simplifier.theory_context (the_context ()) empty_ss	  
urbanc@19477
   116
urbanc@18012
   117
    in
urbanc@19477
   118
	("analysing permutation compositions on the lhs",
urbanc@19477
   119
         EVERY [rtac trans i,
urbanc@19477
   120
                asm_full_simp_tac (ss' addsimprocs [perm_compose]) i,
urbanc@19477
   121
                asm_full_simp_tac (HOL_basic_ss addsimps [perm_aux_fold]) i])
urbanc@18012
   122
    end
urbanc@18012
   123
urbanc@18012
   124
(* applying Stefan's smart congruence tac *)
urbanc@18012
   125
fun apply_cong_tac i = 
urbanc@18012
   126
    ("application of congruence",
urbanc@19477
   127
     (fn st => DatatypeAux.cong_tac i st handle Subscript => no_tac st));
berghofe@17870
   128
urbanc@19477
   129
(* unfolds the definition of permutations     *)
urbanc@19477
   130
(* applied to functions such that             *)
urbanc@19477
   131
(*                                            *)
urbanc@19477
   132
(*   pi o f = rhs                             *)  
urbanc@19477
   133
(*                                            *)
urbanc@19477
   134
(* is transformed to                          *)
urbanc@19477
   135
(*                                            *)
urbanc@19477
   136
(*   %x. pi o (f ((rev pi) o x)) = rhs        *)
urbanc@18012
   137
fun unfold_perm_fun_def_tac i = 
urbanc@18012
   138
    let
berghofe@19494
   139
	val perm_fun_def = thm "Nominal.perm_fun_def"
urbanc@18012
   140
    in
urbanc@18012
   141
	("unfolding of permutations on functions", 
urbanc@19477
   142
         rtac (perm_fun_def RS meta_eq_to_obj_eq RS trans) i)
berghofe@17870
   143
    end
berghofe@17870
   144
urbanc@19477
   145
(* applies the ext-rule such that      *)
urbanc@19477
   146
(*                                     *)
urbanc@19477
   147
(*    f = g    goes to /\x. f x = g x  *)
urbanc@19477
   148
fun ext_fun_tac i = ("extensionality expansion of functions", rtac ext i);
berghofe@17870
   149
urbanc@19477
   150
(* FIXME FIXME FIXME *)
urbanc@19477
   151
(* should be able to analyse pi o fresh_fun () = fresh_fun instances *) 
urbanc@19477
   152
fun fresh_fun_eqvt_tac i =
urbanc@19477
   153
    let
berghofe@19494
   154
	val fresh_fun_equiv = thm "Nominal.fresh_fun_equiv_ineq"
urbanc@19477
   155
    in
urbanc@19477
   156
	("fresh_fun equivariance", rtac (fresh_fun_equiv RS trans) i)
urbanc@19477
   157
    end		       
urbanc@19477
   158
		       
berghofe@17870
   159
(* debugging *)
berghofe@17870
   160
fun DEBUG_tac (msg,tac) = 
urbanc@19169
   161
    CHANGED (EVERY [tac, print_tac ("after "^msg)]); 
berghofe@17870
   162
fun NO_DEBUG_tac (_,tac) = CHANGED tac; 
berghofe@17870
   163
urbanc@19477
   164
(* Main Tactics *)
urbanc@18012
   165
fun perm_simp_tac tactical ss i = 
urbanc@19139
   166
    DETERM (tactical (perm_eval_tac ss i));
urbanc@19139
   167
urbanc@19477
   168
(* perm_full_simp_tac is perm_simp_tac plus additional tactics    *)
urbanc@19477
   169
(* to decide equation that come from support problems             *)
urbanc@19477
   170
(* since it contains looping rules the "recursion" - depth is set *)
urbanc@19477
   171
(* to 10 - this seems to be sufficient in most cases              *)
urbanc@19477
   172
fun perm_full_simp_tac tactical ss =
urbanc@19477
   173
  let fun perm_full_simp_tac_aux tactical ss n = 
urbanc@19477
   174
	  if n=0 then K all_tac
urbanc@19477
   175
	  else DETERM o 
urbanc@19477
   176
	       (FIRST'[fn i => tactical ("splitting conjunctions on the rhs", rtac conjI i),
urbanc@19477
   177
                       fn i => tactical (perm_eval_tac ss i),
urbanc@19477
   178
		       fn i => tactical (perm_compose_tac ss i),
urbanc@19477
   179
		       fn i => tactical (apply_cong_tac i), 
urbanc@19477
   180
                       fn i => tactical (unfold_perm_fun_def_tac i),
urbanc@19477
   181
                       fn i => tactical (ext_fun_tac i), 
urbanc@19477
   182
                       fn i => tactical (fresh_fun_eqvt_tac i)]
urbanc@19477
   183
		      THEN_ALL_NEW (TRY o (perm_full_simp_tac_aux tactical ss (n-1))))
urbanc@19477
   184
  in perm_full_simp_tac_aux tactical ss 10 end;
urbanc@19151
   185
urbanc@19477
   186
(* tactic that first unfolds the support definition           *)
urbanc@19477
   187
(* and strips off the intros, then applies perm_full_simp_tac *)
urbanc@18012
   188
fun supports_tac tactical ss i =
urbanc@18012
   189
  let 
berghofe@19494
   190
      val supports_def = thm "Nominal.op supports_def";
berghofe@19494
   191
      val fresh_def    = thm "Nominal.fresh_def";
berghofe@19494
   192
      val fresh_prod   = thm "Nominal.fresh_prod";
urbanc@18012
   193
      val simps        = [supports_def,symmetric fresh_def,fresh_prod]
berghofe@17870
   194
  in
urbanc@19477
   195
      EVERY [tactical ("unfolding of supports   ", simp_tac (HOL_basic_ss addsimps simps) i),
urbanc@19477
   196
             tactical ("stripping of foralls    ", REPEAT_DETERM (rtac allI i)),
urbanc@19477
   197
             tactical ("geting rid of the imps  ", rtac impI i),
urbanc@19477
   198
             tactical ("eliminating conjuncts   ", REPEAT_DETERM (etac  conjE i)),
urbanc@19477
   199
             tactical ("applying perm_full_simp ", perm_full_simp_tac tactical ss i
urbanc@19477
   200
                                                   (*perm_simp_tac tactical ss i*))]
berghofe@17870
   201
  end;
berghofe@17870
   202
urbanc@19151
   203
urbanc@19477
   204
(* tactic that guesses the finite-support of a goal       *)
urbanc@19477
   205
(* it collects all free variables and tries to show       *)
urbanc@19477
   206
(* that the support of these free variables (op supports) *)
urbanc@19477
   207
(* the goal                                               *)
urbanc@19151
   208
fun collect_vars i (Bound j) vs = if j < i then vs else Bound (j - i) ins vs
urbanc@19151
   209
  | collect_vars i (v as Free _) vs = v ins vs
urbanc@19151
   210
  | collect_vars i (v as Var _) vs = v ins vs
urbanc@19151
   211
  | collect_vars i (Const _) vs = vs
urbanc@19151
   212
  | collect_vars i (Abs (_, _, t)) vs = collect_vars (i+1) t vs
urbanc@19151
   213
  | collect_vars i (t $ u) vs = collect_vars i u (collect_vars i t vs);
urbanc@19151
   214
urbanc@19151
   215
val supports_rule = thm "supports_finite";
urbanc@19151
   216
urbanc@19151
   217
fun finite_guess_tac tactical ss i st =
urbanc@19151
   218
    let val goal = List.nth(cprems_of st, i-1)
urbanc@19151
   219
    in
urbanc@19151
   220
      case Logic.strip_assums_concl (term_of goal) of
berghofe@19494
   221
          _ $ (Const ("op :", _) $ (Const ("Nominal.supp", T) $ x) $
urbanc@19151
   222
            Const ("Finite_Set.Finites", _)) =>
urbanc@19151
   223
          let
urbanc@19151
   224
            val cert = Thm.cterm_of (sign_of_thm st);
urbanc@19151
   225
            val ps = Logic.strip_params (term_of goal);
urbanc@19151
   226
            val Ts = rev (map snd ps);
urbanc@19151
   227
            val vs = collect_vars 0 x [];
urbanc@19151
   228
            val s = foldr (fn (v, s) =>
urbanc@19151
   229
                HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s)
urbanc@19151
   230
              HOLogic.unit vs;
urbanc@19151
   231
            val s' = list_abs (ps,
berghofe@19494
   232
              Const ("Nominal.supp", fastype_of1 (Ts, s) --> body_type T) $ s);
urbanc@19151
   233
            val supports_rule' = Thm.lift_rule goal supports_rule;
urbanc@19151
   234
            val _ $ (_ $ S $ _) =
urbanc@19151
   235
              Logic.strip_assums_concl (hd (prems_of supports_rule'));
urbanc@19151
   236
            val supports_rule'' = Drule.cterm_instantiate
urbanc@19151
   237
              [(cert (head_of S), cert s')] supports_rule'
urbanc@19151
   238
          in
urbanc@19151
   239
            (tactical ("guessing of the right supports-set",
urbanc@19151
   240
                      EVERY [compose_tac (false, supports_rule'', 2) i,
urbanc@19151
   241
                             asm_full_simp_tac ss (i+1),
urbanc@19151
   242
                             supports_tac tactical ss i])) st
urbanc@19151
   243
          end
urbanc@19151
   244
        | _ => Seq.empty
urbanc@19151
   245
    end
urbanc@19151
   246
    handle Subscript => Seq.empty
urbanc@19151
   247
berghofe@17870
   248
in             
berghofe@17870
   249
urbanc@18012
   250
fun simp_meth_setup tac =
urbanc@18046
   251
  Method.only_sectioned_args (Simplifier.simp_modifiers' @ Splitter.split_modifiers)
urbanc@18012
   252
  (Method.SIMPLE_METHOD' HEADGOAL o tac o local_simpset_of);
berghofe@17870
   253
urbanc@19477
   254
val perm_eq_meth            = simp_meth_setup (perm_simp_tac NO_DEBUG_tac);
urbanc@19477
   255
val perm_eq_meth_debug      = simp_meth_setup (perm_simp_tac DEBUG_tac);
urbanc@19477
   256
val perm_full_eq_meth       = simp_meth_setup (perm_full_simp_tac NO_DEBUG_tac);
urbanc@19477
   257
val perm_full_eq_meth_debug = simp_meth_setup (perm_full_simp_tac DEBUG_tac);
urbanc@19477
   258
val supports_meth           = simp_meth_setup (supports_tac NO_DEBUG_tac);
urbanc@19477
   259
val supports_meth_debug     = simp_meth_setup (supports_tac DEBUG_tac);
urbanc@19477
   260
val finite_gs_meth          = simp_meth_setup (finite_guess_tac NO_DEBUG_tac);
urbanc@19477
   261
val finite_gs_meth_debug    = simp_meth_setup (finite_guess_tac DEBUG_tac);
berghofe@17870
   262
berghofe@17870
   263
end
berghofe@17870
   264
berghofe@17870
   265
berghofe@17870
   266