src/HOL/Library/ExecutableSet.thy
author haftmann
Sun, 06 May 2007 21:49:23 +0200
changeset 22838 466599ecf610
parent 22744 5cbe966d67a2
child 22921 475ff421a6a3
permissions -rw-r--r--
tuned
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
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22492
diff changeset
    12
subsection {* Definitional rewrites *}
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20523
diff changeset
    13
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20523
diff changeset
    14
instance set :: (eq) eq ..
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    15
21153
8288c8f203de adapted to changes in codegen_data.ML
haftmann
parents: 21125
diff changeset
    16
lemma [code target: Set]:
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
    17
  "A = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
17632
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    18
  by blast
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    19
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20523
diff changeset
    20
lemma [code func]:
21572
7442833ea2b6 added strict_subset
haftmann
parents: 21511
diff changeset
    21
  "(A\<Colon>'a\<Colon>eq set) = B \<longleftrightarrow> A \<subseteq> B \<and> B \<subseteq> A"
7442833ea2b6 added strict_subset
haftmann
parents: 21511
diff changeset
    22
  by blast
7442833ea2b6 added strict_subset
haftmann
parents: 21511
diff changeset
    23
7442833ea2b6 added strict_subset
haftmann
parents: 21511
diff changeset
    24
lemma [code func]:
22177
515021e98684 improved
haftmann
parents: 21911
diff changeset
    25
  "(A\<Colon>'a\<Colon>eq set) \<subseteq> B \<longleftrightarrow> (\<forall>x\<in>A. x \<in> B)"
515021e98684 improved
haftmann
parents: 21911
diff changeset
    26
  unfolding subset_def ..
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20523
diff changeset
    27
21572
7442833ea2b6 added strict_subset
haftmann
parents: 21511
diff changeset
    28
lemma [code func]:
22177
515021e98684 improved
haftmann
parents: 21911
diff changeset
    29
  "(A\<Colon>'a\<Colon>eq set) \<subset> B \<longleftrightarrow> A \<subseteq> B \<and> A \<noteq> B"
515021e98684 improved
haftmann
parents: 21911
diff changeset
    30
  by blast
21572
7442833ea2b6 added strict_subset
haftmann
parents: 21511
diff changeset
    31
21323
haftmann
parents: 21321
diff changeset
    32
lemma [code]:
haftmann
parents: 21321
diff changeset
    33
  "a \<in> A \<longleftrightarrow> (\<exists>x\<in>A. x = a)"
haftmann
parents: 21321
diff changeset
    34
  unfolding bex_triv_one_point1 ..
17632
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
    35
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
    36
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
    37
  filter_set :: "('a \<Rightarrow> bool) \<Rightarrow> 'a set \<Rightarrow> 'a set" where
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
    38
  "filter_set P xs = {x\<in>xs. P x}"
20597
65fe827aa595 code generation 2 adjustments
haftmann
parents: 20523
diff changeset
    39
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
    40
lemmas [symmetric, code inline] = filter_set_def
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
    41
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
    42
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22492
diff changeset
    43
subsection {* Operations on lists *}
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    44
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22492
diff changeset
    45
subsubsection {* Basic definitions *}
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    46
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    47
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
    48
  flip :: "('a \<Rightarrow> 'b \<Rightarrow> 'c) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'c" where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    49
  "flip f a b = f b a"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
    50
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
    51
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
    52
  member :: "'a list \<Rightarrow> 'a \<Rightarrow> bool" where
22350
b410755a0d5a slightly tuned
haftmann
parents: 22177
diff changeset
    53
  "member xs x \<longleftrightarrow> x \<in> set xs"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
    54
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
    55
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
    56
  insertl :: "'a \<Rightarrow> 'a list \<Rightarrow> 'a list" where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    57
  "insertl x xs = (if member xs x then xs else x#xs)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    58
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    59
lemma
22350
b410755a0d5a slightly tuned
haftmann
parents: 22177
diff changeset
    60
  [code target: List]: "member [] y \<longleftrightarrow> False"
b410755a0d5a slightly tuned
haftmann
parents: 22177
diff changeset
    61
  and [code target: List]: "member (x#xs) y \<longleftrightarrow> y = x \<or> member xs y"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    62
