src/HOL/Basic_BNFs.thy
author blanchet
Mon Jan 20 20:42:43 2014 +0100 (2014-01-20)
changeset 55084 8ee9aabb2bca
parent 55083 0a689157e3ce
child 55707 50cf04dd2584
permissions -rw-r--r--
rationalized lemmas
blanchet@55075
     1
(*  Title:      HOL/Basic_BNFs.thy
blanchet@48975
     2
    Author:     Dmitriy Traytel, TU Muenchen
blanchet@48975
     3
    Author:     Andrei Popescu, TU Muenchen
blanchet@48975
     4
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@48975
     5
    Copyright   2012
blanchet@48975
     6
blanchet@49309
     7
Registration of basic types as bounded natural functors.
blanchet@48975
     8
*)
blanchet@48975
     9
blanchet@49309
    10
header {* Registration of Basic Types as Bounded Natural Functors *}
blanchet@48975
    11
blanchet@48975
    12
theory Basic_BNFs
blanchet@49310
    13
imports BNF_Def
blanchet@48975
    14
begin
blanchet@48975
    15
traytel@54421
    16
bnf ID: 'a
traytel@54421
    17
  map: "id :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
traytel@54421
    18
  sets: "\<lambda>x. {x}"
traytel@54421
    19
  bd: natLeq
traytel@54421
    20
  rel: "id :: ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
traytel@51893
    21
apply (auto simp: Grp_def fun_eq_iff relcompp.simps natLeq_card_order natLeq_cinfinite)
blanchet@48975
    22
apply (rule ordLess_imp_ordLeq[OF finite_ordLess_infinite[OF _ natLeq_Well_order]])
blanchet@49453
    23
apply (auto simp add: Field_card_of Field_natLeq card_of_well_order_on)[3]
blanchet@48975
    24
done
blanchet@48975
    25
traytel@54421
    26
bnf DEADID: 'a
traytel@54421
    27
  map: "id :: 'a \<Rightarrow> 'a"
traytel@54421
    28
  bd: "natLeq +c |UNIV :: 'a set|"
traytel@54421
    29
  rel: "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool"
traytel@54841
    30
by (auto simp add: Grp_def
traytel@51446
    31
  card_order_csum natLeq_card_order card_of_card_order_on
traytel@51446
    32
  cinfinite_csum natLeq_cinfinite)
blanchet@48975
    33
blanchet@49451
    34
definition setl :: "'a + 'b \<Rightarrow> 'a set" where
blanchet@49451
    35
"setl x = (case x of Inl z => {z} | _ => {})"
blanchet@48975
    36
blanchet@49451
    37
definition setr :: "'a + 'b \<Rightarrow> 'b set" where
blanchet@49451
    38
"setr x = (case x of Inr z => {z} | _ => {})"
blanchet@48975
    39
blanchet@49451
    40
lemmas sum_set_defs = setl_def[abs_def] setr_def[abs_def]
blanchet@48975
    41
blanchet@55083
    42
definition
blanchet@55083
    43
   sum_rel :: "('a \<Rightarrow> 'c \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> 'a + 'b \<Rightarrow> 'c + 'd \<Rightarrow> bool"
blanchet@55083
    44
where
blanchet@55083
    45
   "sum_rel R1 R2 x y =
blanchet@55083
    46
     (case (x, y) of (Inl x, Inl y) \<Rightarrow> R1 x y
blanchet@55083
    47
     | (Inr x, Inr y) \<Rightarrow> R2 x y
blanchet@55083
    48
     | _ \<Rightarrow> False)"
blanchet@55083
    49
blanchet@55083
    50
lemma sum_rel_simps[simp]:
blanchet@55083
    51
  "sum_rel R1 R2 (Inl a1) (Inl b1) = R1 a1 b1"
blanchet@55083
    52
  "sum_rel R1 R2 (Inl a1) (Inr b2) = False"
blanchet@55083
    53
  "sum_rel R1 R2 (Inr a2) (Inl b1) = False"
blanchet@55083
    54
  "sum_rel R1 R2 (Inr a2) (Inr b2) = R2 a2 b2"
blanchet@55083
    55
  unfolding sum_rel_def by simp_all
blanchet@55083
    56
traytel@54421
    57
bnf "'a + 'b"
traytel@54421
    58
  map: sum_map
