src/HOL/Induct/SList.ML
author paulson
Thu Aug 06 15:48:13 1998 +0200 (1998-08-06)
changeset 5278 a903b66822e2
parent 5223 4cb05273f764
child 5535 678999604ee9
permissions -rw-r--r--
even more tidying of Goal commands
paulson@3120
     1
(*  Title:      HOL/ex/SList.ML
paulson@3120
     2
    ID:         $Id$
paulson@3120
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@4521
     4
    Copyright   1998  University of Cambridge
paulson@3120
     5
paulson@3120
     6
Definition of type 'a list by a least fixed point
paulson@3120
     7
*)
paulson@3120
     8
paulson@3120
     9
val list_con_defs = [NIL_def, CONS_def];
paulson@3120
    10
wenzelm@5069
    11
Goal "list(A) = {Numb(0)} <+> (A <*> list(A))";
paulson@3120
    12
let val rew = rewrite_rule list_con_defs in  
wenzelm@4089
    13
by (fast_tac (claset() addSIs (equalityI :: map rew list.intrs)
paulson@3120
    14
                      addEs [rew list.elim]) 1)
paulson@3120
    15
end;
paulson@3120
    16
qed "list_unfold";
paulson@3120
    17
paulson@3120
    18
(*This justifies using list in other recursive type definitions*)
paulson@5143
    19
Goalw list.defs "A<=B ==> list(A) <= list(B)";
paulson@3120
    20
by (rtac lfp_mono 1);
paulson@3120
    21
by (REPEAT (ares_tac basic_monos 1));
paulson@3120
    22
qed "list_mono";
paulson@3120
    23
paulson@3120
    24
(*Type checking -- list creates well-founded sets*)
wenzelm@5069
    25
Goalw (list_con_defs @ list.defs) "list(sexp) <= sexp";
paulson@3120
    26
by (rtac lfp_lowerbound 1);
wenzelm@4089
    27
by (fast_tac (claset() addIs sexp.intrs@[sexp_In0I,sexp_In1I]) 1);
paulson@3120
    28
qed "list_sexp";
paulson@3120
    29
paulson@3120
    30
(* A <= sexp ==> list(A) <= sexp *)
paulson@3120
    31
bind_thm ("list_subset_sexp", ([list_mono, list_sexp] MRS subset_trans));
paulson@3120
    32
paulson@3120
    33
