src/HOL/Induct/Sexp.ML
author wenzelm
Fri, 08 Mar 2002 16:24:06 +0100
changeset 13049 ce180e5b7fa0
parent 11481 c77e5401f2ff
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8840
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Sexp
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
     4
    Copyright   1994  University of Cambridge
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
     5
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
     6
S-expressions, general binary trees for defining recursive data structures
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
     7
*)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
     8
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
     9
(** sexp_case **)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    10
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    11
Goalw [sexp_case_def] "sexp_case c d e (Leaf a) = c(a)";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    12
by (Blast_tac 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    13
qed "sexp_case_Leaf";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    14
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    15
Goalw [sexp_case_def] "sexp_case c d e (Numb k) = d(k)";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    16
by (Blast_tac 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    17
qed "sexp_case_Numb";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    18
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    19
Goalw [sexp_case_def] "sexp_case c d e (Scons M N) = e M N";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    20
by (Blast_tac 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    21
qed "sexp_case_Scons";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    22
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    23
Addsimps [sexp_case_Leaf, sexp_case_Numb, sexp_case_Scons];
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    24
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    25
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    26
(** Introduction rules for sexp constructors **)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    27
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    28
val [prem] = goalw Sexp.thy [In0_def] "M: sexp ==> In0(M) : sexp";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    29
by (rtac (prem RS (sexp.NumbI RS sexp.SconsI)) 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    30
qed "sexp_In0I";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    31
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    32
val [prem] = goalw Sexp.thy [In1_def] "M: sexp ==> In1(M) : sexp";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    33
by (rtac (prem RS (sexp.NumbI RS sexp.SconsI)) 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    34
qed "sexp_In1I";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    35
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    36
AddIs sexp.intrs;
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    37
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    38
Goal "range(Leaf) <= sexp";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    39
by (Blast_tac 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    40
qed "range_Leaf_subset_sexp";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    41
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    42
val [major] = goal Sexp.thy "Scons M N : sexp ==> M: sexp & N: sexp";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    43
by (rtac (major RS setup_induction) 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    44
by (etac sexp.induct 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    45
by (ALLGOALS Blast_tac);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    46
qed "Scons_D";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    47
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    48
(** Introduction rules for 'pred_sexp' **)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    49
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    50
Goalw [pred_sexp_def] "pred_sexp <= sexp <*> sexp";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    51
by (Blast_tac 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    52
qed "pred_sexp_subset_Sigma";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    53
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    54
(* (a,b) : pred_sexp^+ ==> a : sexp *)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    55
val trancl_pred_sexpD1 = 
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    56
    pred_sexp_subset_Sigma RS trancl_subset_Sigma RS subsetD RS SigmaD1
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    57
and trancl_pred_sexpD2 = 
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    58
    pred_sexp_subset_Sigma RS trancl_subset_Sigma RS subsetD RS SigmaD2;
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    59
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    60
Goalw [pred_sexp_def]
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    61
    "!!M. [| M: sexp;  N: sexp |] ==> (M, Scons M N) : pred_sexp";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    62
by (Blast_tac 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    63
qed "pred_sexpI1";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    64
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    65
Goalw [pred_sexp_def]
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    66
    "!!M. [| M: sexp;  N: sexp |] ==> (N, Scons M N) : pred_sexp";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    67
by (Blast_tac 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    68
qed "pred_sexpI2";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    69
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    70
(*Combinations involving transitivity and the rules above*)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    71
val pred_sexp_t1 = pred_sexpI1 RS r_into_trancl
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    72
and pred_sexp_t2 = pred_sexpI2 RS r_into_trancl;
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    73
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    74
val pred_sexp_trans1 = pred_sexp_t1 RSN (2, trans_trancl RS transD)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    75
and pred_sexp_trans2 = pred_sexp_t2 RSN (2, trans_trancl RS transD);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    76
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    77
(*Proves goals of the form (M,N):pred_sexp^+ provided M,N:sexp*)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    78
Addsimps (sexp.intrs @ [pred_sexp_t1, pred_sexp_t2,
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    79
                        pred_sexp_trans1, pred_sexp_trans2, cut_apply]);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    80
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    81
val major::prems = goalw Sexp.thy [pred_sexp_def]
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    82
    "[| p : pred_sexp;                                       \
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    83
\       !!M N. [| p = (M, Scons M N);  M: sexp;  N: sexp |] ==> R; \
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    84
\       !!M N. [| p = (N, Scons M N);  M: sexp;  N: sexp |] ==> R  \
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    85
\    |] ==> R";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    86
by (cut_facts_tac [major] 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    87
by (REPEAT (eresolve_tac ([asm_rl,emptyE,insertE,UN_E]@prems) 1));
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    88
qed "pred_sexpE";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    89
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    90
Goal "wf(pred_sexp)";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    91
by (rtac (pred_sexp_subset_Sigma RS wfI) 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    92
by (etac sexp.induct 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    93
by (ALLGOALS (blast_tac (claset() addSEs [allE, pred_sexpE])));
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    94
qed "wf_pred_sexp";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    95
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    96
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    97
(*** sexp_rec -- by wf recursion on pred_sexp ***)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    98
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
    99
Goal "(%M. sexp_rec M c d e) = wfrec pred_sexp \
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   100
                       \ (%g. sexp_case c d (%N1 N2. e N1 N2 (g N1) (g N2)))";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   101
by (simp_tac (HOL_ss addsimps [sexp_rec_def]) 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   102
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   103
(* sexp_rec a c d e =
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   104
   sexp_case c d
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   105
    (%N1 N2.
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   106
        e N1 N2 (cut (%M. sexp_rec M c d e) pred_sexp a N1)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   107
         (cut (%M. sexp_rec M c d e) pred_sexp a N2)) a
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   108
*)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   109
bind_thm("sexp_rec_unfold", 
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   110
	 [result() RS eq_reflection, wf_pred_sexp] MRS def_wfrec);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   111
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   112
(** conversion rules **)
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   113
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   114
Goal "sexp_rec (Leaf a) c d h = c(a)";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   115
by (stac sexp_rec_unfold 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   116
by (rtac sexp_case_Leaf 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   117
qed "sexp_rec_Leaf";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   118
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   119
Goal "sexp_rec (Numb k) c d h = d(k)";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   120
by (stac sexp_rec_unfold 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   121
by (rtac sexp_case_Numb 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   122
qed "sexp_rec_Numb";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   123
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   124
Goal "[| M: sexp;  N: sexp |] ==> \
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   125
\    sexp_rec (Scons M N) c d h = h M N (sexp_rec M c d h) (sexp_rec N c d h)";
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   126
by (rtac (sexp_rec_unfold RS trans) 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   127
by (asm_simp_tac (simpset() addsimps [pred_sexpI1, pred_sexpI2]) 1);
18b76c137c41 moved theory Sexp to Induct examples;
wenzelm
parents:
diff changeset
   128
qed "sexp_rec_Scons";