traytel@54421
    59
  sets: setl setr
traytel@54421
    60
  bd: natLeq
traytel@54421
    61
  wits: Inl Inr
traytel@54421
    62
  rel: sum_rel
blanchet@48975
    63
proof -
blanchet@48975
    64
  show "sum_map id id = id" by (rule sum_map.id)
blanchet@48975
    65
next
blanchet@54486
    66
  fix f1 :: "'o \<Rightarrow> 's" and f2 :: "'p \<Rightarrow> 't" and g1 :: "'s \<Rightarrow> 'q" and g2 :: "'t \<Rightarrow> 'r"
blanchet@48975
    67
  show "sum_map (g1 o f1) (g2 o f2) = sum_map g1 g2 o sum_map f1 f2"
blanchet@48975
    68
    by (rule sum_map.comp[symmetric])
blanchet@48975
    69
next
blanchet@54486
    70
  fix x and f1 :: "'o \<Rightarrow> 'q" and f2 :: "'p \<Rightarrow> 'r" and g1 g2
blanchet@49451
    71
  assume a1: "\<And>z. z \<in> setl x \<Longrightarrow> f1 z = g1 z" and
blanchet@49451
    72
         a2: "\<And>z. z \<in> setr x \<Longrightarrow> f2 z = g2 z"
blanchet@48975
    73
  thus "sum_map f1 f2 x = sum_map g1 g2 x"
blanchet@48975
    74
  proof (cases x)
blanchet@49451
    75
    case Inl thus ?thesis using a1 by (clarsimp simp: setl_def)
blanchet@48975
    76
  next
blanchet@49451
    77
    case Inr thus ?thesis using a2 by (clarsimp simp: setr_def)
blanchet@48975
    78
  qed
blanchet@48975
    79
next
blanchet@54486
    80
  fix f1 :: "'o \<Rightarrow> 'q" and f2 :: "'p \<Rightarrow> 'r"
blanchet@49451
    81
  show "setl o sum_map f1 f2 = image f1 o setl"
blanchet@49451
    82
    by (rule ext, unfold o_apply) (simp add: setl_def split: sum.split)
blanchet@48975
    83
next
blanchet@54486
    84
  fix f1 :: "'o \<Rightarrow> 'q" and f2 :: "'p \<Rightarrow> 'r"
blanchet@49451
    85
  show "setr o sum_map f1 f2 = image f2 o setr"
blanchet@49451
    86
    by (rule ext, unfold o_apply) (simp add: setr_def split: sum.split)
blanchet@48975
    87
next
blanchet@48975
    88
  show "card_order natLeq" by (rule natLeq_card_order)
blanchet@48975
    89
next
blanchet@48975
    90
  show "cinfinite natLeq" by (rule natLeq_cinfinite)
blanchet@48975
    91
next
blanchet@54486
    92
  fix x :: "'o + 'p"
blanchet@49451
    93
  show "|setl x| \<le>o natLeq"
blanchet@48975
    94
    apply (rule ordLess_imp_ordLeq)
blanchet@48975
    95
    apply (rule finite_iff_ordLess_natLeq[THEN iffD1])
blanchet@49451
    96
    by (simp add: setl_def split: sum.split)
blanchet@48975
    97
next
blanchet@54486
    98
  fix x :: "'o + 'p"
blanchet@49451
    99
  show "|setr x| \<le>o natLeq"
blanchet@48975
   100
    apply (rule ordLess_imp_ordLeq)
blanchet@48975
   101
    apply (rule finite_iff_ordLess_natLeq[THEN iffD1])
blanchet@49451
   102
    by (simp add: setr_def split: sum.split)
blanchet@48975
   103
next
traytel@54841
   104
  fix R1 R2 S1 S2
traytel@54841
   105
  show "sum_rel R1 R2 OO sum_rel S1 S2 \<le> sum_rel (R1 OO S1) (R2 OO S2)"
traytel@54841
   106
    by (auto simp: sum_rel_def split: sum.splits)
blanchet@49453
   107
next
blanchet@49453
   108
  fix R S
traytel@51893
   109
  show "sum_rel R S =
traytel@51893
   110
        (Grp {x. setl x \<subseteq> Collect (split R) \<and> setr x \<subseteq> Collect (split S)} (sum_map fst fst))\<inverse>\<inverse> OO
