src/HOL/List.thy
author wenzelm
Fri Oct 10 19:02:28 1997 +0200 (1997-10-10)
changeset 3842 b55686a7b22c
parent 3589 244daa75f890
child 3896 ee8ebb74ec00
permissions -rw-r--r--
fixed dots;
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@3367
     9
List = Divides +
clasohm@923
    10
clasohm@977
    11
datatype 'a list = "[]" ("[]") | "#" '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@1908
    18
  hd          :: 'a list => 'a
nipkow@3465
    19
  set         :: 'a list => 'a set
paulson@1908
    20
  list_all    :: ('a => bool) => ('a list => bool)
paulson@1908
    21
  map         :: ('a=>'b) => ('a list => 'b list)
paulson@1908
    22
  mem         :: ['a, 'a list] => bool                    (infixl 55)
paulson@1908
    23
  nth         :: [nat, 'a list] => 'a
nipkow@2608
    24
  take, drop  :: [nat, 'a list] => 'a list
nipkow@2608
    25
  takeWhile,
nipkow@2608
    26
  dropWhile   :: ('a => bool) => 'a list => 'a list
paulson@1908
    27
  tl,ttl      :: 'a list => 'a list
paulson@1908
    28
  rev         :: 'a list => 'a list
nipkow@3589
    29
  replicate   :: nat => 'a => 'a list
clasohm@923
    30
clasohm@923
    31
syntax
clasohm@923
    32
  (* list Enumeration *)
wenzelm@2262
    33
  "@list"     :: args => 'a list                          ("[(_)]")
clasohm@923
    34
nipkow@2512
    35
  (* Special syntax for filter *)
wenzelm@2262
    36
  "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_:_ ./ _])")
clasohm@923
    37
clasohm@923
    38
translations
clasohm@923
    39
  "[x, xs]"     == "x#[xs]"
clasohm@923
    40
  "[x]"         == "x#[]"
wenzelm@3842
    41
  "[x:xs . P]"  == "filter (%x. P) xs"
clasohm@923
    42
wenzelm@2262
    43
syntax (symbols)
wenzelm@2262
    44
  "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_\\<in>_ ./ _])")
wenzelm@2262
    45
wenzelm@2262
    46
paulson@3342
    47
consts
paulson@3342
    48
  lists        :: 'a set => 'a list set
paulson@3342
    49
paulson@3342
    50
  inductive "lists A"
paulson@3342
    51
  intrs
paulson@3342
    52
    Nil  "[]: lists A"
paulson@3342
    53
    Cons "[| a: A;  l: lists A |] ==> a#l : lists A"
paulson@3342
    54
paulson@3342
    55
paulson@3437
    56
(*Function "size" is overloaded for all datatypes.  Users may refer to the
paulson@3437
    57
  list version as "length".*)
paulson@3437
    58
syntax   length :: 'a list => nat
nipkow@3507
    59
translations  "length"  =>  "size:: _ list => nat"
paulson@3437
    60
clasohm@923
    61
primrec hd list
nipkow@3320
    62
  "hd([]) = arbitrary"
berghofe@1898
    63
  "hd(x#xs) = x"
clasohm@923
    64
primrec tl list
nipkow@3320
    65
  "tl([]) = arbitrary"
berghofe@1898
    66
  "tl(x#xs) = xs"
clasohm@923
    67
primrec ttl list
clasohm@923
    68
  (* a "total" version of tl: *)
berghofe@1898
    69
  "ttl([]) = []"
berghofe@1898
    70
  "ttl(x#xs) = xs"
clasohm@923
    71
primrec "op mem" list
berghofe@1898
    72
  "x mem [] = False"
berghofe@1898
    73
  "x mem (y#ys) = (if y=x then True else x mem ys)"
nipkow@3465
    74
primrec set list
nipkow@3465
    75
  "set [] = {}"
nipkow@3465
    76
  "set (x#xs) = insert x (set xs)"
clasohm@923
    77
primrec list_all list
clasohm@923
    78
  list_all_Nil  "list_all P [] = True"
clasohm@923
    79
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
clasohm@923
    80
primrec map list
berghofe@1898
    81
  "map f [] = []"
berghofe@1898
    82
  "map f (x#xs) = f(x)#map f xs"
clasohm@923
    83
primrec "op @" list
berghofe@1898
    84
  "[] @ ys = ys"
berghofe@1898
    85
  "(x#xs)@ys = x#(xs@ys)"
lcp@1169
    86
primrec rev list
berghofe@1898
    87
  "rev([]) = []"
berghofe@1898
    88
  "rev(x#xs) = rev(xs) @ [x]"
clasohm@923
    89
primrec filter list
berghofe@1898
    90
  "filter P [] = []"
berghofe@1898
    91
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
clasohm@923
    92
primrec foldl list
berghofe@1898
    93
  "foldl f a [] = a"
berghofe@1898
    94
  "foldl f a (x#xs) = foldl f (f a x) xs"
nipkow@2608
    95
primrec concat list
nipkow@2608
    96
  "concat([]) = []"
nipkow@2608
    97
  "concat(x#xs) = x @ concat(xs)"
nipkow@1419
    98
primrec drop list
nipkow@1419
    99
  drop_Nil  "drop n [] = []"
nipkow@1419
   100
  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
nipkow@1419
   101
primrec take list
nipkow@1419
   102
  take_Nil  "take n [] = []"
nipkow@1419
   103
  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
pusch@2738
   104
primrec nth nat
pusch@2738
   105
  "nth 0 xs = hd xs"
pusch@2738
   106
  "nth (Suc n) xs = nth n (tl xs)"
nipkow@2608
   107
primrec takeWhile list
nipkow@2608
   108
  "takeWhile P [] = []"
nipkow@2608
   109
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
nipkow@2608
   110
primrec dropWhile list
nipkow@2608
   111
  "dropWhile P [] = []"
nipkow@3584
   112
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
nipkow@3589
   113
primrec replicate nat
nipkow@3589
   114
replicate_0   "replicate 0 x       = []"
nipkow@3589
   115
replicate_Suc "replicate (Suc n) x = x # replicate n x"
paulson@3196
   116
clasohm@923
   117
end
nipkow@3507
   118
nipkow@3507
   119
ML
nipkow@3507
   120
nipkow@3507
   121
local
nipkow@3507
   122
nipkow@3507
   123
(* translating size::list -> length *)
nipkow@3507
   124
nipkow@3507
   125
fun size_tr' (Type ("fun", (Type ("list", _) :: _))) [t] =
nipkow@3507
   126
      Syntax.const "length" $ t
nipkow@3507
   127
  | size_tr'   _ _ = raise Match;
nipkow@3507
   128
nipkow@3507
   129
in
nipkow@3507
   130
nipkow@3507
   131
val typed_print_translation = [("size", size_tr')];
nipkow@3507
   132
nipkow@3507
   133
end;