src/HOL/Library/Executable_Set.thy
author haftmann
Thu Sep 24 18:29:29 2009 +0200 (2009-09-24)
changeset 32680 faf6924430d9
parent 32647 e54f47f9e28b
child 32705 04ce6bb14d85
permissions -rw-r--r--
subsumed by more general setup in List.thy
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@32587
    11
subsection {* Preprocessor setup *}
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@31998
    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
haftmann@32587
    27
(*declare eq_set_def [symmetric, code_unfold]*)
nipkow@29107
    28
haftmann@23854
    29
lemma [code]:
haftmann@31847
    30
  "eq_set A B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
haftmann@31847
    31
  by (simp add: eq_set_def set_eq)
haftmann@23854
    32
haftmann@31847
    33
declare inter [code]
haftmann@23854
    34
haftmann@31998
    35
declare List_Set.project_def [symmetric, code_unfold]
haftmann@23854
    36
haftmann@32587
    37
definition Inter :: "'a set set \<Rightarrow> 'a set" where
haftmann@32587
    38
  "Inter = Complete_Lattice.Inter"
haftmann@23854
    39
haftmann@32587
    40
declare Inter_def [symmetric, code_unfold]
haftmann@32587
    41
haftmann@32587
    42
definition Union :: "'a set set \<Rightarrow> 'a set" where
haftmann@32587
    43
  "Union = Complete_Lattice.Union"
haftmann@32587
    44
haftmann@32587
    45
declare Union_def [symmetric, code_unfold]
haftmann@32587
    46
haftmann@32587
    47
haftmann@32587
    48
subsection {* Code generator setup *}
haftmann@23854
    49
haftmann@23854
    50
ML {*
haftmann@23854
    51
nonfix inter;
haftmann@23854
    52
nonfix union;
haftmann@23854
    53
nonfix subset;
haftmann@23854
    54
*}
haftmann@23854
    55
haftmann@31847
    56
definition flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c" where
haftmann@31847
    57
  "flip f a b = f b a"
haftmann@23854
    58
haftmann@31850
    59
types_code
haftmann@31850
    60
  fset ("(_/ \<module>fset)")
haftmann@31850
    61
attach {*
haftmann@31850
    62
datatype 'a fset = Set of 'a list;
haftmann@31850
    63
*}
haftmann@31850
    64
haftmann@23854
    65
consts_code
haftmann@31850
    66
  Set ("\<module>Set")
haftmann@31850
    67
haftmann@31850
    68
consts_code
haftmann@31850
    69
  "Set.empty"         ("{*Fset.empty*}")
haftmann@31850
    70
  "List_Set.is_empty" ("{*Fset.is_empty*}")
haftmann@31850
    71
  "Set.insert"        ("{*Fset.insert*}")
haftmann@31850
    72
  "List_Set.remove"   ("{*Fset.remove*}")
haftmann@31850
    73
  "Set.image"         ("{*Fset.map*}")
haftmann@31850
    74
  "List_Set.project"  ("{*Fset.filter*}")
haftmann@31850
    75
  "Ball"              ("{*flip Fset.forall*}")
haftmann@31850
    76
  "Bex"               ("{*flip Fset.exists*}")
haftmann@31850
    77
  "op \<union>"              ("{*Fset.union*}")
haftmann@31850
    78
  "op \<inter>"              ("{*Fset.inter*}")
haftmann@31850
    79
  "op - \<Colon> 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" ("{*flip Fset.subtract*}")
haftmann@32587
    80
  "Union"             ("{*Fset.Union*}")
haftmann@32587
    81
  "Inter"             ("{*Fset.Inter*}")
haftmann@31850
    82
  card                ("{*Fset.card*}")
haftmann@31847
    83
  fold                ("{*foldl o flip*}")
haftmann@23854
    84
haftmann@32647
    85
hide (open) const subset eq_set Inter Union flip
haftmann@32647
    86
haftmann@31847
    87
end