src/HOL/Library/List_Set.thy
author haftmann
Tue Oct 06 15:59:12 2009 +0200 (2009-10-06)
changeset 32880 b8bee63c7202
parent 31851 c04f8c51d0ab
child 33939 fcb50b497763
permissions -rw-r--r--
sets and cosets
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@31807
     6
theory List_Set
haftmann@31807
     7
imports Main
haftmann@31807
     8
begin
haftmann@31807
     9
haftmann@31807
    10
subsection {* Various additional list functions *}
haftmann@31807
    11
haftmann@31807
    12
definition insert :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@31807
    13
  "insert x xs = (if x \<in> set xs then xs else x # xs)"
haftmann@31807
    14
haftmann@31807
    15
definition remove_all :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
haftmann@31807
    16
  "remove_all x xs = filter (Not o op = x) xs"
haftmann@31807
    17
haftmann@31807
    18
haftmann@31807
    19
subsection {* Various additional set functions *}
haftmann@31807
    20
haftmann@31807
    21
definition is_empty :: "'a set \<Rightarrow> bool" where
haftmann@31807
    22
  "is_empty A \<longleftrightarrow> A = {}"
haftmann@31807
    23
haftmann@31807
    24
definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
haftmann@31807
    25
  "remove x A = A - {x}"
haftmann@31807
    26
haftmann@31807
    27
lemma fun_left_comm_idem_remove:
haftmann@31807
    28
  "fun_left_comm_idem remove"
haftmann@31807
    29
proof -
haftmann@31807
    30
  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
haftmann@31807
    31
  show ?thesis by (simp only: fun_left_comm_idem_remove rem)
haftmann@31807
    32
qed
haftmann@31807
    33
haftmann@31807
    34
lemma minus_fold_remove:
haftmann@31807
    35
  assumes "finite A"
haftmann@31807
    36
  shows "B - A = fold remove B A"
haftmann@31807
    37
proof -
haftmann@31807
    38
  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
haftmann@31807
    39
  show ?thesis by (simp only: rem assms minus_fold_remove)
haftmann@31807
    40
qed
haftmann@31807
    41
haftmann@31807
    42
definition project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
haftmann@31807
    43
  "project P A = {a\<in>A. P a}"
haftmann@31807
    44
haftmann@31807
    45
haftmann@31807
    46
subsection {* Basic set operations *}
haftmann@31807
    47
haftmann@31807
    48
lemma is_empty_set:
haftmann@31807
    49
  "is_empty (set xs) \<longleftrightarrow> null xs"
haftmann@31807
    50
  by (simp add: is_empty_def null_empty)
haftmann@31807
    51
haftmann@31807
    52
lemma ball_set:
haftmann@31807
    53
  "(\<forall>x\<in>set xs. P x) \<longleftrightarrow> list_all P xs"
haftmann@31807
    54
  by (rule list_ball_code)
haftmann@31807
    55
haftmann@31807
    56
lemma bex_set:
haftmann@31807
    57
  "(\<exists>x\<in>set xs. P x) \<longleftrightarrow> list_ex P xs"
haftmann@31807
    58
  by (rule list_bex_code)
haftmann@31807
    59
haftmann@31807
    60
lemma empty_set:
haftmann@31807
    61
  "{} = set []"
haftmann@31807
    62
  by simp
haftmann@31807
    63
haftmann@31807
    64
lemma insert_set:
haftmann@31807
    65
  "Set.insert x (set xs) = set (insert x xs)"
haftmann@31807
    66
  by (auto simp add: insert_def)
haftmann@31807
    67
haftmann@32880
    68
lemma insert_set_compl:
haftmann@32880
    69
  "Set.insert x (- set xs) = - set (List_Set.remove_all x xs)"
haftmann@32880
    70
  by (auto simp del: mem_def simp add: remove_all_def)
haftmann@32880
    71
haftmann@31807
    72
lemma remove_set:
haftmann@31807
    73
  "remove x (set xs) = set (remove_all x xs)"
haftmann@31807
    74
  by (auto simp add: remove_def remove_all_def)
haftmann@31807
    75
haftmann@32880
    76
lemma remove_set_compl:
haftmann@32880
    77
  "List_Set.remove x (- set xs) = - set (List_Set.insert x xs)"
haftmann@32880
    78
  by (auto simp del: mem_def simp add: remove_def List_Set.insert_def)
haftmann@32880
    79
haftmann@31807
    80
lemma image_set:
haftmann@31846
    81
  "image f (set xs) = set (map f xs)"
haftmann@31807
    82
  by simp
haftmann@31807
    83
haftmann@31807
    84
lemma project_set:
haftmann@31807
    85
  "project P (set xs) = set (filter P xs)"
haftmann@31807
    86
  by (auto simp add: project_def)
haftmann@31807
    87
