src/HOL/Library/Code_Set.thy
author haftmann
Thu Jun 25 17:07:18 2009 +0200 (2009-06-25)
changeset 31807 039893a9a77d
child 31846 89c37daebfdd
permissions -rw-r--r--
added List_Set and Code_Set theories
haftmann@31807
     1
haftmann@31807
     2
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@31807
     3
haftmann@31807
     4
header {* Executable finite sets *}
haftmann@31807
     5
haftmann@31807
     6
theory Code_Set
haftmann@31807
     7
imports List_Set
haftmann@31807
     8
begin
haftmann@31807
     9
haftmann@31807
    10
lemma foldl_apply_inv:
haftmann@31807
    11
  assumes "\<And>x. g (h x) = x"
haftmann@31807
    12
  shows "foldl f (g s) xs = g (foldl (\<lambda>s x. h (f (g s) x)) s xs)"
haftmann@31807
    13
  by (rule sym, induct xs arbitrary: s) (simp_all add: assms)
haftmann@31807
    14
haftmann@31807
    15
subsection {* Lifting *}
haftmann@31807
    16
haftmann@31807
    17
datatype 'a fset = Fset "'a set"
haftmann@31807
    18
haftmann@31807
    19
primrec member :: "'a fset \<Rightarrow> 'a set" where
haftmann@31807
    20
  "member (Fset A) = A"
haftmann@31807
    21
haftmann@31807
    22
lemma Fset_member [simp]:
haftmann@31807
    23
  "Fset (member A) = A"
haftmann@31807
    24
  by (cases A) simp
haftmann@31807
    25
haftmann@31807
    26
definition Set :: "'a list \<Rightarrow> 'a fset" where
haftmann@31807
    27
  "Set xs = Fset (set xs)"
haftmann@31807
    28
haftmann@31807
    29
lemma member_Set [simp]:
haftmann@31807
    30
  "member (Set xs) = set xs"
haftmann@31807
    31
  by (simp add: Set_def)
haftmann@31807
    32
haftmann@31807
    33
code_datatype Set
haftmann@31807
    34
haftmann@31807
    35
haftmann@31807
    36
subsection {* Basic operations *}
haftmann@31807
    37
haftmann@31807
    38
definition is_empty :: "'a fset \<Rightarrow> bool" where
haftmann@31807
    39
  "is_empty A \<longleftrightarrow> List_Set.is_empty (member A)"
haftmann@31807
    40
haftmann@31807
    41
lemma is_empty_Set [code]:
haftmann@31807
    42
  "is_empty (Set xs) \<longleftrightarrow> null xs"
haftmann@31807
    43
  by (simp add: is_empty_def is_empty_set)
haftmann@31807
    44
haftmann@31807
    45
definition empty :: "'a fset" where
haftmann@31807
    46
  "empty = Fset {}"
haftmann@31807
    47
haftmann@31807
    48
lemma empty_Set [code]:
haftmann@31807
    49
  "empty = Set []"
haftmann@31807
    50
  by (simp add: empty_def Set_def)
haftmann@31807
    51
haftmann@31807
    52
definition insert :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
haftmann@31807
    53
  "insert x A = Fset (Set.insert x (member A))"
haftmann@31807
    54
haftmann@31807
    55
lemma insert_Set [code]:
haftmann@31807
    56
  "insert x (Set xs) = Set (List_Set.insert x xs)"
haftmann@31807
    57
  by (simp add: insert_def Set_def insert_set)
haftmann@31807
    58
haftmann@31807
    59
definition remove :: "'a \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
haftmann@31807
    60
  "remove x A = Fset (List_Set.remove x (member A))"
haftmann@31807
    61
haftmann@31807
    62
lemma remove_Set [code]:
haftmann@31807
    63
  "remove x (Set xs) = Set (remove_all x xs)"
haftmann@31807
    64
  by (simp add: remove_def Set_def remove_set)
haftmann@31807
    65
haftmann@31807
    66
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a fset \<Rightarrow> 'b fset" where
haftmann@31807
    67
  "map f A = Fset (image f (member A))"
haftmann@31807
    68
haftmann@31807
    69
lemma map_Set [code]:
haftmann@31807
    70
  "map f (Set xs) = Set (remdups (List.map f xs))"
haftmann@31807
    71
  by (simp add: map_def Set_def)
haftmann@31807
    72
haftmann@31807
    73
definition project :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
haftmann@31807
    74
  "project P A = Fset (List_Set.project P (member A))"
haftmann@31807
    75
haftmann@31807
    76
lemma project_Set [code]:
haftmann@31807
    77
  "project P (Set xs) = Set (filter P xs)"
haftmann@31807
    78
  by (simp add: project_def Set_def project_set)
haftmann@31807
    79
haftmann@31807
    80
definition forall :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> bool" where
haftmann@31807
    81
  "forall P A \<longleftrightarrow> Ball (member A) P"
haftmann@31807
    82
haftmann@31807
    83
lemma forall_Set [code]:
haftmann@31807
    84
  "forall P (Set xs) \<longleftrightarrow> list_all P xs"
haftmann@31807
    85
  by (simp add: forall_def Set_def ball_set)
haftmann@31807
    86
haftmann@31807
    87
