src/HOL/Library/ExecutableSet.thy
author haftmann
Tue, 07 Feb 2006 08:47:43 +0100
changeset 18963 3adfc9dfb30a
parent 18851 9502ce541f01
child 19041 1a8f08f9f8af
permissions -rw-r--r--
slight improvements in code generation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17632
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Library/ExecutableSet.thy
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
     2
    ID:         $Id$
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
     4
*)
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
     5
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
     6
header {* Implementation of finite sets by lists *}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
     7
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
     8
theory ExecutableSet
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
     9
imports Main
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    10
begin
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    11
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    12
lemma [code target: Set]: "(A = B) = (A \<subseteq> B \<and> B \<subseteq> A)"
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    13
  by blast
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    14
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    15
declare bex_triv_one_point1 [symmetric, standard, code]
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    16
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    17
types_code
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    18
  set ("_ list")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    19
attach (term_of) {*
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    20
fun term_of_set f T [] = Const ("{}", Type ("set", [T]))
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    21
  | term_of_set f T (x :: xs) = Const ("insert",
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    22
      T --> Type ("set", [T]) --> Type ("set", [T])) $ f x $ term_of_set f T xs;
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    23
*}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    24
attach (test) {*
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    25
fun gen_set' aG i j = frequency
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    26
  [(i, fn () => aG j :: gen_set' aG (i-1) j), (1, fn () => [])] ()
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    27
and gen_set aG i = gen_set' aG i i;
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    28
*}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    29
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
    30
code_syntax_tyco set
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
    31
  ml ("_ list")
18757
f0d901bc0686 removed problematic keyword 'atom'
haftmann
parents: 18702
diff changeset
    32
  haskell (target_atom "[_]")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
    33
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
    34
code_syntax_const "{}"
18757
f0d901bc0686 removed problematic keyword 'atom'
haftmann
parents: 18702
diff changeset
    35
  ml (target_atom "[]")
f0d901bc0686 removed problematic keyword 'atom'
haftmann
parents: 18702
diff changeset
    36
  haskell (target_atom "[]")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
    37
17632
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    38
consts_code
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    39
  "{}"      ("[]")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    40
  "insert"  ("(_ ins _)")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    41
  "op Un"   ("(_ union _)")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    42
  "op Int"  ("(_ inter _)")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    43
  "op -" :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" ("(_ \\\\ _)")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    44
  "image"   ("\<module>image")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    45
attach {*
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    46
fun image f S = distinct (map f S);
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    47
*}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    48
  "UNION"   ("\<module>UNION")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    49
attach {*
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    50
fun UNION S f = Library.foldr Library.union (map f S, []);
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    51
*}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    52
  "INTER"   ("\<module>INTER")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    53
attach {*
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    54
fun INTER S f = Library.foldr1 Library.inter (map f S);
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    55
*}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    56
  "Bex"     ("\<module>Bex")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    57
attach {*
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    58
fun Bex S P = Library.exists P S;
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    59
*}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    60
  "Ball"     ("\<module>Ball")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    61
attach {*
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    62
fun Ball S P = Library.forall P S;
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    63
*}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    64
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    65
consts
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    66
  flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    67
  member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    68
  insert_ :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    69
  remove :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    70
  inter :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
    71
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    72
defs
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    73
  flip_def: "flip f a b == f b a"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    74
  member_def: "member xs x == x mem xs"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    75
  insert_def: "insert_ x xs == if member xs x then xs else x#xs"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
    76
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    77
primrec
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    78
  "remove x [] = []"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    79
  "remove x (y#ys) = (if x = y then ys else y # remove x ys)"
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
    80
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    81
primrec
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    82
  "inter [] ys = []"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    83
  "inter (x#xs) ys = (let xs' = inter xs ys in if member ys x then x#xs' else xs')"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    84
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    85
code_syntax_const "insert"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    86
  ml ("{*insert_*} _ _")
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    87
  haskell ("{*insert_*} _ _")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
    88
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    89
code_syntax_const "op Un"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    90
  ml ("{*foldr insert_*} _ _")
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    91
  haskell ("{*foldr insert_*} _ _")
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    92
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    93
code_syntax_const "op -" :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    94
  ml ("{*(flip o foldr) remove*} _ _")
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    95
  haskell ("{*(flip o foldr) remove*} _ _")
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    96
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    97
code_syntax_const "op Int"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    98
  ml ("{*inter*} _ _")
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
    99
  haskell ("{*inter*} _ _")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   100
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   101
code_syntax_const "image"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   102
  ml ("{*\<lambda>f. foldr (insert_ o f)*} _ _ _")
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   103
  haskell ("{*\<lambda>f. foldr (insert_ o f)*} _ _ _")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   104
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   105
code_syntax_const "INTER"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   106
  ml ("{*\<lambda>xs f.  foldr (inter o f) xs*} _ _")
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   107
  haskell ("{*\<lambda>xs f.  foldr (inter o f) xs*} _ _")
18851
9502ce541f01 adaptions to codegen_package
haftmann
parents: 18772
diff changeset
   108
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   109
code_syntax_const "UNION"
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   110
  ml ("{*\<lambda>xs f.  foldr (foldr insert_ o f) xs*} _ _")
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   111
  haskell ("{*\<lambda>xs f.  foldr (foldr insert_ o f) xs*} _ _")
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   112
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   113
code_primconst "Ball"
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   114
ml {*
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   115
fun `_` [] f = true
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   116
  | `_` (x::xs) f = f x andalso `_` xs f;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   117
*}
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   118
haskell {*
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   119
`_` = flip all
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   120
*}
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   121
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   122
code_primconst "Bex"
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   123
ml {*
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   124
fun `_` [] f = false
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   125
  | `_` (x::xs) f = f x orelse `_` xs f;
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   126
*}
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   127
haskell {*
18963
3adfc9dfb30a slight improvements in code generation
haftmann
parents: 18851
diff changeset
   128
`_` = flip any
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   129
*}
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   130
17632
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   131
end