src/HOL/Basic_BNFs.thy
changeset 62324 ae44f16dcea5
parent 61681 ca53150406c9
child 62335 e85c42f4f30a
equal deleted inserted replaced
62323:8c3eec5812d8 62324:ae44f16dcea5
    28   "rel_sum R1 R2 (Inl a1) (Inr b2) = False"
    28   "rel_sum R1 R2 (Inl a1) (Inr b2) = False"
    29   "rel_sum R1 R2 (Inr a2) (Inl b1) = False"
    29   "rel_sum R1 R2 (Inr a2) (Inl b1) = False"
    30   "rel_sum R1 R2 (Inr a2) (Inr b2) = R2 a2 b2"
    30   "rel_sum R1 R2 (Inr a2) (Inr b2) = R2 a2 b2"
    31   by (auto intro: rel_sum.intros elim: rel_sum.cases)
    31   by (auto intro: rel_sum.intros elim: rel_sum.cases)
    32 
    32 
       
    33 inductive
       
    34    pred_sum :: "('a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> 'a + 'b \<Rightarrow> bool" for P1 P2
       
    35 where
       
    36   "P1 a \<Longrightarrow> pred_sum P1 P2 (Inl a)"
       
    37 | "P2 b \<Longrightarrow> pred_sum P1 P2 (Inr b)"
       
    38 
    33 bnf "'a + 'b"
    39 bnf "'a + 'b"
    34   map: map_sum
    40   map: map_sum
    35   sets: setl setr
    41   sets: setl setr
    36   bd: natLeq
    42   bd: natLeq
    37   wits: Inl Inr
    43   wits: Inl Inr
    38   rel: rel_sum
    44   rel: rel_sum
       
    45   pred: pred_sum
    39 proof -
    46 proof -
    40   show "map_sum id id = id" by (rule map_sum.id)
    47   show "map_sum id id = id" by (rule map_sum.id)
    41 next
    48 next
    42   fix f1 :: "'o \<Rightarrow> 's" and f2 :: "'p \<Rightarrow> 't" and g1 :: "'s \<Rightarrow> 'q" and g2 :: "'t \<Rightarrow> 'r"
    49   fix f1 :: "'o \<Rightarrow> 's" and f2 :: "'p \<Rightarrow> 't" and g1 :: "'s \<Rightarrow> 'q" and g2 :: "'t \<Rightarrow> 'r"
    43   show "map_sum (g1 o f1) (g2 o f2) = map_sum g1 g2 o map_sum f1 f2"
    50   show "map_sum (g1 o f1) (g2 o f2) = map_sum g1 g2 o map_sum f1 f2"
    80   fix R1 R2 S1 S2
    87   fix R1 R2 S1 S2
    81   show "rel_sum R1 R2 OO rel_sum S1 S2 \<le> rel_sum (R1 OO S1) (R2 OO S2)"
    88   show "rel_sum R1 R2 OO rel_sum S1 S2 \<le> rel_sum (R1 OO S1) (R2 OO S2)"
    82     by (force elim: rel_sum.cases)
    89     by (force elim: rel_sum.cases)
    83 next
    90 next
    84   fix R S
    91   fix R S
    85   show "rel_sum R S =
    92   show "rel_sum R S = (\<lambda>x y.
    86         (Grp {x. setl x \<subseteq> Collect (case_prod R) \<and> setr x \<subseteq> Collect (case_prod S)} (map_sum fst fst))\<inverse>\<inverse> OO
    93     \<exists>z. (setl z \<subseteq> {(x, y). R x y} \<and> setr z \<subseteq> {(x, y). S x y}) \<and>
    87         Grp {x. setl x \<subseteq> Collect (case_prod R) \<and> setr x \<subseteq> Collect (case_prod S)} (map_sum snd snd)"
    94     map_sum fst fst z = x \<and> map_sum snd snd z = y)"
    88   unfolding sum_set_defs Grp_def relcompp.simps conversep.simps fun_eq_iff
    95   unfolding sum_set_defs relcompp.simps conversep.simps fun_eq_iff
    89   by (fastforce elim: rel_sum.cases split: sum.splits)
    96   by (fastforce elim: rel_sum.cases split: sum.splits)
    90 qed (auto simp: sum_set_defs)
    97 qed (auto simp: sum_set_defs fun_eq_iff pred_sum.simps split: sum.splits)
    91 
    98 
    92 inductive_set fsts :: "'a \<times> 'b \<Rightarrow> 'a set" for p :: "'a \<times> 'b" where
    99 inductive_set fsts :: "'a \<times> 'b \<Rightarrow> 'a set" for p :: "'a \<times> 'b" where
    93   "fst p \<in> fsts p"
   100   "fst p \<in> fsts p"
    94 inductive_set snds :: "'a \<times> 'b \<Rightarrow> 'b set" for p :: "'a \<times> 'b" where
   101 inductive_set snds :: "'a \<times> 'b \<Rightarrow> 'b set" for p :: "'a \<times> 'b" where
    95   "snd p \<in> snds p"
   102   "snd p \<in> snds p"
   100 inductive
   107 inductive
   101   rel_prod :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('c \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> 'a \<times> 'c \<Rightarrow> 'b \<times> 'd \<Rightarrow> bool" for R1 R2
   108   rel_prod :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> ('c \<Rightarrow> 'd \<Rightarrow> bool) \<Rightarrow> 'a \<times> 'c \<Rightarrow> 'b \<times> 'd \<Rightarrow> bool" for R1 R2
   102 where
   109 where
   103   "\<lbrakk>R1 a b; R2 c d\<rbrakk> \<Longrightarrow> rel_prod R1 R2 (a, c) (b, d)"
   110   "\<lbrakk>R1 a b; R2 c d\<rbrakk> \<Longrightarrow> rel_prod R1 R2 (a, c) (b, d)"
   104 
   111 
       
   112 inductive
       
   113   pred_prod :: "('a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> 'a \<times> 'b \<Rightarrow> bool" for P1 P2
       
   114 where
       
   115   "\<lbrakk>P1 a; P2 b\<rbrakk> \<Longrightarrow> pred_prod P1 P2 (a, b)"
       
   116 
   105 lemma rel_prod_apply [code, simp]:
   117 lemma rel_prod_apply [code, simp]:
   106   "rel_prod R1 R2 (a, b) (c, d) \<longleftrightarrow> R1 a c \<and> R2 b d"
   118   "rel_prod R1 R2 (a, b) (c, d) \<longleftrightarrow> R1 a c \<and> R2 b d"
   107   by (auto intro: rel_prod.intros elim: rel_prod.cases)
   119   by (auto intro: rel_prod.intros elim: rel_prod.cases)
   108 
   120 
       
   121 lemma pred_prod_apply [code, simp]:
       
   122   "pred_prod P1 P2 (a, b) \<longleftrightarrow> P1 a \<and> P2 b"
       
   123   by (auto intro: pred_prod.intros elim: pred_prod.cases)
       
   124 
   109 lemma rel_prod_conv:
   125 lemma rel_prod_conv:
   110   "rel_prod R1 R2 = (\<lambda>(a, b) (c, d). R1 a c \<and> R2 b d)"
   126   "rel_prod R1 R2 = (\<lambda>(a, b) (c, d). R1 a c \<and> R2 b d)"
   111   by (rule ext, rule ext) auto
   127   by (rule ext, rule ext) auto
       
   128 
       
   129 definition
       
   130   pred_fun :: "('a \<Rightarrow> bool) \<Rightarrow> ('b \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> bool"
       
   131 where
       
   132   "pred_fun A B = (\<lambda>f. \<forall>x. A x \<longrightarrow> B (f x))"
       
   133 
       
   134 lemma pred_funI: "(\<And>x. A x \<Longrightarrow> B (f x)) \<Longrightarrow> pred_fun A B f"
       
   135   unfolding pred_fun_def by simp
   112 
   136 
   113 bnf "'a \<times> 'b"
   137 bnf "'a \<times> 'b"
   114   map: map_prod
   138   map: map_prod
   115   sets: fsts snds
   139   sets: fsts snds
   116   bd: natLeq
   140   bd: natLeq
   117   rel: rel_prod
   141   rel: rel_prod
       
   142   pred: pred_prod
   118 proof (unfold prod_set_defs)
   143 proof (unfold prod_set_defs)
   119   show "map_prod id id = id" by (rule map_prod.id)
   144   show "map_prod id id = id" by (rule map_prod.id)
   120 next
   145 next
   121   fix f1 f2 g1 g2
   146   fix f1 f2 g1 g2
   122   show "map_prod (g1 o f1) (g2 o f2) = map_prod g1 g2 o map_prod f1 f2"
   147   show "map_prod (g1 o f1) (g2 o f2) = map_prod g1 g2 o map_prod f1 f2"
   148 next
   173 next
   149   fix R1 R2 S1 S2
   174   fix R1 R2 S1 S2
   150   show "rel_prod R1 R2 OO rel_prod S1 S2 \<le> rel_prod (R1 OO S1) (R2 OO S2)" by auto
   175   show "rel_prod R1 R2 OO rel_prod S1 S2 \<le> rel_prod (R1 OO S1) (R2 OO S2)" by auto
   151 next
   176 next
   152   fix R S
   177   fix R S
   153   show "rel_prod R S =
   178   show "rel_prod R S = (\<lambda>x y.
   154         (Grp {x. {fst x} \<subseteq> Collect (case_prod R) \<and> {snd x} \<subseteq> Collect (case_prod S)} (map_prod fst fst))\<inverse>\<inverse> OO
   179     \<exists>z. ({fst z} \<subseteq> {(x, y). R x y} \<and> {snd z} \<subseteq> {(x, y). S x y}) \<and>
   155         Grp {x. {fst x} \<subseteq> Collect (case_prod R) \<and> {snd x} \<subseteq> Collect (case_prod S)} (map_prod snd snd)"
   180       map_prod fst fst z = x \<and> map_prod snd snd z = y)"
   156   unfolding prod_set_defs rel_prod_apply Grp_def relcompp.simps conversep.simps fun_eq_iff
   181   unfolding prod_set_defs rel_prod_apply relcompp.simps conversep.simps fun_eq_iff
   157   by auto
   182   by auto
   158 qed
   183 qed auto
   159 
   184 
   160 bnf "'a \<Rightarrow> 'b"
   185 bnf "'a \<Rightarrow> 'b"
   161   map: "op \<circ>"
   186   map: "op \<circ>"
   162   sets: range
   187   sets: range
   163   bd: "natLeq +c |UNIV :: 'a set|"
   188   bd: "natLeq +c |UNIV :: 'a set|"
   164   rel: "rel_fun op ="
   189   rel: "rel_fun op ="
       
   190   pred: "pred_fun (\<lambda>_. True)"
   165 proof
   191 proof
   166   fix f show "id \<circ> f = id f" by simp
   192   fix f show "id \<circ> f = id f" by simp
   167 next
   193 next
   168   fix f g show "op \<circ> (g \<circ> f) = op \<circ> g \<circ> op \<circ> f"
   194   fix f g show "op \<circ> (g \<circ> f) = op \<circ> g \<circ> op \<circ> f"
   169   unfolding comp_def[abs_def] ..
   195   unfolding comp_def[abs_def] ..
   192 next
   218 next
   193   fix R S
   219   fix R S
   194   show "rel_fun op = R OO rel_fun op = S \<le> rel_fun op = (R OO S)" by (auto simp: rel_fun_def)
   220   show "rel_fun op = R OO rel_fun op = S \<le> rel_fun op = (R OO S)" by (auto simp: rel_fun_def)
   195 next
   221 next
   196   fix R
   222   fix R
   197   show "rel_fun op = R =
   223   show "rel_fun op = R = (\<lambda>x y.
   198         (Grp {x. range x \<subseteq> Collect (case_prod R)} (op \<circ> fst))\<inverse>\<inverse> OO
   224     \<exists>z. range z \<subseteq> {(x, y). R x y} \<and> fst \<circ> z = x \<and> snd \<circ> z = y)"
   199          Grp {x. range x \<subseteq> Collect (case_prod R)} (op \<circ> snd)"
   225   unfolding rel_fun_def subset_iff by (force simp: fun_eq_iff[symmetric])
   200   unfolding rel_fun_def Grp_def fun_eq_iff relcompp.simps conversep.simps subset_iff image_iff
   226 qed (auto simp: pred_fun_def)
   201     comp_apply mem_Collect_eq split_beta bex_UNIV
       
   202   proof (safe, unfold fun_eq_iff[symmetric])
       
   203     fix x xa a b c xb y aa ba
       
   204     assume *: "x = a" "xa = c" "a = ba" "b = aa" "c = (\<lambda>x. snd (b x))" "ba = (\<lambda>x. fst (aa x))" and
       
   205        **: "\<forall>t. (\<exists>x. t = aa x) \<longrightarrow> R (fst t) (snd t)"
       
   206     show "R (x y) (xa y)" unfolding * by (rule mp[OF spec[OF **]]) blast
       
   207   qed force
       
   208 qed
       
   209 
   227 
   210 end
   228 end