src/HOL/Library/List_Set.thy
author wenzelm
Thu Feb 11 23:00:22 2010 +0100 (2010-02-11)
changeset 35115 446c5063e4fd
parent 34977 27ceb64d41ea
child 37023 efc202e1677e
permissions -rw-r--r--
modernized translations;
formal markup of @{syntax_const} and @{const_syntax};
minor tuning;
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 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@31807
    27
  shows "B - A = 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@31807
    75
  "set xs \<union> A = foldl (\<lambda>A x. Set.insert x A) A xs"
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@31807
    82
lemma minus_set:
haftmann@31807
    83
  "A - set xs = foldl (\<lambda>A x. remove x A) A xs"
haftmann@31807
    84
proof -
haftmann@31807
    85
  interpret fun_left_comm_idem remove
haftmann@31807
    86
    by (fact fun_left_comm_idem_remove)
haftmann@31807
    87
  show ?thesis
haftmann@31807
    88
    by (simp add: minus_fold_remove [of _ A] fold_set)
haftmann@31807
    89
qed
haftmann@31807
    90
haftmann@31807
    91
haftmann@31807
    92
subsection {* Derived set operations *}
haftmann@31807
    93
haftmann@31807
    94
lemma member:
haftmann@31807
    95
  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
haftmann@31807
    96
  by simp
haftmann@31807
    97
haftmann@31807
    98
lemma subset_eq:
haftmann@31807
    99
  "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
haftmann@31807
   100
  by (fact subset_eq)
haftmann@31807
   101
haftmann@31807
   102
lemma subset:
haftmann@31807
   103
  "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
haftmann@31807
   104
  by (fact less_le_not_le)
haftmann@31807
   105
haftmann@31807
   106
lemma set_eq:
haftmann@31807
   107
  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
haftmann@31807
   108
  by (fact eq_iff)
haftmann@31807
   109
haftmann@31807
   110
lemma inter:
haftmann@31807
   111
  "A \<inter> B = project (\<lambda>x. x \<in> A) B"
haftmann@31807
   112
  by (auto simp add: project_def)
haftmann@31807
   113
haftmann@31807
   114
end