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