traytel@51893
   111
        Grp {x. setl x \<subseteq> Collect (split R) \<and> setr x \<subseteq> Collect (split S)} (sum_map snd snd)"
traytel@51893
   112
  unfolding setl_def setr_def sum_rel_def Grp_def relcompp.simps conversep.simps fun_eq_iff
blanchet@49453
   113
  by (fastforce split: sum.splits)
blanchet@48975
   114
qed (auto simp: sum_set_defs)
blanchet@48975
   115
blanchet@48975
   116
definition fsts :: "'a \<times> 'b \<Rightarrow> 'a set" where
blanchet@48975
   117
"fsts x = {fst x}"
blanchet@48975
   118
blanchet@48975
   119
definition snds :: "'a \<times> 'b \<Rightarrow> 'b set" where
blanchet@48975
   120
"snds x = {snd x}"
blanchet@48975
   121
blanchet@48975
   122
lemmas prod_set_defs = fsts_def[abs_def] snds_def[abs_def]
blanchet@48975
   123
blanchet@55083
   124
definition
blanchet@55083
   125
  prod_rel :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('c \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> 'a \<times> 'c \<Rightarrow> 'b \<times> 'd \<Rightarrow> bool"
blanchet@55083
   126
where
blanchet@55083
   127
  "prod_rel R1 R2 = (\<lambda>(a, b) (c, d). R1 a c \<and> R2 b d)"
blanchet@55083
   128
blanchet@55083
   129
lemma prod_rel_apply [simp]:
blanchet@55083
   130
  "prod_rel R1 R2 (a, b) (c, d) \<longleftrightarrow> R1 a c \<and> R2 b d"
blanchet@55083
   131
  by (simp add: prod_rel_def)
blanchet@55083
   132
traytel@54421
   133
bnf "'a \<times> 'b"
traytel@54421
   134
  map: map_pair
traytel@54421
   135
  sets: fsts snds
traytel@54421
   136
  bd: natLeq
traytel@54421
   137
  rel: prod_rel
blanchet@48975
   138
proof (unfold prod_set_defs)
blanchet@48975
   139
  show "map_pair id id = id" by (rule map_pair.id)
blanchet@48975
   140
next
blanchet@48975
   141
  fix f1 f2 g1 g2
blanchet@48975
   142
  show "map_pair (g1 o f1) (g2 o f2) = map_pair g1 g2 o map_pair f1 f2"
blanchet@48975
   143
    by (rule map_pair.comp[symmetric])
blanchet@48975
   144
next
blanchet@48975
   145
  fix x f1 f2 g1 g2
blanchet@48975
   146
  assume "\<And>z. z \<in> {fst x} \<Longrightarrow> f1 z = g1 z" "\<And>z. z \<in> {snd x} \<Longrightarrow> f2 z = g2 z"
blanchet@48975
   147
  thus "map_pair f1 f2 x = map_pair g1 g2 x" by (cases x) simp
blanchet@48975
   148
next
blanchet@48975
   149
  fix f1 f2
blanchet@48975
   150
  show "(\<lambda>x. {fst x}) o map_pair f1 f2 = image f1 o (\<lambda>x. {fst x})"
blanchet@48975
   151
    by (rule ext, unfold o_apply) simp
blanchet@48975
   152
next
blanchet@48975
   153
  fix f1 f2
blanchet@48975
   154
  show "(\<lambda>x. {snd x}) o map_pair f1 f2 = image f2 o (\<lambda>x. {snd x})"
blanchet@48975
   155
    by (rule ext, unfold o_apply) simp
blanchet@48975
   156
next
traytel@52635
   157
  show "card_order natLeq" by (rule natLeq_card_order)
blanchet@48975
   158
next
traytel@52635
   159
  show "cinfinite natLeq" by (rule natLeq_cinfinite)
blanchet@48975
   160
next
blanchet@48975
   161
  fix x
traytel@52635
   162
  show "|{fst x}| \<le>o natLeq"
traytel@52635
   163
    by (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq finite.emptyI finite_insert)
blanchet@48975
   164
next
traytel@52635
   165
  fix x
traytel@52635
   166
  show "|{snd x}| \<le>o natLeq"
traytel@52635
   167
    by (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq finite.emptyI finite_insert)
blanchet@48975
   168
next
traytel@54841
   169
  fix R1 R2 S1 S2
traytel@54841
   170
  show "prod_rel R1 R2 OO prod_rel S1 S2 \<le> prod_rel (R1 OO S1) (R2 OO S2)" by auto
blanchet@49453
   171
next
blanchet@49453
   172
  fix R S
traytel@51893
   173
  show "prod_rel R S =
traytel@51893
   174
        (Grp {x. {fst x} \<subseteq> Collect (split R) \<and> {snd x} \<subseteq> Collect (split S)} (map_pair fst fst))\<inverse>\<inverse> OO
traytel@51893
   175
        Grp {x. {fst x} \<subseteq> Collect (split R) \<and> {snd x} \<subseteq> Collect (split S)} (map_pair snd snd)"
traytel@51893
   176
  unfolding prod_set_defs prod_rel_def Grp_def relcompp.simps conversep.simps fun_eq_iff
blanchet@49453
   177
  by auto
traytel@54189
   178
qed
blanchet@48975
   179
traytel@54421
   180
bnf "'a \<Rightarrow> 'b"
traytel@54421
   181
  map: "op \<circ>"
traytel@54421
   182
  sets: range
traytel@54421
   183
  bd: "natLeq +c |UNIV :: 'a set|"
traytel@54421
   184
  rel: "fun_rel op ="
blanchet@48975
   185
proof
blanchet@48975
   186
  fix f show "id \<circ> f = id f" by simp
blanchet@48975
   187
next
blanchet@48975
   188
  fix f g show "op \<circ> (g \<circ> f) = op \<circ> g \<circ> op \<circ> f"
blanchet@48975
   189
  unfolding comp_def[abs_def] ..
blanchet@48975
   190
next
blanchet@48975
   191
  fix x f g
blanchet@48975
   192
  assume "\<And>z. z \<in> range x \<Longrightarrow> f z = g z"
blanchet@48975
   193
  thus "f \<circ> x = g \<circ> x" by auto
blanchet@48975
   194
next
blanchet@48975
   195
  fix f show "range \<circ> op \<circ> f = op ` f \<circ> range"
blanchet@48975
   196
  unfolding image_def comp_def[abs_def] by auto
blanchet@48975
   197
next
blanchet@48975
   198
  show "card_order (natLeq +c |UNIV| )" (is "_ (_ +c ?U)")
blanchet@48975
   199
  apply (rule card_order_csum)
blanchet@48975
   200
  apply (rule natLeq_card_order)
blanchet@48975
   201
  by (rule card_of_card_order_on)
blanchet@48975
   202
(*  *)
blanchet@48975
   203
  show "cinfinite (natLeq +c ?U)"
blanchet@48975
   204
    apply (rule cinfinite_csum)
blanchet@48975
   205
    apply (rule disjI1)
blanchet@48975
   206
    by (rule natLeq_cinfinite)
blanchet@48975
   207
next
blanchet@48975
   208
  fix f :: "'d => 'a"
blanchet@48975
   209
  have "|range f| \<le>o | (UNIV::'d set) |" (is "_ \<le>o ?U") by (rule card_of_image)
blanchet@54486
   210
  also have "?U \<le>o natLeq +c ?U" by (rule ordLeq_csum2) (rule card_of_Card_order)
blanchet@48975
   211
  finally show "|range f| \<le>o natLeq +c ?U" .
blanchet@48975
   212
next
traytel@54841
   213
  fix R S
traytel@54841
   214
  show "fun_rel op = R OO fun_rel op = S \<le> fun_rel op = (R OO S)" by (auto simp: fun_rel_def)
blanchet@49453
   215
next
blanchet@49463
   216
  fix R
traytel@51893
   217
  show "fun_rel op = R =
traytel@51893
   218
        (Grp {x. range x \<subseteq> Collect (split R)} (op \<circ> fst))\<inverse>\<inverse> OO
traytel@51893
   219
         Grp {x. range x \<subseteq> Collect (split R)} (op \<circ> snd)"
traytel@51893
   220
  unfolding fun_rel_def Grp_def fun_eq_iff relcompp.simps conversep.simps  subset_iff image_iff
blanchet@54486
   221
  by auto (force, metis (no_types) pair_collapse)
traytel@54189
   222
qed
traytel@54191
   223
blanchet@48975
   224
end