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