src/HOL/Basic_BNFs.thy
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 58446 e89f57d1e46c
child 58889 5b7a9633cfa8
permissions -rw-r--r--
specialized specification: avoid trivial instances
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
blanchet@49451
    16
definition setl :: "'a + 'b \<Rightarrow> 'a set" where
blanchet@49451
    17
"setl x = (case x of Inl z => {z} | _ => {})"
blanchet@48975
    18
blanchet@49451
    19
definition setr :: "'a + 'b \<Rightarrow> 'b set" where
blanchet@49451
    20
"setr x = (case x of Inr z => {z} | _ => {})"
blanchet@48975
    21
blanchet@49451
    22
lemmas sum_set_defs = setl_def[abs_def] setr_def[abs_def]
blanchet@48975
    23
blanchet@55943
    24
lemma rel_sum_simps[simp]:
blanchet@55943
    25
  "rel_sum R1 R2 (Inl a1) (Inl b1) = R1 a1 b1"
blanchet@55943
    26
  "rel_sum R1 R2 (Inl a1) (Inr b2) = False"
blanchet@55943
    27
  "rel_sum R1 R2 (Inr a2) (Inl b1) = False"
blanchet@55943
    28
  "rel_sum R1 R2 (Inr a2) (Inr b2) = R2 a2 b2"
blanchet@55943
    29
  unfolding rel_sum_def by simp_all
blanchet@55083
    30
traytel@54421
    31
bnf "'a + 'b"
blanchet@55931
    32
  map: map_sum
traytel@54421
    33
  sets: setl setr
traytel@54421
    34
  bd: natLeq
traytel@54421
    35
  wits: Inl Inr
blanchet@55943
    36
  rel: rel_sum
blanchet@48975
    37
proof -
blanchet@55931
    38
  show "map_sum id id = id" by (rule map_sum.id)
blanchet@48975
    39
next
blanchet@54486
    40
  fix f1 :: "'o \<Rightarrow> 's" and f2 :: "'p \<Rightarrow> 't" and g1 :: "'s \<Rightarrow> 'q" and g2 :: "'t \<Rightarrow> 'r"
blanchet@55931
    41
  show "map_sum (g1 o f1) (g2 o f2) = map_sum g1 g2 o map_sum f1 f2"
blanchet@55931
    42
    by (rule map_sum.comp[symmetric])
blanchet@48975
    43
next
blanchet@54486
    44
  fix x and f1 :: "'o \<Rightarrow> 'q" and f2 :: "'p \<Rightarrow> 'r" and g1 g2
blanchet@49451
    45
  assume a1: "\<And>z. z \<in> setl x \<Longrightarrow> f1 z = g1 z" and
blanchet@49451
    46
         a2: "\<And>z. z \<in> setr x \<Longrightarrow> f2 z = g2 z"
blanchet@55931
    47
  thus "map_sum f1 f2 x = map_sum g1 g2 x"
blanchet@48975
    48
  proof (cases x)
blanchet@49451
    49
    case Inl thus ?thesis using a1 by (clarsimp simp: setl_def)
blanchet@48975
    50
  next
blanchet@49451
    51
    case Inr thus ?thesis using a2 by (clarsimp simp: setr_def)
blanchet@48975
    52
  qed
blanchet@48975
    53
next
blanchet@54486
    54
  fix f1 :: "'o \<Rightarrow> 'q" and f2 :: "'p \<Rightarrow> 'r"
blanchet@55931
    55
  show "setl o map_sum f1 f2 = image f1 o setl"
blanchet@49451
    56
    by (rule ext, unfold o_apply) (simp add: setl_def split: sum.split)
blanchet@48975
    57
next
blanchet@54486
    58
  fix f1 :: "'o \<Rightarrow> 'q" and f2 :: "'p \<Rightarrow> 'r"
blanchet@55931
    59
  show "setr o map_sum f1 f2 = image f2 o setr"
blanchet@49451
    60
    by (rule ext, unfold o_apply) (simp add: setr_def split: sum.split)
blanchet@48975
    61
next
blanchet@48975
    62
  show "card_order natLeq" by (rule natLeq_card_order)
blanchet@48975
    63
next
blanchet@48975
    64
  show "cinfinite natLeq" by (rule natLeq_cinfinite)
blanchet@48975
    65
next
blanchet@54486
    66
  fix x :: "'o + 'p"
blanchet@49451
    67
  show "|setl x| \<le>o natLeq"
blanchet@48975
    68
    apply (rule ordLess_imp_ordLeq)
blanchet@48975
    69
    apply (rule finite_iff_ordLess_natLeq[THEN iffD1])
blanchet@49451
    70
    by (simp add: setl_def split: sum.split)
blanchet@48975
    71
next
blanchet@54486
    72
  fix x :: "'o + 'p"
blanchet@49451
    73
  show "|setr x| \<le>o natLeq"
blanchet@48975
    74
    apply (rule ordLess_imp_ordLeq)
blanchet@48975
    75
    apply (rule finite_iff_ordLess_natLeq[THEN iffD1])
