| 
31807
 | 
     1  | 
  | 
| 
 | 
     2  | 
(* Author: Florian Haftmann, TU Muenchen *)
  | 
| 
 | 
     3  | 
  | 
| 
 | 
     4  | 
header {* Relating (finite) sets and lists *}
 | 
| 
 | 
     5  | 
  | 
| 
 | 
     6  | 
theory List_Set
  | 
| 
 | 
     7  | 
imports Main
  | 
| 
 | 
     8  | 
begin
  | 
| 
 | 
     9  | 
  | 
| 
 | 
    10  | 
subsection {* Various additional set functions *}
 | 
| 
 | 
    11  | 
  | 
| 
 | 
    12  | 
definition is_empty :: "'a set \<Rightarrow> bool" where
  | 
| 
 | 
    13  | 
  "is_empty A \<longleftrightarrow> A = {}"
 | 
| 
 | 
    14  | 
  | 
| 
 | 
    15  | 
definition remove :: "'a \<Rightarrow> 'a set \<Rightarrow> 'a set" where
  | 
| 
 | 
    16  | 
  "remove x A = A - {x}"
 | 
| 
 | 
    17  | 
  | 
| 
 | 
    18  | 
lemma fun_left_comm_idem_remove:
  | 
| 
 | 
    19  | 
  "fun_left_comm_idem remove"
  | 
| 
 | 
    20  | 
proof -
  | 
| 
 | 
    21  | 
  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
 | 
| 
 | 
    22  | 
  show ?thesis by (simp only: fun_left_comm_idem_remove rem)
  | 
| 
 | 
    23  | 
qed
  | 
| 
 | 
    24  | 
  | 
| 
 | 
    25  | 
lemma minus_fold_remove:
  | 
| 
 | 
    26  | 
  assumes "finite A"
  | 
| 
 | 
    27  | 
  shows "B - A = fold remove B A"
  | 
| 
 | 
    28  | 
proof -
  | 
| 
 | 
    29  | 
  have rem: "remove = (\<lambda>x A. A - {x})" by (simp add: expand_fun_eq remove_def)
 | 
| 
 | 
    30  | 
  show ?thesis by (simp only: rem assms minus_fold_remove)
  | 
| 
 | 
    31  | 
qed
  | 
| 
 | 
    32  | 
  | 
| 
 | 
    33  | 
definition project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
 | 
| 
 | 
    34  | 
  "project P A = {a\<in>A. P a}"
 | 
| 
 | 
    35  | 
  | 
| 
 | 
    36  | 
  | 
| 
 | 
    37  | 
subsection {* Basic set operations *}
 | 
| 
 | 
    38  | 
  | 
| 
 | 
    39  | 
lemma is_empty_set:
  | 
| 
 | 
    40  | 
  "is_empty (set xs) \<longleftrightarrow> null xs"
  | 
| 
 | 
    41  | 
  by (simp add: is_empty_def null_empty)
  | 
| 
 | 
    42  | 
  | 
| 
 | 
    43  | 
lemma ball_set:
  | 
| 
 | 
    44  | 
  "(\<forall>x\<in>set xs. P x) \<longleftrightarrow> list_all P xs"
  | 
| 
 | 
    45  | 
  by (rule list_ball_code)
  | 
| 
 | 
    46  | 
  | 
| 
 | 
    47  | 
lemma bex_set:
  | 
| 
 | 
    48  | 
  "(\<exists>x\<in>set xs. P x) \<longleftrightarrow> list_ex P xs"
  | 
| 
 | 
    49  | 
  by (rule list_bex_code)
  | 
| 
 | 
    50  | 
  | 
| 
 | 
    51  | 
lemma empty_set:
  | 
| 
 | 
    52  | 
  "{} = set []"
 | 
| 
 | 
    53  | 
  by simp
  | 
| 
 | 
    54  | 
  | 
| 
32880
 | 
    55  | 
lemma insert_set_compl:
  | 
