src/HOL/Library/Enum.thy
author haftmann
Fri, 11 Jun 2010 17:14:02 +0200
changeset 37407 61dd8c145da7
parent 33639 603320b93668
child 37601 2a4fb776ca53
permissions -rw-r--r--
declare lex_prod_def [code del]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31596
c96d7e5df659 tuned whitespace
haftmann
parents: 31482
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen *)
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
     2
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
     3
header {* Finite types as explicit enumerations *}
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
     4
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
     5
theory Enum
30663
0b6aff7451b2 Main is (Complex_Main) base entry point in library theories
haftmann
parents: 29961
diff changeset
     6
imports Map Main
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
     7
begin
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
     8
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
     9
subsection {* Class @{text enum} *}
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    10
29797
08ef36ed2f8a handling type classes without parameters
haftmann
parents: 29024
diff changeset
    11
class enum =
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    12
  fixes enum :: "'a list"
33635
dcaada178c6f moved lemma map_of_zip_map to Map.thy
haftmann
parents: 31596
diff changeset
    13
  assumes UNIV_enum: "UNIV = set enum"
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    14
    and enum_distinct: "distinct enum"
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    15
begin
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    16
29797
08ef36ed2f8a handling type classes without parameters
haftmann
parents: 29024
diff changeset
    17
subclass finite proof
08ef36ed2f8a handling type classes without parameters
haftmann
parents: 29024
diff changeset
    18
qed (simp add: UNIV_enum)
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    19
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    20
lemma enum_all: "set enum = UNIV" unfolding UNIV_enum ..
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    21
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    22
lemma in_enum [intro]: "x \<in> set enum"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    23
  unfolding enum_all by auto
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    24
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    25
lemma enum_eq_I:
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    26
  assumes "\<And>x. x \<in> set xs"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    27
  shows "set enum = set xs"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    28
proof -
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    29
  from assms UNIV_eq_I have "UNIV = set xs" by auto
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    30
  with enum_all show ?thesis by simp
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    31
qed
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    32
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    33
end
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    34
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    35
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    36
subsection {* Equality and order on functions *}
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    37
26513
6f306c8c2c54 explicit class "eq" for operational equality
haftmann
parents: 26444
diff changeset
    38
instantiation "fun" :: (enum, eq) eq
6f306c8c2c54 explicit class "eq" for operational equality
haftmann
parents: 26444
diff changeset
    39
begin
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    40
26513
6f306c8c2c54 explicit class "eq" for operational equality
haftmann
parents: 26444
diff changeset
    41
definition
26732
6ea9de67e576 constant HOL.eq now qualified
haftmann
parents: 26513
diff changeset
    42
  "eq_class.eq f g \<longleftrightarrow> (\<forall>x \<in> set enum. f x = g x)"
26513
6f306c8c2c54 explicit class "eq" for operational equality
haftmann
parents: 26444
diff changeset
    43
31464
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
    44
instance proof
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
    45
qed (simp_all add: eq_fun_def enum_all expand_fun_eq)
26513
6f306c8c2c54 explicit class "eq" for operational equality
haftmann
parents: 26444
diff changeset
    46
6f306c8c2c54 explicit class "eq" for operational equality
haftmann
parents: 26444
diff changeset
    47
end
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    48
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28245
diff changeset
    49
lemma order_fun [code]:
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    50
  fixes f g :: "'a\<Colon>enum \<Rightarrow> 'b\<Colon>order"
26968
bb0a56a66180 added code for quantifiers
haftmann
parents: 26815
diff changeset
    51
  shows "f \<le> g \<longleftrightarrow> list_all (\<lambda>x. f x \<le> g x) enum"
bb0a56a66180 added code for quantifiers
haftmann
parents: 26815
diff changeset
    52
    and "f < g \<longleftrightarrow> f \<le> g \<and> \<not> list_all (\<lambda>x. f x = g x) enum"
28684
48faac324061 tuned proof
haftmann
parents: 28562
diff changeset
    53
  by (simp_all add: list_all_iff enum_all expand_fun_eq le_fun_def order_less_le)
26968
bb0a56a66180 added code for quantifiers
haftmann
parents: 26815
diff changeset
    54
bb0a56a66180 added code for quantifiers
haftmann
parents: 26815
diff changeset
    55
bb0a56a66180 added code for quantifiers
haftmann
parents: 26815
diff changeset
    56
subsection {* Quantifiers *}
bb0a56a66180 added code for quantifiers
haftmann
parents: 26815
diff changeset
    57
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28245
diff changeset
    58