unfolding member_def by (induct xs) simp_all
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    63
22177
515021e98684 improved
haftmann
parents: 21911
diff changeset
    64
fun
515021e98684 improved
haftmann
parents: 21911
diff changeset
    65
  drop_first :: "('a \<Rightarrow> bool) \<Rightarrow> 'a list \<Rightarrow> 'a list" where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    66
  "drop_first f [] = []"
22492
43545e640877 Unified function syntax
krauss
parents: 22350
diff changeset
    67
| "drop_first f (x#xs) = (if f x then xs else x # drop_first f xs)"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    68
declare drop_first.simps [code del]
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    69
declare drop_first.simps [code target: List]
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    70
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    71
declare remove1.simps [code del]
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    72
lemma [code target: List]:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    73
  "remove1 x xs = (if member xs x then drop_first (\<lambda>y. y = x) xs else xs)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    74
proof (cases "member xs x")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    75
  case False thus ?thesis unfolding member_def by (induct xs) auto
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    76
next
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    77
  case True
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    78
  have "remove1 x xs = drop_first (\<lambda>y. y = x) xs" by (induct xs) simp_all
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    79
  with True show ?thesis by simp
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    80
qed
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    81
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    82
lemma member_nil [simp]:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    83
  "member [] = (\<lambda>x. False)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    84
proof
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    85
  fix x
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    86
  show "member [] x = False" unfolding member_def by simp
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    87
qed
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    88
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    89
lemma member_insertl [simp]:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    90
  "x \<in> set (insertl x xs)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    91
  unfolding insertl_def member_def mem_iff by simp
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    92
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    93
lemma insertl_member [simp]:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    94
  fixes xs x
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    95
  assumes member: "member xs x"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    96
  shows "insertl x xs = xs"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    97
  using member unfolding insertl_def by simp
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    98
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
    99
lemma insertl_not_member [simp]:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   100
  fixes xs x
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   101
  assumes member: "\<not> (member xs x)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   102
  shows "insertl x xs = x # xs"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   103
  using member unfolding insertl_def by simp
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   104
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   105
lemma foldr_remove1_empty [simp]:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   106
  "foldr remove1 xs [] = []"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   107
  by (induct xs) simp_all
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   108
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   109
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22492
diff changeset
   110
subsubsection {* Derived definitions *}
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   111
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   112
function unionl :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   113
where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   114
  "unionl [] ys = ys"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   115
| "unionl xs ys = foldr insertl xs ys"
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   116
by pat_completeness auto
21321
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   117
termination by lexicographic_order
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   118
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   119
lemmas unionl_def = unionl.simps(2)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   120
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   121
function intersect :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   122
where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   123
  "intersect [] ys = []"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   124
| "intersect xs [] = []"
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   125
| "intersect xs ys = filter (member xs) ys"
21321
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   126
by pat_completeness auto
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   127
termination by lexicographic_order
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   128
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   129
lemmas intersect_def = intersect.simps(3)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   130
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   131
function subtract :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   132
where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   133
  "subtract [] ys = ys"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   134
| "subtract xs [] = []"
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   135
| "subtract xs ys = foldr remove1 xs ys"
21321
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   136
by pat_completeness auto
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   137
termination by lexicographic_order
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   138
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   139
lemmas subtract_def = subtract.simps(3)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   140
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   141
function map_distinct :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a list \<Rightarrow> 'b list"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   142
where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   143
  "map_distinct f [] = []"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   144
| "map_distinct f xs = foldr (insertl o f) xs []"
21321
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   145
by pat_completeness auto
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   146
termination by lexicographic_order
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   147
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   148
lemmas map_distinct_def = map_distinct.simps(2)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   149
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   150
function unions :: "'a list list \<Rightarrow> 'a list"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   151
where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   152
  "unions [] = []"
22492
43545e640877 Unified function syntax
krauss
parents: 22350
diff changeset
   153