definition exists :: "('a \<Rightarrow> bool) \<Rightarrow> 'a fset \<Rightarrow> bool" where
haftmann@31807
    88
  "exists P A \<longleftrightarrow> Bex (member A) P"
haftmann@31807
    89
haftmann@31807
    90
lemma exists_Set [code]:
haftmann@31807
    91
  "exists P (Set xs) \<longleftrightarrow> list_ex P xs"
haftmann@31807
    92
  by (simp add: exists_def Set_def bex_set)
haftmann@31807
    93
haftmann@31807
    94
haftmann@31807
    95
subsection {* Functorial operations *}
haftmann@31807
    96
haftmann@31807
    97
definition union :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
haftmann@31807
    98
  "union A B = Fset (member A \<union> member B)"
haftmann@31807
    99
haftmann@31807
   100
lemma union_insert [code]:
haftmann@31807
   101
  "union (Set xs) A = foldl (\<lambda>A x. insert x A) A xs"
haftmann@31807
   102
proof -
haftmann@31807
   103
  have "foldl (\<lambda>A x. Set.insert x A) (member A) xs =
haftmann@31807
   104
    member (foldl (\<lambda>A x. Fset (Set.insert x (member A))) A xs)"
haftmann@31807
   105
    by (rule foldl_apply_inv) simp
haftmann@31807
   106
  then show ?thesis by (simp add: union_def union_set insert_def)
haftmann@31807
   107
qed
haftmann@31807
   108
haftmann@31807
   109
definition subtract :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
haftmann@31807
   110
  "subtract A B = Fset (member B - member A)"
haftmann@31807
   111
haftmann@31807
   112
lemma subtract_remove [code]:
haftmann@31807
   113
  "subtract (Set xs) A = foldl (\<lambda>A x. remove x A) A xs"
haftmann@31807
   114
proof -
haftmann@31807
   115
  have "foldl (\<lambda>A x. List_Set.remove x A) (member A) xs =
haftmann@31807
   116
    member (foldl (\<lambda>A x. Fset (List_Set.remove x (member A))) A xs)"
haftmann@31807
   117
    by (rule foldl_apply_inv) simp
haftmann@31807
   118
  then show ?thesis by (simp add: subtract_def minus_set remove_def)
haftmann@31807
   119
qed
haftmann@31807
   120
haftmann@31807
   121
haftmann@31807
   122
subsection {* Derived operations *}
haftmann@31807
   123
haftmann@31807
   124
lemma member_exists [code]:
haftmann@31807
   125
  "member A y \<longleftrightarrow> exists (\<lambda>x. y = x) A"
haftmann@31807
   126
  by (simp add: exists_def mem_def)
haftmann@31807
   127
haftmann@31807
   128
definition subfset_eq :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" where
haftmann@31807
   129
  "subfset_eq A B \<longleftrightarrow> member A \<subseteq> member B"
haftmann@31807
   130
haftmann@31807
   131
lemma subfset_eq_forall [code]:
haftmann@31807
   132
  "subfset_eq A B \<longleftrightarrow> forall (\<lambda>x. member B x) A"
haftmann@31807
   133
  by (simp add: subfset_eq_def subset_eq forall_def mem_def)
haftmann@31807
   134
haftmann@31807
   135
definition subfset :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> bool" where
haftmann@31807
   136
  "subfset A B \<longleftrightarrow> member A \<subset> member B"
haftmann@31807
   137
haftmann@31807
   138
lemma subfset_subfset_eq [code]:
haftmann@31807
   139
  "subfset A B \<longleftrightarrow> subfset_eq A B \<and> \<not> subfset_eq B A"
haftmann@31807
   140
  by (simp add: subfset_def subfset_eq_def subset)
haftmann@31807
   141
haftmann@31807
   142
lemma eq_fset_subfset_eq [code]:
haftmann@31807
   143
  "eq_class.eq A B \<longleftrightarrow> subfset_eq A B \<and> subfset_eq B A"
haftmann@31807
   144
  by (cases A, cases B) (simp add: eq subfset_eq_def set_eq)
haftmann@31807
   145
haftmann@31807
   146
definition inter :: "'a fset \<Rightarrow> 'a fset \<Rightarrow> 'a fset" where
haftmann@31807
   147
  "inter A B = Fset (List_Set.project (member A) (member B))"
haftmann@31807
   148
haftmann@31807
   149
lemma inter_project [code]:
haftmann@31807
   150
  "inter A B = project (member A) B"
haftmann@31807
   151
  by (simp add: inter_def project_def inter)
haftmann@31807
   152
haftmann@31807
   153
haftmann@31807
   154
subsection {* Misc operations *}
haftmann@31807
   155
haftmann@31807
   156
lemma size_fset [code]:
haftmann@31807
   157
  "fset_size f A = 0"
haftmann@31807
   158
  "size A = 0"
haftmann@31807
   159
  by (cases A, simp) (cases A, simp)
haftmann@31807
   160
haftmann@31807
   161
lemma fset_case_code [code]:
haftmann@31807
   162
  "fset_case f A = f (member A)"
haftmann@31807
   163
  by (cases A) simp
haftmann@31807
   164
haftmann@31807
   165
lemma fset_rec_code [code]:
haftmann@31807
   166
  "fset_rec f A = f (member A)"
haftmann@31807
   167
  by (cases A) simp
haftmann@31807
   168
haftmann@31807
   169
end