src/HOL/List.thy
author pusch
Thu Mar 06 16:04:23 1997 +0100 (1997-03-06)
changeset 2738 e28a0668dbfe
parent 2608 450c9b682a92
child 3196 c522bc46aea7
permissions -rw-r--r--
primrec definition for nth
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
clasohm@923
     9
List = Arith +
clasohm@923
    10
clasohm@977
    11
datatype 'a list = "[]" ("[]") | "#" 'a ('a list) (infixr 65)
clasohm@923
    12
clasohm@923
    13
consts
clasohm@923
    14
paulson@1908
    15
  "@"         :: ['a list, 'a list] => 'a list            (infixr 65)
paulson@1908
    16
  filter      :: ['a => bool, 'a list] => 'a list
nipkow@2608
    17
  concat      :: 'a list list => 'a list
paulson@1908
    18
  foldl       :: [['b,'a] => 'b, 'b, 'a list] => 'b
paulson@1908
    19
  hd          :: 'a list => 'a
paulson@1908
    20
  length      :: 'a list => nat
nipkow@2608
    21
  set_of_list :: 'a list => 'a set
paulson@1908
    22
  list_all    :: ('a => bool) => ('a list => bool)
paulson@1908
    23
  map         :: ('a=>'b) => ('a list => 'b list)
paulson@1908
    24
  mem         :: ['a, 'a list] => bool                    (infixl 55)
paulson@1908
    25
  nth         :: [nat, 'a list] => 'a
nipkow@2608
    26
  take, drop  :: [nat, 'a list] => 'a list
nipkow@2608
    27
  takeWhile,
nipkow@2608
    28
  dropWhile   :: ('a => bool) => 'a list => 'a list
paulson@1908
    29
  tl,ttl      :: 'a list => 'a list
paulson@1908
    30
  rev         :: 'a list => 'a list
clasohm@923
    31
clasohm@923
    32
syntax
clasohm@923
    33
  (* list Enumeration *)
wenzelm@2262
    34
  "@list"     :: args => 'a list                          ("[(_)]")
clasohm@923
    35
nipkow@2512
    36
  (* Special syntax for filter *)
wenzelm@2262
    37
  "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_:_ ./ _])")
clasohm@923
    38
clasohm@923
    39
translations
clasohm@923
    40
  "[x, xs]"     == "x#[xs]"
clasohm@923
    41
  "[x]"         == "x#[]"
clasohm@1475
    42
  "[x:xs . P]"  == "filter (%x.P) xs"
clasohm@923
    43
wenzelm@2262
    44
syntax (symbols)
wenzelm@2262
    45
  "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_\\<in>_ ./ _])")
wenzelm@2262
    46
wenzelm@2262
    47
clasohm@923
    48
primrec hd list
berghofe@1898
    49
  "hd([]) = (@x.False)"
berghofe@1898
    50
  "hd(x#xs) = x"
clasohm@923
    51
primrec tl list
berghofe@1898
    52
  "tl([]) = (@x.False)"
berghofe@1898
    53
  "tl(x#xs) = xs"
clasohm@923
    54
primrec ttl list
clasohm@923
    55
  (* a "total" version of tl: *)
berghofe@1898
    56
  "ttl([]) = []"
berghofe@1898
    57
  "ttl(x#xs) = xs"
clasohm@923
    58
primrec "op mem" list
berghofe@1898
    59
  "x mem [] = False"
berghofe@1898
    60
  "x mem (y#ys) = (if y=x then True else x mem ys)"
paulson@1908
    61
primrec set_of_list list
paulson@1908
    62
  "set_of_list [] = {}"
paulson@1908
    63
  "set_of_list (x#xs) = insert x (set_of_list xs)"
clasohm@923
    64
primrec list_all list
clasohm@923
    65
  list_all_Nil  "list_all P [] = True"
clasohm@923
    66
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
clasohm@923
    67
primrec map list
berghofe@1898
    68
  "map f [] = []"
berghofe@1898
    69
  "map f (x#xs) = f(x)#map f xs"
clasohm@923
    70
primrec "op @" list
berghofe@1898
    71
  "[] @ ys = ys"
berghofe@1898
    72
  "(x#xs)@ys = x#(xs@ys)"
lcp@1169
    73
primrec rev list
berghofe@1898
    74
  "rev([]) = []"
berghofe@1898
    75
  "rev(x#xs) = rev(xs) @ [x]"
clasohm@923
    76
primrec filter list
berghofe@1898
    77
  "filter P [] = []"
berghofe@1898
    78
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
clasohm@923
    79
primrec foldl list
berghofe@1898
    80
  "foldl f a [] = a"
berghofe@1898
    81
  "foldl f a (x#xs) = foldl f (f a x) xs"
clasohm@923
    82
primrec length list
berghofe@1898
    83
  "length([]) = 0"
berghofe@1898
    84
  "length(x#xs) = Suc(length(xs))"
nipkow@2608
    85
primrec concat list
nipkow@2608
    86
  "concat([]) = []"
nipkow@2608
    87
  "concat(x#xs) = x @ concat(xs)"
nipkow@1419
    88
primrec drop list
nipkow@1419
    89
  drop_Nil  "drop n [] = []"
nipkow@1419
    90
  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
nipkow@1419
    91
primrec take list
nipkow@1419
    92
  take_Nil  "take n [] = []"
nipkow@1419
    93
  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
pusch@2738
    94
primrec nth nat
pusch@2738
    95
  "nth 0 xs = hd xs"
pusch@2738
    96
  "nth (Suc n) xs = nth n (tl xs)"
nipkow@2608
    97
primrec takeWhile list
nipkow@2608
    98
  "takeWhile P [] = []"
nipkow@2608
    99
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
nipkow@2608
   100
primrec dropWhile list
nipkow@2608
   101
  "dropWhile P [] = []"
nipkow@2608
   102
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else xs)"
clasohm@923
   103
end