src/HOL/ex/Term.ML
author clasohm
Mon, 05 Feb 1996 21:29:06 +0100
changeset 1476 608483c2122a
parent 1465 5d7a7e439cec
child 1811 9083542fd861
permissions -rw-r--r--
expanded tabs; incorporated Konrad's changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1266
diff changeset
     1
(*  Title:      HOL/ex/Term
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     2
    ID:         $Id$
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1266
diff changeset
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     4
    Copyright   1992  University of Cambridge
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     5
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     6
Terms over a given alphabet -- function applications; illustrates list functor
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     7
  (essentially the same type as in Trees & Forests)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     8
*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
     9
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    10
open Term;
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    11
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    12
(*** Monotonicity and unfolding of the function ***)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    13
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    14
goal Term.thy "term(A) = A <*> list(term(A))";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    15
by (fast_tac (univ_cs addSIs (equalityI :: term.intrs)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    16
                      addEs [term.elim]) 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    17
qed "term_unfold";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    18
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    19
(*This justifies using term in other recursive type definitions*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    20
goalw Term.thy term.defs "!!A B. A<=B ==> term(A) <= term(B)";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    21
by (REPEAT (ares_tac ([lfp_mono, list_mono] @ basic_monos) 1));
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    22
qed "term_mono";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    23
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    24
(** Type checking -- term creates well-founded sets **)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    25
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    26
goalw Term.thy term.defs "term(sexp) <= sexp";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    27
by (rtac lfp_lowerbound 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    28
by (fast_tac (univ_cs addIs [sexp.SconsI, list_sexp RS subsetD]) 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    29
qed "term_sexp";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    30
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    31
(* A <= sexp ==> term(A) <= sexp *)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    32
bind_thm ("term_subset_sexp", ([term_mono, term_sexp] MRS subset_trans));
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    33
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    34
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    35
(** Elimination -- structural induction on the set term(A) **)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    36
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    37
(*Induction for the set term(A) *)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    38
val [major,minor] = goal Term.thy 
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    39
    "[| M: term(A);  \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    40
\       !!x zs. [| x: A;  zs: list(term(A));  zs: list({x.R(x)}) \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    41
\               |] ==> R(x$zs)  \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    42
\    |] ==> R(M)";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    43
by (rtac (major RS term.induct) 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    44
by (REPEAT (eresolve_tac ([minor] @
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1266
diff changeset
    45
                ([Int_lower1,Int_lower2] RL [list_mono RS subsetD])) 1));
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    46
(*Proof could also use  mono_Int RS subsetD RS IntE *)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    47
qed "Term_induct";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    48
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    49
(*Induction on term(A) followed by induction on list *)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    50
val major::prems = goal Term.thy
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    51
    "[| M: term(A);  \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    52
\       !!x.      [| x: A |] ==> R(x$NIL);  \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    53
\       !!x z zs. [| x: A;  z: term(A);  zs: list(term(A));  R(x$zs)  \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    54
\                 |] ==> R(x $ CONS z zs)  \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    55
\    |] ==> R(M)";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    56
by (rtac (major RS Term_induct) 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    57
by (etac list.induct 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    58
by (REPEAT (ares_tac prems 1));
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    59
qed "Term_induct2";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    60
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    61
(*** Structural Induction on the abstract type 'a term ***)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    62
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    63
val Rep_term_in_sexp =
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    64
    Rep_term RS (range_Leaf_subset_sexp RS term_subset_sexp RS subsetD);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    65
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    66
(*Induction for the abstract type 'a term*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    67
val prems = goalw Term.thy [App_def,Rep_Tlist_def,Abs_Tlist_def]
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    68
    "[| !!x ts. list_all R ts ==> R(App x ts)  \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    69
\    |] ==> R(t)";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    70
by (rtac (Rep_term_inverse RS subst) 1);   (*types force good instantiation*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    71
by (res_inst_tac [("P","Rep_term(t) : sexp")] conjunct2 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    72
by (rtac (Rep_term RS Term_induct) 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    73
by (REPEAT (ares_tac [conjI, sexp.SconsI, term_subset_sexp RS 
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    74
    list_subset_sexp, range_Leaf_subset_sexp] 1
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    75
     ORELSE etac rev_subsetD 1));
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    76
by (eres_inst_tac [("A1","term(?u)"), ("f1","Rep_term"), ("g1","Abs_term")]
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1266
diff changeset
    77
        (Abs_map_inverse RS subst) 1);
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    78
by (rtac (range_Leaf_subset_sexp RS term_subset_sexp) 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    79
by (etac Abs_term_inverse 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    80
by (etac rangeE 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    81
by (hyp_subst_tac 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    82
by (resolve_tac prems 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    83
by (etac list.induct 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    84
by (etac CollectE 2);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    85
by (stac Abs_map_CONS 2);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    86
by (etac conjunct1 2);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    87
by (etac rev_subsetD 2);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    88
by (rtac list_subset_sexp 2);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    89
by (fast_tac set_cs 2);
1266
3ae9fe3c0f68 added local simpsets
clasohm
parents: 972
diff changeset
    90
by (ALLGOALS Asm_simp_tac);
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    91
qed "term_induct";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    92
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    93
(*Induction for the abstract type 'a term*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    94
val prems = goal Term.thy 
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    95
    "[| !!x. R(App x Nil);  \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    96
\       !!x t ts. R(App x ts) ==> R(App x (t#ts))  \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    97
\    |] ==> R(t)";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    98
by (rtac term_induct 1);  (*types force good instantiation*)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
    99
by (etac rev_mp 1);
1266
3ae9fe3c0f68 added local simpsets
clasohm
parents: 972
diff changeset
   100
by (rtac list_induct2 1);  (*types force good instantiation*)
3ae9fe3c0f68 added local simpsets
clasohm
parents: 972
diff changeset
   101
by (ALLGOALS (asm_simp_tac (!simpset addsimps prems)));
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   102
qed "term_induct2";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   103
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   104
(*Perform induction on xs. *)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   105
fun term_ind2_tac a i = 
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   106
    EVERY [res_inst_tac [("t",a)] term_induct2 i,
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1266
diff changeset
   107
           rename_last_tac a ["1","s"] (i+1)];
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   108
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   109
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   110
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   111
(*** Term_rec -- by wf recursion on pred_sexp ***)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   112
1476
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   113
goal Term.thy
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   114
   "(%M. Term_rec M d) = wfrec (trancl pred_sexp) \
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   115
                             \ (%g. Split(%x y. d x y (Abs_map g y)))";
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   116
by (simp_tac (HOL_ss addsimps [Term_rec_def]) 1);
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   117
bind_thm("Term_rec_unfold", (wf_pred_sexp RS wf_trancl) RS 
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   118
                            ((result() RS eq_reflection) RS def_wfrec));
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   119
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   120
(*---------------------------------------------------------------------------
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   121
 * Old:
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   122
 * val Term_rec_unfold =
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   123
 *     wf_pred_sexp RS wf_trancl RS (Term_rec_def RS def_wfrec);
608483c2122a expanded tabs; incorporated Konrad's changes
clasohm
parents: 1465
diff changeset
   124
 *---------------------------------------------------------------------------*)
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   125
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   126
(** conversion rules **)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   127
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   128
val [prem] = goal Term.thy
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   129
    "N: list(term(A)) ==>  \
972
e61b058d58d2 changed syntax of tuples from <..., ...> to (..., ...)
clasohm
parents: 969
diff changeset
   130
\    !M. (N,M): pred_sexp^+ --> \
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   131
\        Abs_map (cut h (pred_sexp^+) M) N = \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   132
\        Abs_map h N";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   133
by (rtac (prem RS list.induct) 1);
1266
3ae9fe3c0f68 added local simpsets
clasohm
parents: 972
diff changeset
   134
by (Simp_tac 1);
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   135
by (strip_tac 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   136
by (etac (pred_sexp_CONS_D RS conjE) 1);
1266
3ae9fe3c0f68 added local simpsets
clasohm
parents: 972
diff changeset
   137
by (asm_simp_tac (!simpset addsimps [trancl_pred_sexpD1]) 1);
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   138
qed "Abs_map_lemma";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   139
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   140
val [prem1,prem2,A_subset_sexp] = goal Term.thy
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   141
    "[| M: sexp;  N: list(term(A));  A<=sexp |] ==> \
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   142
\    Term_rec (M$N) d = d M N (Abs_map (%Z. Term_rec Z d) N)";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   143
by (rtac (Term_rec_unfold RS trans) 1);
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   144
by (simp_tac (HOL_ss addsimps
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   145
      [Split,
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   146
       prem2 RS Abs_map_lemma RS spec RS mp, pred_sexpI2 RS r_into_trancl,
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   147
       prem1, prem2 RS rev_subsetD, list_subset_sexp,
1266
3ae9fe3c0f68 added local simpsets
clasohm
parents: 972
diff changeset
   148
       term_subset_sexp, A_subset_sexp]) 1);
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   149
qed "Term_rec";
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   150
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   151
(*** term_rec -- by Term_rec ***)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   152
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   153
local
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   154
  val Rep_map_type1 = read_instantiate_sg (sign_of Term.thy)
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   155
                        [("f","Rep_term")] Rep_map_type;
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   156
  val Rep_Tlist = Rep_term RS Rep_map_type1;
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   157
  val Rep_Term_rec = range_Leaf_subset_sexp RSN (2,Rep_Tlist RSN(2,Term_rec));
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   158
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   159
  (*Now avoids conditional rewriting with the premise N: list(term(A)),
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   160
    since A will be uninstantiated and will cause rewriting to fail. *)
1266
3ae9fe3c0f68 added local simpsets
clasohm
parents: 972
diff changeset
   161
  val term_rec_ss = HOL_ss
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   162
      addsimps [Rep_Tlist RS (rangeI RS term.APP_I RS Abs_term_inverse),  
1266
3ae9fe3c0f68 added local simpsets
clasohm
parents: 972
diff changeset
   163
                Rep_term_in_sexp, Rep_Term_rec, Rep_term_inverse, inj_Leaf,
3ae9fe3c0f68 added local simpsets
clasohm
parents: 972
diff changeset
   164
                Inv_f_f, Abs_Rep_map, map_ident2, sexp.LeafI]
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   165
in
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   166
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   167
val term_rec = prove_goalw Term.thy
1465
5d7a7e439cec expanded tabs
clasohm
parents: 1266
diff changeset
   168
         [term_rec_def, App_def, Rep_Tlist_def, Abs_Tlist_def]
969
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   169
    "term_rec (App f ts) d = d f ts (map (%t. term_rec t d) ts)"
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   170
 (fn _ => [simp_tac term_rec_ss 1])
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   171
b051e2fc2e34 converted ex with curried function application
clasohm
parents:
diff changeset
   172
end;