| "unions xs = foldr unionl xs []"
21321
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   154
by pat_completeness auto
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   155
termination by lexicographic_order
9022a90f6fdd auto_term => lexicographic_order
krauss
parents: 21191
diff changeset
   156
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   157
lemmas unions_def = unions.simps(2)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   158
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   159
consts intersects :: "'a list list \<Rightarrow> 'a list"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   160
primrec
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   161
  "intersects (x#xs) = foldr intersect xs x"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   162
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   163
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
   164
  map_union :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   165
  "map_union xs f = unions (map f xs)"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
   166
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
   167
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
   168
  map_inter :: "'a list \<Rightarrow> ('a \<Rightarrow> 'b list) \<Rightarrow> 'b list" where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   169
  "map_inter xs f = intersects (map f xs)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   170
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   171
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22492
diff changeset
   172
subsection {* Isomorphism proofs *}
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   173
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   174
lemma iso_member:
22350
b410755a0d5a slightly tuned
haftmann
parents: 22177
diff changeset
   175
  "member xs x \<longleftrightarrow> x \<in> set xs"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   176
  unfolding member_def mem_iff ..
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   177
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   178
lemma iso_insert:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   179
  "set (insertl x xs) = insert x (set xs)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   180
  unfolding insertl_def iso_member by (simp add: Set.insert_absorb)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   181
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   182
lemma iso_remove1:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   183
  assumes distnct: "distinct xs"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   184
  shows "set (remove1 x xs) = set xs - {x}"
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   185
  using distnct set_remove1_eq by auto
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   186
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   187
lemma iso_union:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   188
  "set (unionl xs ys) = set xs \<union> set ys"
20523
36a59e5d0039 Major update to function package, including new syntax and the (only theoretical)
krauss
parents: 20503
diff changeset
   189
  unfolding unionl_def
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   190
  by (induct xs arbitrary: ys) (simp_all add: iso_insert)
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   191
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   192
lemma iso_intersect:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   193
  "set (intersect xs ys) = set xs \<inter> set ys"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   194
  unfolding intersect_def Int_def by (simp add: Int_def iso_member) auto
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   195
22177
515021e98684 improved
haftmann
parents: 21911
diff changeset
   196
definition
515021e98684 improved
haftmann
parents: 21911
diff changeset
   197
  subtract' :: "'a list \<Rightarrow> 'a list \<Rightarrow> 'a list" where
515021e98684 improved
haftmann
parents: 21911
diff changeset
   198
  "subtract' = flip subtract"
515021e98684 improved
haftmann
parents: 21911
diff changeset
   199
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   200
lemma iso_subtract:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   201
  fixes ys
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   202
  assumes distnct: "distinct ys"
22177
515021e98684 improved
haftmann
parents: 21911
diff changeset
   203
  shows "set (subtract' ys xs) = set ys - set xs"
515021e98684 improved
haftmann
parents: 21911
diff changeset
   204
  and "distinct (subtract' ys xs)"
515021e98684 improved
haftmann
parents: 21911
diff changeset
   205
  unfolding subtract'_def flip_def subtract_def
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   206
  using distnct by (induct xs arbitrary: ys) auto
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   207
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   208
lemma iso_map_distinct:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   209
  "set (map_distinct f xs) = image f (set xs)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   210
  unfolding map_distinct_def by (induct xs) (simp_all add: iso_insert)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   211
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   212
lemma iso_unions:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   213
  "set (unions xss) = \<Union> set (map set xss)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   214
unfolding unions_def proof (induct xss)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   215
  case Nil show ?case by simp
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   216
next
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   217
  case (Cons xs xss) thus ?case by (induct xs) (simp_all add: iso_insert)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   218
qed
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   219
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   220
lemma iso_intersects:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   221
  "set (intersects (xs#xss)) = \<Inter> set (map set (xs#xss))"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   222
  by (induct xss) (simp_all add: Int_def iso_member, auto)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   223
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   224
lemma iso_UNION:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   225
  "set (map_union xs f) = UNION (set xs) (set o f)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   226
  unfolding map_union_def iso_unions by simp
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   227
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   228
lemma iso_INTER:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   229
  "set (map_inter (x#xs) f) = INTER (set (x#xs)) (set o f)"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   230
  unfolding map_inter_def iso_intersects by (induct xs) (simp_all add: iso_member, auto)
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   231
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   232
definition
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
   233
  Blall :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   234
  "Blall = flip list_all"
21404
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
   235
