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