src/HOL/Library/Executable_Set.thy
author haftmann
Sun, 28 Jun 2009 11:02:27 +0200
changeset 31847 7de0e20ca24d
parent 30664 167da873c3b3
child 31850 e81d0f04ffdf
permissions -rw-r--r--
Executable_Set now based on Code_Set
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Executable_Set.thy
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     3
*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     4
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     5
header {* Implementation of finite sets by lists *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     6
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     7
theory Executable_Set
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
     8
imports Main Code_Set
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     9
begin
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    10
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    11
subsection {* Derived set operations *}
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    12
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    13
declare member [code] 
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    14
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 27487
diff changeset
    15
definition subset :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 27487
diff changeset
    16
  "subset = op \<le>"
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 27487
diff changeset
    17
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 27487
diff changeset
    18
declare subset_def [symmetric, code unfold]
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 27487
diff changeset
    19
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    20
lemma [code]:
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    21
  "subset A B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    22
  by (simp add: subset_def subset_eq)
28522
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 27487
diff changeset
    23
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 27487
diff changeset
    24
definition eq_set :: "'a set \<Rightarrow> 'a set \<Rightarrow> bool" where
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 27487
diff changeset
    25
  [code del]: "eq_set = op ="
eacb54d9e78d only one theorem table for both code generators
haftmann
parents: 27487
diff changeset
    26
29107
e70b9c2bee14 code for {x:A. P(x)} and for fold
nipkow
parents: 28939
diff changeset
    27
(* FIXME allow for Stefan's code generator:
e70b9c2bee14 code for {x:A. P(x)} and for fold
nipkow
parents: 28939
diff changeset
    28
declare set_eq_subset[code unfold]
e70b9c2bee14 code for {x:A. P(x)} and for fold
nipkow
parents: 28939
diff changeset
    29
*)
e70b9c2bee14 code for {x:A. P(x)} and for fold
nipkow
parents: 28939
diff changeset
    30
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    31
lemma [code]:
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    32
  "eq_set A B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    33
  by (simp add: eq_set_def set_eq)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    34
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    35
declare inter [code]
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    36
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    37
declare Inter_image_eq [symmetric, code]
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    38
declare Union_image_eq [symmetric, code]
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    39
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    40
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    41
subsection {* Rewrites for primitive operations *}
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    42
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    43
declare List_Set.project_def [symmetric, code unfold]
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    44
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    45
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    46
subsection {* code generator setup *}
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    47
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    48
text {* eliminate popular infixes *}
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    49
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    50
ML {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    51
nonfix inter;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    52
nonfix union;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    53
nonfix subset;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    54
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    55
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    56
text {* type @{typ "'a fset"} *}
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    57
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    58
types_code
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    59
  fset ("(_/ list)")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    60
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    61
consts_code
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    62
  Set ("_")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    63
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    64
text {* primitive operations *}
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    65
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    66
definition flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c" where
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    67
  "flip f a b = f b a"
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    68
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    69
consts_code
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    70
  "Set.empty"         ("{*Code_Set.empty*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    71
  "List_Set.is_empty" ("{*Code_Set.is_empty*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    72
  "Set.insert"        ("{*Code_Set.insert*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    73
  "List_Set.remove"   ("{*Code_Set.remove*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    74
  "Set.image"         ("{*Code_Set.map*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    75
  "List_Set.project"  ("{*Code_Set.filter*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    76
  "Ball"              ("{*flip Code_Set.forall*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    77
  "Bex"               ("{*flip Code_Set.exists*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    78
  "op \<union>"              ("{*Code_Set.union*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    79
  "op \<inter>"              ("{*Code_Set.inter*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    80
  "op - \<Colon> 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" ("{*flip subtract*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    81
  "Set.Union"         ("{*Code_Set.union*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    82
  "Set.Inter"         ("{*Code_Set.inter*}")
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    83
  fold                ("{*foldl o flip*}")
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    84
31847
7de0e20ca24d Executable_Set now based on Code_Set
haftmann
parents: 30664
diff changeset
    85
end