src/HOL/Basic_BNFs.thy
author blanchet
Mon Jan 20 18:24:56 2014 +0100 (2014-01-20)
changeset 55062 6d3fad6f01c9
parent 55058 4e700eb471d4
child 55075 b3d0a02a756d
permissions -rw-r--r--
made BNF compile after move to HOL
     1 (*  Title:      HOL/BNF/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    (*FIXME: define relators here, reuse in Lifting_* once this theory is in HOL*)
    15   Lifting_Sum
    16   Lifting_Product
    17 begin
    18 
    19 bnf ID: 'a
    20   map: "id :: ('a \<Rightarrow> 'b) \<Rightarrow> 'a \<Rightarrow> 'b"
    21   sets: "\<lambda>x. {x}"
    22   bd: natLeq
    23   rel: "id :: ('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a \<Rightarrow> 'b \<Rightarrow> bool"
    24 apply (auto simp: Grp_def fun_eq_iff relcompp.simps natLeq_card_order natLeq_cinfinite)
    25 apply (rule ordLess_imp_ordLeq[OF finite_ordLess_infinite[OF _ natLeq_Well_order]])
    26 apply (auto simp add: Field_card_of Field_natLeq card_of_well_order_on)[3]
    27 done
    28 
    29 bnf DEADID: 'a
    30   map: "id :: 'a \<Rightarrow> 'a"
    31   bd: "natLeq +c |UNIV :: 'a set|"
    32   rel: "op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool"
    33 by (auto simp add: Grp_def
    34   card_order_csum natLeq_card_order card_of_card_order_on
    35   cinfinite_csum natLeq_cinfinite)
    36 
    37 definition setl :: "'a + 'b \<Rightarrow> 'a set" where
    38 "setl x = (case x of Inl z => {z} | _ => {})"
    39 
    40 definition setr :: "'a + 'b \<Rightarrow> 'b set" where
    41 "setr x = (case x of Inr z => {z} | _ => {})"
    42 
    43 lemmas sum_set_defs = setl_def[abs_def] setr_def[abs_def]
    44 
    45 bnf "'a + 'b"
    46   map: sum_map
    47   sets: setl setr
    48   bd: natLeq
    49   wits: Inl Inr
    50   rel: sum_rel
    51 proof -
    52   show "sum_map id id = id" by (rule sum_map.id)
    53 next
    54   fix f1 :: "'o \<Rightarrow> 's" and f2 :: "'p \<Rightarrow> 't" and g1 :: "'s \<Rightarrow> 'q" and g2 :: "'t \<Rightarrow> 'r"
    55   show "sum_map (g1 o f1) (g2 o f2) = sum_map g1 g2 o sum_map f1 f2"
    56     by (rule sum_map.comp[symmetric])
    57 next
    58   fix x and f1 :: "'o \<Rightarrow> 'q" and f2 :: "'p \<Rightarrow> 'r" and g1 g2
    59   assume a1: "\<And>z. z \<in> setl x \<Longrightarrow> f1 z = g1 z" and
    60          a2: "\<And>z. z \<in> setr x \<Longrightarrow> f2 z = g2 z"
    61   thus "sum_map f1 f2 x = sum_map g1 g2 x"
    62   proof (cases x)
    63     case Inl thus ?thesis using a1 by (clarsimp simp: setl_def)
    64   next
    65     case Inr thus ?thesis using a2 by (clarsimp simp: setr_def)
    66   qed
    67 next
    68   fix f1 :: "'o \<Rightarrow> 'q" and f2 :: "'p \<Rightarrow> 'r"
    69   show "setl o sum_map f1 f2 = image f1 o setl"
    70     by (rule ext, unfold o_apply) (simp add: setl_def split: sum.split)
    71 next
    72   fix f1 :: "'o \<Rightarrow> 'q" and f2 :: "'p \<Rightarrow> 'r"
    73   show "setr o sum_map f1 f2 = image f2 o setr"
    74     by (rule ext, unfold o_apply) (simp add: setr_def split: sum.split)
    75 next
    76   show "card_order natLeq" by (rule natLeq_card_order)
    77 next
    78   show "cinfinite natLeq" by (rule natLeq_cinfinite)
    79 next
    80   fix x :: "'o + 'p"
    81   show "|setl x| \<le>o natLeq"
    82     apply (rule ordLess_imp_ordLeq)
    83     apply (rule finite_iff_ordLess_natLeq[THEN iffD1])
    84     by (simp add: setl_def split: sum.split)
    85 next
    86   fix x :: "'o + 'p"
    87   show "|setr x| \<le>o natLeq"
    88     apply (rule ordLess_imp_ordLeq)
    89     apply (rule finite_iff_ordLess_natLeq[THEN iffD1])
    90     by (simp add: setr_def split: sum.split)
    91 next
    92   fix R1 R2 S1 S2
    93   show "sum_rel R1 R2 OO sum_rel S1 S2 \<le> sum_rel (R1 OO S1) (R2 OO S2)"
    94     by (auto simp: sum_rel_def split: sum.splits)
    95 next
    96   fix R S
    97   show "sum_rel R S =
    98         (Grp {x. setl x \<subseteq> Collect (split R) \<and> setr x \<subseteq> Collect (split S)} (sum_map fst fst))\<inverse>\<inverse> OO
    99         Grp {x. setl x \<subseteq> Collect (split R) \<and> setr x \<subseteq> Collect (split S)} (sum_map snd snd)"
   100   unfolding setl_def setr_def sum_rel_def Grp_def relcompp.simps conversep.simps fun_eq_iff
   101   by (fastforce split: sum.splits)
   102 qed (auto simp: sum_set_defs)
   103 
   104 definition fsts :: "'a \<times> 'b \<Rightarrow> 'a set" where
   105 "fsts x = {fst x}"
   106 
   107 definition snds :: "'a \<times> 'b \<Rightarrow> 'b set" where
   108 "snds x = {snd x}"
   109 
   110 lemmas prod_set_defs = fsts_def[abs_def] snds_def[abs_def]
   111 
   112 bnf "'a \<times> 'b"
   113   map: map_pair
   114   sets: fsts snds
   115   bd: natLeq
   116   rel: prod_rel
   117 proof (unfold prod_set_defs)
   118   show "map_pair id id = id" by (rule map_pair.id)
   119 next
   120   fix f1 f2 g1 g2
   121   show "map_pair (g1 o f1) (g2 o f2) = map_pair g1 g2 o map_pair f1 f2"
   122     by (rule map_pair.comp[symmetric])
   123 next
   124   fix x f1 f2 g1 g2
   125   assume "\<And>z. z \<in> {fst x} \<Longrightarrow> f1 z = g1 z" "\<And>z. z \<in> {snd x} \<Longrightarrow> f2 z = g2 z"
   126   thus "map_pair f1 f2 x = map_pair g1 g2 x" by (cases x) simp
   127 next
   128   fix f1 f2
   129   show "(\<lambda>x. {fst x}) o map_pair f1 f2 = image f1 o (\<lambda>x. {fst x})"
   130     by (rule ext, unfold o_apply) simp
   131 next
   132   fix f1 f2
   133   show "(\<lambda>x. {snd x}) o map_pair f1 f2 = image f2 o (\<lambda>x. {snd x})"
   134     by (rule ext, unfold o_apply) simp
   135 next
   136   show "card_order natLeq" by (rule natLeq_card_order)
   137 next
   138   show "cinfinite natLeq" by (rule natLeq_cinfinite)
   139 next
   140   fix x
   141   show "|{fst x}| \<le>o natLeq"
   142     by (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq finite.emptyI finite_insert)
   143 next
   144   fix x
   145   show "|{snd x}| \<le>o natLeq"
   146     by (metis ordLess_imp_ordLeq finite_iff_ordLess_natLeq finite.emptyI finite_insert)
   147 next
   148   fix R1 R2 S1 S2
   149   show "prod_rel R1 R2 OO prod_rel S1 S2 \<le> prod_rel (R1 OO S1) (R2 OO S2)" by auto
   150 next
   151   fix R S
   152   show "prod_rel R S =
   153         (Grp {x. {fst x} \<subseteq> Collect (split R) \<and> {snd x} \<subseteq> Collect (split S)} (map_pair fst fst))\<inverse>\<inverse> OO
   154         Grp {x. {fst x} \<subseteq> Collect (split R) \<and> {snd x} \<subseteq> Collect (split S)} (map_pair snd snd)"
   155   unfolding prod_set_defs prod_rel_def Grp_def relcompp.simps conversep.simps fun_eq_iff
   156   by auto
   157 qed
   158 
   159 bnf "'a \<Rightarrow> 'b"
   160   map: "op \<circ>"
   161   sets: range
   162   bd: "natLeq +c |UNIV :: 'a set|"
   163   rel: "fun_rel op ="
   164 proof
   165   fix f show "id \<circ> f = id f" by simp
   166 next
   167   fix f g show "op \<circ> (g \<circ> f) = op \<circ> g \<circ> op \<circ> f"
   168   unfolding comp_def[abs_def] ..
   169 next
   170   fix x f g
   171   assume "\<And>z. z \<in> range x \<Longrightarrow> f z = g z"
   172   thus "f \<circ> x = g \<circ> x" by auto
   173 next
   174   fix f show "range \<circ> op \<circ> f = op ` f \<circ> range"
   175   unfolding image_def comp_def[abs_def] by auto
   176 next
   177   show "card_order (natLeq +c |UNIV| )" (is "_ (_ +c ?U)")
   178   apply (rule card_order_csum)
   179   apply (rule natLeq_card_order)
   180   by (rule card_of_card_order_on)
   181 (*  *)
   182   show "cinfinite (natLeq +c ?U)"
   183     apply (rule cinfinite_csum)
   184     apply (rule disjI1)
   185     by (rule natLeq_cinfinite)
   186 next
   187   fix f :: "'d => 'a"
   188   have "|range f| \<le>o | (UNIV::'d set) |" (is "_ \<le>o ?U") by (rule card_of_image)
   189   also have "?U \<le>o natLeq +c ?U" by (rule ordLeq_csum2) (rule card_of_Card_order)
   190   finally show "|range f| \<le>o natLeq +c ?U" .
   191 next
   192   fix R S
   193   show "fun_rel op = R OO fun_rel op = S \<le> fun_rel op = (R OO S)" by (auto simp: fun_rel_def)
   194 next
   195   fix R
   196   show "fun_rel op = R =
   197         (Grp {x. range x \<subseteq> Collect (split R)} (op \<circ> fst))\<inverse>\<inverse> OO
   198          Grp {x. range x \<subseteq> Collect (split R)} (op \<circ> snd)"
   199   unfolding fun_rel_def Grp_def fun_eq_iff relcompp.simps conversep.simps  subset_iff image_iff
   200   by auto (force, metis (no_types) pair_collapse)
   201 qed
   202 
   203 end