src/HOL/List.thy
author clasohm
Mon Feb 05 21:27:16 1996 +0100 (1996-02-05)
changeset 1475 7f5a4cd08209
parent 1419 a6a034a47a71
child 1812 debfc40b7756
permissions -rw-r--r--
expanded tabs; renamed subtype to typedef;
incorporated Konrad's changes
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
clasohm@923
     6
Definition of type 'a list as a datatype. This allows primrec to work.
clasohm@923
     7
clasohm@923
     8
*)
clasohm@923
     9
clasohm@923
    10
List = Arith +
clasohm@923
    11
clasohm@977
    12
datatype 'a list = "[]" ("[]") | "#" 'a ('a list) (infixr 65)
clasohm@923
    13
clasohm@923
    14
consts
clasohm@923
    15
clasohm@1475
    16
  "@"       :: ['a list, 'a list] => 'a list            (infixr 65)
clasohm@1370
    17
  drop      :: [nat, 'a list] => 'a list
clasohm@1370
    18
  filter    :: ['a => bool, 'a list] => 'a list
clasohm@1370
    19
  flat      :: 'a list list => 'a list
clasohm@1370
    20
  foldl     :: [['b,'a] => 'b, 'b, 'a list] => 'b
clasohm@1370
    21
  hd        :: 'a list => 'a
clasohm@1370
    22
  length    :: 'a list => nat
clasohm@1370
    23
  list_all  :: ('a => bool) => ('a list => bool)
clasohm@1370
    24
  map       :: ('a=>'b) => ('a list => 'b list)
clasohm@1475
    25
  mem       :: ['a, 'a list] => bool                    (infixl 55)
clasohm@1370
    26
  nth       :: [nat, 'a list] => 'a
clasohm@1370
    27
  take      :: [nat, 'a list] => 'a list
clasohm@1370
    28
  tl,ttl    :: 'a list => 'a list
clasohm@1370
    29
  rev       :: 'a list => 'a list
clasohm@923
    30
clasohm@923
    31
syntax
clasohm@923
    32
  (* list Enumeration *)
clasohm@1370
    33
  "@list"   :: args => 'a list                        ("[(_)]")
clasohm@923
    34
clasohm@923
    35
  (* Special syntax for list_all and filter *)
clasohm@1475
    36
  "@Alls"       :: [idt, 'a list, bool] => bool ("(2Alls _:_./ _)" 10)
clasohm@1475
    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@923
    42
clasohm@1475
    43
  "[x:xs . P]"  == "filter (%x.P) xs"
clasohm@1475
    44
  "Alls x:xs.P" == "list_all (%x.P) xs"
clasohm@923
    45
clasohm@923
    46
primrec hd list
clasohm@923
    47
  hd_Nil  "hd([]) = (@x.False)"
clasohm@923
    48
  hd_Cons "hd(x#xs) = x"
clasohm@923
    49
primrec tl list
clasohm@923
    50
  tl_Nil  "tl([]) = (@x.False)"
clasohm@923
    51
  tl_Cons "tl(x#xs) = xs"
clasohm@923
    52
primrec ttl list
clasohm@923
    53
  (* a "total" version of tl: *)
clasohm@923
    54
  ttl_Nil  "ttl([]) = []"
clasohm@923
    55
  ttl_Cons "ttl(x#xs) = xs"
clasohm@923
    56
primrec "op mem" list
clasohm@923
    57
  mem_Nil  "x mem [] = False"
clasohm@965
    58
  mem_Cons "x mem (y#ys) = (if y=x then True else x mem ys)"
clasohm@923
    59
primrec list_all list
clasohm@923
    60
  list_all_Nil  "list_all P [] = True"
clasohm@923
    61
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
clasohm@923
    62
primrec map list
clasohm@923
    63
  map_Nil  "map f [] = []"
clasohm@923
    64
  map_Cons "map f (x#xs) = f(x)#map f xs"
clasohm@923
    65
primrec "op @" list
clasohm@923
    66
  append_Nil  "[] @ ys = ys"
clasohm@923
    67
  append_Cons "(x#xs)@ys = x#(xs@ys)"
lcp@1169
    68
primrec rev list
lcp@1169
    69
  rev_Nil  "rev([]) = []"
lcp@1169
    70
  rev_Cons "rev(x#xs) = rev(xs) @ [x]"
clasohm@923
    71
primrec filter list
clasohm@923
    72
  filter_Nil  "filter P [] = []"
clasohm@965
    73
  filter_Cons "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
clasohm@923
    74
primrec foldl list
clasohm@923
    75
  foldl_Nil  "foldl f a [] = a"
clasohm@923
    76
  foldl_Cons "foldl f a (x#xs) = foldl f (f a x) xs"
clasohm@923
    77
primrec length list
clasohm@923
    78
  length_Nil  "length([]) = 0"
clasohm@923
    79
  length_Cons "length(x#xs) = Suc(length(xs))"
clasohm@923
    80
primrec flat list
clasohm@923
    81
  flat_Nil  "flat([]) = []"
clasohm@923
    82
  flat_Cons "flat(x#xs) = x @ flat(xs)"
nipkow@1419
    83
primrec drop list
nipkow@1419
    84
  drop_Nil  "drop n [] = []"
nipkow@1419
    85
  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
nipkow@1419
    86
primrec take list
nipkow@1419
    87
  take_Nil  "take n [] = []"
nipkow@1419
    88
  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
clasohm@923
    89
defs
nipkow@1327
    90
  nth_def  "nth(n) == nat_rec n hd (%m r xs. r(tl(xs)))"
clasohm@923
    91
end