haftmann@31807
    88
haftmann@31807
    89
subsection {* Functorial set operations *}
haftmann@31807
    90
haftmann@31807
    91
lemma union_set:
haftmann@31807
    92
  "set xs \<union> A = foldl (\<lambda>A x. Set.insert x A) A xs"
haftmann@31807
    93
proof -
haftmann@31807
    94
  interpret fun_left_comm_idem Set.insert
haftmann@31807
    95
    by (fact fun_left_comm_idem_insert)
haftmann@31807
    96
  show ?thesis by (simp add: union_fold_insert fold_set)
haftmann@31807
    97
qed
haftmann@31807
    98
haftmann@31807
    99
lemma minus_set:
haftmann@31807
   100
  "A - set xs = foldl (\<lambda>A x. remove x A) A xs"
haftmann@31807
   101
proof -
haftmann@31807
   102
  interpret fun_left_comm_idem remove
haftmann@31807
   103
    by (fact fun_left_comm_idem_remove)
haftmann@31807
   104
  show ?thesis
haftmann@31807
   105
    by (simp add: minus_fold_remove [of _ A] fold_set)
haftmann@31807
   106
qed
haftmann@31807
   107
haftmann@31807
   108
lemma Inter_set:
haftmann@32880
   109
  "Inter (set As) = foldl (op \<inter>) UNIV As"
haftmann@31807
   110
proof -
haftmann@32880
   111
  have "fold (op \<inter>) UNIV (set As) = foldl (\<lambda>y x. x \<inter> y) UNIV As"
haftmann@31807
   112
    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
haftmann@32880
   113
  then show ?thesis
haftmann@32880
   114
    by (simp only: Inter_fold_inter finite_set Int_commute)
haftmann@31807
   115
qed
haftmann@31807
   116
haftmann@31807
   117
lemma Union_set:
haftmann@31807
   118
  "Union (set As) = foldl (op \<union>) {} As"
haftmann@31807
   119
proof -
haftmann@31807
   120
  have "fold (op \<union>) {} (set As) = foldl (\<lambda>y x. x \<union> y) {} As"
haftmann@31807
   121
    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
haftmann@31807
   122
  then show ?thesis
haftmann@31807
   123
    by (simp only: Union_fold_union finite_set Un_commute)
haftmann@31807
   124
qed
haftmann@31807
   125
haftmann@31807
   126
lemma INTER_set:
haftmann@32880
   127
  "INTER (set As) f = foldl (\<lambda>B A. f A \<inter> B) UNIV As"
haftmann@31807
   128
proof -
haftmann@32880
   129
  have "fold (\<lambda>A. op \<inter> (f A)) UNIV (set As) = foldl (\<lambda>B A. f A \<inter> B) UNIV As"
haftmann@31807
   130
    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
haftmann@32880
   131
  then show ?thesis
haftmann@32880
   132
    by (simp only: INTER_fold_inter finite_set)
haftmann@31807
   133
qed
haftmann@31807
   134
haftmann@31807
   135
lemma UNION_set:
haftmann@31807
   136
  "UNION (set As) f = foldl (\<lambda>B A. f A \<union> B) {} As"
haftmann@31807
   137
proof -
haftmann@31807
   138
  have "fold (\<lambda>A. op \<union> (f A)) {} (set As) = foldl (\<lambda>B A. f A \<union> B) {} As"
haftmann@31807
   139
    by (rule fun_left_comm_idem.fold_set, unfold_locales, auto)
haftmann@31807
   140
  then show ?thesis
haftmann@31807
   141
    by (simp only: UNION_fold_union finite_set)
haftmann@31807
   142
qed
haftmann@31807
   143
haftmann@31807
   144
haftmann@31807
   145
subsection {* Derived set operations *}
haftmann@31807
   146
haftmann@31807
   147
lemma member:
haftmann@31807
   148
  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
haftmann@31807
   149
  by simp
haftmann@31807
   150
haftmann@31807
   151
lemma subset_eq:
haftmann@31807
   152
  "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
haftmann@31807
   153
  by (fact subset_eq)
haftmann@31807
   154
haftmann@31807
   155
lemma subset:
haftmann@31807
   156
  "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
haftmann@31807
   157
  by (fact less_le_not_le)
haftmann@31807
   158
haftmann@31807
   159
lemma set_eq:
haftmann@31807
   160
  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
haftmann@31807
   161
  by (fact eq_iff)
haftmann@31807
   162
haftmann@31807
   163
lemma inter:
haftmann@31807
   164
  "A \<inter> B = project (\<lambda>x. x \<in> A) B"
haftmann@31807
   165
  by (auto simp add: project_def)
haftmann@31807
   166
haftmann@31851
   167
haftmann@31851
   168
hide (open) const insert
haftmann@31851
   169
haftmann@31807
   170
end