src/HOL/Library/Executable_Set.thy
author wenzelm
Mon, 17 Mar 2008 22:34:26 +0100
changeset 26312 e9a65675e5e8
parent 25885 6fbc3f54f819
child 26816 e82229ee8f43
permissions -rw-r--r--
avoid rebinding of existing facts;
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
    ID:         $Id$
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     4
*)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     5
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     6
header {* Implementation of finite sets by lists *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     7
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
     8
theory Executable_Set
25595
6c48275f9c76 switched import from Main to List
haftmann
parents: 24423
diff changeset
     9
imports List
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    10
begin
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    11
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    12
subsection {* Definitional rewrites *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    13
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    14
lemma [code target: Set]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    15
  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    16
  by blast
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    17
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    18
lemma [code]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    19
  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. x = a)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    20
  unfolding bex_triv_one_point1 ..
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    21
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    22
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    23
  filter_set :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    24
  "filter_set P xs = {x\<in>xs. P x}"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    25
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    26
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    27
subsection {* Operations on lists *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    28
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    29
subsubsection {* Basic definitions *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    30
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    31
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    32
  flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    33
  "flip f a b = f b a"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    34
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    35
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    36
  member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    37
  "member xs x \<longleftrightarrow> x \<in> set xs"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    38
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    39
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    40
  insertl :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    41
  "insertl x xs = (if member xs x then xs else x#xs)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    42
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    43
lemma [code target: List]: "member [] y \<longleftrightarrow> False"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    44
  and [code target: List]: "member (x#xs) y \<longleftrightarrow> y = x \<or> member xs y"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    45
  unfolding member_def by (induct xs) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    46
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    47
fun
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    48
  drop_first :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    49
  "drop_first f [] = []"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    50
| "drop_first f (x#xs) = (if f x then xs else x # drop_first f xs)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    51
declare drop_first.simps [code del]
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    52
declare drop_first.simps [code target: List]
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    53
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    54
declare remove1.simps [code del]
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    55
lemma [code target: List]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    56
  "remove1 x xs = (if member xs x then drop_first (\<lambda>y. y = x) xs else xs)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    57
proof (cases "member xs x")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    58
  case False thus ?thesis unfolding member_def by (induct xs) auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    59
next
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    60
  case True
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    61
  have "remove1 x xs = drop_first (\<lambda>y. y = x) xs" by (induct xs) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    62
  with True show ?thesis by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    63
qed
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    64
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    65
lemma member_nil [simp]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    66
  "member [] = (\<lambda>x. False)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    67
proof
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    68
  fix x
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    69
  show "member [] x = False" unfolding member_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    70
qed
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    71
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    72
lemma member_insertl [simp]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    73
  "x \<in> set (insertl x xs)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    74
  unfolding insertl_def member_def mem_iff by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    75
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    76
lemma insertl_member [simp]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    77
  fixes xs x
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    78
  assumes member: "member xs x"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    79
  shows "insertl x xs = xs"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    80
  using member unfolding insertl_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    81
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    82
lemma insertl_not_member [simp]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    83
  fixes xs x
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    84
  assumes member: "\<not> (member xs x)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    85
  shows "insertl x xs = x # xs"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    86
  using member unfolding insertl_def by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    87
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    88
lemma foldr_remove1_empty [simp]:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    89
  "foldr remove1 xs [] = []"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    90
  by (induct xs) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    91
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    92
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    93
subsubsection {* Derived definitions *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    94
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    95
function unionl :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    96
where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    97
  "unionl [] ys = ys"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    98
| "unionl xs ys = foldr insertl xs ys"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
    99
by pat_completeness auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   100
termination by lexicographic_order
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   101
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   102
lemmas unionl_eq = unionl.simps(2)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   103
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   104
function intersect :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   105
where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   106
  "intersect [] ys = []"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   107
| "intersect xs [] = []"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   108
| "intersect xs ys = filter (member xs) ys"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   109
by pat_completeness auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   110
termination by lexicographic_order
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   111
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   112
lemmas intersect_eq = intersect.simps(3)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   113
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   114
function subtract :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   115
where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   116
  "subtract [] ys = ys"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   117
| "subtract xs [] = []"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   118
| "subtract xs ys = foldr remove1 xs ys"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   119
by pat_completeness auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   120
termination by lexicographic_order
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   121
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   122
lemmas subtract_eq = subtract.simps(3)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   123
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   124
function map_distinct :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   125
where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   126
  "map_distinct f [] = []"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   127
| "map_distinct f xs = foldr (insertl o f) xs []"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   128
by pat_completeness auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   129
termination by lexicographic_order
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   130
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   131
lemmas map_distinct_eq = map_distinct.simps(2)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   132
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   133
function unions :: "'a list list \<Rightarrow> 'a list"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   134
where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   135
  "unions [] = []"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   136
| "unions xs = foldr unionl xs []"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   137
by pat_completeness auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   138
termination by lexicographic_order
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   139
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   140
lemmas unions_eq = unions.simps(2)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   141
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   142
consts intersects :: "'a list list \<Rightarrow> 'a list"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   143
primrec
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   144
  "intersects (x#xs) = foldr intersect xs x"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   145
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   146
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   147
  map_union :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   148
  "map_union xs f = unions (map f xs)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   149
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   150
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   151
  map_inter :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   152
  "map_inter xs f = intersects (map f xs)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   153
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   154
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   155
subsection {* Isomorphism proofs *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   156
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   157
lemma iso_member:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   158
  "member xs x \<longleftrightarrow> x \<in> set xs"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   159
  unfolding member_def mem_iff ..
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   160
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   161
lemma iso_insert:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   162
  "set (insertl x xs) = insert x (set xs)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   163
  unfolding insertl_def iso_member by (simp add: Set.insert_absorb)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   164
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   165
lemma iso_remove1:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   166
  assumes distnct: "distinct xs"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   167
  shows "set (remove1 x xs) = set xs - {x}"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   168
  using distnct set_remove1_eq by auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   169
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   170
lemma iso_union:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   171
  "set (unionl xs ys) = set xs \<union> set ys"
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   172
  unfolding unionl_eq
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   173
  by (induct xs arbitrary: ys) (simp_all add: iso_insert)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   174
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   175
lemma iso_intersect:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   176
  "set (intersect xs ys) = set xs \<inter> set ys"
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   177
  unfolding intersect_eq Int_def by (simp add: Int_def iso_member) auto
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   178
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   179
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   180
  subtract' :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   181
  "subtract' = flip subtract"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   182
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   183
lemma iso_subtract:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   184
  fixes ys
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   185
  assumes distnct: "distinct ys"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   186
  shows "set (subtract' ys xs) = set ys - set xs"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   187
    and "distinct (subtract' ys xs)"
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   188
  unfolding subtract'_def flip_def subtract_eq
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   189
  using distnct by (induct xs arbitrary: ys) auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   190
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   191
lemma iso_map_distinct:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   192
  "set (map_distinct f xs) = image f (set xs)"
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   193
  unfolding map_distinct_eq by (induct xs) (simp_all add: iso_insert)
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   194
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   195
lemma iso_unions:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   196
  "set (unions xss) = \<Union> set (map set xss)"
26312
e9a65675e5e8 avoid rebinding of existing facts;
wenzelm
parents: 25885
diff changeset
   197
  unfolding unions_eq
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   198
proof (induct xss)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   199
  case Nil show ?case by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   200
next
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   201
  case (Cons xs xss) thus ?case by (induct xs) (simp_all add: iso_insert)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   202
qed
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   203
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   204
lemma iso_intersects:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   205
  "set (intersects (xs#xss)) = \<Inter> set (map set (xs#xss))"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   206
  by (induct xss) (simp_all add: Int_def iso_member, auto)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   207
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   208
lemma iso_UNION:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   209
  "set (map_union xs f) = UNION (set xs) (set o f)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   210
  unfolding map_union_def iso_unions by simp
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   211
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   212
lemma iso_INTER:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   213
  "set (map_inter (x#xs) f) = INTER (set (x#xs)) (set o f)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   214
  unfolding map_inter_def iso_intersects by (induct xs) (simp_all add: iso_member, auto)
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   215
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   216
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   217
  Blall :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   218
  "Blall = flip list_all"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   219
definition
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   220
  Blex :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   221
  "Blex = flip list_ex"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   222
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   223
lemma iso_Ball:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   224
  "Blall xs f = Ball (set xs) f"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   225
  unfolding Blall_def flip_def by (induct xs) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   226
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   227
lemma iso_Bex:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   228
  "Blex xs f = Bex (set xs) f"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   229
  unfolding Blex_def flip_def by (induct xs) simp_all
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   230
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   231
lemma iso_filter:
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   232
  "set (filter P xs) = filter_set P (set xs)"
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   233
  unfolding filter_set_def by (induct xs) auto
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   234
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   235
subsection {* code generator setup *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   236
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   237
ML {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   238
nonfix inter;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   239
nonfix union;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   240
nonfix subset;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   241
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   242
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   243
subsubsection {* type serializations *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   244
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   245
types_code
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   246
  set ("_ list")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   247
attach (term_of) {*
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   248
fun term_of_set f T [] = Const ("{}", Type ("set", [T]))
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   249
  | term_of_set f T (x :: xs) = Const ("insert",
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   250
      T --> Type ("set", [T]) --> Type ("set", [T])) $ f x $ term_of_set f T xs;
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   251
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   252
attach (test) {*
25885
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   253
fun gen_set' aG aT i j = frequency
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   254
  [(i, fn () =>
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   255
      let
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   256
        val (x, t) = aG j;
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   257
        val (xs, ts) = gen_set' aG aT (i-1) j
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   258
      in
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   259
        (x :: xs, fn () => Const ("insert",
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   260
           aT --> Type ("set", [aT]) --> Type ("set", [aT])) $ t () $ ts ())
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   261
      end),
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   262
   (1, fn () => ([], fn () => Const ("{}", Type ("set", [aT]))))] ()
6fbc3f54f819 New interface for test data generators.
berghofe
parents: 25595
diff changeset
   263
and gen_set aG aT i = gen_set' aG aT i i;
23854
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   264
*}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   265
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   266
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   267
subsubsection {* const serializations *}
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   268
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   269
consts_code
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   270
  "{}" ("{*[]*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   271
  insert ("{*insertl*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   272
  "op \<union>" ("{*unionl*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   273
  "op \<inter>" ("{*intersect*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   274
  "op - \<Colon> 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" ("{* flip subtract *}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   275
  image ("{*map_distinct*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   276
  Union ("{*unions*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   277
  Inter ("{*intersects*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   278
  UNION ("{*map_union*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   279
  INTER ("{*map_inter*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   280
  Ball ("{*Blall*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   281
  Bex ("{*Blex*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   282
  filter_set ("{*filter*}")
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   283
688a8a7bcd4e uniform naming conventions for CG theories
haftmann
parents:
diff changeset
   284
end