src/HOL/List.thy
author nipkow
Mon Jan 10 16:06:43 2000 +0100 (2000-01-10)
changeset 8115 c802042066e8
parent 8000 acafa0f15131
child 8490 6e0f23304061
permissions -rw-r--r--
Forgot to "call" MicroJava in makefile.
Added list_all2 to List.
clasohm@923
     1
(*  Title:      HOL/List.thy
clasohm@923
     2
    ID:         $Id$
clasohm@923
     3
    Author:     Tobias Nipkow
clasohm@923
     4
    Copyright   1994 TU Muenchen
clasohm@923
     5
nipkow@2512
     6
The datatype of finite lists.
clasohm@923
     7
*)
clasohm@923
     8
paulson@7032
     9
List = Datatype + WF_Rel + NatBin +
clasohm@923
    10
wenzelm@7224
    11
datatype 'a list = Nil ("[]") | Cons 'a ('a list) (infixr "#" 65)
clasohm@923
    12
clasohm@923
    13
consts
paulson@1908
    14
  "@"         :: ['a list, 'a list] => 'a list            (infixr 65)
paulson@1908
    15
  filter      :: ['a => bool, 'a list] => 'a list
nipkow@2608
    16
  concat      :: 'a list list => 'a list
paulson@1908
    17
  foldl       :: [['b,'a] => 'b, 'b, 'a list] => 'b
paulson@8000
    18
  foldr       :: [['a,'b] => 'b, 'a list, 'b] => 'b
nipkow@3896
    19
  hd, last    :: 'a list => 'a
nipkow@3465
    20
  set         :: 'a list => 'a set
oheimb@5518
    21
  list_all    :: ('a => bool) => ('a list => bool)
nipkow@8115
    22
  list_all2   :: ('a => 'b => bool) => 'a list => 'b list => bool
paulson@1908
    23
  map         :: ('a=>'b) => ('a list => 'b list)
oheimb@5518
    24
  mem         :: ['a, 'a list] => bool                    (infixl 55)
nipkow@4502
    25
  nth         :: ['a list, nat] => 'a			  (infixl "!" 100)
nipkow@5077
    26
  list_update :: 'a list => nat => 'a => 'a list
nipkow@2608
    27
  take, drop  :: [nat, 'a list] => 'a list
nipkow@2608
    28
  takeWhile,
nipkow@2608
    29
  dropWhile   :: ('a => bool) => 'a list => 'a list
nipkow@3896
    30
  tl, butlast :: 'a list => 'a list
paulson@1908
    31
  rev         :: 'a list => 'a list
oheimb@4132
    32
  zip	      :: "'a list => 'b list => ('a * 'b) list"
nipkow@5427
    33
  upt         :: nat => nat => nat list                   ("(1[_../_'(])")
nipkow@4605
    34
  remdups     :: 'a list => 'a list
nipkow@4605
    35
  nodups      :: "'a list => bool"
nipkow@3589
    36
  replicate   :: nat => 'a => 'a list
clasohm@923
    37
nipkow@5077
    38
nonterminals
nipkow@5077
    39
  lupdbinds  lupdbind
nipkow@5077
    40
clasohm@923
    41
syntax
clasohm@923
    42
  (* list Enumeration *)
wenzelm@2262
    43
  "@list"     :: args => 'a list                          ("[(_)]")
clasohm@923
    44
nipkow@2512
    45
  (* Special syntax for filter *)
oheimb@5295
    46
  "@filter"   :: [pttrn, 'a list, bool] => 'a list        ("(1[_:_ ./ _])")
clasohm@923
    47
nipkow@5077
    48
  (* list update *)
nipkow@5077
    49
  "_lupdbind"      :: ['a, 'a] => lupdbind            ("(2_ :=/ _)")
nipkow@5077
    50
  ""               :: lupdbind => lupdbinds           ("_")
nipkow@5077
    51
  "_lupdbinds"     :: [lupdbind, lupdbinds] => lupdbinds ("_,/ _")
nipkow@5077
    52
  "_LUpdate"       :: ['a, lupdbinds] => 'a           ("_/[(_)]" [900,0] 900)
nipkow@5077
    53
nipkow@5427
    54
  upto        :: nat => nat => nat list                   ("(1[_../_])")
nipkow@5427
    55
clasohm@923
    56
translations
clasohm@923
    57
  "[x, xs]"     == "x#[xs]"
clasohm@923
    58
  "[x]"         == "x#[]"
wenzelm@3842
    59
  "[x:xs . P]"  == "filter (%x. P) xs"
clasohm@923
    60
nipkow@5077
    61
  "_LUpdate xs (_lupdbinds b bs)"  == "_LUpdate (_LUpdate xs b) bs"
nipkow@5077
    62
  "xs[i:=x]"                       == "list_update xs i x"
nipkow@5077
    63
nipkow@5427
    64
  "[i..j]" == "[i..(Suc j)(]"
nipkow@5427
    65
nipkow@5427
    66
wenzelm@2262
    67
syntax (symbols)
oheimb@5295
    68
  "@filter"   :: [pttrn, 'a list, bool] => 'a list        ("(1[_\\<in>_ ./ _])")
wenzelm@2262
    69
wenzelm@2262
    70
paulson@3342
    71
consts
paulson@3342
    72
  lists        :: 'a set => 'a list set
paulson@3342
    73
paulson@3342
    74
  inductive "lists A"
paulson@3342
    75
  intrs
paulson@3342
    76
    Nil  "[]: lists A"
paulson@3342
    77
    Cons "[| a: A;  l: lists A |] ==> a#l : lists A"
paulson@3342
    78
paulson@3342
    79
paulson@3437
    80
(*Function "size" is overloaded for all datatypes.  Users may refer to the
paulson@3437
    81
  list version as "length".*)
paulson@3437
    82
syntax   length :: 'a list => nat
nipkow@3507
    83
translations  "length"  =>  "size:: _ list => nat"
paulson@3437
    84
berghofe@5183
    85
primrec
nipkow@3320
    86
  "hd([]) = arbitrary"
berghofe@1898
    87
  "hd(x#xs) = x"
berghofe@5183
    88
primrec
nipkow@3896
    89
  "tl([]) = []"
berghofe@1898
    90
  "tl(x#xs) = xs"
berghofe@5183
    91
primrec
nipkow@3896
    92
  "last [] = arbitrary"
nipkow@3896
    93
  "last(x#xs) = (if xs=[] then x else last xs)"
berghofe@5183
    94
primrec
nipkow@3896
    95
  "butlast [] = []"
nipkow@3896
    96
  "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
berghofe@5183
    97
primrec
oheimb@5518
    98
  "x mem [] = False"
oheimb@5518
    99
  "x mem (y#ys) = (if y=x then True else x mem ys)"
oheimb@5518
   100
primrec
nipkow@3465
   101
  "set [] = {}"
nipkow@3465
   102
  "set (x#xs) = insert x (set xs)"
berghofe@5183
   103
primrec
oheimb@5518
   104
  list_all_Nil  "list_all P [] = True"
oheimb@5518
   105
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
oheimb@5518
   106
primrec
berghofe@1898
   107
  "map f [] = []"
berghofe@1898
   108
  "map f (x#xs) = f(x)#map f xs"
berghofe@5183
   109
primrec
berghofe@5183
   110
  append_Nil  "[]    @ys = ys"
berghofe@5183
   111
  append_Cons "(x#xs)@ys = x#(xs@ys)"
berghofe@5183
   112
primrec
berghofe@1898
   113
  "rev([]) = []"
berghofe@1898
   114
  "rev(x#xs) = rev(xs) @ [x]"
berghofe@5183
   115
primrec
berghofe@1898
   116
  "filter P [] = []"
berghofe@1898
   117
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
berghofe@5183
   118
primrec
paulson@6141
   119
  foldl_Nil  "foldl f a [] = a"
paulson@6141
   120
  foldl_Cons "foldl f a (x#xs) = foldl f (f a x) xs"
berghofe@5183
   121
primrec
paulson@8000
   122
  "foldr f [] a = a"
paulson@8000
   123
  "foldr f (x#xs) a = f x (foldr f xs a)"
paulson@8000
   124
primrec
nipkow@2608
   125
  "concat([]) = []"
nipkow@2608
   126
  "concat(x#xs) = x @ concat(xs)"
berghofe@5183
   127
primrec
nipkow@1419
   128
  drop_Nil  "drop n [] = []"
nipkow@1419
   129
  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
pusch@6408
   130
  (* Warning: simpset does not contain this definition but separate theorems 
pusch@6408
   131
     for n=0 / n=Suc k*)
berghofe@5183
   132
primrec
nipkow@1419
   133
  take_Nil  "take n [] = []"
nipkow@1419
   134
  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
pusch@6408
   135
  (* Warning: simpset does not contain this definition but separate theorems 
pusch@6408
   136
     for n=0 / n=Suc k*)
pusch@6408
   137
primrec 
pusch@6408
   138
  nth_Cons  "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
pusch@6408
   139
  (* Warning: simpset does not contain this definition but separate theorems 
pusch@6408
   140
     for n=0 / n=Suc k*)
berghofe@5183
   141
primrec
nipkow@5077
   142
 "    [][i:=v] = []"
nipkow@5077
   143
 "(x#xs)[i:=v] = (case i of 0     => v # xs 
nipkow@5077
   144
			  | Suc j => x # xs[j:=v])"
berghofe@5183
   145
primrec
nipkow@2608
   146
  "takeWhile P [] = []"
nipkow@2608
   147
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
berghofe@5183
   148
primrec
nipkow@2608
   149
  "dropWhile P [] = []"
nipkow@3584
   150
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
berghofe@5183
   151
primrec
oheimb@4132
   152
  "zip xs []     = []"
nipkow@6306
   153
  "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
pusch@6408
   154
  (* Warning: simpset does not contain this definition but separate theorems 
pusch@6408
   155
     for xs=[] / xs=z#zs *)
nipkow@5427
   156
primrec
nipkow@5427
   157
  "[i..0(] = []"
nipkow@5427
   158
  "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
berghofe@5183
   159
primrec
nipkow@4605
   160
  "nodups []     = True"
nipkow@4605
   161
  "nodups (x#xs) = (x ~: set xs & nodups xs)"
berghofe@5183
   162
primrec
nipkow@4605
   163
  "remdups [] = []"
nipkow@4605
   164
  "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
berghofe@5183
   165
primrec
oheimb@5443
   166
  replicate_0   "replicate  0      x = []"
berghofe@5183
   167
  replicate_Suc "replicate (Suc n) x = x # replicate n x"
nipkow@8115
   168
defs
nipkow@8115
   169
 list_all2_def
nipkow@8115
   170
 "list_all2 P xs ys == length xs = length ys & (!(x,y):set(zip xs ys). P x y)"
nipkow@8115
   171
paulson@3196
   172
pusch@6408
   173
(** Lexicographic orderings on lists **)
nipkow@5281
   174
nipkow@5281
   175
consts
nipkow@5281
   176
 lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
nipkow@5281
   177
primrec
nipkow@5281
   178
"lexn r 0       = {}"
nipkow@5281
   179
"lexn r (Suc n) = (prod_fun (split op#) (split op#) `` (r ** lexn r n)) Int
nipkow@5281
   180
                  {(xs,ys). length xs = Suc n & length ys = Suc n}"
nipkow@5281
   181
nipkow@5281
   182
constdefs
nipkow@5281
   183
 lex :: "('a * 'a)set => ('a list * 'a list)set"
nipkow@5281
   184
"lex r == UN n. lexn r n"
nipkow@5281
   185
nipkow@5281
   186
 lexico :: "('a * 'a)set => ('a list * 'a list)set"
nipkow@5281
   187
"lexico r == inv_image (less_than ** lex r) (%xs. (length xs, xs))"
nipkow@5281
   188
clasohm@923
   189
end
nipkow@3507
   190
nipkow@3507
   191
ML
nipkow@3507
   192
nipkow@3507
   193
local
nipkow@3507
   194
nipkow@3507
   195
(* translating size::list -> length *)
nipkow@3507
   196
wenzelm@4151
   197
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
nipkow@3507
   198
      Syntax.const "length" $ t
wenzelm@4151
   199
  | size_tr' _ _ _ = raise Match;
nipkow@3507
   200
nipkow@3507
   201
in
nipkow@3507
   202
nipkow@3507
   203
val typed_print_translation = [("size", size_tr')];
nipkow@3507
   204
nipkow@3507
   205
end;