src/HOL/Library/More_Set.thy
author haftmann
Mon, 26 Dec 2011 18:32:43 +0100
changeset 45986 c9e50153e5ae
parent 45974 2b043ed911ac
permissions -rw-r--r--
moved various set operations to theory Set (resp. Product_Type)
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
37024
e938a0b5286e renamed List_Set to the now more appropriate More_Set
haftmann
parents: 37023
diff changeset
     6
theory More_Set
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
     7
imports Main More_List
31807
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
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42868
diff changeset
    10
lemma comp_fun_idem_remove:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    11
  "comp_fun_idem Set.remove"
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    12
proof -
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    13
  have rem: "Set.remove = (\<lambda>x A. A - {x})" by (simp add: fun_eq_iff remove_def)
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42868
diff changeset
    14
  show ?thesis by (simp only: comp_fun_idem_remove rem)
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    15
qed
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    16
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    17
lemma minus_fold_remove:
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    18
  assumes "finite A"
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    19
  shows "B - A = Finite_Set.fold Set.remove B A"
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    20
proof -
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    21
  have rem: "Set.remove = (\<lambda>x A. A - {x})" by (simp add: fun_eq_iff remove_def)
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    22
  show ?thesis by (simp only: rem assms minus_fold_remove)
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
45012
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
    25
lemma bounded_Collect_code [code_unfold_post]:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    26
  "{x \<in> A. P x} = Set.project P A"
45012
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
    27
  by (simp add: project_def)
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
    28
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    29
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    30
subsection {* Basic set operations *}
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    31
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    32
lemma is_empty_set:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    33
  "Set.is_empty (set xs) \<longleftrightarrow> List.null xs"
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    34
  by (simp add: Set.is_empty_def null_def)
31807
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
lemma empty_set:
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    37
  "{} = set []"
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    38
  by simp
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    39
32880
b8bee63c7202 sets and cosets
haftmann
parents: 31851
diff changeset
    40
lemma insert_set_compl:
34977
27ceb64d41ea dropped some redundancies
haftmann
parents: 34007
diff changeset
    41
  "insert x (- set xs) = - set (removeAll x xs)"
27ceb64d41ea dropped some redundancies
haftmann
parents: 34007
diff changeset
    42
  by auto
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    43
32880
b8bee63c7202 sets and cosets
haftmann
parents: 31851
diff changeset
    44
lemma remove_set_compl:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    45
  "Set.remove x (- set xs) = - set (List.insert x xs)"
44326
2b088d74beb3 tuned proof
haftmann
parents: 42871
diff changeset
    46
  by (auto simp add: remove_def List.insert_def)
32880
b8bee63c7202 sets and cosets
haftmann
parents: 31851
diff changeset
    47
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    48
lemma image_set:
31846
89c37daebfdd added Inter, Union
haftmann
parents: 31807
diff changeset
    49
  "image f (set xs) = set (map f xs)"
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    50
  by simp
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    51
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    52
lemma project_set:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    53
  "Set.project P (set xs) = set (filter P xs)"
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    54
  by (auto simp add: project_def)
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    55
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    56
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    57
subsection {* Functorial set operations *}
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    58
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    59
lemma union_set:
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    60
  "set xs \<union> A = fold Set.insert xs A"
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    61
proof -
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42868
diff changeset
    62
  interpret comp_fun_idem Set.insert
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42868
diff changeset
    63
    by (fact comp_fun_idem_insert)
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    64
  show ?thesis by (simp add: union_fold_insert fold_set)
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    65
qed
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    66
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    67
lemma union_set_foldr:
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    68
  "set xs \<union> A = foldr Set.insert xs A"
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    69
proof -
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    70
  have "\<And>x y :: 'a. insert y \<circ> insert x = insert x \<circ> insert y"
45012
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
    71
    by auto
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    72
  then show ?thesis by (simp add: union_set foldr_fold)
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    73
qed
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    74
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    75
lemma minus_set:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    76
  "A - set xs = fold Set.remove xs A"
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    77
proof -
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    78
  interpret comp_fun_idem Set.remove
