src/HOL/Library/More_Set.thy
author kuncar
Fri Dec 09 18:07:04 2011 +0100 (2011-12-09)
changeset 45802 b16f976db515
parent 45012 060f76635bfe
child 45974 2b043ed911ac
permissions -rw-r--r--
Quotient_Info stores only relation maps
haftmann@31807
     1
haftmann@31807
     2
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@31807
     3
haftmann@31807
     4
header {* Relating (finite) sets and lists *}
haftmann@31807
     5
haftmann@37024
     6
theory More_Set
haftmann@37023
     7
imports Main More_List
haftmann@31807
     8
begin
haftmann@31807
     9
haftmann@31807
    10
subsection {* Various additional set functions *}
haftmann@31807
    11
haftmann@31807
    12
definition is_empty :: "'a set \<Rightarrow> bool" where
haftmann@31807
    13
  "is_empty A \<longleftrightarrow> A = {}"
haftmann@31807
    14
haftmann@31807
    15
definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
haftmann@31807
    16
  "remove x A = A - {x}"
haftmann@31807
    17
haftmann@42871
    18
lemma comp_fun_idem_remove:
haftmann@42871
    19
  "comp_fun_idem remove"
haftmann@31807
    20
proof -
nipkow@39302
    21
  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: fun_eq_iff remove_def)
haftmann@42871
    22
  show ?thesis by (simp only: comp_fun_idem_remove rem)
haftmann@31807
    23
qed
haftmann@31807
    24
haftmann@31807
    25
lemma minus_fold_remove:
haftmann@31807
    26
  assumes "finite A"
haftmann@37023
    27
  shows "B - A = Finite_Set.fold remove B A"
haftmann@31807
    28
proof -
nipkow@39302
    29
  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: fun_eq_iff remove_def)
haftmann@31807
    30
  show ?thesis by (simp only: rem assms minus_fold_remove)
haftmann@31807
    31
qed
haftmann@31807
    32
haftmann@31807
    33
definition project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
haftmann@45012
    34
  "project P A = {a \<in> A. P a}"
haftmann@45012
    35
haftmann@45012
    36
lemma bounded_Collect_code [code_unfold_post]:
haftmann@45012
    37
  "{x \<in> A. P x} = project P A"
haftmann@45012
    38
  by (simp add: project_def)
haftmann@45012
    39
haftmann@45012
    40
definition product :: "'a set \<Rightarrow> 'b set \<Rightarrow> ('a \<times> 'b) set" where
haftmann@45012
    41
  "product A B = Sigma A (\<lambda>_. B)"
haftmann@45012
    42
haftmann@45012
    43
hide_const (open) product
haftmann@45012
    44
haftmann@45012
    45
lemma [code_unfold_post]:
haftmann@45012
    46
  "Sigma A (\<lambda>_. B) = More_Set.product A B"
haftmann@45012
    47
  by (simp add: product_def)
haftmann@31807
    48
haftmann@31807
    49
haftmann@31807
    50
subsection {* Basic set operations *}
haftmann@31807
    51
haftmann@31807
    52
lemma is_empty_set:
haftmann@37595
    53
  "is_empty (set xs) \<longleftrightarrow> List.null xs"
haftmann@37595
    54
  by (simp add: is_empty_def null_def)
haftmann@31807
    55
haftmann@31807
    56
lemma empty_set:
haftmann@31807
    57
  "{} = set []"
haftmann@31807
    58
  by simp
haftmann@31807
    59
haftmann@32880
    60
lemma insert_set_compl:
haftmann@34977
    61
  "insert x (- set xs) = - set (removeAll x xs)"
haftmann@34977
    62
  by auto
haftmann@31807
    63
haftmann@32880
    64
lemma remove_set_compl:
haftmann@34977
    65
  "remove x (- set xs) = - set (List.insert x xs)"
haftmann@44326
    66
  by (auto simp add: remove_def List.insert_def)
haftmann@32880
    67
haftmann@31807
    68
lemma image_set:
haftmann@31846
    69
  "image f (set xs) = set (map f xs)"
haftmann@31807
    70
  by simp
haftmann@31807
    71
haftmann@31807
    72
lemma project_set:
haftmann@31807
    73
  "project P (set xs) = set (filter P xs)"
haftmann@31807
    74
  by (auto simp add: project_def)
haftmann@31807
    75
haftmann@31807
    76
haftmann@31807
    77
