src/CCL/ex/List.ML
author wenzelm
Tue, 24 Jan 2006 00:43:31 +0100
changeset 18768 6e97b57cdcba
parent 17456 bcf7544875b2
permissions -rw-r--r--
removed the_params;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
     1
(*  Title:      CCL/ex/List.ML
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     2
    ID:         $Id$
1459
d12da312eff4 expanded tabs
clasohm
parents: 757
diff changeset
     3
    Author:     Martin Coen, Cambridge University Computer Laboratory
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
    Copyright   1993  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     6
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     7
val list_defs = [map_def,comp_def,append_def,filter_def,flat_def,
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     8
                 insert_def,isort_def,partition_def,qsort_def];
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     9
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    10
(****)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    12
val listBs = map (fn s=>prove_goalw (the_context ()) list_defs s (fn _ => [simp_tac term_ss 1]))
3837
d7f033c74b38 fixed dots;
wenzelm
parents: 1459
diff changeset
    13
     ["(f o g) = (%a. f(g(a)))",
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    14
      "(f o g)(a) = f(g(a))",
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    15
      "map(f,[]) = []",
290
37d580c16af5 changed "." to "$" and added parentheses to eliminate ambiguity
clasohm
parents: 8
diff changeset
    16
      "map(f,x$xs) = f(x)$map(f,xs)",
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
      "[] @ m = m",
290
37d580c16af5 changed "." to "$" and added parentheses to eliminate ambiguity
clasohm
parents: 8
diff changeset
    18
      "x$xs @ m = x$(xs @ m)",
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
      "filter(f,[]) = []",
290
37d580c16af5 changed "." to "$" and added parentheses to eliminate ambiguity
clasohm
parents: 8
diff changeset
    20
      "filter(f,x$xs) = if f`x then x$filter(f,xs) else filter(f,xs)",
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
      "flat([]) = []",
290
37d580c16af5 changed "." to "$" and added parentheses to eliminate ambiguity
clasohm
parents: 8
diff changeset
    22
      "flat(x$xs) = x @ flat(xs)",
37d580c16af5 changed "." to "$" and added parentheses to eliminate ambiguity
clasohm
parents: 8
diff changeset
    23
      "insert(f,a,[]) = a$[]",
37d580c16af5 changed "." to "$" and added parentheses to eliminate ambiguity
clasohm
parents: 8
diff changeset
    24
      "insert(f,a,x$xs) = if f`a`x then a$x$xs else x$insert(f,a,xs)"];
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
8
c3d2c6dcf3f0 Installation of new simplfier. Previously appeared to set up the old
lcp
parents: 0
diff changeset
    26
val list_ss = nat_ss addsimps listBs;
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    27
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    28
(****)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    29
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    30
val [prem] = goal (the_context ()) "n:Nat ==> map(f) ^ n ` [] = []";
1459
d12da312eff4 expanded tabs
clasohm
parents: 757
diff changeset
    31
by (rtac (prem RS Nat_ind) 1);
8
c3d2c6dcf3f0 Installation of new simplfier. Previously appeared to set up the old
lcp
parents: 0
diff changeset
    32
by (ALLGOALS (asm_simp_tac list_ss));
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    33
qed "nmapBnil";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    34
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    35
val [prem] = goal (the_context ()) "n:Nat ==> map(f)^n`(x$xs) = (f^n`x)$(map(f)^n`xs)";
1459
d12da312eff4 expanded tabs
clasohm
parents: 757
diff changeset
    36
by (rtac (prem RS Nat_ind) 1);
8
c3d2c6dcf3f0 Installation of new simplfier. Previously appeared to set up the old
lcp
parents: 0
diff changeset
    37
by (ALLGOALS (asm_simp_tac list_ss));
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    38
qed "nmapBcons";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    39
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    40
(***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    41
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    42
val prems = goalw (the_context ()) [map_def]
3837
d7f033c74b38 fixed dots;
wenzelm
parents: 1459
diff changeset
    43
  "[| !!x. x:A==>f(x):B;  l : List(A) |] ==> map(f,l) : List(B)";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
by (typechk_tac prems 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    45
qed "mapT";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    46
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    47
val prems = goalw (the_context ()) [append_def]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    48
  "[| l : List(A);  m : List(A) |] ==> l @ m : List(A)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    49
by (typechk_tac prems 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    50
qed "appendT";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    51
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    52
val prems = goal (the_context ())
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    53
  "[| l : {l:List(A). m : {m:List(A).P(l @ m)}} |] ==> l @ m : {x:List(A). P(x)}";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    54
by (cut_facts_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    55
by (fast_tac (set_cs addSIs [SubtypeI,appendT] addSEs [SubtypeE]) 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    56
qed "appendTS";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    57
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    58
val prems = goalw (the_context ()) [filter_def]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    59
  "[| f:A->Bool;   l : List(A) |] ==> filter(f,l) : List(A)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    60
by (typechk_tac prems 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    61
qed "filterT";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    62
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    63
val prems = goalw (the_context ()) [flat_def]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    64
  "l : List(List(A)) ==> flat(l) : List(A)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    65
by (typechk_tac (appendT::prems) 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    66
qed "flatT";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    67
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    68
val prems = goalw (the_context ()) [insert_def]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    69
  "[|  f : A->A->Bool; a:A; l : List(A) |] ==> insert(f,a,l) : List(A)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    70
by (typechk_tac prems 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    71
qed "insertT";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    72
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    73
val prems = goal (the_context ())
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    74
  "[| f : {f:A->A->Bool. a : {a:A. l : {l:List(A).P(insert(f,a,l))}}} |] ==> \
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    75
\  insert(f,a,l)  : {x:List(A). P(x)}";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    76
by (cut_facts_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    77
by (fast_tac (set_cs addSIs [SubtypeI,insertT] addSEs [SubtypeE]) 1);
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    78
qed "insertTS";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    79
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    80
val prems = goalw (the_context ()) [partition_def]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    81
  "[| f:A->Bool;  l : List(A) |] ==> partition(f,l) : List(A)*List(A)";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    82
by (typechk_tac prems 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    83
by clean_ccs_tac;
1459
d12da312eff4 expanded tabs
clasohm
parents: 757
diff changeset
    84
by (rtac (ListPRI RS wfstI RS (ListPR_wf RS wmap_wf RS wfI)) 2);
d12da312eff4 expanded tabs
clasohm
parents: 757
diff changeset
    85
by (rtac (ListPRI RS wfstI RS (ListPR_wf RS wmap_wf RS wfI)) 1);
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    86
by (REPEAT (atac 1));
757
2ca12511676d added qed and qed_goal[w]
clasohm
parents: 290
diff changeset
    87
qed "partitionT";
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    88
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    89
(*** Correctness Conditions for Insertion Sort ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    90
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    91
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    92
val prems = goalw (the_context ()) [isort_def]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    93
    "f:A->A->Bool ==> isort(f) : PROD l:List(A).{x: List(A). Ord(f,x) & Perm(x,l)}";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    94
by (gen_ccs_tac  ([insertTS,insertT]@prems) 1);
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    95
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    96
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    97
(*** Correctness Conditions for Quick Sort ***)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    98
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    99
val prems = goalw (the_context ()) [qsort_def]
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   100
    "f:A->A->Bool ==> qsort(f) : PROD l:List(A).{x: List(A). Ord(f,x) & Perm(x,l)}";
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   101
by (gen_ccs_tac  ([partitionT,appendTS,appendT]@prems) 1);