lemma all_code [code]: "(\<forall>x. P x) \<longleftrightarrow> list_all P enum"
26968
bb0a56a66180 added code for quantifiers
haftmann
parents: 26815
diff changeset
    59
  by (simp add: list_all_iff enum_all)
bb0a56a66180 added code for quantifiers
haftmann
parents: 26815
diff changeset
    60
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28245
diff changeset
    61
lemma exists_code [code]: "(\<exists>x. P x) \<longleftrightarrow> \<not> list_all (Not o P) enum"
26968
bb0a56a66180 added code for quantifiers
haftmann
parents: 26815
diff changeset
    62
  by (simp add: list_all_iff enum_all)
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    63
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    64
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    65
subsection {* Default instances *}
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
    66
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    67
primrec n_lists :: "nat \<Rightarrow> 'a list \<Rightarrow> 'a list list" where
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    68
  "n_lists 0 xs = [[]]"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    69
  | "n_lists (Suc n) xs = concat (map (\<lambda>ys. map (\<lambda>y. y # ys) xs) (n_lists n xs))"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    70
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    71
lemma n_lists_Nil [simp]: "n_lists n [] = (if n = 0 then [[]] else [])"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    72
  by (induct n) simp_all
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    73
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    74
lemma length_n_lists: "length (n_lists n xs) = length xs ^ n"
33639
603320b93668 New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents: 33635
diff changeset
    75
  by (induct n) (auto simp add: length_concat o_def listsum_triv)
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    76
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    77
lemma length_n_lists_elem: "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    78
  by (induct n arbitrary: ys) auto
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    79
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    80
lemma set_n_lists: "set (n_lists n xs) = {ys. length ys = n \<and> set ys \<subseteq> set xs}"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    81
proof (rule set_ext)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    82
  fix ys :: "'a list"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    83
  show "ys \<in> set (n_lists n xs) \<longleftrightarrow> ys \<in> {ys. length ys = n \<and> set ys \<subseteq> set xs}"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    84
  proof -
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    85
    have "ys \<in> set (n_lists n xs) \<Longrightarrow> length ys = n"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    86
      by (induct n arbitrary: ys) auto
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    87
    moreover have "\<And>x. ys \<in> set (n_lists n xs) \<Longrightarrow> x \<in> set ys \<Longrightarrow> x \<in> set xs"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    88
      by (induct n arbitrary: ys) auto
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    89
    moreover have "set ys \<subseteq> set xs \<Longrightarrow> ys \<in> set (n_lists (length ys) xs)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    90
      by (induct ys) auto
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    91
    ultimately show ?thesis by auto
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    92
  qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    93
qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    94
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    95
lemma distinct_n_lists:
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    96
  assumes "distinct xs"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    97
  shows "distinct (n_lists n xs)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    98
