src/CCL/ex/List.thy
author wenzelm
Wed, 03 Nov 2010 21:53:56 +0100
changeset 40335 3e4bb6e7c3ca
parent 35762 af3ff2ba4c54
child 42155 ffe99b07c9c0
permissions -rw-r--r--
feeder: treat header as escaped utf8 to allow initial ML text to refer to non-ASCII file/directory names (e.g. "Documents/" on Chinese Ubuntu);
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.thy
1474
3f7d67927fe2 expanded tabs
clasohm
parents: 1149
diff changeset
     2
    Author:     Martin Coen, Cambridge University Computer Laboratory
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     3
    Copyright   1993  University of Cambridge
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     4
*)
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
     5
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
     6
header {* Programs defined over lists *}
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
     7
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
     8
theory List
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
     9
imports Nat
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    10
begin
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    11
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    12
consts
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    13
  map       :: "[i=>i,i]=>i"
24825
c4f13ab78f9d avoid unnamed infixes;
wenzelm
parents: 20140
diff changeset
    14
  comp      :: "[i=>i,i=>i]=>i=>i"    (infixr "o" 55)
c4f13ab78f9d avoid unnamed infixes;
wenzelm
parents: 20140
diff changeset
    15
  append    :: "[i,i]=>i"             (infixr "@" 55)
c4f13ab78f9d avoid unnamed infixes;
wenzelm
parents: 20140
diff changeset
    16
  member    :: "[i,i]=>i"             (infixr "mem" 55)
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    17
  filter    :: "[i,i]=>i"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    18
  flat      :: "i=>i"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    19
  partition :: "[i,i]=>i"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    20
  insert    :: "[i,i,i]=>i"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    21
  isort     :: "i=>i"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    22
  qsort     :: "i=>i"
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    23
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    24
axioms
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    25
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    26
  map_def:     "map(f,l)   == lrec(l,[],%x xs g. f(x)$g)"
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    27
  comp_def:    "f o g == (%x. f(g(x)))"
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    28
  append_def:  "l @ m == lrec(l,m,%x xs g. x$g)"
24825
c4f13ab78f9d avoid unnamed infixes;
wenzelm
parents: 20140
diff changeset
    29
  member_def:  "a mem l == lrec(l,false,%h t g. if eq(a,h) then true else g)"
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    30
  filter_def:  "filter(f,l) == lrec(l,[],%x xs g. if f`x then x$g else g)"
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    31
  flat_def:    "flat(l) == lrec(l,[],%h t g. h @ g)"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    32
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    33
  insert_def:  "insert(f,a,l) == lrec(l,a$[],%h t g. if f`a`h then a$h$t else h$g)"
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    34
  isort_def:   "isort(f) == lam l. lrec(l,[],%h t g. insert(f,h,g))"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    35
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    36
  partition_def:
1149
5750eba8820d removed \...\ inside strings
clasohm
parents: 290
diff changeset
    37
  "partition(f,l) == letrec part l a b be lcase(l,<a,b>,%x xs.
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    38
                            if f`x then part(xs,x$a,b) else part(xs,a,x$b))
1149
5750eba8820d removed \...\ inside strings
clasohm
parents: 290
diff changeset
    39
                    in part(l,[],[])"
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    40
  qsort_def:   "qsort(f) == lam l. letrec qsortx l be lcase(l,[],%h t.
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    41
                                   let p be partition(f`h,t)
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
    42
                                   in split(p,%x y. qsortx(x) @ h$qsortx(y)))
