src/HOL/List.thy
author berghofe
Fri Aug 02 12:16:11 1996 +0200 (1996-08-02)
changeset 1898 260a9711f507
parent 1824 44254696843a
child 1908 55d8e38262a8
permissions -rw-r--r--
Simplified primrec definitions.
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
paulson@1812
     8
TODO: delete list_all from this theory and from ex/Sorting, etc.
paulson@1812
     9
      use setOfList instead
clasohm@923
    10
*)
clasohm@923
    11
clasohm@923
    12
List = Arith +
clasohm@923
    13
clasohm@977
    14
datatype 'a list = "[]" ("[]") | "#" 'a ('a list) (infixr 65)
clasohm@923
    15
clasohm@923
    16
consts
clasohm@923
    17
clasohm@1475
    18
  "@"       :: ['a list, 'a list] => 'a list            (infixr 65)
clasohm@1370
    19
  drop      :: [nat, 'a list] => 'a list
clasohm@1370
    20
  filter    :: ['a => bool, 'a list] => 'a list
clasohm@1370
    21
  flat      :: 'a list list => 'a list
clasohm@1370
    22
  foldl     :: [['b,'a] => 'b, 'b, 'a list] => 'b
clasohm@1370
    23
  hd        :: 'a list => 'a
clasohm@1370
    24
  length    :: 'a list => nat
paulson@1812
    25
  setOfList :: ('a list => 'a set)
clasohm@1370
    26
  list_all  :: ('a => bool) => ('a list => bool)
clasohm@1370
    27
  map       :: ('a=>'b) => ('a list => 'b list)
clasohm@1475
    28
  mem       :: ['a, 'a list] => bool                    (infixl 55)
clasohm@1370
    29
  nth       :: [nat, 'a list] => 'a
clasohm@1370
    30
  take      :: [nat, 'a list] => 'a list
clasohm@1370
    31
  tl,ttl    :: 'a list => 'a list
clasohm@1370
    32
  rev       :: 'a list => 'a list
clasohm@923
    33
clasohm@923
    34
syntax
clasohm@923
    35
  (* list Enumeration *)
clasohm@1370
    36
  "@list"   :: args => 'a list                        ("[(_)]")
clasohm@923
    37
clasohm@923
    38
  (* Special syntax for list_all and filter *)
clasohm@1475
    39
  "@Alls"       :: [idt, 'a list, bool] => bool ("(2Alls _:_./ _)" 10)
clasohm@1475
    40
  "@filter"     :: [idt, 'a list, bool] => 'a list      ("(1[_:_ ./ _])")
clasohm@923
    41
clasohm@923
    42
translations
clasohm@923
    43
  "[x, xs]"     == "x#[xs]"
clasohm@923
    44
  "[x]"         == "x#[]"
clasohm@923
    45
clasohm@1475
    46
  "[x:xs . P]"  == "filter (%x.P) xs"
clasohm@1475
    47
  "Alls x:xs.P" == "list_all (%x.P) xs"
clasohm@923
    48
clasohm@923
    49
primrec hd list
berghofe@1898
    50
  "hd([]) = (@x.False)"
berghofe@1898
    51
  "hd(x#xs) = x"
clasohm@923
    52
primrec tl list
berghofe@1898
    53
  "tl([]) = (@x.False)"
berghofe@1898
    54
  "tl(x#xs) = xs"
clasohm@923
    55
primrec ttl list
clasohm@923
    56
  (* a "total" version of tl: *)
berghofe@1898
    57
  "ttl([]) = []"
berghofe@1898
    58
  "ttl(x#xs) = xs"
clasohm@923
    59
primrec "op mem" list
berghofe@1898
    60
  "x mem [] = False"
berghofe@1898
    61
  "x mem (y#ys) = (if y=x then True else x mem ys)"
paulson@1812
    62
primrec setOfList list
berghofe@1898
    63
  "setOfList [] = {}"
berghofe@1898
    64
  "setOfList (x#xs) = insert x (setOfList xs)"
clasohm@923
    65
primrec list_all list
clasohm@923
    66
  list_all_Nil  "list_all P [] = True"
clasohm@923
    67
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
clasohm@923
    68
primrec map list
berghofe@1898
    69
  "map f [] = []"
berghofe@1898
    70
  "map f (x#xs) = f(x)#map f xs"
clasohm@923
    71
primrec "op @" list
berghofe@1898
    72
  "[] @ ys = ys"
berghofe@1898
    73
  "(x#xs)@ys = x#(xs@ys)"
lcp@1169
    74
primrec rev list
berghofe@1898
    75
  "rev([]) = []"
berghofe@1898
    76
  "rev(x#xs) = rev(xs) @ [x]"
clasohm@923
    77
primrec filter list
berghofe@1898
    78
  "filter P [] = []"
berghofe@1898
    79
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
clasohm@923
    80
primrec foldl list
berghofe@1898
    81
  "foldl f a [] = a"
berghofe@1898
    82
  "foldl f a (x#xs) = foldl f (f a x) xs"
clasohm@923
    83
primrec length list
berghofe@1898
    84
  "length([]) = 0"
berghofe@1898
    85
  "length(x#xs) = Suc(length(xs))"
clasohm@923
    86
primrec flat list
berghofe@1898
    87
  "flat([]) = []"
berghofe@1898
    88
  "flat(x#xs) = x @ flat(xs)"
nipkow@1419
    89
primrec drop list
nipkow@1419
    90
  drop_Nil  "drop n [] = []"
nipkow@1419
    91
  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
nipkow@1419
    92
primrec take list
nipkow@1419
    93
  take_Nil  "take n [] = []"
nipkow@1419
    94
  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
clasohm@923
    95
defs
berghofe@1824
    96
  nth_def  "nth(n) == nat_rec hd (%m r xs. r(tl(xs))) n"
clasohm@923
    97
end
berghofe@1898
    98