src/HOL/Library/Executable_Set.thy
author haftmann
Mon Jun 29 12:18:56 2009 +0200 (2009-06-29)
changeset 31850 e81d0f04ffdf
parent 31847 7de0e20ca24d
child 31998 2c7a24f74db9
permissions -rw-r--r--
Executable_Set is now a simple wrapper around Fset
     1 (*  Title:      HOL/Library/Executable_Set.thy
     2     Author:     Stefan Berghofer, TU Muenchen
     3 *)
     4 
     5 header {* Implementation of finite sets by lists *}
     6 
     7 theory Executable_Set
     8 imports Main Fset
     9 begin
    10 
    11 subsection {* Derived set operations *}
    12 
    13 declare member [code] 
    14 
    15 definition subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
    16   "subset = op \<le>"
    17 
    18 declare subset_def [symmetric, code unfold]
    19 
    20 lemma [code]:
    21   "subset A B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
    22   by (simp add: subset_def subset_eq)
    23 
    24 definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
    25   [code del]: "eq_set = op ="
    26 
    27 (* FIXME allow for Stefan's code generator:
    28 declare set_eq_subset[code unfold]
    29 *)
    30 
    31 lemma [code]:
    32   "eq_set A B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
    33   by (simp add: eq_set_def set_eq)
    34 
    35 declare inter [code]
    36 
    37 declare Inter_image_eq [symmetric, code]
    38 declare Union_image_eq [symmetric, code]
    39 
    40 
    41 subsection {* Rewrites for primitive operations *}
    42 
    43 declare List_Set.project_def [symmetric, code unfold]
    44 
    45 
    46 subsection {* code generator setup *}
    47 
    48 ML {*
    49 nonfix inter;
    50 nonfix union;
    51 nonfix subset;
    52 *}
    53 
    54 definition flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c" where
    55   "flip f a b = f b a"
    56 
    57 types_code
    58   fset ("(_/ \<module>fset)")
    59 attach {*
    60 datatype 'a fset = Set of 'a list;
    61 *}
    62 
    63 consts_code
    64   Set ("\<module>Set")
    65 
    66 consts_code
    67   "Set.empty"         ("{*Fset.empty*}")
    68   "List_Set.is_empty" ("{*Fset.is_empty*}")
    69   "Set.insert"        ("{*Fset.insert*}")
    70   "List_Set.remove"   ("{*Fset.remove*}")
    71   "Set.image"         ("{*Fset.map*}")
    72   "List_Set.project"  ("{*Fset.filter*}")
    73   "Ball"              ("{*flip Fset.forall*}")
    74   "Bex"               ("{*flip Fset.exists*}")
    75   "op \<union>"              ("{*Fset.union*}")
    76   "op \<inter>"              ("{*Fset.inter*}")
    77   "op - \<Colon> 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" ("{*flip Fset.subtract*}")
    78   "Set.Union"         ("{*Fset.Union*}")
    79   "Set.Inter"         ("{*Fset.Inter*}")
    80   card                ("{*Fset.card*}")
    81   fold                ("{*foldl o flip*}")
    82 
    83 end