src/HOL/Nominal/nominal_permeq.ML
author wenzelm
Thu Mar 20 00:20:44 2008 +0100 (2008-03-20)
changeset 26343 0dd2eab7b296
parent 26338 f8ed02f22433
child 26806 40b411ec05aa
permissions -rw-r--r--
simplified get_thm(s): back to plain name argument;
     1 (*  Title:      HOL/Nominal/nominal_permeq.ML
     2     ID:         $Id$
     3     Authors:    Christian Urban, Julien Narboux, TU Muenchen
     4 
     5 Methods for simplifying permutations and
     6 for analysing equations involving permutations.
     7 *)
     8 
     9 (*
    10 FIXMES:
    11 
    12  - allow the user to give an explicit set S in the
    13    fresh_guess tactic which is then verified
    14 
    15  - the perm_compose tactic does not do an "outermost
    16    rewriting" and can therefore not deal with goals
    17    like
    18 
    19       [(a,b)] o pi1 o pi2 = ....
    20 
    21    rather it tries to permute pi1 over pi2, which 
    22    results in a failure when used with the 
    23    perm_(full)_simp tactics
    24 
    25 *)
    26 
    27 
    28 signature NOMINAL_PERMEQ =
    29 sig
    30   val perm_simproc_fun : simproc
    31   val perm_simproc_app : simproc
    32 
    33   val perm_simp_tac : simpset -> int -> tactic
    34   val perm_full_simp_tac : simpset -> int -> tactic
    35   val supports_tac : simpset -> int -> tactic
    36   val finite_guess_tac : simpset -> int -> tactic
    37   val fresh_guess_tac : simpset -> int -> tactic
    38 
    39   val perm_simp_meth : Method.src -> Proof.context -> Proof.method
    40   val perm_simp_meth_debug : Method.src -> Proof.context -> Proof.method
    41   val perm_full_simp_meth : Method.src -> Proof.context -> Proof.method
    42   val perm_full_simp_meth_debug : Method.src -> Proof.context -> Proof.method
    43   val supports_meth : Method.src -> Proof.context -> Proof.method
    44   val supports_meth_debug : Method.src -> Proof.context -> Proof.method
    45   val finite_guess_meth : Method.src -> Proof.context -> Proof.method
    46   val finite_guess_meth_debug : Method.src -> Proof.context -> Proof.method
    47   val fresh_guess_meth : Method.src -> Proof.context -> Proof.method
    48   val fresh_guess_meth_debug : Method.src -> Proof.context -> Proof.method
    49 end
    50 
    51 structure NominalPermeq : NOMINAL_PERMEQ =
    52 struct
    53 
    54 (* some lemmas needed below *)
    55 val finite_emptyI = @{thm "finite.emptyI"};
    56 val finite_Un     = @{thm "finite_Un"};
    57 val conj_absorb   = @{thm "conj_absorb"};
    58 val not_false     = @{thm "not_False_eq_True"}
    59 val perm_fun_def  = @{thm "Nominal.perm_fun_def"};
    60 val perm_eq_app   = @{thm "Nominal.pt_fun_app_eq"};
    61 val supports_def  = @{thm "Nominal.supports_def"};
    62 val fresh_def     = @{thm "Nominal.fresh_def"};
    63 val fresh_prod    = @{thm "Nominal.fresh_prod"};
    64 val fresh_unit    = @{thm "Nominal.fresh_unit"};
    65 val supports_rule = @{thm "supports_finite"};
    66 val supp_prod     = @{thm "supp_prod"};
    67 val supp_unit     = @{thm "supp_unit"};
    68 val pt_perm_compose_aux = @{thm "pt_perm_compose_aux"};
    69 val cp1_aux             = @{thm "cp1_aux"};
    70 val perm_aux_fold       = @{thm "perm_aux_fold"}; 
    71 val supports_fresh_rule = @{thm "supports_fresh"};
    72 
    73 (* pulls out dynamically a thm via the proof state *)
    74 fun dynamic_thms st name = PureThy.get_thms (theory_of_thm st) name;
    75 fun dynamic_thm  st name = PureThy.get_thm  (theory_of_thm st) name;
    76 
    77 
    78 (* needed in the process of fully simplifying permutations *)
    79 val strong_congs = [@{thm "if_cong"}]
    80 (* needed to avoid warnings about overwritten congs *)
    81 val weak_congs   = [@{thm "if_weak_cong"}]
    82 
    83 (* FIXME comment *)
    84 (* a tactical which fails if the tactic taken as an argument generates does not solve the sub goal i *)
    85 fun SOLVEI t = t THEN_ALL_NEW (fn i => no_tac);
    86 
    87 (* debugging *)
    88 fun DEBUG_tac (msg,tac) = 
    89     CHANGED (EVERY [print_tac ("before "^msg), tac, print_tac ("after "^msg)]); 
    90 fun NO_DEBUG_tac (_,tac) = CHANGED tac; 
    91 
    92 
    93 (* simproc that deals with instances of permutations in front *)
    94 (* of applications; just adding this rule to the simplifier   *)
    95 (* would loop; it also needs careful tuning with the simproc  *)
    96 (* for functions to avoid further possibilities for looping   *)
    97 fun perm_simproc_app' sg ss redex =
    98   let 
    99     (* the "application" case is only applicable when the head of f is not a *)
   100     (* constant or when (f x) is a permuation with two or more arguments     *)
   101     fun applicable_app t = 
   102           (case (strip_comb t) of
   103 	      (Const ("Nominal.perm",_),ts) => (length ts) >= 2
   104             | (Const _,_) => false
   105             | _ => true)
   106   in
   107     case redex of 
   108         (* case pi o (f x) == (pi o f) (pi o x)          *)
   109         (Const("Nominal.perm",
   110           Type("fun",[Type("List.list",[Type("*",[Type(n,_),_])]),_])) $ pi $ (f $ x)) => 
   111             (if (applicable_app f) then
   112               let
   113                 val name = Sign.base_name n
   114                 val at_inst = PureThy.get_thm sg ("at_" ^ name ^ "_inst")
   115                 val pt_inst = PureThy.get_thm sg ("pt_" ^ name ^ "_inst")
   116               in SOME ((at_inst RS (pt_inst RS perm_eq_app)) RS eq_reflection) end
   117             else NONE)
   118       | _ => NONE
   119   end
   120 
   121 val perm_simproc_app = Simplifier.simproc @{theory} "perm_simproc_app"
   122   ["Nominal.perm pi x"] perm_simproc_app';
   123 
   124 (* a simproc that deals with permutation instances in front of functions  *)
   125 fun perm_simproc_fun' sg ss redex = 
   126    let 
   127      fun applicable_fun t =
   128        (case (strip_comb t) of
   129           (Abs _ ,[]) => true
   130 	| (Const ("Nominal.perm",_),_) => false
   131         | (Const _, _) => true
   132 	| _ => false)
   133    in
   134      case redex of 
   135        (* case pi o f == (%x. pi o (f ((rev pi)o x))) *)     
   136        (Const("Nominal.perm",_) $ pi $ f)  => 
   137           (if (applicable_fun f) then SOME (perm_fun_def) else NONE)
   138       | _ => NONE
   139    end
   140 
   141 val perm_simproc_fun = Simplifier.simproc @{theory} "perm_simproc_fun"
   142   ["Nominal.perm pi x"] perm_simproc_fun';
   143 
   144 (* function for simplyfying permutations *)
   145 fun perm_simp_gen dyn_thms eqvt_thms ss i = 
   146     ("general simplification of permutations", fn st =>
   147     let
   148        val ss' = Simplifier.theory_context (theory_of_thm st) ss
   149          addsimps (maps (dynamic_thms st) dyn_thms @ eqvt_thms)
   150          delcongs weak_congs
   151          addcongs strong_congs
   152          addsimprocs [perm_simproc_fun, perm_simproc_app]
   153     in
   154       asm_full_simp_tac ss' i st
   155     end);
   156 
   157 (* general simplification of permutations and permutation that arose from eqvt-problems *)
   158 fun perm_simp ss = 
   159     let val simps = ["perm_swap","perm_fresh_fresh","perm_bij","perm_pi_simp","swap_simps"]
   160     in 
   161 	perm_simp_gen simps [] ss
   162     end;
   163 
   164 fun eqvt_simp ss = 
   165     let val simps = ["perm_swap","perm_fresh_fresh","perm_pi_simp"]
   166 	val eqvts_thms = NominalThmDecls.get_eqvt_thms (Simplifier.the_context ss);
   167     in 
   168 	perm_simp_gen simps eqvts_thms ss
   169     end;
   170 
   171 
   172 (* main simplification tactics for permutations *)
   173 fun perm_simp_tac tactical ss i = DETERM (tactical (perm_simp ss i));
   174 fun eqvt_simp_tac tactical ss i = DETERM (tactical (eqvt_simp ss i)); 
   175 
   176 
   177 (* applies the perm_compose rule such that                             *)
   178 (*   pi o (pi' o lhs) = rhs                                            *)
   179 (* is transformed to                                                   *) 
   180 (*  (pi o pi') o (pi' o lhs) = rhs                                     *)
   181 (*                                                                     *)
   182 (* this rule would loop in the simplifier, so some trick is used with  *)
   183 (* generating perm_aux'es for the outermost permutation and then un-   *)
   184 (* folding the definition                                              *)
   185 
   186 fun perm_compose_simproc' sg ss redex =
   187   (case redex of
   188      (Const ("Nominal.perm", Type ("fun", [Type ("List.list", 
   189        [Type ("*", [T as Type (tname,_),_])]),_])) $ pi1 $ (Const ("Nominal.perm", 
   190          Type ("fun", [Type ("List.list", [Type ("*", [U as Type (uname,_),_])]),_])) $ 
   191           pi2 $ t)) =>
   192     let
   193       val tname' = Sign.base_name tname
   194       val uname' = Sign.base_name uname
   195     in
   196       if pi1 <> pi2 then  (* only apply the composition rule in this case *)
   197         if T = U then    
   198           SOME (Drule.instantiate'
   199             [SOME (ctyp_of sg (fastype_of t))]
   200             [SOME (cterm_of sg pi1), SOME (cterm_of sg pi2), SOME (cterm_of sg t)]
   201             (mk_meta_eq ([PureThy.get_thm sg ("pt_"^tname'^"_inst"),
   202              PureThy.get_thm sg ("at_"^tname'^"_inst")] MRS pt_perm_compose_aux)))
   203         else
   204           SOME (Drule.instantiate'
   205             [SOME (ctyp_of sg (fastype_of t))]
   206             [SOME (cterm_of sg pi1), SOME (cterm_of sg pi2), SOME (cterm_of sg t)]
   207             (mk_meta_eq (PureThy.get_thm sg ("cp_"^tname'^"_"^uname'^"_inst") RS 
   208              cp1_aux)))
   209       else NONE
   210     end
   211   | _ => NONE);
   212 
   213 val perm_compose_simproc = Simplifier.simproc @{theory} "perm_compose"
   214   ["Nominal.perm pi1 (Nominal.perm pi2 t)"] perm_compose_simproc';
   215 
   216 fun perm_compose_tac ss i = 
   217   ("analysing permutation compositions on the lhs",
   218    fn st => EVERY
   219      [rtac trans i,
   220       asm_full_simp_tac (Simplifier.theory_context (theory_of_thm st) empty_ss
   221         addsimprocs [perm_compose_simproc]) i,
   222       asm_full_simp_tac (HOL_basic_ss addsimps [perm_aux_fold]) i] st);
   223 
   224 
   225 (* applying Stefan's smart congruence tac *)
   226 fun apply_cong_tac i = 
   227     ("application of congruence",
   228      (fn st => DatatypeAux.cong_tac i st handle Subscript => no_tac st));
   229 
   230 
   231 (* unfolds the definition of permutations     *)
   232 (* applied to functions such that             *)
   233 (*     pi o f = rhs                           *)  
   234 (* is transformed to                          *)
   235 (*     %x. pi o (f ((rev pi) o x)) = rhs      *)
   236 fun unfold_perm_fun_def_tac i =
   237     ("unfolding of permutations on functions", 
   238       rtac (perm_fun_def RS meta_eq_to_obj_eq RS trans) i)
   239 
   240 (* applies the ext-rule such that      *)
   241 (*                                     *)
   242 (*    f = g   goes to  /\x. f x = g x  *)
   243 fun ext_fun_tac i = ("extensionality expansion of functions", rtac ext i);
   244 
   245 
   246 (* perm_full_simp_tac is perm_simp plus additional tactics        *)
   247 (* to decide equation that come from support problems             *)
   248 (* since it contains looping rules the "recursion" - depth is set *)
   249 (* to 10 - this seems to be sufficient in most cases              *)
   250 fun perm_full_simp_tac tactical ss =
   251   let fun perm_full_simp_tac_aux tactical ss n = 
   252 	  if n=0 then K all_tac
   253 	  else DETERM o 
   254 	       (FIRST'[fn i => tactical ("splitting conjunctions on the rhs", rtac conjI i),
   255                        fn i => tactical (perm_simp ss i),
   256 		       fn i => tactical (perm_compose_tac ss i),
   257 		       fn i => tactical (apply_cong_tac i), 
   258                        fn i => tactical (unfold_perm_fun_def_tac i),
   259                        fn i => tactical (ext_fun_tac i)]
   260 		      THEN_ALL_NEW (TRY o (perm_full_simp_tac_aux tactical ss (n-1))))
   261   in perm_full_simp_tac_aux tactical ss 10 end;
   262 
   263 
   264 (* tactic that tries to solve "supports"-goals; first it *)
   265 (* unfolds the support definition and strips off the     *)
   266 (* intros, then applies eqvt_simp_tac                    *)
   267 fun supports_tac tactical ss i =
   268   let 
   269      val simps        = [supports_def,symmetric fresh_def,fresh_prod]
   270   in
   271       EVERY [tactical ("unfolding of supports   ", simp_tac (HOL_basic_ss addsimps simps) i),
   272              tactical ("stripping of foralls    ", REPEAT_DETERM (rtac allI i)),
   273              tactical ("geting rid of the imps  ", rtac impI i),
   274              tactical ("eliminating conjuncts   ", REPEAT_DETERM (etac  conjE i)),
   275              tactical ("applying eqvt_simp      ", eqvt_simp_tac tactical ss i )]
   276   end;
   277 
   278 
   279 (* tactic that guesses the finite-support of a goal        *)
   280 (* it first collects all free variables and tries to show  *)
   281 (* that the support of these free variables (op supports)  *)
   282 (* the goal                                                *)
   283 fun collect_vars i (Bound j) vs = if j < i then vs else insert (op =) (Bound (j - i)) vs
   284   | collect_vars i (v as Free _) vs = insert (op =) v vs
   285   | collect_vars i (v as Var _) vs = insert (op =) v vs
   286   | collect_vars i (Const _) vs = vs
   287   | collect_vars i (Abs (_, _, t)) vs = collect_vars (i+1) t vs
   288   | collect_vars i (t $ u) vs = collect_vars i u (collect_vars i t vs);
   289 
   290 fun finite_guess_tac tactical ss i st =
   291     let val goal = List.nth(cprems_of st, i-1)
   292     in
   293       case Logic.strip_assums_concl (term_of goal) of
   294           _ $ (Const ("Finite_Set.finite", _) $ (Const ("Nominal.supp", T) $ x)) =>
   295           let
   296             val cert = Thm.cterm_of (Thm.theory_of_thm st);
   297             val ps = Logic.strip_params (term_of goal);
   298             val Ts = rev (map snd ps);
   299             val vs = collect_vars 0 x [];
   300             val s = Library.foldr (fn (v, s) =>
   301                 HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s)
   302               (vs, HOLogic.unit);
   303             val s' = list_abs (ps,
   304               Const ("Nominal.supp", fastype_of1 (Ts, s) --> body_type T) $ s);
   305             val supports_rule' = Thm.lift_rule goal supports_rule;
   306             val _ $ (_ $ S $ _) =
   307               Logic.strip_assums_concl (hd (prems_of supports_rule'));
   308             val supports_rule'' = Drule.cterm_instantiate
   309               [(cert (head_of S), cert s')] supports_rule'
   310             val fin_supp = dynamic_thms st ("fin_supp")
   311             val ss' = ss addsimps [supp_prod,supp_unit,finite_Un,finite_emptyI,conj_absorb]@fin_supp
   312           in
   313             (tactical ("guessing of the right supports-set",
   314                       EVERY [compose_tac (false, supports_rule'', 2) i,
   315                              asm_full_simp_tac ss' (i+1),
   316                              supports_tac tactical ss i])) st
   317           end
   318         | _ => Seq.empty
   319     end
   320     handle Subscript => Seq.empty
   321 
   322 
   323 (* tactic that guesses whether an atom is fresh for an expression  *)
   324 (* it first collects all free variables and tries to show that the *) 
   325 (* support of these free variables (op supports) the goal          *)
   326 fun fresh_guess_tac tactical ss i st =
   327     let 
   328 	val goal = List.nth(cprems_of st, i-1)
   329         val fin_supp = dynamic_thms st ("fin_supp")
   330         val fresh_atm = dynamic_thms st ("fresh_atm")
   331 	val ss1 = ss addsimps [symmetric fresh_def,fresh_prod,fresh_unit,conj_absorb,not_false]@fresh_atm
   332         val ss2 = ss addsimps [supp_prod,supp_unit,finite_Un,finite_emptyI,conj_absorb]@fin_supp
   333     in
   334       case Logic.strip_assums_concl (term_of goal) of
   335           _ $ (Const ("Nominal.fresh", Type ("fun", [T, _])) $ _ $ t) => 
   336           let
   337             val cert = Thm.cterm_of (Thm.theory_of_thm st);
   338             val ps = Logic.strip_params (term_of goal);
   339             val Ts = rev (map snd ps);
   340             val vs = collect_vars 0 t [];
   341             val s = Library.foldr (fn (v, s) =>
   342                 HOLogic.pair_const (fastype_of1 (Ts, v)) (fastype_of1 (Ts, s)) $ v $ s)
   343               (vs, HOLogic.unit);
   344             val s' = list_abs (ps,
   345               Const ("Nominal.supp", fastype_of1 (Ts, s) --> (HOLogic.mk_setT T)) $ s);
   346             val supports_fresh_rule' = Thm.lift_rule goal supports_fresh_rule;
   347             val _ $ (_ $ S $ _) =
   348               Logic.strip_assums_concl (hd (prems_of supports_fresh_rule'));
   349             val supports_fresh_rule'' = Drule.cterm_instantiate
   350               [(cert (head_of S), cert s')] supports_fresh_rule'
   351           in
   352             (tactical ("guessing of the right set that supports the goal", 
   353                       (EVERY [compose_tac (false, supports_fresh_rule'', 3) i,
   354                              asm_full_simp_tac ss1 (i+2),
   355                              asm_full_simp_tac ss2 (i+1), 
   356                              supports_tac tactical ss i]))) st
   357           end
   358           (* when a term-constructor contains more than one binder, it is useful    *) 
   359           (* in nominal_primrecs to try whether the goal can be solved by an hammer *)
   360         | _ => (tactical ("if it is not of the form _\<sharp>_, then try the simplifier",   
   361                           (asm_full_simp_tac (HOL_ss addsimps [fresh_prod]@fresh_atm) i))) st
   362     end
   363     handle Subscript => Seq.empty;
   364 
   365 (* setup so that the simpset is used which is active at the moment when the tactic is called *)
   366 fun local_simp_meth_setup tac =
   367   Method.only_sectioned_args (Simplifier.simp_modifiers' @ Splitter.split_modifiers)
   368   (Method.SIMPLE_METHOD' o tac o local_simpset_of) ;
   369 
   370 (* uses HOL_basic_ss only and fails if the tactic does not solve the subgoal *)
   371 
   372 fun basic_simp_meth_setup debug tac =
   373   Method.sectioned_args 
   374    (fn (ctxt,l) => ((),((Simplifier.map_ss (fn _ => HOL_basic_ss) ctxt),l)))
   375    (Simplifier.simp_modifiers' @ Splitter.split_modifiers)
   376    (fn _ => Method.SIMPLE_METHOD' o (fn ss => if debug then (tac ss) else SOLVEI (tac ss)) o local_simpset_of);
   377 
   378 
   379 val perm_simp_meth            = local_simp_meth_setup (perm_simp_tac NO_DEBUG_tac);
   380 val perm_simp_meth_debug      = local_simp_meth_setup (perm_simp_tac DEBUG_tac);
   381 val perm_full_simp_meth       = local_simp_meth_setup (perm_full_simp_tac NO_DEBUG_tac);
   382 val perm_full_simp_meth_debug = local_simp_meth_setup (perm_full_simp_tac DEBUG_tac);
   383 val supports_meth             = local_simp_meth_setup (supports_tac NO_DEBUG_tac);
   384 val supports_meth_debug       = local_simp_meth_setup (supports_tac DEBUG_tac);
   385 
   386 val finite_guess_meth         = basic_simp_meth_setup false (finite_guess_tac NO_DEBUG_tac);
   387 val finite_guess_meth_debug   = basic_simp_meth_setup true (finite_guess_tac DEBUG_tac);
   388 val fresh_guess_meth          = basic_simp_meth_setup false (fresh_guess_tac NO_DEBUG_tac);
   389 val fresh_guess_meth_debug    = basic_simp_meth_setup true (fresh_guess_tac DEBUG_tac);
   390 
   391 val perm_simp_tac = perm_simp_tac NO_DEBUG_tac;
   392 val perm_full_simp_tac = perm_full_simp_tac NO_DEBUG_tac;
   393 val supports_tac = supports_tac NO_DEBUG_tac;
   394 val finite_guess_tac = finite_guess_tac NO_DEBUG_tac;
   395 val fresh_guess_tac = fresh_guess_tac NO_DEBUG_tac;
   396 
   397 end