(*Induction for the type 'a list *)
paulson@3120
    34
val prems = goalw SList.thy [Nil_def,Cons_def]
paulson@3120
    35
    "[| P(Nil);   \
paulson@3120
    36
\       !!x xs. P(xs) ==> P(x # xs) |]  ==> P(l)";
paulson@3120
    37
by (rtac (Rep_list_inverse RS subst) 1);   (*types force good instantiation*)
paulson@3120
    38
by (rtac (Rep_list RS list.induct) 1);
paulson@3120
    39
by (REPEAT (ares_tac prems 1
paulson@3120
    40
     ORELSE eresolve_tac [rangeE, ssubst, Abs_list_inverse RS subst] 1));
paulson@3120
    41
qed "list_induct2";
paulson@3120
    42
paulson@3120
    43
(*Perform induction on xs. *)
paulson@3120
    44
fun list_ind_tac a M = 
paulson@3120
    45
    EVERY [res_inst_tac [("l",a)] list_induct2 M,
paulson@3120
    46
           rename_last_tac a ["1"] (M+1)];
paulson@3120
    47
paulson@3120
    48
(*** Isomorphisms ***)
paulson@3120
    49
wenzelm@5069
    50
Goal "inj(Rep_list)";
paulson@3120
    51
by (rtac inj_inverseI 1);
paulson@3120
    52
by (rtac Rep_list_inverse 1);
paulson@3120
    53
qed "inj_Rep_list";
paulson@3120
    54
wenzelm@5069
    55
Goal "inj_on Abs_list (list(range Leaf))";
nipkow@4831
    56
by (rtac inj_on_inverseI 1);
paulson@3120
    57
by (etac Abs_list_inverse 1);
nipkow@4831
    58
qed "inj_on_Abs_list";
paulson@3120
    59
paulson@3120
    60
(** Distinctness of constructors **)
paulson@3120
    61
wenzelm@5069
    62
Goalw list_con_defs "CONS M N ~= NIL";
paulson@3120
    63
by (rtac In1_not_In0 1);
paulson@3120
    64
qed "CONS_not_NIL";
paulson@3120
    65
bind_thm ("NIL_not_CONS", (CONS_not_NIL RS not_sym));
paulson@3120
    66
paulson@3120
    67
bind_thm ("CONS_neq_NIL", (CONS_not_NIL RS notE));
paulson@3120
    68
val NIL_neq_CONS = sym RS CONS_neq_NIL;
paulson@3120
    69
wenzelm@5069
    70
Goalw [Nil_def,Cons_def] "x # xs ~= Nil";
nipkow@4831
    71
by (rtac (CONS_not_NIL RS (inj_on_Abs_list RS inj_on_contraD)) 1);
paulson@3120
    72
by (REPEAT (resolve_tac (list.intrs @ [rangeI, Rep_list]) 1));
paulson@3120
    73
qed "Cons_not_Nil";
paulson@3120
    74
paulson@3120
    75
bind_thm ("Nil_not_Cons", Cons_not_Nil RS not_sym);
paulson@3120
    76
paulson@3120
    77
(** Injectiveness of CONS and Cons **)
paulson@3120
    78
wenzelm@5069
    79
Goalw [CONS_def] "(CONS K M=CONS L N) = (K=L & M=N)";
wenzelm@4089
    80
by (fast_tac (claset() addSEs [Scons_inject, make_elim In1_inject]) 1);
paulson@3120
    81
qed "CONS_CONS_eq";
paulson@3120
    82
paulson@3120
    83
(*For reasoning about abstract list constructors*)
paulson@3120
    84
AddIs ([Rep_list] @ list.intrs);
paulson@3120
    85
paulson@3120
    86
AddIffs [CONS_not_NIL, NIL_not_CONS, CONS_CONS_eq];
paulson@3120
    87
nipkow@4831
    88
AddSDs [inj_on_Abs_list RS inj_onD,
paulson@3120
    89
        inj_Rep_list RS injD, Leaf_inject];
paulson@3120
    90
wenzelm@5069
    91
Goalw [Cons_def] "(x#xs=y#ys) = (x=y & xs=ys)";
paulson@3120
    92
by (Fast_tac 1);
paulson@3120
    93
qed "Cons_Cons_eq";
paulson@3120
    94
bind_thm ("Cons_inject2", (Cons_Cons_eq RS iffD1 RS conjE));
paulson@3120
    95
paulson@3120
    96
val [major] = goal SList.thy "CONS M N: list(A) ==> M: A & N: list(A)";
paulson@3120
    97
by (rtac (major RS setup_induction) 1);
paulson@3120
    98
by (etac list.induct 1);
paulson@3120
    99
by (ALLGOALS (Fast_tac));
paulson@3120
   100
qed "CONS_D";
paulson@3120
   101
paulson@3120
   102
val prems = goalw SList.thy [CONS_def,In1_def]
paulson@3120
   103
    "CONS M N: sexp ==> M: sexp & N: sexp";
paulson@3120
   104
by (cut_facts_tac prems 1);
wenzelm@4089
   105
by (fast_tac (claset() addSDs [Scons_D]) 1);
paulson@3120
   106
qed "sexp_CONS_D";
paulson@3120
   107
paulson@3120
   108
paulson@3120
   109
(*Reasoning about constructors and their freeness*)
paulson@3120
   110
Addsimps list.intrs;
paulson@3120
   111
paulson@3120
   112
AddIffs [Cons_not_Nil, Nil_not_Cons, Cons_Cons_eq];
paulson@3120
   113
paulson@5143
   114
Goal "N: list(A) ==> !M. N ~= CONS M N";
paulson@3120
   115
by (etac list.induct 1);
paulson@3120
   116
by (ALLGOALS Asm_simp_tac);
paulson@3120
   117
qed "not_CONS_self";
paulson@3120
   118
wenzelm@5069
   119
Goal "!x. l ~= x#l";
paulson@3120
   120
by (list_ind_tac "l" 1);
paulson@3120
   121
by (ALLGOALS Asm_simp_tac);
paulson@3120
   122
qed "not_Cons_self2";
paulson@3120
   123
paulson@3120
   124
wenzelm@5069
   125
Goal "(xs ~= []) = (? y ys. xs = y#ys)";
paulson@3120
   126
by (list_ind_tac "xs" 1);
paulson@3120
   127
by (Simp_tac 1);
paulson@3120
   128
by (Asm_simp_tac 1);
paulson@3120
   129
by (REPEAT(resolve_tac [exI,refl,conjI] 1));
paulson@3120
   130
qed "neq_Nil_conv2";
paulson@3120
   131
paulson@3120
   132
(** Conversion rules for List_case: case analysis operator **)
paulson@3120
   133
wenzelm@5069
   134
Goalw [List_case_def,NIL_def] "List_case c h NIL = c";
paulson@3120
   135
by (rtac Case_In0 1);
paulson@3120
   136
qed "List_case_NIL";
paulson@3120
   137
wenzelm@5069
   138
Goalw [List_case_def,CONS_def]  "List_case c h (CONS M N) = h M N";
paulson@4521
   139
by (Simp_tac 1);
paulson@3120
   140
qed "List_case_CONS";
paulson@3120
   141
paulson@4521
   142
Addsimps [List_case_NIL, List_case_CONS];
paulson@4521
   143
paulson@4521
   144
paulson@3120
   145
(*** List_rec -- by wf recursion on pred_sexp ***)
paulson@3120
   146
paulson@3120
   147
(* The trancl(pred_sexp) is essential because pred_sexp_CONS_I1,2 would not
paulson@3120
   148
   hold if pred_sexp^+ were changed to pred_sexp. *)
paulson@3120
   149
paulson@5278
   150
Goal "(%M. List_rec M c d) = wfrec (trancl pred_sexp) \
paulson@3120
   151
                           \     (%g. List_case c (%x y. d x y (g y)))";
paulson@3120
   152
by (simp_tac (HOL_ss addsimps [List_rec_def]) 1);
paulson@3120
   153
val List_rec_unfold = standard 
paulson@3120
   154
  ((wf_pred_sexp RS wf_trancl) RS ((result() RS eq_reflection) RS def_wfrec));
paulson@3120
   155
paulson@3120
   156
(*---------------------------------------------------------------------------
paulson@3120
   157
 * Old:
paulson@3120
   158
 * val List_rec_unfold = [List_rec_def,wf_pred_sexp RS wf_trancl] MRS def_wfrec
paulson@3120
   159
 *                     |> standard;
paulson@3120
   160
 *---------------------------------------------------------------------------*)
paulson@3120
   161
paulson@3120
   162
(** pred_sexp lemmas **)
paulson@3120
   163
wenzelm@5069
   164
Goalw [CONS_def,In1_def]
paulson@5148
   165
    "[| M: sexp;  N: sexp |] ==> (M, CONS M N) : pred_sexp^+";
paulson@3120
   166
by (Asm_simp_tac 1);
paulson@3120
   167
qed "pred_sexp_CONS_I1";
paulson@3120
   168
wenzelm@5069
   169
Goalw [CONS_def,In1_def]
paulson@5148
   170
    "[| M: sexp;  N: sexp |] ==> (N, CONS M N) : pred_sexp^+";
paulson@3120
   171
by (Asm_simp_tac 1);
paulson@3120
   172
qed "pred_sexp_CONS_I2";
paulson@3120
   173
paulson@3120
   174
val [prem] = goal SList.thy
paulson@3120
   175
    "(CONS M1 M2, N) : pred_sexp^+ ==> \
paulson@3120
   176
\    (M1,N) : pred_sexp^+ & (M2,N) : pred_sexp^+";
paulson@3120
   177
by (rtac (prem RS (pred_sexp_subset_Sigma RS trancl_subset_Sigma RS 
paulson@3120
   178
                   subsetD RS SigmaE2)) 1);
paulson@3120
   179
by (etac (sexp_CONS_D RS conjE) 1);
paulson@3120
   180
by (REPEAT (ares_tac [conjI, pred_sexp_CONS_I1, pred_sexp_CONS_I2,
paulson@3120
   181
                      prem RSN (2, trans_trancl RS transD)] 1));
paulson@3120
   182
qed "pred_sexp_CONS_D";
paulson@3120
   183
paulson@3120
   184
(** Conversion rules for List_rec **)
paulson@3120
   185
wenzelm@5069
   186
Goal "List_rec NIL c h = c";
paulson@3120
   187
by (rtac (List_rec_unfold RS trans) 1);
paulson@4521
   188
by (Simp_tac 1);
paulson@3120
   189
qed "List_rec_NIL";
paulson@3120
   190
paulson@5143
   191
Goal "[| M: sexp;  N: sexp |] ==> \
paulson@3120
   192
\    List_rec (CONS M N) c h = h M N (List_rec N c h)";
paulson@3120
   193
by (rtac (List_rec_unfold RS trans) 1);
paulson@4521
   194
by (asm_simp_tac (simpset() addsimps [pred_sexp_CONS_I2]) 1);
paulson@3120
   195
qed "List_rec_CONS";
paulson@3120
   196
paulson@4521
   197
Addsimps [List_rec_NIL, List_rec_CONS];
paulson@4521
   198
paulson@4521
   199
paulson@3120
   200
(*** list_rec -- by List_rec ***)
paulson@3120
   201
paulson@3120
   202
val Rep_list_in_sexp =
paulson@3120
   203
    [range_Leaf_subset_sexp RS list_subset_sexp, Rep_list] MRS subsetD;
paulson@3120
   204
paulson@3120
   205
local
paulson@4521
   206
  val list_rec_simps = [Abs_list_inverse, Rep_list_inverse,
paulson@3120
   207
                        Rep_list, rangeI, inj_Leaf, inv_f_f,
paulson@3120
   208
                        sexp.LeafI, Rep_list_in_sexp]
paulson@3120
   209
in
paulson@3120
   210
  val list_rec_Nil = prove_goalw SList.thy [list_rec_def, Nil_def]
paulson@3120
   211
      "list_rec Nil c h = c"
wenzelm@4089
   212
   (fn _=> [simp_tac (simpset() addsimps list_rec_simps) 1]);
paulson@3120
   213
paulson@3120
   214
  val list_rec_Cons = prove_goalw SList.thy [list_rec_def, Cons_def]
paulson@3120
   215
      "list_rec (a#l) c h = h a l (list_rec l c h)"
wenzelm@4089
   216
   (fn _=> [simp_tac (simpset() addsimps list_rec_simps) 1]);
paulson@3120
   217
end;
paulson@3120
   218
paulson@3120
   219
Addsimps [List_rec_NIL, List_rec_CONS, list_rec_Nil, list_rec_Cons];
paulson@3120
   220
paulson@3120
   221
paulson@3120
   222
(*Type checking.  Useful?*)
paulson@3120
   223
val major::A_subset_sexp::prems = goal SList.thy
paulson@3120
   224
    "[| M: list(A);     \
paulson@3120
   225
\       A<=sexp;        \
paulson@3120
   226
\       c: C(NIL);      \
paulson@3120
   227
\       !!x y r. [| x: A;  y: list(A);  r: C(y) |] ==> h x y r: C(CONS x y) \
paulson@3120
   228
\    |] ==> List_rec M c h : C(M :: 'a item)";
paulson@3120
   229
val sexp_ListA_I = A_subset_sexp RS list_subset_sexp RS subsetD;
paulson@3120
   230
val sexp_A_I = A_subset_sexp RS subsetD;
paulson@3120
   231
by (rtac (major RS list.induct) 1);
wenzelm@4089
   232
by (ALLGOALS(asm_simp_tac (simpset() addsimps ([sexp_A_I,sexp_ListA_I]@prems))));
paulson@3120
   233
qed "List_rec_type";
paulson@3120
   234
paulson@3120
   235
(** Generalized map functionals **)
paulson@3120
   236
wenzelm@5069
   237
Goalw [Rep_map_def] "Rep_map f Nil = NIL";
paulson@3120
   238
by (rtac list_rec_Nil 1);
paulson@3120
   239
qed "Rep_map_Nil";
paulson@3120
   240
wenzelm@5069
   241
Goalw [Rep_map_def]
paulson@3120
   242
    "Rep_map f (x#xs) = CONS (f x) (Rep_map f xs)";
paulson@3120
   243
by (rtac list_rec_Cons 1);
paulson@3120
   244
qed "Rep_map_Cons";
paulson@3120
   245
paulson@5143
   246
val prems = Goalw [Rep_map_def] "(!!x. f(x): A) ==> Rep_map f xs: list(A)";
paulson@3120
   247
by (rtac list_induct2 1);
paulson@5143
   248
by (ALLGOALS (asm_simp_tac (simpset() addsimps prems)));
paulson@3120
   249
qed "Rep_map_type";
paulson@3120
   250
wenzelm@5069
   251
Goalw [Abs_map_def] "Abs_map g NIL = Nil";
paulson@3120
   252
by (rtac List_rec_NIL 1);
paulson@3120
   253
qed "Abs_map_NIL";
paulson@3120
   254
paulson@3120
   255
val prems = goalw SList.thy [Abs_map_def]
paulson@3120
   256
    "[| M: sexp;  N: sexp |] ==> \
paulson@3120
   257
\    Abs_map g (CONS M N) = g(M) # Abs_map g N";
paulson@3120
   258
by (REPEAT (resolve_tac (List_rec_CONS::prems) 1));
paulson@3120
   259
qed "Abs_map_CONS";
paulson@3120
   260
paulson@3120
   261
(*These 2 rules ease the use of primitive recursion.  NOTE USE OF == *)
paulson@3120
   262
val [rew] = goal SList.thy
paulson@3120
   263
    "[| !!xs. f(xs) == list_rec xs c h |] ==> f([]) = c";
paulson@3120
   264
by (rewtac rew);
paulson@3120
   265
by (rtac list_rec_Nil 1);
paulson@3120
   266
qed "def_list_rec_Nil";
paulson@3120
   267
paulson@3120
   268
val [rew] = goal SList.thy
paulson@3120
   269
    "[| !!xs. f(xs) == list_rec xs c h |] ==> f(x#xs) = h x xs (f xs)";
paulson@3120
   270
by (rewtac rew);
paulson@3120
   271
by (rtac list_rec_Cons 1);
paulson@3120
   272
qed "def_list_rec_Cons";
paulson@3120
   273
paulson@3120
   274
fun list_recs def =
paulson@3120
   275
      [standard (def RS def_list_rec_Nil),
paulson@3120
   276
       standard (def RS def_list_rec_Cons)];
paulson@3120
   277
paulson@3120
   278
(*** Unfolding the basic combinators ***)
paulson@3120
   279
paulson@3120
   280
val [null_Nil, null_Cons]               = list_recs null_def;
paulson@3120
   281
val [_, hd_Cons]                        = list_recs hd_def;
paulson@3120
   282
val [_, tl_Cons]                        = list_recs tl_def;
paulson@3120
   283
val [ttl_Nil, ttl_Cons]                 = list_recs ttl_def;
paulson@3120
   284
val [append_Nil3, append_Cons]          = list_recs append_def;
paulson@3120
   285
val [mem_Nil, mem_Cons]                 = list_recs mem_def;
paulson@3649
   286
val [set_Nil, set_Cons]                 = list_recs set_def;
paulson@3120
   287
val [map_Nil, map_Cons]                 = list_recs map_def;
paulson@3120
   288
val [list_case_Nil, list_case_Cons]     = list_recs list_case_def;
paulson@3120
   289
val [filter_Nil, filter_Cons]           = list_recs filter_def;
paulson@3120
   290
paulson@3120
   291
Addsimps
paulson@3120
   292
  [null_Nil, ttl_Nil,
paulson@3120
   293
   mem_Nil, mem_Cons,
paulson@3120
   294
   list_case_Nil, list_case_Cons,
paulson@3120
   295
   append_Nil3, append_Cons,
paulson@3649
   296
   set_Nil, set_Cons, 
paulson@3120
   297
   map_Nil, map_Cons,
paulson@3120
   298
   filter_Nil, filter_Cons];
paulson@3120
   299
paulson@3120
   300
paulson@3120
   301
(** @ - append **)
paulson@3120
   302
wenzelm@5069
   303
Goal "(xs@ys)@zs = xs@(ys@zs)";
paulson@3120
   304
by (list_ind_tac "xs" 1);
paulson@3120
   305
by (ALLGOALS Asm_simp_tac);
paulson@3120
   306
qed "append_assoc2";
paulson@3120
   307
wenzelm@5069
   308
Goal "xs @ [] = xs";
paulson@3120
   309
by (list_ind_tac "xs" 1);
paulson@3120
   310
by (ALLGOALS Asm_simp_tac);
paulson@3120
   311
qed "append_Nil4";
paulson@3120
   312
paulson@3120
   313
(** mem **)
paulson@3120
   314
wenzelm@5069
   315
Goal "x mem (xs@ys) = (x mem xs | x mem ys)";
paulson@3120
   316
by (list_ind_tac "xs" 1);
nipkow@4686
   317
by (ALLGOALS Asm_simp_tac);
paulson@3120
   318
qed "mem_append2";
paulson@3120
   319
wenzelm@5069
   320
Goal "x mem [x:xs. P(x)] = (x mem xs & P(x))";
paulson@3120
   321
by (list_ind_tac "xs" 1);
nipkow@4686
   322
by (ALLGOALS Asm_simp_tac);
paulson@3120
   323
qed "mem_filter2";
paulson@3120
   324
paulson@3120
   325
paulson@3120
   326
(** The functional "map" **)
paulson@3120
   327
paulson@3120
   328
Addsimps [Rep_map_Nil, Rep_map_Cons, Abs_map_NIL, Abs_map_CONS];
paulson@3120
   329
paulson@3120
   330
val [major,A_subset_sexp,minor] = goal SList.thy 
paulson@3120
   331
    "[| M: list(A);  A<=sexp;  !!z. z: A ==> f(g(z)) = z |] \
paulson@3120
   332
\    ==> Rep_map f (Abs_map g M) = M";
paulson@3120
   333
by (rtac (major RS list.induct) 1);
wenzelm@4089
   334
by (ALLGOALS (asm_simp_tac (simpset() addsimps [sexp_A_I,sexp_ListA_I,minor])));
paulson@3120
   335
qed "Abs_map_inverse";
paulson@3120
   336
paulson@3120
   337
(*Rep_map_inverse is obtained via Abs_Rep_map and map_ident*)
paulson@3120
   338
paulson@3120
   339
(** list_case **)
paulson@3120
   340
paulson@5278
   341
Goal "P(list_case a f xs) = ((xs=[] --> P(a)) & \
paulson@3120
   342
\                        (!y ys. xs=y#ys --> P(f y ys)))";
paulson@3120
   343
by (list_ind_tac "xs" 1);
paulson@3120
   344
by (ALLGOALS Asm_simp_tac);
nipkow@4831
   345
qed "split_list_case2";
paulson@3120
   346
paulson@3120
   347
paulson@3120
   348
(** Additional mapping lemmas **)
paulson@3120
   349
wenzelm@5069
   350
Goal "map (%x. x) xs = xs";
paulson@3120
   351
by (list_ind_tac "xs" 1);
paulson@3120
   352
by (ALLGOALS Asm_simp_tac);
paulson@3120
   353
qed "map_ident2";
paulson@3120
   354
wenzelm@5069
   355
Goal "map f (xs@ys) = map f xs @ map f ys";
paulson@3120
   356
by (list_ind_tac "xs" 1);
paulson@3120
   357
by (ALLGOALS Asm_simp_tac);
paulson@3120
   358
qed "map_append2";
paulson@3120
   359
wenzelm@5069
   360
Goalw [o_def] "map (f o g) xs = map f (map g xs)";
paulson@3120
   361
by (list_ind_tac "xs" 1);
paulson@3120
   362
by (ALLGOALS Asm_simp_tac);
paulson@3120
   363
qed "map_compose2";
paulson@3120
   364
paulson@5143
   365
val prems = 
paulson@5143
   366
Goal "(!!x. f(x): sexp) ==> \
paulson@3120
   367
\       Abs_map g (Rep_map f xs) = map (%t. g(f(t))) xs";
paulson@3120
   368
by (list_ind_tac "xs" 1);
paulson@4521
   369
by (ALLGOALS (asm_simp_tac(simpset() addsimps
paulson@5143
   370
			   (prems@[Rep_map_type, list_sexp RS subsetD]))));
paulson@3120
   371
qed "Abs_Rep_map";
paulson@3120
   372
paulson@3120
   373
Addsimps [append_Nil4, map_ident2];