blanchet@49451
    76
    by (simp add: setr_def split: sum.split)
blanchet@48975
    77
next
traytel@54841
    78
  fix R1 R2 S1 S2
blanchet@55943
    79
  show "rel_sum R1 R2 OO rel_sum S1 S2 \<le> rel_sum (R1 OO S1) (R2 OO S2)"
blanchet@55943
    80
    by (auto simp: rel_sum_def split: sum.splits)
blanchet@49453
    81
next
blanchet@49453
    82
  fix R S
blanchet@55943
    83
  show "rel_sum R S =
blanchet@55931
    84
        (Grp {x. setl x \<subseteq> Collect (split R) \<and> setr x \<subseteq> Collect (split S)} (map_sum fst fst))\<inverse>\<inverse> OO
blanchet@55931
    85
        Grp {x. setl x \<subseteq> Collect (split R) \<and> setr x \<subseteq> Collect (split S)} (map_sum snd snd)"
blanchet@55943
    86
  unfolding setl_def setr_def rel_sum_def Grp_def relcompp.simps conversep.simps fun_eq_iff
blanchet@49453
    87
  by (fastforce split: sum.splits)
blanchet@48975
    88
qed (auto simp: sum_set_defs)
blanchet@48975
    89
blanchet@48975
    90
definition fsts :: "'a \<times> 'b \<Rightarrow> 'a set" where
blanchet@48975
    91
"fsts x = {fst x}"
blanchet@48975
    92
blanchet@48975
    93
definition snds :: "'a \<times> 'b \<Rightarrow> 'b set" where
blanchet@48975
    94
"snds x = {snd x}"
blanchet@48975
    95
blanchet@48975
    96
lemmas prod_set_defs = fsts_def[abs_def] snds_def[abs_def]
blanchet@48975
    97
blanchet@55083
    98
