src/HOL/Isar_examples/NestedDatatype.thy
 author wenzelm Thu Dec 22 00:28:43 2005 +0100 (2005-12-22) changeset 18460 9a1458cb2956 parent 18153 a084aa91f701 child 23373 ead82c82da9e permissions -rw-r--r--
tuned induct proofs;
```     1
```
```     2 (* \$Id\$ *)
```
```     3
```
```     4 header {* Nested datatypes *}
```
```     5
```
```     6 theory NestedDatatype imports Main begin
```
```     7
```
```     8 subsection {* Terms and substitution *}
```
```     9
```
```    10 datatype ('a, 'b) "term" =
```
```    11     Var 'a
```
```    12   | App 'b "('a, 'b) term list"
```
```    13
```
```    14 consts
```
```    15   subst_term :: "('a => ('a, 'b) term) => ('a, 'b) term => ('a, 'b) term"
```
```    16   subst_term_list ::
```
```    17     "('a => ('a, 'b) term) => ('a, 'b) term list => ('a, 'b) term list"
```
```    18
```
```    19 primrec (subst)
```
```    20   "subst_term f (Var a) = f a"
```
```    21   "subst_term f (App b ts) = App b (subst_term_list f ts)"
```
```    22   "subst_term_list f [] = []"
```
```    23   "subst_term_list f (t # ts) = subst_term f t # subst_term_list f ts"
```
```    24
```
```    25
```
```    26 text {*
```
```    27  \medskip A simple lemma about composition of substitutions.
```
```    28 *}
```
```    29
```
```    30 lemma "subst_term (subst_term f1 o f2) t =
```
```    31       subst_term f1 (subst_term f2 t)"
```
```    32   and "subst_term_list (subst_term f1 o f2) ts =
```
```    33       subst_term_list f1 (subst_term_list f2 ts)"
```
```    34   by (induct t and ts) simp_all
```
```    35
```
```    36 lemma "subst_term (subst_term f1 o f2) t =
```
```    37   subst_term f1 (subst_term f2 t)"
```
```    38 proof -
```
```    39   let "?P t" = ?thesis
```
```    40   let ?Q = "\<lambda>ts. subst_term_list (subst_term f1 o f2) ts =
```
```    41     subst_term_list f1 (subst_term_list f2 ts)"
```
```    42   show ?thesis
```
```    43   proof (induct t)
```
```    44     fix a show "?P (Var a)" by simp
```
```    45   next
```
```    46     fix b ts assume "?Q ts"
```
```    47     thus "?P (App b ts)" by (simp add: o_def)
```
```    48   next
```
```    49     show "?Q []" by simp
```
```    50   next
```
```    51     fix t ts
```
```    52     assume "?P t" "?Q ts" thus "?Q (t # ts)" by simp
```
```    53   qed
```
```    54 qed
```
```    55
```
```    56
```
```    57 subsection {* Alternative induction *}
```
```    58
```
```    59 theorem term_induct' [case_names Var App]:
```
```    60   assumes var: "!!a. P (Var a)"
```
```    61     and app: "!!b ts. list_all P ts ==> P (App b ts)"
```
```    62   shows "P t"
```
```    63 proof (induct t)
```
```    64   fix a show "P (Var a)" by (rule var)
```
```    65 next
```
```    66   fix b t ts assume "list_all P ts"
```
```    67   thus "P (App b ts)" by (rule app)
```
```    68 next
```
```    69   show "list_all P []" by simp
```
```    70 next
```
```    71   fix t ts assume "P t" "list_all P ts"
```
```    72   thus "list_all P (t # ts)" by simp
```
```    73 qed
```
```    74
```
```    75 lemma
```
```    76   "subst_term (subst_term f1 o f2) t = subst_term f1 (subst_term f2 t)"
```
```    77 proof (induct t rule: term_induct')
```
```    78   case (Var a)
```
```    79   show ?case by (simp add: o_def)
```
```    80 next
```
```    81   case (App b ts)
```
```    82   thus ?case by (induct ts) simp_all
```
```    83 qed
```
```    84
```
```    85 end
```