1149
5750eba8820d removed \...\ inside strings
clasohm
parents: 290
diff changeset
    43
                          in qsortx(l)"
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
    44
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    45
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    46
lemmas list_defs = map_def comp_def append_def filter_def flat_def
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    47
  insert_def isort_def partition_def qsort_def
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    48
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    49
lemma listBs [simp]:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    50
  "!!f g. (f o g) = (%a. f(g(a)))"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    51
  "!!a f g. (f o g)(a) = f(g(a))"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    52
  "!!f. map(f,[]) = []"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    53
  "!!f x xs. map(f,x$xs) = f(x)$map(f,xs)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    54
  "!!m. [] @ m = m"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    55
  "!!x xs m. x$xs @ m = x$(xs @ m)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    56
  "!!f. filter(f,[]) = []"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    57
  "!!f x xs. filter(f,x$xs) = if f`x then x$filter(f,xs) else filter(f,xs)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    58
  "flat([]) = []"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    59
  "!!x xs. flat(x$xs) = x @ flat(xs)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    60
  "!!a f. insert(f,a,[]) = a$[]"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    61
  "!!a f xs. insert(f,a,x$xs) = if f`a`x then a$x$xs else x$insert(f,a,xs)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    62
  by (simp_all add: list_defs)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    63
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    64
lemma nmapBnil: "n:Nat ==> map(f) ^ n ` [] = []"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    65
  apply (erule Nat_ind)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    66
   apply simp_all
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    67
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    68
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    69
lemma nmapBcons: "n:Nat ==> map(f)^n`(x$xs) = (f^n`x)$(map(f)^n`xs)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    70
  apply (erule Nat_ind)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    71
   apply simp_all
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    72
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    73
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    74
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    75
lemma mapT: "[| !!x. x:A==>f(x):B;  l : List(A) |] ==> map(f,l) : List(B)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    76
  apply (unfold map_def)
27221
31328dc30196 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 24825
diff changeset
    77
  apply (tactic "typechk_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    78
  apply blast
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    79
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    80
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    81
lemma appendT: "[| l : List(A);  m : List(A) |] ==> l @ m : List(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    82
  apply (unfold append_def)
27221
31328dc30196 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 24825
diff changeset
    83
  apply (tactic "typechk_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    84
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    85
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    86
lemma appendTS:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    87
  "[| l : {l:List(A). m : {m:List(A).P(l @ m)}} |] ==> l @ m : {x:List(A). P(x)}"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    88
  by (blast intro!: SubtypeI appendT elim!: SubtypeE)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    89
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    90
lemma filterT: "[| f:A->Bool;   l : List(A) |] ==> filter(f,l) : List(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    91
  apply (unfold filter_def)
27221
31328dc30196 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 24825
diff changeset
    92
  apply (tactic "typechk_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    93
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    94
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    95
lemma flatT: "l : List(List(A)) ==> flat(l) : List(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    96
  apply (unfold flat_def)
27221
31328dc30196 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 24825
diff changeset
    97
  apply (tactic {* typechk_tac @{context} @{thms appendT} 1 *})
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    98
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
    99
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   100
lemma insertT: "[|  f : A->A->Bool; a:A; l : List(A) |] ==> insert(f,a,l) : List(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   101
  apply (unfold insert_def)
27221
31328dc30196 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 24825
diff changeset
   102
  apply (tactic "typechk_tac @{context} [] 1")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   103
  done
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   104
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   105
lemma insertTS:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   106
  "[| f : {f:A->A->Bool. a : {a:A. l : {l:List(A).P(insert(f,a,l))}}} |] ==>  
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   107
   insert(f,a,l)  : {x:List(A). P(x)}"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   108
  by (blast intro!: SubtypeI insertT elim!: SubtypeE)
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   109
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   110
lemma partitionT:
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   111
  "[| f:A->Bool;  l : List(A) |] ==> partition(f,l) : List(A)*List(A)"
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   112
  apply (unfold partition_def)
27221
31328dc30196 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 24825
diff changeset
   113
  apply (tactic "typechk_tac @{context} [] 1")
31328dc30196 proper context for tactics derived from res_inst_tac;
wenzelm
parents: 24825
diff changeset
   114
  apply (tactic "clean_ccs_tac @{context}")
20140
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   115
  apply (rule ListPRI [THEN wfstI, THEN ListPR_wf [THEN wmap_wf, THEN wfI]])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   116
    apply assumption+
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   117
  apply (rule ListPRI [THEN wfstI, THEN ListPR_wf [THEN wmap_wf, THEN wfI]])
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   118
   apply assumption+
98acc6d0fab6 removed obsolete ML files;
wenzelm
parents: 17456
diff changeset
   119
  done
17456
bcf7544875b2 converted to Isar theory format;
wenzelm
parents: 3837
diff changeset
   120
0
a5a9c433f639 Initial revision
clasohm
parents:
diff changeset
   121
end