42871
1c0b99f950d9 names of fold_set locales resemble name of characteristic property more closely
haftmann
parents: 42868
diff changeset
    79
    by (fact comp_fun_idem_remove)
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    80
  show ?thesis
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    81
    by (simp add: minus_fold_remove [of _ A] fold_set)
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    82
qed
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    83
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    84
lemma minus_set_foldr:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    85
  "A - set xs = foldr Set.remove xs A"
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    86
proof -
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
    87
  have "\<And>x y :: 'a. Set.remove y \<circ> Set.remove x = Set.remove x \<circ> Set.remove y"
45012
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
    88
    by (auto simp add: remove_def)
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    89
  then show ?thesis by (simp add: minus_set foldr_fold)
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    90
qed
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
    91
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    92
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    93
subsection {* Derived set operations *}
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    94
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    95
lemma member:
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    96
  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. a = x)"
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    97
  by simp
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    98
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
    99
lemma subset_eq:
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   100
  "A \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   101
  by (fact subset_eq)
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   102
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   103
lemma subset:
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   104
  "A \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> \<not> B \<subseteq> A"
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   105
  by (fact less_le_not_le)
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   106
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   107
lemma set_eq:
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   108
  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   109
  by (fact eq_iff)
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   110
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   111
lemma inter:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
   112
  "A \<inter> B = Set.project (\<lambda>x. x \<in> A) B"
31807
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   113
  by (auto simp add: project_def)
039893a9a77d added List_Set and Code_Set theories
haftmann
parents:
diff changeset
   114
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
   115
45012
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   116
subsection {* Theorems on relations *}
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   117
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   118
lemma product_code:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
   119
  "Product_Type.product (set xs) (set ys) = set [(x, y). x \<leftarrow> xs, y \<leftarrow> ys]"
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
   120
  by (auto simp add: Product_Type.product_def)
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
   121
45012
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   122
lemma Id_on_set:
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   123
  "Id_on (set xs) = set [(x, x). x \<leftarrow> xs]"
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   124
  by (auto simp add: Id_on_def)
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   125
45974
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   126
lemma trancl_set_ntrancl: "trancl (set xs) = ntrancl (card (set xs) - 1) (set xs)"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   127
  by (simp add: finite_trancl_ntranl)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   128
45012
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   129
lemma set_rel_comp:
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   130
  "set xys O set yzs = set ([(fst xy, snd yz). xy \<leftarrow> xys, yz \<leftarrow> yzs, snd xy = fst yz])"
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   131
  by (auto simp add: Bex_def)
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   132
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   133
lemma wf_set:
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   134
  "wf (set xs) = acyclic (set xs)"
060f76635bfe tuned specification and lemma distribution among theories; tuned proofs
haftmann
parents: 44326
diff changeset
   135
  by (simp add: wf_iff_acyclic_if_finite)
37023
efc202e1677e added theory More_List
haftmann
parents: 34977
diff changeset
   136
