src/HOL/Library/List_Set.thy
author haftmann
Thu May 20 16:35:54 2010 +0200 (2010-05-20)
changeset 37023 efc202e1677e
parent 34977 27ceb64d41ea
permissions -rw-r--r--
added theory More_List
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@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@31807
    18
lemma fun_left_comm_idem_remove:
haftmann@31807
    19
  "fun_left_comm_idem remove"
haftmann@31807
    20
proof -
haftmann@31807
    21
  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
haftmann@31807
    22
  show ?thesis by (simp only: fun_left_comm_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 -
haftmann@31807
    29
  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq 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@31807
    34
  "project P A = {a\<in>A. P a}"
haftmann@31807
    35
haftmann@31807
    36
haftmann@31807
    37
subsection {* Basic set operations *}
haftmann@31807
    38
haftmann@31807
    39
lemma is_empty_set:
haftmann@31807
    40
  "is_empty (set xs) \<longleftrightarrow> null xs"
haftmann@31807
    41
  by (simp add: is_empty_def null_empty)
haftmann@31807
    42
haftmann@31807
    43
lemma ball_set:
haftmann@31807
    44
  "(\<forall>x\<in>set xs. P x) \<longleftrightarrow> list_all P xs"
haftmann@31807
    45
  by (rule list_ball_code)
haftmann@31807
    46
haftmann@31807
    47
lemma bex_set:
haftmann@31807
    48
  "(\<exists>x\<in>set xs. P x) \<longleftrightarrow> list_ex P xs"
haftmann@31807
    49
  by (rule list_bex_code)
haftmann@31807
    50
haftmann@31807
    51
lemma empty_set:
haftmann@31807
    52
  "{} = set []"
haftmann@31807
    53
  by simp
haftmann@31807
    54
haftmann@32880
    55
lemma insert_set_compl:
haftmann@34977
    56
  "insert x (- set xs) = - set (removeAll x xs)"
haftmann@34977
    57
  by auto
haftmann@31807
    58
haftmann@32880
    59
lemma remove_set_compl:
haftmann@34977
    60
  "remove x (- set xs) = - set (List.insert x xs)"
haftmann@34977
    61
  by (auto simp del: mem_def simp add: remove_def List.insert_def)
haftmann@32880
    62
haftmann@31807
    63
lemma image_set:
haftmann@31846
    64
  "image f (set xs) = set (map f xs)"
haftmann@31807
    65
  by simp
haftmann@31807
    66
haftmann@31807
    67
lemma project_set:
haftmann@31807
    68
  "project P (set xs) = set (filter P xs)"
haftmann@31807
    69
  by (auto simp add: project_def)
haftmann@31807
    70
haftmann@31807
    71
haftmann@31807
    72
subsection {* Functorial set operations *}
haftmann@31807
    73
haftmann@31807
    74
lemma union_set:
haftmann@37023
    75
  "set xs \<union> A = fold Set.insert xs A"
haftmann@31807
    76
proof -
haftmann@31807
    77
  interpret fun_left_comm_idem Set.insert
haftmann@31807
    78
    by (fact fun_left_comm_idem_insert)
haftmann@31807
    79
  show ?thesis by (simp add: union_fold_insert fold_set)
haftmann@31807
    80
qed
haftmann@31807
    81
haftmann@37023
    82
lemma union_set_foldr:
haftmann@37023
    83
  "set xs \<union> A = foldr Set.insert xs A"
haftmann@37023
    84
proof -
haftmann@37023
    85
  have "\<And>x y :: 'a. insert y \<circ> insert x = insert x \<circ> insert y"
haftmann@37023
    86
    by (auto intro: ext)
haftmann@37023
    87
  then show ?thesis by (simp add: union_set foldr_fold)
haftmann@37023
    88
qed
haftmann@37023
    89
haftmann@31807
    90
lemma minus_set:
haftmann@37023
    91
  "A - set xs = fold remove xs A"
haftmann@31807
    92
proof -
haftmann@31807
    93
  interpret fun_left_comm_idem remove
haftmann@31807
    94
    by (fact fun_left_comm_idem_remove)
haftmann@31807
    95
  show ?thesis
haftmann@31807
    96
    by (simp add: minus_fold_remove [of _ A] fold_set)
haftmann@31807
    97
qed
haftmann@31807
    98
haftmann@37023
    99
lemma minus_set_foldr:
haftmann@37023
   100
  "A - set xs = foldr remove xs A"
haftmann@37023
   101
proof -
haftmann@37023
   102
  have "\<And>x y :: 'a. remove y \<circ> remove x = remove x \<circ> remove y"
haftmann@37023
   103
    by (auto simp add: remove_def intro: ext)
haftmann@37023
   104
  then show ?thesis by (simp add: minus_set foldr_fold)
haftmann@37023
   105
qed
haftmann@37023
   106
haftmann@31807
   107
haftmann@31807
   108
subsection {* Derived set operations *}
haftmann@31807
   109
haftmann@31807
   110
lemma member:
haftmann@31807
   111
  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
haftmann@31807
   112
  by simp
haftmann@31807
   113
haftmann@31807
   114
lemma subset_eq:
haftmann@31807
   115
  "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
haftmann@31807
   116
  by (fact subset_eq)
haftmann@31807
   117
haftmann@31807
   118
lemma subset:
haftmann@31807
   119
  "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
haftmann@31807
   120
  by (fact less_le_not_le)
haftmann@31807
   121
haftmann@31807
   122
lemma set_eq:
haftmann@31807
   123
  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
haftmann@31807
   124
  by (fact eq_iff)
haftmann@31807
   125
haftmann@31807
   126
lemma inter:
haftmann@31807
   127
  "A \<inter> B = project (\<lambda>x. x \<in> A) B"
haftmann@31807
   128
  by (auto simp add: project_def)
haftmann@31807
   129
haftmann@37023
   130
haftmann@37023
   131
subsection {* Various lemmas *}
haftmann@37023
   132
haftmann@37023
   133
lemma not_set_compl:
haftmann@37023
   134
  "Not \<circ> set xs = - set xs"
haftmann@37023
   135
  by (simp add: fun_Compl_def bool_Compl_def comp_def expand_fun_eq)
haftmann@37023
   136
haftmann@31807
   137
end