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