src/HOL/Library/ExecutableSet.thy
changeset 21404 eb85850d3eb7
parent 21385 cf398bb8a8be
child 21454 a1937c51ed88
equal deleted inserted replaced
21403:dd58f13a8eb4 21404:eb85850d3eb7
    12 section {* Definitional rewrites *}
    12 section {* Definitional rewrites *}
    13 
    13 
    14 instance set :: (eq) eq ..
    14 instance set :: (eq) eq ..
    15 
    15 
    16 definition
    16 definition
    17   minus_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"
    17   minus_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" where
    18   "minus_set xs ys = ys - xs"
    18   "minus_set xs ys = ys - xs"
    19 
    19 
    20 lemma [code inline]:
    20 lemma [code inline]:
    21   "op - = (\<lambda>xs ys. minus_set ys xs)"
    21   "op - = (\<lambda>xs ys. minus_set ys xs)"
    22   unfolding minus_set_def ..
    22   unfolding minus_set_def ..
    23 
    23 
    24 definition
    24 definition
    25   subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool"
    25   subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
    26   subset_def: "subset = op \<subseteq>"
    26   "subset = op \<subseteq>"
    27 
    27 
    28 lemmas [symmetric, code inline] = subset_def
    28 lemmas [symmetric, code inline] = subset_def
    29 
    29 
    30 lemma [code target: Set]:
    30 lemma [code target: Set]:
    31   "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
    31   "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
    42 lemma [code]:
    42 lemma [code]:
    43   "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. x = a)"
    43   "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. x = a)"
    44   unfolding bex_triv_one_point1 ..
    44   unfolding bex_triv_one_point1 ..
    45 
    45 
    46 definition
    46 definition
    47   filter_set :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set"
    47   filter_set :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
    48   filter_set_def: "filter_set P xs = {x\<in>xs. P x}"
    48   "filter_set P xs = {x\<in>xs. P x}"
    49 
    49 
    50 lemmas [symmetric, code inline] = filter_set_def
    50 lemmas [symmetric, code inline] = filter_set_def
    51 
    51 
    52 
    52 
    53 section {* Operations on lists *}
    53 section {* Operations on lists *}
    54 
    54 
    55 subsection {* Basic definitions *}
    55 subsection {* Basic definitions *}
    56 
    56 
    57 definition
    57 definition
    58   flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c"
    58   flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c" where
    59   "flip f a b = f b a"
    59   "flip f a b = f b a"
    60   member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool"
    60 
       
    61 definition
       
    62   member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where
    61   "member xs x = (x \<in> set xs)"
    63   "member xs x = (x \<in> set xs)"
    62   insertl :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
    64 
       
    65 definition
       
    66   insertl :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
    63   "insertl x xs = (if member xs x then xs else x#xs)"
    67   "insertl x xs = (if member xs x then xs else x#xs)"
    64 
    68 
    65 lemma
    69 lemma
    66   [code target: List]: "member [] y = False"
    70   [code target: List]: "member [] y = False"
    67   and [code target: List]: "member (x#xs) y = (y = x \<or> member xs y)"
    71   and [code target: List]: "member (x#xs) y = (y = x \<or> member xs y)"
   172 consts intersects :: "'a list list \<Rightarrow> 'a list"
   176 consts intersects :: "'a list list \<Rightarrow> 'a list"
   173 primrec
   177 primrec
   174   "intersects (x#xs) = foldr intersect xs x"
   178   "intersects (x#xs) = foldr intersect xs x"
   175 
   179 
   176 definition
   180 definition
   177   map_union :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list"
   181   map_union :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where
   178   "map_union xs f = unions (map f xs)"
   182   "map_union xs f = unions (map f xs)"
   179   map_inter :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list"
   183 
       
   184 definition
       
   185   map_inter :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where
   180   "map_inter xs f = intersects (map f xs)"
   186   "map_inter xs f = intersects (map f xs)"
   181 
   187 
   182 
   188 
   183 section {* Isomorphism proofs *}
   189 section {* Isomorphism proofs *}
   184 
   190 
   235 lemma iso_INTER:
   241 lemma iso_INTER:
   236   "set (map_inter (x#xs) f) = INTER (set (x#xs)) (set o f)"
   242   "set (map_inter (x#xs) f) = INTER (set (x#xs)) (set o f)"
   237   unfolding map_inter_def iso_intersects by (induct xs) (simp_all add: iso_member, auto)
   243   unfolding map_inter_def iso_intersects by (induct xs) (simp_all add: iso_member, auto)
   238 
   244 
   239 definition
   245 definition
   240   Blall :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   246   Blall :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
   241   "Blall = flip list_all"
   247   "Blall = flip list_all"
   242   Blex :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool"
   248 definition
       
   249   Blex :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
   243   "Blex = flip list_ex"
   250   "Blex = flip list_ex"
   244 
   251 
   245 lemma iso_Ball:
   252 lemma iso_Ball:
   246   "Blall xs f = Ball (set xs) f"
   253   "Blall xs f = Ball (set xs) f"
   247   unfolding Blall_def flip_def by (induct xs) simp_all
   254   unfolding Blall_def flip_def by (induct xs) simp_all