src/HOL/Isar_Examples/Nested_Datatype.thy
author haftmann
Wed Jun 30 16:46:44 2010 +0200 (2010-06-30)
changeset 37659 14cabf5fa710
parent 37597 a02ea93e88c6
child 37671 fa53d267dab3
permissions -rw-r--r--
more speaking names
     1 header {* Nested datatypes *}
     2 
     3 theory Nested_Datatype
     4 imports Main
     5 begin
     6 
     7 subsection {* Terms and substitution *}
     8 
     9 datatype ('a, 'b) "term" =
    10     Var 'a
    11   | App 'b "('a, 'b) term list"
    12 
    13 primrec subst_term :: "('a => ('a, 'b) term) => ('a, 'b) term => ('a, 'b) term" and
    14   subst_term_list :: "('a => ('a, 'b) term) => ('a, 'b) term list => ('a, 'b) term list" where
    15   "subst_term f (Var a) = f a"
    16 | "subst_term f (App b ts) = App b (subst_term_list f ts)"
    17 | "subst_term_list f [] = []"
    18 | "subst_term_list f (t # ts) = subst_term f t # subst_term_list f ts"
    19 
    20 lemmas subst_simps = subst_term_subst_term_list.simps
    21 
    22 text {*
    23  \medskip A simple lemma about composition of substitutions.
    24 *}
    25 
    26 lemma "subst_term (subst_term f1 o f2) t =
    27       subst_term f1 (subst_term f2 t)"
    28   and "subst_term_list (subst_term f1 o f2) ts =
    29       subst_term_list f1 (subst_term_list f2 ts)"
    30   by (induct t and ts) simp_all
    31 
    32 lemma "subst_term (subst_term f1 o f2) t =
    33   subst_term f1 (subst_term f2 t)"
    34 proof -
    35   let "?P t" = ?thesis
    36   let ?Q = "\<lambda>ts. subst_term_list (subst_term f1 o f2) ts =
    37     subst_term_list f1 (subst_term_list f2 ts)"
    38   show ?thesis
    39   proof (induct t)
    40     fix a show "?P (Var a)" by simp
    41   next
    42     fix b ts assume "?Q ts"
    43     then show "?P (App b ts)"
    44       by (simp only: subst_simps)
    45   next
    46     show "?Q []" by simp
    47   next
    48     fix t ts
    49     assume "?P t" "?Q ts" then show "?Q (t # ts)"
    50       by (simp only: subst_simps)
    51   qed
    52 qed
    53 
    54 
    55 subsection {* Alternative induction *}
    56 
    57 theorem term_induct' [case_names Var App]:
    58   assumes var: "!!a. P (Var a)"
    59     and app: "!!b ts. (\<forall>t \<in> set ts. P t) ==> P (App b ts)"
    60   shows "P t"
    61 proof (induct t)
    62   fix a show "P (Var a)" by (rule var)
    63 next
    64   fix b t ts assume "\<forall>t \<in> set ts. P t"
    65   then show "P (App b ts)" by (rule app)
    66 next
    67   show "\<forall>t \<in> set []. P t" by simp
    68 next
    69   fix t ts assume "P t" "\<forall>t' \<in> set ts. P t'"
    70   then show "\<forall>t' \<in> set (t # ts). P t'" by simp
    71 qed
    72 
    73 lemma
    74   "subst_term (subst_term f1 o f2) t = subst_term f1 (subst_term f2 t)"
    75 proof (induct t rule: term_induct')
    76   case (Var a)
    77   show ?case by (simp add: o_def)
    78 next
    79   case (App b ts)
    80   then show ?case by (induct ts) simp_all
    81 qed
    82 
    83 end