proof (rule card_distinct)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
    99
  from assms have card_length: "card (set xs) = length xs" by (rule distinct_card)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   100
  have "card (set (n_lists n xs)) = card (set xs) ^ n"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   101
  proof (induct n)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   102
    case 0 then show ?case by simp
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   103
  next
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   104
    case (Suc n)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   105
    moreover have "card (\<Union>ys\<in>set (n_lists n xs). (\<lambda>y. y # ys) ` set xs)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   106
      = (\<Sum>ys\<in>set (n_lists n xs). card ((\<lambda>y. y # ys) ` set xs))"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   107
      by (rule card_UN_disjoint) auto
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   108
    moreover have "\<And>ys. card ((\<lambda>y. y # ys) ` set xs) = card (set xs)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   109
      by (rule card_image) (simp add: inj_on_def)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   110
    ultimately show ?case by auto
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   111
  qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   112
  also have "\<dots> = length xs ^ n" by (simp add: card_length)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   113
  finally show "card (set (n_lists n xs)) = length (n_lists n xs)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   114
    by (simp add: length_n_lists)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   115
qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   116
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   117
lemma map_of_zip_enum_is_Some:
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   118
  assumes "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   119
  shows "\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   120
proof -
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   121
  from assms have "x \<in> set (enum \<Colon> 'a\<Colon>enum list) \<longleftrightarrow>
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   122
    (\<exists>y. map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x = Some y)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   123
    by (auto intro!: map_of_zip_is_Some)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   124
  then show ?thesis using enum_all by auto
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   125
qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   126
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   127
lemma map_of_zip_enum_inject:
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   128
  fixes xs ys :: "'b\<Colon>enum list"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   129
  assumes length: "length xs = length (enum \<Colon> 'a\<Colon>enum list)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   130
      "length ys = length (enum \<Colon> 'a\<Colon>enum list)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   131
    and map_of: "the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   132
  shows "xs = ys"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   133
proof -
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   134
  have "map_of (zip (enum \<Colon> 'a list) xs) = map_of (zip (enum \<Colon> 'a list) ys)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   135
  proof
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   136
    fix x :: 'a
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   137
    from length map_of_zip_enum_is_Some obtain y1 y2
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   138
      where "map_of (zip (enum \<Colon> 'a list) xs) x = Some y1"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   139
        and "map_of (zip (enum \<Colon> 'a list) ys) x = Some y2" by blast
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   140
    moreover from map_of have "the (map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x) = the (map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   141
      by (auto dest: fun_cong)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   142
    ultimately show "map_of (zip (enum \<Colon> 'a\<Colon>enum list) xs) x = map_of (zip (enum \<Colon> 'a\<Colon>enum list) ys) x"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   143
      by simp
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   144
  qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   145
  with length enum_distinct show "xs = ys" by (rule map_of_zip_inject)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   146
qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   147
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   148
instantiation "fun" :: (enum, enum) enum
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   149
begin
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   150
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   151
definition
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28245
diff changeset
   152
  [code del]: "enum = map (\<lambda>ys. the o map_of (zip (enum\<Colon>'a list) ys)) (n_lists (length (enum\<Colon>'a\<Colon>enum list)) enum)"
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   153
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   154
instance proof
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   155
  show "UNIV = set (enum \<Colon> ('a \<Rightarrow> 'b) list)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   156
  proof (rule UNIV_eq_I)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   157
    fix f :: "'a \<Rightarrow> 'b"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   158
    have "f = the \<circ> map_of (zip (enum \<Colon> 'a\<Colon>enum list) (map f enum))"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   159
      by (auto simp add: map_of_zip_map expand_fun_eq)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   160
    then show "f \<in> set enum"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   161
      by (auto simp add: enum_fun_def set_n_lists)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   162
  qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   163
next
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   164
  from map_of_zip_enum_inject
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   165
  show "distinct (enum \<Colon> ('a \<Rightarrow> 'b) list)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   166
    by (auto intro!: inj_onI simp add: enum_fun_def
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   167
      distinct_map distinct_n_lists enum_distinct set_n_lists enum_all)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   168
qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   169
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   170
end
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   171
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28245
diff changeset
   172
lemma enum_fun_code [code]: "enum = (let enum_a = (enum \<Colon> 'a\<Colon>{enum, eq} list)
28245
9767dd8e1e54 celver code lemma avoid type ambiguity problem with Haskell
haftmann
parents: 27487
diff changeset
   173
  in map (\<lambda>ys. the o map_of (zip enum_a ys)) (n_lists (length enum_a) enum))"
9767dd8e1e54 celver code lemma avoid type ambiguity problem with Haskell
haftmann
parents: 27487
diff changeset
   174
  by (simp add: enum_fun_def Let_def)
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   175
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   176
instantiation unit :: enum
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   177
begin
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   178
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   179
definition
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   180
  "enum = [()]"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   181
31464
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   182
instance proof
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   183
qed (simp_all add: enum_unit_def UNIV_unit)
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   184
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   185
end
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   186
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   187
instantiation bool :: enum
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   188
begin
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   189
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   190
definition
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   191
  "enum = [False, True]"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   192
31464
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   193
instance proof
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   194
qed (simp_all add: enum_bool_def UNIV_bool)
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   195
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   196
end
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   197
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   198
primrec product :: "'a list \<Rightarrow> 'b list \<Rightarrow> ('a \<times> 'b) list" where
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   199
  "product [] _ = []"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   200
  | "product (x#xs) ys = map (Pair x) ys @ product xs ys"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   201
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   202
lemma product_list_set:
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   203
  "set (product xs ys) = set xs \<times> set ys"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   204
  by (induct xs) auto
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   205
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   206
lemma distinct_product:
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   207
  assumes "distinct xs" and "distinct ys"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   208
  shows "distinct (product xs ys)"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   209
  using assms by (induct xs)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   210
    (auto intro: inj_onI simp add: product_list_set distinct_map)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   211
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   212
instantiation * :: (enum, enum) enum
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   213
begin
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   214
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   215
definition
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   216
  "enum = product enum enum"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   217
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   218
instance by default
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   219
  (simp_all add: enum_prod_def product_list_set distinct_product enum_all enum_distinct)
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   220
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   221
end
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   222
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   223
instantiation "+" :: (enum, enum) enum
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   224
begin
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   225
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   226
definition
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   227
  "enum = map Inl enum @ map Inr enum"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   228
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   229
instance by default
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   230
  (auto simp add: enum_all enum_sum_def, case_tac x, auto intro: inj_onI simp add: distinct_map enum_distinct)
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   231
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   232
end
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   233
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   234
primrec sublists :: "'a list \<Rightarrow> 'a list list" where
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   235
  "sublists [] = [[]]"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   236
  | "sublists (x#xs) = (let xss = sublists xs in map (Cons x) xss @ xss)"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   237
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   238
lemma length_sublists:
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   239
  "length (sublists xs) = Suc (Suc (0\<Colon>nat)) ^ length xs"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   240
  by (induct xs) (simp_all add: Let_def)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   241
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   242
lemma sublists_powset:
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   243
  "set ` set (sublists xs) = Pow (set xs)"
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   244
proof -
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   245
  have aux: "\<And>x A. set ` Cons x ` A = insert x ` set ` A"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   246
    by (auto simp add: image_def)
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   247
  have "set (map set (sublists xs)) = Pow (set xs)"
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   248
    by (induct xs)
33639
603320b93668 New list theorems; added map_map to simpset, this is the prefered direction; allow sorting by a key
hoelzl
parents: 33635
diff changeset
   249
      (simp_all add: aux Let_def Pow_insert Un_commute comp_def del: map_map)
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   250
  then show ?thesis by simp
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   251
qed
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   252
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   253
lemma distinct_set_sublists:
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   254
  assumes "distinct xs"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   255
  shows "distinct (map set (sublists xs))"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   256
proof (rule card_distinct)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   257
  have "finite (set xs)" by rule
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   258
  then have "card (Pow (set xs)) = Suc (Suc 0) ^ card (set xs)" by (rule card_Pow)
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   259
  with assms distinct_card [of xs]
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   260
    have "card (Pow (set xs)) = Suc (Suc 0) ^ length xs" by simp
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   261
  then show "card (set (map set (sublists xs))) = length (map set (sublists xs))"
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   262
    by (simp add: sublists_powset length_sublists)
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   263
qed
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   264
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   265
instantiation nibble :: enum
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   266
begin
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   267
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   268
definition
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   269
  "enum = [Nibble0, Nibble1, Nibble2, Nibble3, Nibble4, Nibble5, Nibble6, Nibble7,
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   270
    Nibble8, Nibble9, NibbleA, NibbleB, NibbleC, NibbleD, NibbleE, NibbleF]"
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   271
31464
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   272
instance proof
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   273
qed (simp_all add: enum_nibble_def UNIV_nibble)
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   274
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   275
end
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   276
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   277
instantiation char :: enum
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   278
begin
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   279
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   280
definition
28562
4e74209f113e `code func` now just `code`
haftmann
parents: 28245
diff changeset
   281
  [code del]: "enum = map (split Char) (product enum enum)"
26444
6a5faa5bcf19 instance for functions, explicit characters
haftmann
parents: 26348
diff changeset
   282
31482
7288382fd549 using constant "chars"
haftmann
parents: 31464
diff changeset
   283
lemma enum_chars [code]:
7288382fd549 using constant "chars"
haftmann
parents: 31464
diff changeset
   284
  "enum = chars"
7288382fd549 using constant "chars"
haftmann
parents: 31464
diff changeset
   285
  unfolding enum_char_def chars_def enum_nibble_def by simp
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   286
31464
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   287
instance proof
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   288
qed (auto intro: char.exhaust injI simp add: enum_char_def product_list_set enum_all full_SetCompr_eq [symmetric]
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   289
  distinct_map distinct_product enum_distinct)
26348
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   290
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   291
end
0f8e23edd357 added theory Library/Enum.thy
haftmann
parents:
diff changeset
   292
29024
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   293
instantiation option :: (enum) enum
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   294
begin
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   295
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   296
definition
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   297
  "enum = None # map Some enum"
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   298
31464
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   299
instance proof
b2aca38301c4 tuned proofs
haftmann
parents: 31193
diff changeset
   300
qed (auto simp add: enum_all enum_option_def, rule option.exhaust, auto intro: simp add: distinct_map enum_distinct)
29024
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   301
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   302
end
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   303
6cfa380af73b instantiation option :: (enum) enum
huffman
parents: 28684
diff changeset
   304
end