src/HOL/Codatatype/Examples/Lambda_Term.thy
changeset 49220 a6260b4fb410
parent 49128 1a86ef0a0210
child 49222 cbe8c859817c
equal deleted inserted replaced
49219:c28dd8326f9a 49220:a6260b4fb410
    20 
    20 
    21 section {* Customization of terms *}
    21 section {* Customization of terms *}
    22 
    22 
    23 subsection{* Set and map *}
    23 subsection{* Set and map *}
    24 
    24 
    25 lemma trmBNF_set2_Lt: "trmBNF_set2 (Inr (Inr (Inr (xts, t)))) = snd ` (fset xts) \<union> {t}"
    25 lemma pre_trm_set2_Lt: "pre_trm_set2 (Inr (Inr (Inr (xts, t)))) = snd ` (fset xts) \<union> {t}"
    26 unfolding trmBNF_set2_def sum_set_defs prod_set_defs collect_def[abs_def]
    26 unfolding pre_trm_set2_def sum_set_defs prod_set_defs collect_def[abs_def]
    27 by auto
    27 by auto
    28 
    28 
    29 lemma trmBNF_set2_Var: "\<And>x. trmBNF_set2 (Inl x) = {}"
    29 lemma pre_trm_set2_Var: "\<And>x. pre_trm_set2 (Inl x) = {}"
    30 and trmBNF_set2_App:
    30 and pre_trm_set2_App:
    31 "\<And>t1 t2. trmBNF_set2 (Inr (Inl t1t2)) = {fst t1t2, snd t1t2}"
    31 "\<And>t1 t2. pre_trm_set2 (Inr (Inl t1t2)) = {fst t1t2, snd t1t2}"
    32 and trmBNF_set2_Lam:
    32 and pre_trm_set2_Lam:
    33 "\<And>x t. trmBNF_set2 (Inr (Inr (Inl (x, t)))) = {t}"
    33 "\<And>x t. pre_trm_set2 (Inr (Inr (Inl (x, t)))) = {t}"
    34 unfolding trmBNF_set2_def sum_set_defs prod_set_defs collect_def[abs_def]
    34 unfolding pre_trm_set2_def sum_set_defs prod_set_defs collect_def[abs_def]
    35 by auto
    35 by auto
    36 
    36 
    37 lemma trmBNF_map:
    37 lemma pre_trm_map:
    38 "\<And> a1. trmBNF_map f1 f2 (Inl a1) = Inl (f1 a1)"
    38 "\<And> a1. pre_trm_map f1 f2 (Inl a1) = Inl (f1 a1)"
    39 "\<And> a2 b2. trmBNF_map f1 f2 (Inr (Inl (a2,b2))) = Inr (Inl (f2 a2, f2 b2))"
    39 "\<And> a2 b2. pre_trm_map f1 f2 (Inr (Inl (a2,b2))) = Inr (Inl (f2 a2, f2 b2))"
    40 "\<And> a1 a2. trmBNF_map f1 f2 (Inr (Inr (Inl (a1,a2)))) = Inr (Inr (Inl (f1 a1, f2 a2)))"
    40 "\<And> a1 a2. pre_trm_map f1 f2 (Inr (Inr (Inl (a1,a2)))) = Inr (Inr (Inl (f1 a1, f2 a2)))"
    41 "\<And> a1a2s a2.
    41 "\<And> a1a2s a2.
    42    trmBNF_map f1 f2 (Inr (Inr (Inr (a1a2s, a2)))) =
    42    pre_trm_map f1 f2 (Inr (Inr (Inr (a1a2s, a2)))) =
    43    Inr (Inr (Inr (map_fset (\<lambda> (a1', a2'). (f1 a1', f2 a2')) a1a2s, f2 a2)))"
    43    Inr (Inr (Inr (map_fset (\<lambda> (a1', a2'). (f1 a1', f2 a2')) a1a2s, f2 a2)))"
    44 unfolding trmBNF_map_def collect_def[abs_def] map_pair_def by auto
    44 unfolding pre_trm_map_def collect_def[abs_def] map_pair_def by auto
    45 
    45 
    46 
    46 
    47 subsection{* Constructors *}
    47 subsection{* Constructors *}
    48 
    48 
    49 definition "Var x \<equiv> trm_fld (Inl x)"
    49 definition "Var x \<equiv> trm_fld (Inl x)"
    85 and Lam: "\<And> x t. phi t \<Longrightarrow> phi (Lam x t)"
    85 and Lam: "\<And> x t. phi t \<Longrightarrow> phi (Lam x t)"
    86 and Lt: "\<And> xts t. \<lbrakk>\<And> x1 t1. (x1,t1) |\<in>| xts \<Longrightarrow> phi t1; phi t\<rbrakk> \<Longrightarrow> phi (Lt xts t)"
    86 and Lt: "\<And> xts t. \<lbrakk>\<And> x1 t1. (x1,t1) |\<in>| xts \<Longrightarrow> phi t1; phi t\<rbrakk> \<Longrightarrow> phi (Lt xts t)"
    87 shows "phi t"
    87 shows "phi t"
    88 proof(induct rule: trm.fld_induct)
    88 proof(induct rule: trm.fld_induct)
    89   fix u :: "'a + 'a trm \<times> 'a trm + 'a \<times> 'a trm + ('a \<times> 'a trm) fset \<times> 'a trm"
    89   fix u :: "'a + 'a trm \<times> 'a trm + 'a \<times> 'a trm + ('a \<times> 'a trm) fset \<times> 'a trm"
    90   assume IH: "\<And>t. t \<in> trmBNF_set2 u \<Longrightarrow> phi t"
    90   assume IH: "\<And>t. t \<in> pre_trm_set2 u \<Longrightarrow> phi t"
    91   show "phi (trm_fld u)"
    91   show "phi (trm_fld u)"
    92   proof(cases u)
    92   proof(cases u)
    93     case (Inl x)
    93     case (Inl x)
    94     show ?thesis using Var unfolding Var_def Inl .
    94     show ?thesis using Var unfolding Var_def Inl .
    95   next
    95   next
    97     show ?thesis
    97     show ?thesis
    98     proof(cases uu)
    98     proof(cases uu)
    99       case (Inl t1t2)
    99       case (Inl t1t2)
   100       obtain t1 t2 where t1t2: "t1t2 = (t1,t2)" by (cases t1t2, blast)
   100       obtain t1 t2 where t1t2: "t1t2 = (t1,t2)" by (cases t1t2, blast)
   101       show ?thesis unfolding Inr1 Inl t1t2 App_def[symmetric] apply(rule App)
   101       show ?thesis unfolding Inr1 Inl t1t2 App_def[symmetric] apply(rule App)
   102       using IH unfolding Inr1 Inl trmBNF_set2_App t1t2 fst_conv snd_conv by blast+
   102       using IH unfolding Inr1 Inl pre_trm_set2_App t1t2 fst_conv snd_conv by blast+
   103     next
   103     next
   104       case (Inr uuu) note Inr2 = Inr
   104       case (Inr uuu) note Inr2 = Inr
   105       show ?thesis
   105       show ?thesis
   106       proof(cases uuu)
   106       proof(cases uuu)
   107         case (Inl xt)
   107         case (Inl xt)
   108         obtain x t where xt: "xt = (x,t)" by (cases xt, blast)
   108         obtain x t where xt: "xt = (x,t)" by (cases xt, blast)
   109         show ?thesis unfolding Inr1 Inr2 Inl xt Lam_def[symmetric] apply(rule Lam)
   109         show ?thesis unfolding Inr1 Inr2 Inl xt Lam_def[symmetric] apply(rule Lam)
   110         using IH unfolding Inr1 Inr2 Inl trmBNF_set2_Lam xt by blast
   110         using IH unfolding Inr1 Inr2 Inl pre_trm_set2_Lam xt by blast
   111       next
   111       next
   112         case (Inr xts_t)
   112         case (Inr xts_t)
   113         obtain xts t where xts_t: "xts_t = (xts,t)" by (cases xts_t, blast)
   113         obtain xts t where xts_t: "xts_t = (xts,t)" by (cases xts_t, blast)
   114         show ?thesis unfolding Inr1 Inr2 Inr xts_t Lt_def[symmetric] apply(rule Lt) using IH
   114         show ?thesis unfolding Inr1 Inr2 Inr xts_t Lt_def[symmetric] apply(rule Lt) using IH
   115         unfolding Inr1 Inr2 Inr trmBNF_set2_Lt xts_t fset_fset_member image_def by auto
   115         unfolding Inr1 Inr2 Inr pre_trm_set2_Lt xts_t fset_fset_member image_def by auto
   116       qed
   116       qed
   117     qed
   117     qed
   118   qed
   118   qed
   119 qed
   119 qed
   120 
   120 
   139 
   139 
   140 definition "trmrec var app lam lt \<equiv> trm_fld_rec (sumJoin4 var app lam lt)"
   140 definition "trmrec var app lam lt \<equiv> trm_fld_rec (sumJoin4 var app lam lt)"
   141 
   141 
   142 lemma trmrec_Var[simp]:
   142 lemma trmrec_Var[simp]:
   143 "trmrec var app lam lt (Var x) = var x"
   143 "trmrec var app lam lt (Var x) = var x"
   144 unfolding trmrec_def Var_def trm.fld_rec trmBNF_map(1) by simp
   144 unfolding trmrec_def Var_def trm.fld_rec pre_trm_map(1) by simp
   145 
   145 
   146 lemma trmrec_App[simp]:
   146 lemma trmrec_App[simp]:
   147 "trmrec var app lam lt (App t1 t2) =
   147 "trmrec var app lam lt (App t1 t2) =
   148  app t1 (trmrec var app lam lt t1) t2 (trmrec var app lam lt t2)"
   148  app t1 (trmrec var app lam lt t1) t2 (trmrec var app lam lt t2)"
   149 unfolding trmrec_def App_def trm.fld_rec trmBNF_map(2) convol_def by simp
   149 unfolding trmrec_def App_def trm.fld_rec pre_trm_map(2) convol_def by simp
   150 
   150 
   151 lemma trmrec_Lam[simp]:
   151 lemma trmrec_Lam[simp]:
   152 "trmrec var app lam lt (Lam x t) = lam x t (trmrec var app lam lt t)"
   152 "trmrec var app lam lt (Lam x t) = lam x t (trmrec var app lam lt t)"
   153 unfolding trmrec_def Lam_def trm.fld_rec trmBNF_map(3) convol_def by simp
   153 unfolding trmrec_def Lam_def trm.fld_rec pre_trm_map(3) convol_def by simp
   154 
   154 
   155 lemma trmrec_Lt[simp]:
   155 lemma trmrec_Lt[simp]:
   156 "trmrec var app lam lt (Lt xts t) =
   156 "trmrec var app lam lt (Lt xts t) =
   157  lt (map_fset (\<lambda> (x,t). (x,t,trmrec var app lam lt t)) xts) t (trmrec var app lam lt t)"
   157  lt (map_fset (\<lambda> (x,t). (x,t,trmrec var app lam lt t)) xts) t (trmrec var app lam lt t)"
   158 unfolding trmrec_def Lt_def trm.fld_rec trmBNF_map(4) convol_def by simp
   158 unfolding trmrec_def Lt_def trm.fld_rec pre_trm_map(4) convol_def by simp
   159 
   159 
   160 definition
   160 definition
   161 "sumJoinI4 f1 f2 f3 f4 \<equiv>
   161 "sumJoinI4 f1 f2 f3 f4 \<equiv>
   162 \<lambda> k. (case k of
   162 \<lambda> k. (case k of
   163  Inl x1 \<Rightarrow> f1 x1
   163  Inl x1 \<Rightarrow> f1 x1
   176 (* The iterator has a simpler, hence more manageable type. *)
   176 (* The iterator has a simpler, hence more manageable type. *)
   177 definition "trmiter var app lam lt \<equiv> trm_fld_iter (sumJoinI4 var app lam lt)"
   177 definition "trmiter var app lam lt \<equiv> trm_fld_iter (sumJoinI4 var app lam lt)"
   178 
   178 
   179 lemma trmiter_Var[simp]:
   179 lemma trmiter_Var[simp]:
   180 "trmiter var app lam lt (Var x) = var x"
   180 "trmiter var app lam lt (Var x) = var x"
   181 unfolding trmiter_def Var_def trm.fld_iter trmBNF_map(1) by simp
   181 unfolding trmiter_def Var_def trm.fld_iter pre_trm_map(1) by simp
   182 
   182 
   183 lemma trmiter_App[simp]:
   183 lemma trmiter_App[simp]:
   184 "trmiter var app lam lt (App t1 t2) =
   184 "trmiter var app lam lt (App t1 t2) =
   185  app (trmiter var app lam lt t1) (trmiter var app lam lt t2)"
   185  app (trmiter var app lam lt t1) (trmiter var app lam lt t2)"
   186 unfolding trmiter_def App_def trm.fld_iter trmBNF_map(2) by simp
   186 unfolding trmiter_def App_def trm.fld_iter pre_trm_map(2) by simp
   187 
   187 
   188 lemma trmiter_Lam[simp]:
   188 lemma trmiter_Lam[simp]:
   189 "trmiter var app lam lt (Lam x t) = lam x (trmiter var app lam lt t)"
   189 "trmiter var app lam lt (Lam x t) = lam x (trmiter var app lam lt t)"
   190 unfolding trmiter_def Lam_def trm.fld_iter trmBNF_map(3) by simp
   190 unfolding trmiter_def Lam_def trm.fld_iter pre_trm_map(3) by simp
   191 
   191 
   192 lemma trmiter_Lt[simp]:
   192 lemma trmiter_Lt[simp]:
   193 "trmiter var app lam lt (Lt xts t) =
   193 "trmiter var app lam lt (Lt xts t) =
   194  lt (map_fset (\<lambda> (x,t). (x,trmiter var app lam lt t)) xts) (trmiter var app lam lt t)"
   194  lt (map_fset (\<lambda> (x,t). (x,trmiter var app lam lt t)) xts) (trmiter var app lam lt t)"
   195 unfolding trmiter_def Lt_def trm.fld_iter trmBNF_map(4) by simp
   195 unfolding trmiter_def Lt_def trm.fld_iter pre_trm_map(4) by simp
   196 
   196 
   197 
   197 
   198 subsection{* Example: The set of all variables varsOf and free variables fvarsOf of a term: *}
   198 subsection{* Example: The set of all variables varsOf and free variables fvarsOf of a term: *}
   199 
   199 
   200 definition "varsOf = trmiter
   200 definition "varsOf = trmiter