Quot/Nominal/Nominal2_Base.thy
changeset 947 fa810f01f7b5
child 1008 7c633507a809
equal deleted inserted replaced
946:46cc6708c3b3 947:fa810f01f7b5
       
     1 (*  Title:      Nominal2_Base
       
     2     Authors:    Brian Huffman, Christian Urban
       
     3 
       
     4     Basic definitions and lemma infrastructure for 
       
     5     Nominal Isabelle. 
       
     6 *)
       
     7 theory Nominal2_Base
       
     8 imports Main Infinite_Set
       
     9 begin
       
    10 
       
    11 section {* Atoms and Sorts *}
       
    12 
       
    13 text {* A simple implementation for atom_sorts is strings. *}
       
    14 (* types atom_sort = string *)
       
    15 
       
    16 text {* To deal with Church-like binding we use trees of  
       
    17   strings as sorts. *}
       
    18 
       
    19 datatype atom_sort = Sort "string" "atom_sort list"
       
    20 
       
    21 datatype atom = Atom atom_sort nat
       
    22 
       
    23 
       
    24 text {* Basic projection function. *}
       
    25 
       
    26 primrec
       
    27   sort_of :: "atom \<Rightarrow> atom_sort"
       
    28 where
       
    29   "sort_of (Atom s i) = s"
       
    30 
       
    31 
       
    32 text {* There are infinitely many atoms of each sort. *}
       
    33 lemma INFM_sort_of_eq: 
       
    34   shows "INFM a. sort_of a = s"
       
    35 proof -
       
    36   have "INFM i. sort_of (Atom s i) = s" by simp
       
    37   moreover have "inj (Atom s)" by (simp add: inj_on_def)
       
    38   ultimately show "INFM a. sort_of a = s" by (rule INFM_inj)
       
    39 qed
       
    40 
       
    41 lemma infinite_sort_of_eq:
       
    42   shows "infinite {a. sort_of a = s}"
       
    43   using INFM_sort_of_eq unfolding INFM_iff_infinite .
       
    44 
       
    45 lemma atom_infinite [simp]: 
       
    46   shows "infinite (UNIV :: atom set)"
       
    47   using subset_UNIV infinite_sort_of_eq
       
    48   by (rule infinite_super)
       
    49 
       
    50 lemma obtain_atom:
       
    51   fixes X :: "atom set"
       
    52   assumes X: "finite X"
       
    53   obtains a where "a \<notin> X" "sort_of a = s"
       
    54 proof -
       
    55   from X have "MOST a. a \<notin> X"
       
    56     unfolding MOST_iff_cofinite by simp
       
    57   with INFM_sort_of_eq
       
    58   have "INFM a. sort_of a = s \<and> a \<notin> X"
       
    59     by (rule INFM_conjI)
       
    60   then obtain a where "a \<notin> X" "sort_of a = s"
       
    61     by (auto elim: INFM_E)
       
    62   then show ?thesis ..
       
    63 qed
       
    64 
       
    65 section {* Sort-Respecting Permutations *}
       
    66 
       
    67 typedef perm =
       
    68   "{f. bij f \<and> finite {a. f a \<noteq> a} \<and> (\<forall>a. sort_of (f a) = sort_of a)}"
       
    69 proof
       
    70   show "id \<in> ?perm" by simp
       
    71 qed
       
    72 
       
    73 lemma permI:
       
    74   assumes "bij f" and "MOST x. f x = x" and "\<And>a. sort_of (f a) = sort_of a"
       
    75   shows "f \<in> perm"
       
    76   using assms unfolding perm_def MOST_iff_cofinite by simp
       
    77 
       
    78 lemma perm_is_bij: "f \<in> perm \<Longrightarrow> bij f"
       
    79   unfolding perm_def by simp
       
    80 
       
    81 lemma perm_is_finite: "f \<in> perm \<Longrightarrow> finite {a. f a \<noteq> a}"
       
    82   unfolding perm_def by simp
       
    83 
       
    84 lemma perm_is_sort_respecting: "f \<in> perm \<Longrightarrow> sort_of (f a) = sort_of a"
       
    85   unfolding perm_def by simp
       
    86 
       
    87 lemma perm_MOST: "f \<in> perm \<Longrightarrow> MOST x. f x = x"
       
    88   unfolding perm_def MOST_iff_cofinite by simp
       
    89 
       
    90 lemma perm_id: "id \<in> perm"
       
    91   unfolding perm_def by simp
       
    92 
       
    93 lemma perm_comp:
       
    94   assumes f: "f \<in> perm" and g: "g \<in> perm"
       
    95   shows "(f \<circ> g) \<in> perm"
       
    96 apply (rule permI)
       
    97 apply (rule bij_comp)
       
    98 apply (rule perm_is_bij [OF g])
       
    99 apply (rule perm_is_bij [OF f])
       
   100 apply (rule MOST_rev_mp [OF perm_MOST [OF g]])
       
   101 apply (rule MOST_rev_mp [OF perm_MOST [OF f]])
       
   102 apply (simp)
       
   103 apply (simp add: perm_is_sort_respecting [OF f])
       
   104 apply (simp add: perm_is_sort_respecting [OF g])
       
   105 done
       
   106 
       
   107 lemma perm_inv:
       
   108   assumes f: "f \<in> perm"
       
   109   shows "(inv f) \<in> perm"
       
   110 apply (rule permI)
       
   111 apply (rule bij_imp_bij_inv)
       
   112 apply (rule perm_is_bij [OF f])
       
   113 apply (rule MOST_mono [OF perm_MOST [OF f]])
       
   114 apply (erule subst, rule inv_f_f)
       
   115 apply (rule bij_is_inj [OF perm_is_bij [OF f]])
       
   116 apply (rule perm_is_sort_respecting [OF f, THEN sym, THEN trans])
       
   117 apply (simp add: surj_f_inv_f [OF bij_is_surj [OF perm_is_bij [OF f]]])
       
   118 done
       
   119 
       
   120 lemma bij_Rep_perm: "bij (Rep_perm p)"
       
   121   using Rep_perm [of p] unfolding perm_def by simp
       
   122 
       
   123 lemma finite_Rep_perm: "finite {a. Rep_perm p a \<noteq> a}"
       
   124   using Rep_perm [of p] unfolding perm_def by simp
       
   125 
       
   126 lemma sort_of_Rep_perm: "sort_of (Rep_perm p a) = sort_of a"
       
   127   using Rep_perm [of p] unfolding perm_def by simp
       
   128 
       
   129 lemma Rep_perm_ext:
       
   130   "Rep_perm p1 = Rep_perm p2 \<Longrightarrow> p1 = p2"
       
   131   by (simp add: expand_fun_eq Rep_perm_inject [symmetric])
       
   132 
       
   133 
       
   134 subsection {* Permutations form a group *}
       
   135 
       
   136 instantiation perm :: group_add
       
   137 begin
       
   138 
       
   139 definition
       
   140   "0 = Abs_perm id"
       
   141 
       
   142 definition
       
   143   "- p = Abs_perm (inv (Rep_perm p))"
       
   144 
       
   145 definition
       
   146   "p + q = Abs_perm (Rep_perm p \<circ> Rep_perm q)"
       
   147 
       
   148 definition
       
   149   "(p1::perm) - p2 = p1 + - p2"
       
   150 
       
   151 lemma Rep_perm_0: "Rep_perm 0 = id"
       
   152   unfolding zero_perm_def
       
   153   by (simp add: Abs_perm_inverse perm_id)
       
   154 
       
   155 lemma Rep_perm_add:
       
   156   "Rep_perm (p1 + p2) = Rep_perm p1 \<circ> Rep_perm p2"
       
   157   unfolding plus_perm_def
       
   158   by (simp add: Abs_perm_inverse perm_comp Rep_perm)
       
   159 
       
   160 lemma Rep_perm_uminus:
       
   161   "Rep_perm (- p) = inv (Rep_perm p)"
       
   162   unfolding uminus_perm_def
       
   163   by (simp add: Abs_perm_inverse perm_inv Rep_perm)
       
   164 
       
   165 instance
       
   166 apply default
       
   167 unfolding Rep_perm_inject [symmetric]
       
   168 unfolding minus_perm_def
       
   169 unfolding Rep_perm_add
       
   170 unfolding Rep_perm_uminus
       
   171 unfolding Rep_perm_0
       
   172 by (simp_all add: o_assoc inv_o_cancel [OF bij_is_inj [OF bij_Rep_perm]])
       
   173 
       
   174 end
       
   175 
       
   176 
       
   177 section {* Implementation of swappings *}
       
   178 
       
   179 definition
       
   180   swap :: "atom \<Rightarrow> atom \<Rightarrow> perm" ("'(_ \<rightleftharpoons> _')")
       
   181 where
       
   182   "(a \<rightleftharpoons> b) =
       
   183     Abs_perm (if sort_of a = sort_of b 
       
   184               then (\<lambda>c. if a = c then b else if b = c then a else c) 
       
   185               else id)"
       
   186 
       
   187 lemma Rep_perm_swap:
       
   188   "Rep_perm (a \<rightleftharpoons> b) =
       
   189     (if sort_of a = sort_of b 
       
   190      then (\<lambda>c. if a = c then b else if b = c then a else c)
       
   191      else id)"
       
   192 unfolding swap_def
       
   193 apply (rule Abs_perm_inverse)
       
   194 apply (rule permI)
       
   195 apply (auto simp add: bij_def inj_on_def surj_def)[1]
       
   196 apply (rule MOST_rev_mp [OF MOST_neq(1) [of a]])
       
   197 apply (rule MOST_rev_mp [OF MOST_neq(1) [of b]])
       
   198 apply (simp)
       
   199 apply (simp)
       
   200 done
       
   201 
       
   202 lemmas Rep_perm_simps =
       
   203   Rep_perm_0
       
   204   Rep_perm_add
       
   205   Rep_perm_uminus
       
   206   Rep_perm_swap
       
   207 
       
   208 lemma swap_different_sorts [simp]:
       
   209   "sort_of a \<noteq> sort_of b \<Longrightarrow> (a \<rightleftharpoons> b) = 0"
       
   210   by (rule Rep_perm_ext) (simp add: Rep_perm_simps)
       
   211 
       
   212 lemma swap_cancel:
       
   213   "(a \<rightleftharpoons> b) + (a \<rightleftharpoons> b) = 0"
       
   214 by (rule Rep_perm_ext) 
       
   215    (simp add: Rep_perm_simps expand_fun_eq)
       
   216 
       
   217 lemma swap_self [simp]:
       
   218   "(a \<rightleftharpoons> a) = 0"
       
   219   by (rule Rep_perm_ext, simp add: Rep_perm_simps expand_fun_eq)
       
   220 
       
   221 lemma minus_swap [simp]:
       
   222   "- (a \<rightleftharpoons> b) = (a \<rightleftharpoons> b)"
       
   223   by (rule minus_unique [OF swap_cancel])
       
   224 
       
   225 lemma swap_commute:
       
   226   "(a \<rightleftharpoons> b) = (b \<rightleftharpoons> a)"
       
   227   by (rule Rep_perm_ext)
       
   228      (simp add: Rep_perm_swap expand_fun_eq)
       
   229 
       
   230 lemma swap_triple:
       
   231   assumes "a \<noteq> b" and "c \<noteq> b"
       
   232   assumes "sort_of a = sort_of b" "sort_of b = sort_of c"
       
   233   shows "(a \<rightleftharpoons> c) + (b \<rightleftharpoons> c) + (a \<rightleftharpoons> c) = (a \<rightleftharpoons> b)"
       
   234   using assms
       
   235   by (rule_tac Rep_perm_ext)
       
   236      (auto simp add: Rep_perm_simps expand_fun_eq)
       
   237 
       
   238 
       
   239 section {* Permutation Types *}
       
   240 
       
   241 text {*
       
   242   Infix syntax for @{text permute} has higher precedence than
       
   243   addition, but lower than unary minus.
       
   244 *}
       
   245 
       
   246 class pt =
       
   247   fixes permute :: "perm \<Rightarrow> 'a \<Rightarrow> 'a" ("_ \<bullet> _" [76, 75] 75)
       
   248   assumes permute_zero [simp]: "0 \<bullet> x = x"
       
   249   assumes permute_plus [simp]: "(p + q) \<bullet> x = p \<bullet> (q \<bullet> x)"
       
   250 begin
       
   251 
       
   252 lemma permute_diff [simp]:
       
   253   shows "(p - q) \<bullet> x = p \<bullet> - q \<bullet> x"
       
   254   unfolding diff_minus by simp
       
   255 
       
   256 lemma permute_minus_cancel [simp]:
       
   257   shows "p \<bullet> - p \<bullet> x = x"
       
   258   and   "- p \<bullet> p \<bullet> x = x"
       
   259   unfolding permute_plus [symmetric] by simp_all
       
   260 
       
   261 lemma permute_swap_cancel [simp]:
       
   262   shows "(a \<rightleftharpoons> b) \<bullet> (a \<rightleftharpoons> b) \<bullet> x = x"
       
   263   unfolding permute_plus [symmetric]
       
   264   by (simp add: swap_cancel)
       
   265 
       
   266 lemma permute_swap_cancel2 [simp]:
       
   267   shows "(a \<rightleftharpoons> b) \<bullet> (b \<rightleftharpoons> a) \<bullet> x = x"
       
   268   unfolding permute_plus [symmetric]
       
   269   by (simp add: swap_commute)
       
   270 
       
   271 lemma inj_permute [simp]: 
       
   272   shows "inj (permute p)"
       
   273   by (rule inj_on_inverseI)
       
   274      (rule permute_minus_cancel)
       
   275 
       
   276 lemma surj_permute [simp]: 
       
   277   shows "surj (permute p)"
       
   278   by (rule surjI, rule permute_minus_cancel)
       
   279 
       
   280 lemma bij_permute [simp]: 
       
   281   shows "bij (permute p)"
       
   282   by (rule bijI [OF inj_permute surj_permute])
       
   283 
       
   284 lemma inv_permute: 
       
   285   shows "inv (permute p) = permute (- p)"
       
   286   by (rule inv_equality) (simp_all)
       
   287 
       
   288 lemma permute_minus: 
       
   289   shows "permute (- p) = inv (permute p)"
       
   290   by (simp add: inv_permute)
       
   291 
       
   292 lemma permute_eq_iff [simp]: 
       
   293   shows "p \<bullet> x = p \<bullet> y \<longleftrightarrow> x = y"
       
   294   by (rule inj_permute [THEN inj_eq])
       
   295 
       
   296 end
       
   297 
       
   298 subsection {* Permutations for atoms *}
       
   299 
       
   300 instantiation atom :: pt
       
   301 begin
       
   302 
       
   303 definition
       
   304   "p \<bullet> a = Rep_perm p a"
       
   305 
       
   306 instance 
       
   307 apply(default)
       
   308 apply(simp_all add: permute_atom_def Rep_perm_simps)
       
   309 done
       
   310 
       
   311 end
       
   312 
       
   313 lemma sort_of_permute [simp]:
       
   314   shows "sort_of (p \<bullet> a) = sort_of a"
       
   315   unfolding permute_atom_def by (rule sort_of_Rep_perm)
       
   316 
       
   317 lemma swap_atom:
       
   318   shows "(a \<rightleftharpoons> b) \<bullet> c =
       
   319            (if sort_of a = sort_of b
       
   320             then (if c = a then b else if c = b then a else c) else c)"
       
   321   unfolding permute_atom_def
       
   322   by (simp add: Rep_perm_swap)
       
   323 
       
   324 lemma swap_atom_simps [simp]:
       
   325   "sort_of a = sort_of b \<Longrightarrow> (a \<rightleftharpoons> b) \<bullet> a = b"
       
   326   "sort_of a = sort_of b \<Longrightarrow> (a \<rightleftharpoons> b) \<bullet> b = a"
       
   327   "c \<noteq> a \<Longrightarrow> c \<noteq> b \<Longrightarrow> (a \<rightleftharpoons> b) \<bullet> c = c"
       
   328   unfolding swap_atom by simp_all
       
   329 
       
   330 lemma expand_perm_eq:
       
   331   fixes p q :: "perm"
       
   332   shows "p = q \<longleftrightarrow> (\<forall>a::atom. p \<bullet> a = q \<bullet> a)"
       
   333   unfolding permute_atom_def
       
   334   by (metis Rep_perm_ext ext)
       
   335 
       
   336 
       
   337 subsection {* Permutations for permutations *}
       
   338 
       
   339 instantiation perm :: pt
       
   340 begin
       
   341 
       
   342 definition
       
   343   "p \<bullet> q = p + q - p"
       
   344 
       
   345 instance
       
   346 apply default
       
   347 apply (simp add: permute_perm_def)
       
   348 apply (simp add: permute_perm_def diff_minus minus_add add_assoc)
       
   349 done
       
   350 
       
   351 end
       
   352 
       
   353 lemma permute_self: "p \<bullet> p = p"
       
   354 unfolding permute_perm_def by (simp add: diff_minus add_assoc)
       
   355 
       
   356 lemma permute_eqvt:
       
   357   shows "p \<bullet> (q \<bullet> x) = (p \<bullet> q) \<bullet> (p \<bullet> x)"
       
   358   unfolding permute_perm_def by simp
       
   359 
       
   360 lemma zero_perm_eqvt:
       
   361   shows "p \<bullet> (0::perm) = 0"
       
   362   unfolding permute_perm_def by simp
       
   363 
       
   364 lemma add_perm_eqvt:
       
   365   fixes p p1 p2 :: perm
       
   366   shows "p \<bullet> (p1 + p2) = p \<bullet> p1 + p \<bullet> p2"
       
   367   unfolding permute_perm_def
       
   368   by (simp add: expand_perm_eq)
       
   369 
       
   370 lemma swap_eqvt:
       
   371   shows "p \<bullet> (a \<rightleftharpoons> b) = (p \<bullet> a \<rightleftharpoons> p \<bullet> b)"
       
   372   unfolding permute_perm_def
       
   373   by (auto simp add: swap_atom expand_perm_eq)
       
   374 
       
   375 
       
   376 subsection {* Permutations for functions *}
       
   377 
       
   378 instantiation "fun" :: (pt, pt) pt
       
   379 begin
       
   380 
       
   381 definition
       
   382   "p \<bullet> f = (\<lambda>x. p \<bullet> (f (- p \<bullet> x)))"
       
   383 
       
   384 instance
       
   385 apply default
       
   386 apply (simp add: permute_fun_def)
       
   387 apply (simp add: permute_fun_def minus_add)
       
   388 done
       
   389 
       
   390 end
       
   391 
       
   392 lemma permute_fun_app_eq:
       
   393   shows "p \<bullet> (f x) = (p \<bullet> f) (p \<bullet> x)"
       
   394 unfolding permute_fun_def by simp
       
   395 
       
   396 
       
   397 subsection {* Permutations for booleans *}
       
   398 
       
   399 instantiation bool :: pt
       
   400 begin
       
   401 
       
   402 definition "p \<bullet> (b::bool) = b"
       
   403 
       
   404 instance
       
   405 apply(default) 
       
   406 apply(simp_all add: permute_bool_def)
       
   407 done
       
   408 
       
   409 end
       
   410 
       
   411 lemma Not_eqvt:
       
   412   shows "p \<bullet> (\<not> A) = (\<not> (p \<bullet> A))"
       
   413 by (simp add: permute_bool_def)
       
   414 
       
   415 
       
   416 subsection {* Permutations for sets *}
       
   417 
       
   418 lemma permute_set_eq:
       
   419   fixes x::"'a::pt"
       
   420   and   p::"perm"
       
   421   shows "(p \<bullet> X) = {p \<bullet> x | x. x \<in> X}"
       
   422   apply(auto simp add: permute_fun_def permute_bool_def mem_def)
       
   423   apply(rule_tac x="- p \<bullet> x" in exI)
       
   424   apply(simp)
       
   425   done
       
   426 
       
   427 lemma permute_set_eq_image:
       
   428   shows "p \<bullet> X = permute p ` X"
       
   429 unfolding permute_set_eq by auto
       
   430 
       
   431 lemma permute_set_eq_vimage:
       
   432   shows "p \<bullet> X = permute (- p) -` X"
       
   433 unfolding permute_fun_def permute_bool_def
       
   434 unfolding vimage_def Collect_def mem_def ..
       
   435 
       
   436 subsection {* Permutations for units *}
       
   437 
       
   438 instantiation unit :: pt
       
   439 begin
       
   440 
       
   441 definition "p \<bullet> (u::unit) = u"
       
   442 
       
   443 instance proof
       
   444 qed (simp_all add: permute_unit_def)
       
   445 
       
   446 end
       
   447 
       
   448 
       
   449 subsection {* Permutations for products *}
       
   450 
       
   451 instantiation "*" :: (pt, pt) pt
       
   452 begin
       
   453 
       
   454 primrec 
       
   455   permute_prod 
       
   456 where
       
   457   Pair_eqvt: "p \<bullet> (x, y) = (p \<bullet> x, p \<bullet> y)"
       
   458 
       
   459 instance
       
   460 by default auto
       
   461 
       
   462 end
       
   463 
       
   464 
       
   465 subsection {* Permutations for sums *}
       
   466 
       
   467 instantiation "+" :: (pt, pt) pt
       
   468 begin
       
   469 
       
   470 primrec 
       
   471   permute_sum 
       
   472 where
       
   473   "p \<bullet> (Inl x) = Inl (p \<bullet> x)"
       
   474 | "p \<bullet> (Inr y) = Inr (p \<bullet> y)"
       
   475 
       
   476 instance proof
       
   477 qed (case_tac [!] x, simp_all)
       
   478 
       
   479 end
       
   480 
       
   481 subsection {* Permutations for lists *}
       
   482 
       
   483 instantiation list :: (pt) pt
       
   484 begin
       
   485 
       
   486 primrec 
       
   487   permute_list 
       
   488 where
       
   489   "p \<bullet> [] = []"
       
   490 | "p \<bullet> (x # xs) = p \<bullet> x # p \<bullet> xs"
       
   491 
       
   492 instance proof
       
   493 qed (induct_tac [!] x, simp_all)
       
   494 
       
   495 end
       
   496 
       
   497 subsection {* Permutations for options *}
       
   498 
       
   499 instantiation option :: (pt) pt
       
   500 begin
       
   501 
       
   502 primrec 
       
   503   permute_option 
       
   504 where
       
   505   "p \<bullet> None = None"
       
   506 | "p \<bullet> (Some x) = Some (p \<bullet> x)"
       
   507 
       
   508 instance proof
       
   509 qed (induct_tac [!] x, simp_all)
       
   510 
       
   511 end
       
   512 
       
   513 subsection {* Permutations for @{typ char}, @{typ nat}, and @{typ int} *}
       
   514 
       
   515 instantiation char :: pt
       
   516 begin
       
   517 
       
   518 definition "p \<bullet> (c::char) = c"
       
   519 
       
   520 instance proof
       
   521 qed (simp_all add: permute_char_def)
       
   522 
       
   523 end
       
   524 
       
   525 instantiation nat :: pt
       
   526 begin
       
   527 
       
   528 definition "p \<bullet> (n::nat) = n"
       
   529 
       
   530 instance proof
       
   531 qed (simp_all add: permute_nat_def)
       
   532 
       
   533 end
       
   534 
       
   535 instantiation int :: pt
       
   536 begin
       
   537 
       
   538 definition "p \<bullet> (i::int) = i"
       
   539 
       
   540 instance proof
       
   541 qed (simp_all add: permute_int_def)
       
   542 
       
   543 end
       
   544 
       
   545 
       
   546 section {* Pure types *}
       
   547 
       
   548 text {* Pure types will have always empty support. *}
       
   549 
       
   550 class pure = pt +
       
   551   assumes permute_pure: "p \<bullet> x = x"
       
   552 
       
   553 text {* Types @{typ unit} and @{typ bool} are pure. *}
       
   554 
       
   555 instance unit :: pure
       
   556 proof qed (rule permute_unit_def)
       
   557 
       
   558 instance bool :: pure
       
   559 proof qed (rule permute_bool_def)
       
   560 
       
   561 text {* Other type constructors preserve purity. *}
       
   562 
       
   563 instance "fun" :: (pure, pure) pure
       
   564 by default (simp add: permute_fun_def permute_pure)
       
   565 
       
   566 instance "*" :: (pure, pure) pure
       
   567 by default (induct_tac x, simp add: permute_pure)
       
   568 
       
   569 instance "+" :: (pure, pure) pure
       
   570 by default (induct_tac x, simp_all add: permute_pure)
       
   571 
       
   572 instance list :: (pure) pure
       
   573 by default (induct_tac x, simp_all add: permute_pure)
       
   574 
       
   575 instance option :: (pure) pure
       
   576 by default (induct_tac x, simp_all add: permute_pure)
       
   577 
       
   578 
       
   579 subsection {* Types @{typ char}, @{typ nat}, and @{typ int} *}
       
   580 
       
   581 instance char :: pure
       
   582 proof qed (rule permute_char_def)
       
   583 
       
   584 instance nat :: pure
       
   585 proof qed (rule permute_nat_def)
       
   586 
       
   587 instance int :: pure
       
   588 proof qed (rule permute_int_def)
       
   589 
       
   590 
       
   591 subsection {* Supp, Freshness and Supports *}
       
   592 
       
   593 context pt
       
   594 begin
       
   595 
       
   596 definition
       
   597   supp :: "'a \<Rightarrow> atom set"
       
   598 where
       
   599   "supp x = {a. infinite {b. (a \<rightleftharpoons> b) \<bullet> x \<noteq> x}}"
       
   600 
       
   601 end
       
   602 
       
   603 definition
       
   604   fresh :: "atom \<Rightarrow> 'a::pt \<Rightarrow> bool" ("_ \<sharp> _" [55, 55] 55)
       
   605 where   
       
   606   "a \<sharp> x \<equiv> a \<notin> supp x"
       
   607 
       
   608 lemma supp_conv_fresh: 
       
   609   shows "supp x = {a. \<not> a \<sharp> x}"
       
   610   unfolding fresh_def by simp
       
   611 
       
   612 lemma swap_rel_trans:
       
   613   assumes "sort_of a = sort_of b"
       
   614   assumes "sort_of b = sort_of c"
       
   615   assumes "(a \<rightleftharpoons> c) \<bullet> x = x"
       
   616   assumes "(b \<rightleftharpoons> c) \<bullet> x = x"
       
   617   shows "(a \<rightleftharpoons> b) \<bullet> x = x"
       
   618 proof (cases)
       
   619   assume "a = b \<or> c = b"
       
   620   with assms show "(a \<rightleftharpoons> b) \<bullet> x = x" by auto
       
   621 next
       
   622   assume *: "\<not> (a = b \<or> c = b)"
       
   623   have "((a \<rightleftharpoons> c) + (b \<rightleftharpoons> c) + (a \<rightleftharpoons> c)) \<bullet> x = x"
       
   624     using assms by simp
       
   625   also have "(a \<rightleftharpoons> c) + (b \<rightleftharpoons> c) + (a \<rightleftharpoons> c) = (a \<rightleftharpoons> b)"
       
   626     using assms * by (simp add: swap_triple)
       
   627   finally show "(a \<rightleftharpoons> b) \<bullet> x = x" .
       
   628 qed
       
   629 
       
   630 lemma swap_fresh_fresh:
       
   631   assumes a: "a \<sharp> x" 
       
   632   and     b: "b \<sharp> x"
       
   633   shows "(a \<rightleftharpoons> b) \<bullet> x = x"
       
   634 proof (cases)
       
   635   assume asm: "sort_of a = sort_of b" 
       
   636   have "finite {c. (a \<rightleftharpoons> c) \<bullet> x \<noteq> x}" "finite {c. (b \<rightleftharpoons> c) \<bullet> x \<noteq> x}" 
       
   637     using a b unfolding fresh_def supp_def by simp_all
       
   638   then have "finite ({c. (a \<rightleftharpoons> c) \<bullet> x \<noteq> x} \<union> {c. (b \<rightleftharpoons> c) \<bullet> x \<noteq> x})" by simp
       
   639   then obtain c 
       
   640     where "(a \<rightleftharpoons> c) \<bullet> x = x" "(b \<rightleftharpoons> c) \<bullet> x = x" "sort_of c = sort_of b"
       
   641     by (rule obtain_atom) (auto)
       
   642   then show "(a \<rightleftharpoons> b) \<bullet> x = x" using asm by (rule_tac swap_rel_trans) (simp_all)
       
   643 next
       
   644   assume "sort_of a \<noteq> sort_of b"
       
   645   then show "(a \<rightleftharpoons> b) \<bullet> x = x" by simp
       
   646 qed
       
   647 
       
   648 
       
   649 subsection {* supp and fresh are equivariant *}
       
   650 
       
   651 lemma finite_Collect_bij:
       
   652   assumes a: "bij f"
       
   653   shows "finite {x. P (f x)} = finite {x. P x}"
       
   654 by (metis a finite_vimage_iff vimage_Collect_eq)
       
   655 
       
   656 lemma fresh_permute_iff:
       
   657   shows "(p \<bullet> a) \<sharp> (p \<bullet> x) \<longleftrightarrow> a \<sharp> x"
       
   658 proof -
       
   659   have "(p \<bullet> a) \<sharp> (p \<bullet> x) \<longleftrightarrow> finite {b. (p \<bullet> a \<rightleftharpoons> b) \<bullet> p \<bullet> x \<noteq> p \<bullet> x}"
       
   660     unfolding fresh_def supp_def by simp
       
   661   also have "\<dots> \<longleftrightarrow> finite {b. (p \<bullet> a \<rightleftharpoons> p \<bullet> b) \<bullet> p \<bullet> x \<noteq> p \<bullet> x}"
       
   662     using bij_permute by (rule finite_Collect_bij [symmetric])
       
   663   also have "\<dots> \<longleftrightarrow> finite {b. p \<bullet> (a \<rightleftharpoons> b) \<bullet> x \<noteq> p \<bullet> x}"
       
   664     by (simp only: permute_eqvt [of p] swap_eqvt)
       
   665   also have "\<dots> \<longleftrightarrow> finite {b. (a \<rightleftharpoons> b) \<bullet> x \<noteq> x}"
       
   666     by (simp only: permute_eq_iff)
       
   667   also have "\<dots> \<longleftrightarrow> a \<sharp> x"
       
   668     unfolding fresh_def supp_def by simp
       
   669   finally show ?thesis .
       
   670 qed
       
   671 
       
   672 lemma fresh_eqvt:
       
   673   shows "p \<bullet> (a \<sharp> x) = (p \<bullet> a) \<sharp> (p \<bullet> x)"
       
   674   by (simp add: permute_bool_def fresh_permute_iff)
       
   675 
       
   676 lemma supp_eqvt:
       
   677   fixes  p  :: "perm"
       
   678   and    x   :: "'a::pt"
       
   679   shows "p \<bullet> (supp x) = supp (p \<bullet> x)"
       
   680   unfolding supp_conv_fresh
       
   681   unfolding permute_fun_def Collect_def
       
   682   by (simp add: Not_eqvt fresh_eqvt)
       
   683 
       
   684 subsection {* supports *}
       
   685 
       
   686 definition
       
   687   supports :: "atom set \<Rightarrow> 'a::pt \<Rightarrow> bool" (infixl "supports" 80)
       
   688 where  
       
   689   "S supports x \<equiv> \<forall>a b. (a \<notin> S \<and> b \<notin> S \<longrightarrow> (a \<rightleftharpoons> b) \<bullet> x = x)"
       
   690 
       
   691 lemma supp_is_subset:
       
   692   fixes S :: "atom set"
       
   693   and   x :: "'a::pt"
       
   694   assumes a1: "S supports x"
       
   695   and     a2: "finite S"
       
   696   shows "(supp x) \<subseteq> S"
       
   697 proof (rule ccontr)
       
   698   assume "\<not>(supp x \<subseteq> S)"
       
   699   then obtain a where b1: "a \<in> supp x" and b2: "a \<notin> S" by auto
       
   700   from a1 b2 have "\<forall>b. b \<notin> S \<longrightarrow> (a \<rightleftharpoons> b) \<bullet> x = x" by (unfold supports_def) (auto)
       
   701   hence "{b. (a \<rightleftharpoons> b) \<bullet> x \<noteq> x} \<subseteq> S" by auto
       
   702   with a2 have "finite {b. (a \<rightleftharpoons> b)\<bullet>x \<noteq> x}" by (simp add: finite_subset)
       
   703   then have "a \<notin> (supp x)" unfolding supp_def by simp
       
   704   with b1 show False by simp
       
   705 qed
       
   706 
       
   707 lemma supports_finite:
       
   708   fixes S :: "atom set"
       
   709   and   x :: "'a::pt"
       
   710   assumes a1: "S supports x"
       
   711   and     a2: "finite S"
       
   712   shows "finite (supp x)"
       
   713 proof -
       
   714   have "(supp x) \<subseteq> S" using a1 a2 by (rule supp_is_subset)
       
   715   then show "finite (supp x)" using a2 by (simp add: finite_subset)
       
   716 qed
       
   717 
       
   718 lemma supp_supports:
       
   719   fixes x :: "'a::pt"
       
   720   shows "(supp x) supports x"
       
   721 proof (unfold supports_def, intro strip)
       
   722   fix a b
       
   723   assume "a \<notin> (supp x) \<and> b \<notin> (supp x)"
       
   724   then have "a \<sharp> x" and "b \<sharp> x" by (simp_all add: fresh_def)
       
   725   then show "(a \<rightleftharpoons> b) \<bullet> x = x" by (rule swap_fresh_fresh)
       
   726 qed
       
   727 
       
   728 lemma supp_is_least_supports:
       
   729   fixes S :: "atom set"
       
   730   and   x :: "'a::pt"
       
   731   assumes  a1: "S supports x"
       
   732   and      a2: "finite S"
       
   733   and      a3: "\<And>S'. finite S' \<Longrightarrow> (S' supports x) \<Longrightarrow> S \<subseteq> S'"
       
   734   shows "(supp x) = S"
       
   735 proof (rule equalityI)
       
   736   show "(supp x) \<subseteq> S" using a1 a2 by (rule supp_is_subset)
       
   737   with a2 have fin: "finite (supp x)" by (rule rev_finite_subset)
       
   738   have "(supp x) supports x" by (rule supp_supports)
       
   739   with fin a3 show "S \<subseteq> supp x" by blast
       
   740 qed
       
   741 
       
   742 lemma subsetCI: 
       
   743   shows "(\<And>x. x \<in> A \<Longrightarrow> x \<notin> B \<Longrightarrow> False) \<Longrightarrow> A \<subseteq> B"
       
   744   by auto
       
   745 
       
   746 lemma finite_supp_unique:
       
   747   assumes a1: "S supports x"
       
   748   assumes a2: "finite S"
       
   749   assumes a3: "\<And>a b. \<lbrakk>a \<in> S; b \<notin> S; sort_of a = sort_of b\<rbrakk> \<Longrightarrow> (a \<rightleftharpoons> b) \<bullet> x \<noteq> x"
       
   750   shows "(supp x) = S"
       
   751   using a1 a2
       
   752 proof (rule supp_is_least_supports)
       
   753   fix S'
       
   754   assume "finite S'" and "S' supports x"
       
   755   show "S \<subseteq> S'"
       
   756   proof (rule subsetCI)
       
   757     fix a
       
   758     assume "a \<in> S" and "a \<notin> S'"
       
   759     have "finite (S \<union> S')"
       
   760       using `finite S` `finite S'` by simp
       
   761     then obtain b where "b \<notin> S \<union> S'" and "sort_of b = sort_of a"
       
   762       by (rule obtain_atom)
       
   763     then have "b \<notin> S" and "b \<notin> S'"  and "sort_of a = sort_of b"
       
   764       by simp_all
       
   765     then have "(a \<rightleftharpoons> b) \<bullet> x = x"
       
   766       using `a \<notin> S'` `S' supports x` by (simp add: supports_def)
       
   767     moreover have "(a \<rightleftharpoons> b) \<bullet> x \<noteq> x"
       
   768       using `a \<in> S` `b \<notin> S` `sort_of a = sort_of b`
       
   769       by (rule a3)
       
   770     ultimately show "False" by simp
       
   771   qed
       
   772 qed
       
   773 
       
   774 section {* Finitely-supported types *}
       
   775 
       
   776 class fs = pt +
       
   777   assumes finite_supp: "finite (supp x)"
       
   778 
       
   779 lemma pure_supp: 
       
   780   shows "supp (x::'a::pure) = {}"
       
   781   unfolding supp_def by (simp add: permute_pure)
       
   782 
       
   783 lemma pure_fresh:
       
   784   fixes x::"'a::pure"
       
   785   shows "a \<sharp> x"
       
   786   unfolding fresh_def by (simp add: pure_supp)
       
   787 
       
   788 instance pure < fs
       
   789 by default (simp add: pure_supp)
       
   790 
       
   791 
       
   792 subsection  {* Type @{typ atom} is finitely-supported. *}
       
   793 
       
   794 lemma supp_atom:
       
   795   shows "supp a = {a}"
       
   796 apply (rule finite_supp_unique)
       
   797 apply (clarsimp simp add: supports_def)
       
   798 apply simp
       
   799 apply simp
       
   800 done
       
   801 
       
   802 lemma fresh_atom: 
       
   803   shows "a \<sharp> b \<longleftrightarrow> a \<noteq> b"
       
   804   unfolding fresh_def supp_atom by simp
       
   805 
       
   806 instance atom :: fs
       
   807 by default (simp add: supp_atom)
       
   808 
       
   809 
       
   810 section {* Type @{typ perm} is finitely-supported. *}
       
   811 
       
   812 lemma perm_swap_eq:
       
   813   shows "(a \<rightleftharpoons> b) \<bullet> p = p \<longleftrightarrow> (p \<bullet> (a \<rightleftharpoons> b)) = (a \<rightleftharpoons> b)"
       
   814 unfolding permute_perm_def
       
   815 by (metis add_diff_cancel minus_perm_def)
       
   816 
       
   817 lemma supports_perm: 
       
   818   shows "{a. p \<bullet> a \<noteq> a} supports p"
       
   819   unfolding supports_def
       
   820   by (simp add: perm_swap_eq swap_eqvt)
       
   821 
       
   822 lemma finite_perm_lemma: 
       
   823   shows "finite {a::atom. p \<bullet> a \<noteq> a}"
       
   824   using finite_Rep_perm [of p]
       
   825   unfolding permute_atom_def .
       
   826 
       
   827 lemma supp_perm:
       
   828   shows "supp p = {a. p \<bullet> a \<noteq> a}"
       
   829 apply (rule finite_supp_unique)
       
   830 apply (rule supports_perm)
       
   831 apply (rule finite_perm_lemma)
       
   832 apply (simp add: perm_swap_eq swap_eqvt)
       
   833 apply (auto simp add: expand_perm_eq swap_atom)
       
   834 done
       
   835 
       
   836 lemma fresh_perm: 
       
   837   shows "a \<sharp> p \<longleftrightarrow> p \<bullet> a = a"
       
   838 unfolding fresh_def by (simp add: supp_perm)
       
   839 
       
   840 lemma supp_swap:
       
   841   shows "supp (a \<rightleftharpoons> b) = (if a = b \<or> sort_of a \<noteq> sort_of b then {} else {a, b})"
       
   842   by (auto simp add: supp_perm swap_atom)
       
   843 
       
   844 lemma fresh_zero_perm: 
       
   845   shows "a \<sharp> (0::perm)"
       
   846   unfolding fresh_perm by simp
       
   847 
       
   848 lemma supp_zero_perm: 
       
   849   shows "supp (0::perm) = {}"
       
   850   unfolding supp_perm by simp
       
   851 
       
   852 lemma supp_plus_perm:
       
   853   fixes p q::perm
       
   854   shows "supp (p + q) \<subseteq> supp p \<union> supp q"
       
   855   by (auto simp add: supp_perm)
       
   856 
       
   857 lemma supp_minus_perm:
       
   858   fixes p::perm
       
   859   shows "supp (- p) = supp p"
       
   860   apply(auto simp add: supp_perm)
       
   861   apply(metis permute_minus_cancel)+
       
   862   done
       
   863 
       
   864 instance perm :: fs
       
   865 by default (simp add: supp_perm finite_perm_lemma)
       
   866 
       
   867 
       
   868 section {* Finite Support instances for other types *}
       
   869 
       
   870 subsection {* Type @{typ "'a \<times> 'b"} is finitely-supported. *}
       
   871 
       
   872 lemma supp_Pair: 
       
   873   shows "supp (x, y) = supp x \<union> supp y"
       
   874   by (simp add: supp_def Collect_imp_eq Collect_neg_eq)
       
   875 
       
   876 lemma fresh_Pair: 
       
   877   shows "a \<sharp> (x, y) \<longleftrightarrow> a \<sharp> x \<and> a \<sharp> y"
       
   878   by (simp add: fresh_def supp_Pair)
       
   879 
       
   880 instance "*" :: (fs, fs) fs
       
   881 apply default
       
   882 apply (induct_tac x)
       
   883 apply (simp add: supp_Pair finite_supp)
       
   884 done
       
   885 
       
   886 
       
   887 subsection {* Type @{typ "'a + 'b"} is finitely supported *}
       
   888 
       
   889 lemma supp_Inl: 
       
   890   shows "supp (Inl x) = supp x"
       
   891   by (simp add: supp_def)
       
   892 
       
   893 lemma supp_Inr: 
       
   894   shows "supp (Inr x) = supp x"
       
   895   by (simp add: supp_def)
       
   896 
       
   897 lemma fresh_Inl: 
       
   898   shows "a \<sharp> Inl x \<longleftrightarrow> a \<sharp> x"
       
   899   by (simp add: fresh_def supp_Inl)
       
   900 
       
   901 lemma fresh_Inr: 
       
   902   shows "a \<sharp> Inr y \<longleftrightarrow> a \<sharp> y"
       
   903   by (simp add: fresh_def supp_Inr)
       
   904 
       
   905 instance "+" :: (fs, fs) fs
       
   906 apply default
       
   907 apply (induct_tac x)
       
   908 apply (simp_all add: supp_Inl supp_Inr finite_supp)
       
   909 done
       
   910 
       
   911 subsection {* Type @{typ "'a option"} is finitely supported *}
       
   912 
       
   913 lemma supp_None: 
       
   914   shows "supp None = {}"
       
   915 by (simp add: supp_def)
       
   916 
       
   917 lemma supp_Some: 
       
   918   shows "supp (Some x) = supp x"
       
   919   by (simp add: supp_def)
       
   920 
       
   921 lemma fresh_None: 
       
   922   shows "a \<sharp> None"
       
   923   by (simp add: fresh_def supp_None)
       
   924 
       
   925 lemma fresh_Some: 
       
   926   shows "a \<sharp> Some x \<longleftrightarrow> a \<sharp> x"
       
   927   by (simp add: fresh_def supp_Some)
       
   928 
       
   929 instance option :: (fs) fs
       
   930 apply default
       
   931 apply (induct_tac x)
       
   932 apply (simp_all add: supp_None supp_Some finite_supp)
       
   933 done
       
   934 
       
   935 subsubsection {* Type @{typ "'a list"} is finitely supported *}
       
   936 
       
   937 lemma supp_Nil: 
       
   938   shows "supp [] = {}"
       
   939   by (simp add: supp_def)
       
   940 
       
   941 lemma supp_Cons: 
       
   942   shows "supp (x # xs) = supp x \<union> supp xs"
       
   943 by (simp add: supp_def Collect_imp_eq Collect_neg_eq)
       
   944 
       
   945 lemma fresh_Nil: 
       
   946   shows "a \<sharp> []"
       
   947   by (simp add: fresh_def supp_Nil)
       
   948 
       
   949 lemma fresh_Cons: 
       
   950   shows "a \<sharp> (x # xs) \<longleftrightarrow> a \<sharp> x \<and> a \<sharp> xs"
       
   951   by (simp add: fresh_def supp_Cons)
       
   952 
       
   953 instance list :: (fs) fs
       
   954 apply default
       
   955 apply (induct_tac x)
       
   956 apply (simp_all add: supp_Nil supp_Cons finite_supp)
       
   957 done
       
   958 
       
   959 end