45974
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   137
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   138
subsection {* Code generator setup *}
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   139
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   140
definition coset :: "'a list \<Rightarrow> 'a set" where
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   141
  [simp]: "coset xs = - set xs"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   142
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   143
code_datatype set coset
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   144
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   145
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   146
subsection {* Basic operations *}
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   147
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   148
lemma [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   149
  "x \<in> set xs \<longleftrightarrow> List.member xs x"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   150
  "x \<in> coset xs \<longleftrightarrow> \<not> List.member xs x"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   151
  by (simp_all add: member_def)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   152
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   153
lemma [code_unfold]:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
   154
  "A = {} \<longleftrightarrow> Set.is_empty A"
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
   155
  by (simp add: Set.is_empty_def)
45974
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   156
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   157
declare empty_set [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   158
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   159
declare is_empty_set [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   160
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   161
lemma UNIV_coset [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   162
  "UNIV = coset []"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   163
  by simp
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   164
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   165
lemma insert_code [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   166
  "insert x (set xs) = set (List.insert x xs)"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   167
  "insert x (coset xs) = coset (removeAll x xs)"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   168
  by simp_all
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   169
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   170
lemma remove_code [code]:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
   171
  "Set.remove x (set xs) = set (removeAll x xs)"
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
   172
  "Set.remove x (coset xs) = coset (List.insert x xs)"
45974
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   173
  by (simp_all add: remove_def Compl_insert)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   174
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   175
declare image_set [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   176
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   177
declare project_set [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   178
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   179
lemma Ball_set [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   180
  "Ball (set xs) P \<longleftrightarrow> list_all P xs"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   181
  by (simp add: list_all_iff)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   182
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   183
lemma Bex_set [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   184
  "Bex (set xs) P \<longleftrightarrow> list_ex P xs"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   185
  by (simp add: list_ex_iff)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   186
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   187
lemma card_set [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   188
  "card (set xs) = length (remdups xs)"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   189
proof -
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   190
  have "card (set (remdups xs)) = length (remdups xs)"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   191
    by (rule distinct_card) simp
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   192
  then show ?thesis by simp
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   193
qed
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   194
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   195
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   196
subsection {* Derived operations *}
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   197
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   198
declare subset_eq [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   199
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   200
declare subset [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   201
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   202
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   203
subsection {* Functorial operations *}
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   204
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   205
lemma inter_code [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   206
  "A \<inter> set xs = set (List.filter (\<lambda>x. x \<in> A) xs)"
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
   207
  "A \<inter> coset xs = foldr Set.remove xs A"
45974
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   208
  by (simp add: inter project_def) (simp add: Diff_eq [symmetric] minus_set_foldr)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   209
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   210
lemma subtract_code [code]:
45986
c9e50153e5ae moved various set operations to theory Set (resp. Product_Type)
haftmann
parents: 45974
diff changeset
   211
  "A - set xs = foldr Set.remove xs A"
45974
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   212
  "A - coset xs = set (List.filter (\<lambda>x. x \<in> A) xs)"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   213
  by (auto simp add: minus_set_foldr)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   214
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   215
lemma union_code [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   216
  "set xs \<union> A = foldr insert xs A"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   217
  "coset xs \<union> A = coset (List.filter (\<lambda>x. x \<notin> A) xs)"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   218
  by (auto simp add: union_set_foldr)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   219
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   220
definition Inf :: "'a::complete_lattice set \<Rightarrow> 'a" where
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   221
  [simp]: "Inf = Complete_Lattices.Inf"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   222
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   223
hide_const (open) Inf
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   224
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   225
lemma [code_unfold_post]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   226
  "Inf = More_Set.Inf"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   227
  by simp
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   228
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   229
definition Sup :: "'a::complete_lattice set \<Rightarrow> 'a" where
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   230
  [simp]: "Sup = Complete_Lattices.Sup"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   231
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   232
hide_const (open) Sup
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   233
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   234
lemma [code_unfold_post]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   235
  "Sup = More_Set.Sup"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   236
  by simp
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   237
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   238
lemma Inf_code [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   239
  "More_Set.Inf (set xs) = foldr inf xs top"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   240
  "More_Set.Inf (coset []) = bot"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   241
  by (simp_all add: Inf_set_foldr)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   242
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   243
lemma Sup_sup [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   244
  "More_Set.Sup (set xs) = foldr sup xs bot"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   245
  "More_Set.Sup (coset []) = top"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   246
  by (simp_all add: Sup_set_foldr)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   247
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   248
lemma INF_code [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   249
  "INFI (set xs) f = foldr (inf \<circ> f) xs top"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   250
  by (simp add: INF_def Inf_set_foldr image_set foldr_map del: set_map)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   251
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   252
lemma SUP_sup [code]:
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   253
  "SUPR (set xs) f = foldr (sup \<circ> f) xs bot"
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   254
  by (simp add: SUP_def Sup_set_foldr image_set foldr_map del: set_map)
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   255
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   256
hide_const (open) coset
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   257
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   258
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   259
subsection {* Operations on relations *}
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   260
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   261
text {* Initially contributed by Tjark Weber. *}
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   262
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   263
declare Domain_fst [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   264
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   265
declare Range_snd [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   266
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   267
declare trans_join [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   268
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   269
declare irrefl_distinct [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   270
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   271
declare trancl_set_ntrancl [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   272
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   273
declare acyclic_irrefl [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   274
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   275
declare product_code [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   276
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   277
declare Id_on_set [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   278
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   279
declare set_rel_comp [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   280
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   281
declare wf_set [code]
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   282
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   283
end
2b043ed911ac added setup for executable code
haftmann
parents: 45012
diff changeset
   284