src/HOL/BNF/Examples/Derivation_Trees/Gram_Lang.thy
changeset 49878 8ce596cae2a3
parent 49877 b75555ec30a4
child 49879 5e323695f26e
equal deleted inserted replaced
49877:b75555ec30a4 49878:8ce596cae2a3
    11 imports DTree
    11 imports DTree
    12 begin
    12 begin
    13 
    13 
    14 
    14 
    15 (* We assume that the sets of terminals, and the left-hand sides of 
    15 (* We assume that the sets of terminals, and the left-hand sides of 
    16 productions are finite and that the grammar has no unused nonterminals. *)
    16 productions are finite and that the grammar has no unused nonterminals. *)  
    17 consts P :: "(N \<times> (T + N) set) set"
    17 consts P :: "(N \<times> (T + N) set) set"
    18 axiomatization where
    18 axiomatization where
    19     finite_N: "finite (UNIV::N set)"
    19     finite_N: "finite (UNIV::N set)"
    20 and finite_in_P: "\<And> n tns. (n,tns) \<in> P \<longrightarrow> finite tns"
    20 and finite_in_P: "\<And> n tns. (n,tns) \<in> P \<longrightarrow> finite tns"
    21 and used: "\<And> n. \<exists> tns. (n,tns) \<in> P"
    21 and used: "\<And> n. \<exists> tns. (n,tns) \<in> P"
   938 lemma wf_pick:
   938 lemma wf_pick:
   939 assumes tr0: "wf tr0" and n: "inItr UNIV tr0 n"
   939 assumes tr0: "wf tr0" and n: "inItr UNIV tr0 n"
   940 shows "wf (pick n)"
   940 shows "wf (pick n)"
   941 using wf_subtr[OF tr0 subtr_pick[OF n]] .
   941 using wf_subtr[OF tr0 subtr_pick[OF n]] .
   942 
   942 
   943 definition "regOf_r n \<equiv> root (pick n)"
   943 definition "H_r n \<equiv> root (pick n)"
   944 definition "regOf_c n \<equiv> (id \<oplus> root) ` cont (pick n)"
   944 definition "H_c n \<equiv> (id \<oplus> root) ` cont (pick n)"
   945 
   945 
   946 (* The regular tree of a function: *)
   946 (* The regular tree of a function: *)
   947 definition regOf :: "N \<Rightarrow> dtree" where
   947 definition H :: "N \<Rightarrow> dtree" where
   948 "regOf \<equiv> unfold regOf_r regOf_c"
   948 "H \<equiv> unfold H_r H_c"
   949 
   949 
   950 lemma finite_regOf_c: "finite (regOf_c n)"
   950 lemma finite_H_c: "finite (H_c n)"
   951 unfolding regOf_c_def by (metis finite_cont finite_imageI)
   951 unfolding H_c_def by (metis finite_cont finite_imageI)
   952 
   952 
   953 lemma root_regOf_pick: "root (regOf n) = root (pick n)"
   953 lemma root_H_pick: "root (H n) = root (pick n)"
   954 using unfold(1)[of regOf_r regOf_c n] unfolding regOf_def regOf_r_def by simp
   954 using unfold(1)[of H_r H_c n] unfolding H_def H_r_def by simp
   955 
   955 
   956 lemma root_regOf[simp]:
   956 lemma root_H[simp]:
   957 assumes "inItr UNIV tr0 n"
   957 assumes "inItr UNIV tr0 n"
   958 shows "root (regOf n) = n"
   958 shows "root (H n) = n"
   959 unfolding root_regOf_pick root_pick[OF assms] ..
   959 unfolding root_H_pick root_pick[OF assms] ..
   960 
   960 
   961 lemma cont_regOf[simp]:
   961 lemma cont_H[simp]:
   962 "cont (regOf n) = (id \<oplus> (regOf o root)) ` cont (pick n)"
   962 "cont (H n) = (id \<oplus> (H o root)) ` cont (pick n)"
   963 apply(subst id_o[symmetric, of id]) unfolding sum_map.comp[symmetric]
   963 apply(subst id_o[symmetric, of id]) unfolding sum_map.comp[symmetric]
   964 unfolding image_compose unfolding regOf_c_def[symmetric]
   964 unfolding image_compose unfolding H_c_def[symmetric]
   965 using unfold(2)[of regOf_c n regOf_r, OF finite_regOf_c]
   965 using unfold(2)[of H_c n H_r, OF finite_H_c]
   966 unfolding regOf_def ..
   966 unfolding H_def ..
   967 
   967 
   968 lemma Inl_cont_regOf[simp]:
   968 lemma Inl_cont_H[simp]:
   969 "Inl -` (cont (regOf n)) = Inl -` (cont (pick n))"
   969 "Inl -` (cont (H n)) = Inl -` (cont (pick n))"
   970 unfolding cont_regOf by simp
   970 unfolding cont_H by simp
   971 
   971 
   972 lemma Inr_cont_regOf:
   972 lemma Inr_cont_H:
   973 "Inr -` (cont (regOf n)) = (regOf \<circ> root) ` (Inr -` cont (pick n))"
   973 "Inr -` (cont (H n)) = (H \<circ> root) ` (Inr -` cont (pick n))"
   974 unfolding cont_regOf by simp
   974 unfolding cont_H by simp
   975 
   975 
   976 lemma subtr_regOf:
   976 lemma subtr_H:
   977 assumes n: "inItr UNIV tr0 n" and "subtr UNIV tr1 (regOf n)"
   977 assumes n: "inItr UNIV tr0 n" and "subtr UNIV tr1 (H n)"
   978 shows "\<exists> n1. inItr UNIV tr0 n1 \<and> tr1 = regOf n1"
   978 shows "\<exists> n1. inItr UNIV tr0 n1 \<and> tr1 = H n1"
   979 proof-
   979 proof-
   980   {fix tr ns assume "subtr UNIV tr1 tr"
   980   {fix tr ns assume "subtr UNIV tr1 tr"
   981    hence "tr = regOf n \<longrightarrow> (\<exists> n1. inItr UNIV tr0 n1 \<and> tr1 = regOf n1)"
   981    hence "tr = H n \<longrightarrow> (\<exists> n1. inItr UNIV tr0 n1 \<and> tr1 = H n1)"
   982    proof (induct rule: subtr_UNIV_inductL)
   982    proof (induct rule: subtr_UNIV_inductL)
   983      case (Step tr2 tr1 tr)
   983      case (Step tr2 tr1 tr)
   984      show ?case proof
   984      show ?case proof
   985        assume "tr = regOf n"
   985        assume "tr = H n"
   986        then obtain n1 where tr2: "Inr tr2 \<in> cont tr1"
   986        then obtain n1 where tr2: "Inr tr2 \<in> cont tr1"
   987        and tr1_tr: "subtr UNIV tr1 tr" and n1: "inItr UNIV tr0 n1" and tr1: "tr1 = regOf n1"
   987        and tr1_tr: "subtr UNIV tr1 tr" and n1: "inItr UNIV tr0 n1" and tr1: "tr1 = H n1"
   988        using Step by auto
   988        using Step by auto
   989        obtain tr2' where tr2: "tr2 = regOf (root tr2')"
   989        obtain tr2' where tr2: "tr2 = H (root tr2')"
   990        and tr2': "Inr tr2' \<in> cont (pick n1)"
   990        and tr2': "Inr tr2' \<in> cont (pick n1)"
   991        using tr2 Inr_cont_regOf[of n1]
   991        using tr2 Inr_cont_H[of n1]
   992        unfolding tr1 image_def o_def using vimage_eq by auto
   992        unfolding tr1 image_def o_def using vimage_eq by auto
   993        have "inItr UNIV tr0 (root tr2')"
   993        have "inItr UNIV tr0 (root tr2')"
   994        using inItr.Base inItr.Ind n1 pick subtr_inItr tr2' by (metis iso_tuple_UNIV_I)
   994        using inItr.Base inItr.Ind n1 pick subtr_inItr tr2' by (metis iso_tuple_UNIV_I)
   995        thus "\<exists>n2. inItr UNIV tr0 n2 \<and> tr2 = regOf n2" using tr2 by blast
   995        thus "\<exists>n2. inItr UNIV tr0 n2 \<and> tr2 = H n2" using tr2 by blast
   996      qed
   996      qed
   997    qed(insert n, auto)
   997    qed(insert n, auto)
   998   }
   998   }
   999   thus ?thesis using assms by auto
   999   thus ?thesis using assms by auto
  1000 qed
  1000 qed
  1001 
  1001 
  1002 lemma root_regOf_root:
  1002 lemma root_H_root:
  1003 assumes n: "inItr UNIV tr0 n" and t_tr: "t_tr \<in> cont (pick n)"
  1003 assumes n: "inItr UNIV tr0 n" and t_tr: "t_tr \<in> cont (pick n)"
  1004 shows "(id \<oplus> (root \<circ> regOf \<circ> root)) t_tr = (id \<oplus> root) t_tr"
  1004 shows "(id \<oplus> (root \<circ> H \<circ> root)) t_tr = (id \<oplus> root) t_tr"
  1005 using assms apply(cases t_tr)
  1005 using assms apply(cases t_tr)
  1006   apply (metis (lifting) sum_map.simps(1))
  1006   apply (metis (lifting) sum_map.simps(1))
  1007   using pick regOf_def regOf_r_def unfold(1)
  1007   using pick H_def H_r_def unfold(1)
  1008       inItr.Base o_apply subtr_StepL subtr_inItr sum_map.simps(2)
  1008       inItr.Base o_apply subtr_StepL subtr_inItr sum_map.simps(2)
  1009   by (metis UNIV_I)
  1009   by (metis UNIV_I)
  1010 
  1010 
  1011 lemma regOf_P:
  1011 lemma H_P:
  1012 assumes tr0: "wf tr0" and n: "inItr UNIV tr0 n"
  1012 assumes tr0: "wf tr0" and n: "inItr UNIV tr0 n"
  1013 shows "(n, (id \<oplus> root) ` cont (regOf n)) \<in> P" (is "?L \<in> P")
  1013 shows "(n, (id \<oplus> root) ` cont (H n)) \<in> P" (is "?L \<in> P")
  1014 proof-
  1014 proof-
  1015   have "?L = (n, (id \<oplus> root) ` cont (pick n))"
  1015   have "?L = (n, (id \<oplus> root) ` cont (pick n))"
  1016   unfolding cont_regOf image_compose[symmetric] sum_map.comp id_o o_assoc
  1016   unfolding cont_H image_compose[symmetric] sum_map.comp id_o o_assoc
  1017   unfolding Pair_eq apply(rule conjI[OF refl]) apply(rule image_cong[OF refl])
  1017   unfolding Pair_eq apply(rule conjI[OF refl]) apply(rule image_cong[OF refl])
  1018   by(rule root_regOf_root[OF n])
  1018   by(rule root_H_root[OF n])
  1019   moreover have "... \<in> P" by (metis (lifting) wf_pick root_pick wf_P n tr0)
  1019   moreover have "... \<in> P" by (metis (lifting) wf_pick root_pick wf_P n tr0)
  1020   ultimately show ?thesis by simp
  1020   ultimately show ?thesis by simp
  1021 qed
  1021 qed
  1022 
  1022 
  1023 lemma wf_regOf:
  1023 lemma wf_H:
  1024 assumes tr0: "wf tr0" and "inItr UNIV tr0 n"
  1024 assumes tr0: "wf tr0" and "inItr UNIV tr0 n"
  1025 shows "wf (regOf n)"
  1025 shows "wf (H n)"
  1026 proof-
  1026 proof-
  1027   {fix tr have "\<exists> n. inItr UNIV tr0 n \<and> tr = regOf n \<Longrightarrow> wf tr"
  1027   {fix tr have "\<exists> n. inItr UNIV tr0 n \<and> tr = H n \<Longrightarrow> wf tr"
  1028    proof (induct rule: wf_raw_coind)
  1028    proof (induct rule: wf_raw_coind)
  1029      case (Hyp tr)
  1029      case (Hyp tr)
  1030      then obtain n where n: "inItr UNIV tr0 n" and tr: "tr = regOf n" by auto
  1030      then obtain n where n: "inItr UNIV tr0 n" and tr: "tr = H n" by auto
  1031      show ?case apply safe
  1031      show ?case apply safe
  1032      apply (metis (lifting) regOf_P root_regOf n tr tr0)
  1032      apply (metis (lifting) H_P root_H n tr tr0)
  1033      unfolding tr Inr_cont_regOf unfolding inj_on_def apply clarsimp using root_regOf
  1033      unfolding tr Inr_cont_H unfolding inj_on_def apply clarsimp using root_H
  1034      apply (metis UNIV_I inItr.Base n pick subtr2.simps subtr_inItr subtr_subtr2)
  1034      apply (metis UNIV_I inItr.Base n pick subtr2.simps subtr_inItr subtr_subtr2)
  1035      by (metis n subtr.Refl subtr_StepL subtr_regOf tr UNIV_I)
  1035      by (metis n subtr.Refl subtr_StepL subtr_H tr UNIV_I)
  1036    qed
  1036    qed
  1037   }
  1037   }
  1038   thus ?thesis using assms by blast
  1038   thus ?thesis using assms by blast
  1039 qed
  1039 qed
  1040 
  1040 
  1041 (* The regular cut of a tree: *)
  1041 (* The regular cut of a tree: *)
  1042 definition "rcut \<equiv> regOf (root tr0)"
  1042 definition "rcut \<equiv> H (root tr0)"
  1043 
  1043 
  1044 lemma reg_rcut: "reg regOf rcut"
  1044 lemma reg_rcut: "reg H rcut"
  1045 unfolding reg_def rcut_def
  1045 unfolding reg_def rcut_def
  1046 by (metis inItr.Base root_regOf subtr_regOf UNIV_I)
  1046 by (metis inItr.Base root_H subtr_H UNIV_I)
  1047 
  1047 
  1048 lemma rcut_reg:
  1048 lemma rcut_reg:
  1049 assumes "reg regOf tr0"
  1049 assumes "reg H tr0"
  1050 shows "rcut = tr0"
  1050 shows "rcut = tr0"
  1051 using assms unfolding rcut_def reg_def by (metis subtr.Refl UNIV_I)
  1051 using assms unfolding rcut_def reg_def by (metis subtr.Refl UNIV_I)
  1052 
  1052 
  1053 lemma rcut_eq: "rcut = tr0 \<longleftrightarrow> reg regOf tr0"
  1053 lemma rcut_eq: "rcut = tr0 \<longleftrightarrow> reg H tr0"
  1054 using reg_rcut rcut_reg by metis
  1054 using reg_rcut rcut_reg by metis
  1055 
  1055 
  1056 lemma regular_rcut: "regular rcut"
  1056 lemma regular_rcut: "regular rcut"
  1057 using reg_rcut unfolding regular_def by blast
  1057 using reg_rcut unfolding regular_def by blast
  1058 
  1058 
  1059 lemma Fr_rcut: "Fr UNIV rcut \<subseteq> Fr UNIV tr0"
  1059 lemma Fr_rcut: "Fr UNIV rcut \<subseteq> Fr UNIV tr0"
  1060 proof safe
  1060 proof safe
  1061   fix t assume "t \<in> Fr UNIV rcut"
  1061   fix t assume "t \<in> Fr UNIV rcut"
  1062   then obtain tr where t: "Inl t \<in> cont tr" and tr: "subtr UNIV tr (regOf (root tr0))"
  1062   then obtain tr where t: "Inl t \<in> cont tr" and tr: "subtr UNIV tr (H (root tr0))"
  1063   using Fr_subtr[of UNIV "regOf (root tr0)"] unfolding rcut_def
  1063   using Fr_subtr[of UNIV "H (root tr0)"] unfolding rcut_def
  1064   by (metis (full_types) Fr_def inFr_subtr mem_Collect_eq)
  1064   by (metis (full_types) Fr_def inFr_subtr mem_Collect_eq)
  1065   obtain n where n: "inItr UNIV tr0 n" and tr: "tr = regOf n" using tr
  1065   obtain n where n: "inItr UNIV tr0 n" and tr: "tr = H n" using tr
  1066   by (metis (lifting) inItr.Base subtr_regOf UNIV_I)
  1066   by (metis (lifting) inItr.Base subtr_H UNIV_I)
  1067   have "Inl t \<in> cont (pick n)" using t using Inl_cont_regOf[of n] unfolding tr
  1067   have "Inl t \<in> cont (pick n)" using t using Inl_cont_H[of n] unfolding tr
  1068   by (metis (lifting) vimageD vimageI2)
  1068   by (metis (lifting) vimageD vimageI2)
  1069   moreover have "subtr UNIV (pick n) tr0" using subtr_pick[OF n] ..
  1069   moreover have "subtr UNIV (pick n) tr0" using subtr_pick[OF n] ..
  1070   ultimately show "t \<in> Fr UNIV tr0" unfolding Fr_subtr_cont by auto
  1070   ultimately show "t \<in> Fr UNIV tr0" unfolding Fr_subtr_cont by auto
  1071 qed
  1071 qed
  1072 
  1072 
  1073 lemma wf_rcut:
  1073 lemma wf_rcut:
  1074 assumes "wf tr0"
  1074 assumes "wf tr0"
  1075 shows "wf rcut"
  1075 shows "wf rcut"
  1076 unfolding rcut_def using wf_regOf[OF assms inItr.Base] by simp
  1076 unfolding rcut_def using wf_H[OF assms inItr.Base] by simp
  1077 
  1077 
  1078 lemma root_rcut[simp]: "root rcut = root tr0"
  1078 lemma root_rcut[simp]: "root rcut = root tr0"
  1079 unfolding rcut_def
  1079 unfolding rcut_def
  1080 by (metis (lifting) root_regOf inItr.Base reg_def reg_root subtr_rootR_in)
  1080 by (metis (lifting) root_H inItr.Base reg_def reg_root subtr_rootR_in)
  1081 
  1081 
  1082 end (* context *)
  1082 end (* context *)
  1083 
  1083 
  1084 
  1084 
  1085 subsection{* Recursive description of the regular tree frontiers *}
  1085 subsection{* Recursive description of the regular tree frontiers *}