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