definition
eb85850d3eb7 more robust syntax for definition/abbreviation/notation;
wenzelm
parents: 21385
diff changeset
   236
  Blex :: "'a list \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   237
  "Blex = flip list_ex"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   238
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   239
lemma iso_Ball:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   240
  "Blall xs f = Ball (set xs) f"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   241
  unfolding Blall_def flip_def by (induct xs) simp_all
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   242
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   243
lemma iso_Bex:
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   244
  "Blex xs f = Bex (set xs) f"
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   245
  unfolding Blex_def flip_def by (induct xs) simp_all
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   246
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   247
lemma iso_filter:
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   248
  "set (filter P xs) = filter_set P (set xs)"
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   249
  unfolding filter_set_def by (induct xs) auto
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   250
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22492
diff changeset
   251
subsection {* code generator setup *}
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   252
21008
330a8a6dd53c explicit nonfix for union and inter
haftmann
parents: 20934
diff changeset
   253
ML {*
330a8a6dd53c explicit nonfix for union and inter
haftmann
parents: 20934
diff changeset
   254
nonfix inter;
330a8a6dd53c explicit nonfix for union and inter
haftmann
parents: 20934
diff changeset
   255
nonfix union;
21875
5df10a17644e explicit nonfix declaration for ML "subset"
haftmann
parents: 21572
diff changeset
   256
nonfix subset;
21008
330a8a6dd53c explicit nonfix for union and inter
haftmann
parents: 20934
diff changeset
   257
*}
330a8a6dd53c explicit nonfix for union and inter
haftmann
parents: 20934
diff changeset
   258
21191
c00161fbf990 code generator module naming improved
haftmann
parents: 21153
diff changeset
   259
code_modulename SML
c00161fbf990 code generator module naming improved
haftmann
parents: 21153
diff changeset
   260
  ExecutableSet List
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   261
  Set List
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   262
21911
e29bcab0c81c added OCaml code generation (without dictionaries)
haftmann
parents: 21875
diff changeset
   263
code_modulename OCaml
e29bcab0c81c added OCaml code generation (without dictionaries)
haftmann
parents: 21875
diff changeset
   264
  ExecutableSet List
e29bcab0c81c added OCaml code generation (without dictionaries)
haftmann
parents: 21875
diff changeset
   265
  Set List
e29bcab0c81c added OCaml code generation (without dictionaries)
haftmann
parents: 21875
diff changeset
   266
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   267
code_modulename Haskell
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   268
  ExecutableSet List
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   269
  Set List
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   270
21063
3c5074f028c8 slight cleanup
haftmann
parents: 21046
diff changeset
   271
definition [code inline]:
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   272
  "empty_list = []"
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   273
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   274
lemma [code func]:
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   275
  "insert (x \<Colon> 'a\<Colon>eq) = insert x" ..
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   276
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   277
lemma [code func]:
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   278
  "(xs \<Colon> 'a\<Colon>eq set) \<union> ys = xs \<union> ys" ..
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   279
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   280
lemma [code func]:
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   281
  "(xs \<Colon> 'a\<Colon>eq set) \<inter> ys = xs \<inter> ys" ..
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   282
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   283
lemma [code func]:
22177
515021e98684 improved
haftmann
parents: 21911
diff changeset
   284
  "(op -) (xs \<Colon> 'a\<Colon>eq set) = (op -) (xs \<Colon> 'a\<Colon>eq set)" ..
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   285
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   286
lemma [code func]:
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   287
  "image (f \<Colon> 'a \<Rightarrow> 'b\<Colon>eq) = image f" ..
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   288
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   289
lemma [code func]:
22744
5cbe966d67a2 Isar definitions are now added explicitly to code theorem table
haftmann
parents: 22665
diff changeset
   290
  "Union (xss \<Colon> 'a\<Colon>eq set set) = Union xss" ..
5cbe966d67a2 Isar definitions are now added explicitly to code theorem table
haftmann
parents: 22665
diff changeset
   291
5cbe966d67a2 Isar definitions are now added explicitly to code theorem table
haftmann
parents: 22665
diff changeset
   292
lemma [code func]:
5cbe966d67a2 Isar definitions are now added explicitly to code theorem table
haftmann
parents: 22665
diff changeset
   293
  "Inter (xss \<Colon> 'a\<Colon>eq set set) = Inter xss" ..