definition
blanchet@55944
    99
  rel_prod :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('c \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> 'a \<times> 'c \<Rightarrow> 'b \<times> 'd \<Rightarrow> bool"
blanchet@55083
   100
where
blanchet@55944
   101
  "rel_prod R1 R2 = (\<lambda>(a, b) (c, d). R1 a c \<and> R2 b d)"
blanchet@55083
   102
blanchet@55944
   103
lemma rel_prod_apply [simp]:
blanchet@55944
   104
  "rel_prod R1 R2 (a, b) (c, d) \<longleftrightarrow> R1 a c \<and> R2 b d"
blanchet@55944
   105
  by (simp add: rel_prod_def)
blanchet@55083
   106
traytel@54421
   107
bnf "'a \<times> 'b"
blanchet@55932
   108
  map: map_prod
traytel@54421
   109
  sets: fsts snds
traytel@54421
   110
  bd: natLeq
blanchet@55944
   111
  rel: rel_prod
blanchet@48975
   112
proof (unfold prod_set_defs)
blanchet@55932
   113
  show "map_prod id id = id" by (rule map_prod.id)
blanchet@48975
   114
next
blanchet@48975
   115
  fix f1 f2 g1 g2
blanchet@55932
   116
  show "map_prod (g1 o f1) (g2 o f2) = map_prod g1 g2 o map_prod f1 f2"
blanchet@55932
   117
    by (rule map_prod.comp[symmetric])
blanchet@48975
   118
next
blanchet@48975
   119
  fix x f1 f2 g1 g2
blanchet@48975
   120
  assume "\<And>z. z \<in> {fst x} \<Longrightarrow> f1 z = g1 z" "\<And>z. z \<in> {snd x} \<Longrightarrow> f2 z = g2 z"
blanchet@55932
   121
  thus "map_prod f1 f2 x = map_prod g1 g2 x" by (cases x) simp
blanchet@48975
   122
next
blanchet@48975
   123
  fix f1 f2
blanchet@55932
   124
  show "(\<lambda>x. {fst x}) o map_prod f1 f2 = image f1 o (\<lambda>x. {fst x})"
blanchet@48975
   125
    by (rule ext, unfold o_apply) simp
blanchet@48975
   126
next
blanchet@48975
   127
  fix f1 f2
blanchet@55932
   128
  show "(\<lambda>x. {snd x}) o map_prod f1 f2 = image f2 o (\<lambda>x. {snd x})"
blanchet@48975
   129
    by (rule ext, unfold o_apply) simp
blanchet@48975
   130
next
traytel@52635
   131
  show "card_order natLeq" by (rule natLeq_card_order)
blanchet@48975
   132
next
traytel@52635
   133
  show "cinfinite natLeq" by (rule natLeq_cinfinite)
blanchet@48975
   134
next
blanchet@48975
   135
  fix x
traytel@52635
   136
  show "|{fst x}| \<le>o natLeq"
traytel@55811
   137
    by (rule ordLess_imp_ordLeq) (simp add: finite_iff_ordLess_natLeq[symmetric])
blanchet@48975
   138
next
traytel@52635
   139
  fix x
traytel@52635
   140
  show "|{snd x}| \<le>o natLeq"
traytel@55811
   141
    by (rule ordLess_imp_ordLeq) (simp add: finite_iff_ordLess_natLeq[symmetric])
blanchet@48975
   142
next
traytel@54841
   143
  fix R1 R2 S1 S2
blanchet@55944
   144
  show "rel_prod R1 R2 OO rel_prod S1 S2 \<le> rel_prod (R1 OO S1) (R2 OO S2)" by auto
blanchet@49453
   145
next
blanchet@49453
   146
  fix R S
blanchet@55944
   147
  show "rel_prod R S =
blanchet@55932
   148
        (Grp {x. {fst x} \<subseteq> Collect (split R) \<and> {snd x} \<subseteq> Collect (split S)} (map_prod fst fst))\<inverse>\<inverse> OO
blanchet@55932
   149
        Grp {x. {fst x} \<subseteq> Collect (split R) \<and> {snd x} \<subseteq> Collect (split S)} (map_prod snd snd)"
blanchet@55944
   150
  unfolding prod_set_defs rel_prod_def Grp_def relcompp.simps conversep.simps fun_eq_iff
blanchet@49453
   151
  by auto
traytel@54189
   152
qed
blanchet@48975
   153
traytel@54421
   154
bnf "'a \<Rightarrow> 'b"
traytel@54421
   155
  map: "op \<circ>"
traytel@54421
   156
  sets: range
traytel@54421
   157
  bd: "natLeq +c |UNIV :: 'a set|"
blanchet@55945
   158
  rel: "rel_fun op ="
blanchet@48975
   159
proof
blanchet@48975
   160
  fix f show "id \<circ> f = id f" by simp
blanchet@48975
   161
next
blanchet@48975
   162
  fix f g show "op \<circ> (g \<circ> f) = op \<circ> g \<circ> op \<circ> f"
blanchet@48975
   163
  unfolding comp_def[abs_def] ..
blanchet@48975
   164
next
blanchet@48975
   165
  fix x f g
blanchet@48975
   166
  assume "\<And>z. z \<in> range x \<Longrightarrow> f z = g z"
blanchet@48975
   167
  thus "f \<circ> x = g \<circ> x" by auto
blanchet@48975
   168
next
blanchet@48975
   169
  fix f show "range \<circ> op \<circ> f = op ` f \<circ> range"
haftmann@56077
   170
    by (auto simp add: fun_eq_iff)
blanchet@48975
   171
next
blanchet@48975
   172
  show "card_order (natLeq +c |UNIV| )" (is "_ (_ +c ?U)")
blanchet@48975
   173
  apply (rule card_order_csum)
blanchet@48975
   174
  apply (rule natLeq_card_order)
blanchet@48975
   175
  by (rule card_of_card_order_on)
blanchet@48975
   176
(*  *)
blanchet@48975
   177
  show "cinfinite (natLeq +c ?U)"
blanchet@48975
   178
    apply (rule cinfinite_csum)
blanchet@48975
   179
    apply (rule disjI1)
blanchet@48975
   180
    by (rule natLeq_cinfinite)
blanchet@48975
   181
next
blanchet@48975
   182
  fix f :: "'d => 'a"
blanchet@48975
   183
  have "|range f| \<le>o | (UNIV::'d set) |" (is "_ \<le>o ?U") by (rule card_of_image)
blanchet@54486
   184
  also have "?U \<le>o natLeq +c ?U" by (rule ordLeq_csum2) (rule card_of_Card_order)
blanchet@48975
   185
  finally show "|range f| \<le>o natLeq +c ?U" .
blanchet@48975
   186
next
traytel@54841
   187
  fix R S
blanchet@55945
   188
  show "rel_fun op = R OO rel_fun op = S \<le> rel_fun op = (R OO S)" by (auto simp: rel_fun_def)
blanchet@49453
   189
next
blanchet@49463
   190
  fix R
blanchet@55945
   191
  show "rel_fun op = R =
traytel@51893
   192
        (Grp {x. range x \<subseteq> Collect (split R)} (op \<circ> fst))\<inverse>\<inverse> OO
traytel@51893
   193
         Grp {x. range x \<subseteq> Collect (split R)} (op \<circ> snd)"
blanchet@55945
   194
  unfolding rel_fun_def Grp_def fun_eq_iff relcompp.simps conversep.simps subset_iff image_iff
traytel@55811
   195
    comp_apply mem_Collect_eq split_beta bex_UNIV
traytel@55811
   196
  proof (safe, unfold fun_eq_iff[symmetric])
traytel@55811
   197
    fix x xa a b c xb y aa ba
traytel@55811
   198
    assume *: "x = a" "xa = c" "a = ba" "b = aa" "c = (\<lambda>x. snd (b x))" "ba = (\<lambda>x. fst (aa x))" and
traytel@55811
   199
       **: "\<forall>t. (\<exists>x. t = aa x) \<longrightarrow> R (fst t) (snd t)"
traytel@55811
   200
    show "R (x y) (xa y)" unfolding * by (rule mp[OF spec[OF **]]) blast
traytel@55811
   201
  qed force
traytel@54189
   202
qed
traytel@54191
   203
blanchet@48975
   204
end