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