| 
34977
 | 
    56  | 
  "insert x (- set xs) = - set (removeAll x xs)"
  | 
| 
 | 
    57  | 
  by auto
  | 
| 
31807
 | 
    58  | 
  | 
| 
32880
 | 
    59  | 
lemma remove_set_compl:
  | 
| 
34977
 | 
    60  | 
  "remove x (- set xs) = - set (List.insert x xs)"
  | 
| 
 | 
    61  | 
  by (auto simp del: mem_def simp add: remove_def List.insert_def)
  | 
| 
32880
 | 
    62  | 
  | 
| 
31807
 | 
    63  | 
lemma image_set:
  | 
| 
31846
 | 
    64  | 
  "image f (set xs) = set (map f xs)"
  | 
| 
31807
 | 
    65  | 
  by simp
  | 
| 
 | 
    66  | 
  | 
| 
 | 
    67  | 
lemma project_set:
  | 
| 
 | 
    68  | 
  "project P (set xs) = set (filter P xs)"
  | 
| 
 | 
    69  | 
  by (auto simp add: project_def)
  | 
| 
 | 
    70  | 
  | 
| 
 | 
    71  | 
  | 
| 
 | 
    72  | 
subsection {* Functorial set operations *}
 | 
| 
 | 
    73  | 
  | 
| 
 | 
    74  | 
lemma union_set:
  | 
| 
 | 
    75  | 
  "set xs \<union> A = foldl (\<lambda>A x. Set.insert x A) A xs"
  | 
| 
 | 
    76  | 
proof -
  | 
| 
 | 
    77  | 
  interpret fun_left_comm_idem Set.insert
  | 
| 
 | 
    78  | 
    by (fact fun_left_comm_idem_insert)
  | 
| 
 | 
    79  | 
  show ?thesis by (simp add: union_fold_insert fold_set)
  | 
| 
 | 
    80  | 
qed
  | 
| 
 | 
    81  | 
  | 
| 
 | 
    82  | 
lemma minus_set:
  | 
| 
 | 
    83  | 
  "A - set xs = foldl (\<lambda>A x. remove x A) A xs"
  | 
| 
 | 
    84  | 
proof -
  | 
| 
 | 
    85  | 
  interpret fun_left_comm_idem remove
  | 
| 
 | 
    86  | 
    by (fact fun_left_comm_idem_remove)
  | 
| 
 | 
    87  | 
  show ?thesis
  | 
| 
 | 
    88  | 
    by (simp add: minus_fold_remove [of _ A] fold_set)
  | 
| 
 | 
    89  | 
qed
  | 
| 
 | 
    90  | 
  | 
| 
 | 
    91  | 
  | 
| 
 | 
    92  | 
subsection {* Derived set operations *}
 | 
| 
 | 
    93  | 
  | 
| 
 | 
    94  | 
lemma member:
  | 
| 
 | 
    95  | 
  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
  | 
| 
 | 
    96  | 
  by simp
  | 
| 
 | 
    97  | 
  | 
| 
 | 
    98  | 
lemma subset_eq:
  | 
| 
 | 
    99  | 
  "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
  | 
| 
 | 
   100  | 
  by (fact subset_eq)
  | 
| 
 | 
   101  | 
  | 
| 
 | 
   102  | 
lemma subset:
  | 
| 
 | 
   103  | 
  "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
  | 
| 
 | 
   104  | 
  by (fact less_le_not_le)
  | 
| 
 | 
   105  | 
  | 
| 
 | 
   106  | 
lemma set_eq:
  | 
| 
 | 
   107  | 
  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
  | 
| 
 | 
   108  | 
  by (fact eq_iff)
  | 
| 
 | 
   109  | 
  | 
| 
 | 
   110  | 
lemma inter:
  | 
| 
 | 
   111  | 
  "A \<inter> B = project (\<lambda>x. x \<in> A) B"
  | 
| 
 | 
   112  | 
  by (auto simp add: project_def)
  | 
| 
 | 
   113  | 
  | 
| 
 | 
   114  | 
end  |