5cbe966d67a2 Isar definitions are now added explicitly to code theorem table
haftmann
parents: 22665
diff changeset
   294
5cbe966d67a2 Isar definitions are now added explicitly to code theorem table
haftmann
parents: 22665
diff changeset
   295
lemma [code func]:
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   296
  "UNION xs (f \<Colon> 'a \<Rightarrow> 'b\<Colon>eq set) = UNION xs f" ..
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   297
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   298
lemma [code func]:
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   299
  "INTER xs (f \<Colon> 'a \<Rightarrow> 'b\<Colon>eq set) = INTER xs f" ..
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   300
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   301
lemma [code func]:
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   302
  "Ball (xs \<Colon> 'a\<Colon>type set) = Ball xs" ..
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   303
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   304
lemma [code func]:
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   305
  "Bex (xs \<Colon> 'a\<Colon>type set) = Bex xs" ..
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   306
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   307
lemma [code func]:
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   308
  "filter_set P (xs \<Colon> 'a\<Colon>type set) = filter_set P xs" ..
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   309
22744
5cbe966d67a2 Isar definitions are now added explicitly to code theorem table
haftmann
parents: 22665
diff changeset
   310
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   311
code_abstype "'a set" "'a list" where
21115
f4e79a09c305 dropped junk
haftmann
parents: 21063
diff changeset
   312
  "{}" \<equiv> empty_list
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   313
  insert \<equiv> insertl
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   314
  "op \<union>" \<equiv> unionl
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   315
  "op \<inter>" \<equiv> intersect
22177
515021e98684 improved
haftmann
parents: 21911
diff changeset
   316
  "op - \<Colon> 'a set \<Rightarrow> 'a set \<Rightarrow> 'a set" \<equiv> subtract'
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   317
  image \<equiv> map_distinct
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   318
  Union \<equiv> unions
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   319
  Inter \<equiv> intersects
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   320
  UNION \<equiv> map_union
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   321
  INTER \<equiv> map_inter
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   322
  Ball \<equiv> Blall
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   323
  Bex \<equiv> Blex
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   324
  filter_set \<equiv> filter
20934
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   325
2b872c161747 added code_abstype
haftmann
parents: 20840
diff changeset
   326
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22492
diff changeset
   327
subsubsection {* type serializations *}
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   328
17632
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   329
types_code
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   330
  set ("_ list")
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   331
attach (term_of) {*
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   332
fun term_of_set f T [] = Const ("{}", Type ("set", [T]))
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   333
  | term_of_set f T (x :: xs) = Const ("insert",
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   334
      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
   335
*}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   336
attach (test) {*
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   337
fun gen_set' aG i j = frequency
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   338
  [(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
   339
and gen_set aG i = gen_set' aG i i;
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   340
*}
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   341
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   342
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22492
diff changeset
   343
subsubsection {* const serializations *}
18702
7dc7dcd63224 substantial improvements in code generator
haftmann
parents: 17632
diff changeset
   344
17632
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   345
consts_code
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   346
  "{}"      ("[]")
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   347
  "insert"  ("{*insertl*}")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   348
  "op Un"   ("{*unionl*}")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   349
  "op Int"  ("{*intersect*}")
22838
haftmann
parents: 22744
diff changeset
   350
  "minus" :: "'a set \<Rightarrow> 'a set \<Rightarrow> 'a set"
19791
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   351
            ("{*flip subtract*}")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   352
  "image"   ("{*map_distinct*}")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   353
  "Union"   ("{*unions*}")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   354
  "Inter"   ("{*intersects*}")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   355
  "UNION"   ("{*map_union*}")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   356
  "INTER"   ("{*map_inter*}")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   357
  "Ball"    ("{*Blall*}")
ab326de16ad5 refined code generation
haftmann
parents: 19233
diff changeset
   358
  "Bex"     ("{*Blex*}")
21385
cf398bb8a8be added filter_set; adaptions to more strict type discipline for code lemmas
haftmann
parents: 21323
diff changeset
   359
  "filter_set" ("{*filter*}")
17632
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   360
13d6a689efe9 New theory for implementing finite sets by lists.
berghofe
parents:
diff changeset
   361
end