subsection {* Functorial set operations *}
haftmann@31807
    78
haftmann@31807
    79
lemma union_set:
haftmann@37023
    80
  "set xs \<union> A = fold Set.insert xs A"
haftmann@31807
    81
proof -
haftmann@42871
    82
  interpret comp_fun_idem Set.insert
haftmann@42871
    83
    by (fact comp_fun_idem_insert)
haftmann@31807
    84
  show ?thesis by (simp add: union_fold_insert fold_set)
haftmann@31807
    85
qed
haftmann@31807
    86
haftmann@37023
    87
lemma union_set_foldr:
haftmann@37023
    88
  "set xs \<union> A = foldr Set.insert xs A"
haftmann@37023
    89
proof -
haftmann@37023
    90
  have "\<And>x y :: 'a. insert y \<circ> insert x = insert x \<circ> insert y"
haftmann@45012
    91
    by auto
haftmann@37023
    92
  then show ?thesis by (simp add: union_set foldr_fold)
haftmann@37023
    93
qed
haftmann@37023
    94
haftmann@31807
    95
lemma minus_set:
haftmann@37023
    96
  "A - set xs = fold remove xs A"
haftmann@31807
    97
proof -
haftmann@42871
    98
  interpret comp_fun_idem remove
haftmann@42871
    99
    by (fact comp_fun_idem_remove)
haftmann@31807
   100
  show ?thesis
haftmann@31807
   101
    by (simp add: minus_fold_remove [of _ A] fold_set)
haftmann@31807
   102
qed
haftmann@31807
   103
haftmann@37023
   104
lemma minus_set_foldr:
haftmann@37023
   105
  "A - set xs = foldr remove xs A"
haftmann@37023
   106
proof -
haftmann@37023
   107
  have "\<And>x y :: 'a. remove y \<circ> remove x = remove x \<circ> remove y"
haftmann@45012
   108
    by (auto simp add: remove_def)
haftmann@37023
   109
  then show ?thesis by (simp add: minus_set foldr_fold)
haftmann@37023
   110
qed
haftmann@37023
   111
haftmann@31807
   112
haftmann@31807
   113
subsection {* Derived set operations *}
haftmann@31807
   114
haftmann@31807
   115
lemma member:
haftmann@31807
   116
  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
haftmann@31807
   117
  by simp
haftmann@31807
   118
haftmann@31807
   119
lemma subset_eq:
haftmann@31807
   120
  "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
haftmann@31807
   121
  by (fact subset_eq)
haftmann@31807
   122
haftmann@31807
   123
lemma subset:
haftmann@31807
   124
  "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
haftmann@31807
   125
  by (fact less_le_not_le)
haftmann@31807
   126
haftmann@31807
   127
lemma set_eq:
haftmann@31807
   128
  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
haftmann@31807
   129
  by (fact eq_iff)
haftmann@31807
   130
haftmann@31807
   131
lemma inter:
haftmann@31807
   132
  "A \<inter> B = project (\<lambda>x. x \<in> A) B"
haftmann@31807
   133
  by (auto simp add: project_def)
haftmann@31807
   134
haftmann@37023
   135
haftmann@45012
   136
subsection {* Theorems on relations *}
haftmann@45012
   137
haftmann@45012
   138
lemma product_code:
haftmann@45012
   139
  "More_Set.product (set xs) (set ys) = set [(x, y). x \<leftarrow> xs, y \<leftarrow> ys]"
haftmann@45012
   140
  by (auto simp add: product_def)
haftmann@37023
   141
haftmann@45012
   142
lemma Id_on_set:
haftmann@45012
   143
  "Id_on (set xs) = set [(x, x). x \<leftarrow> xs]"
haftmann@45012
   144
  by (auto simp add: Id_on_def)
haftmann@45012
   145
haftmann@45012
   146
lemma set_rel_comp:
haftmann@45012
   147
  "set xys O set yzs = set ([(fst xy, snd yz). xy \<leftarrow> xys, yz \<leftarrow> yzs, snd xy = fst yz])"
haftmann@45012
   148
  by (auto simp add: Bex_def)
haftmann@45012
   149
haftmann@45012
   150
lemma wf_set:
haftmann@45012
   151
  "wf (set xs) = acyclic (set xs)"
haftmann@45012
   152
  by (simp add: wf_iff_acyclic_if_finite)
haftmann@37023
   153
haftmann@37024
   154
end