src/HOL/List.thy
author wenzelm
Thu Jan 23 14:19:16 1997 +0100 (1997-01-23)
changeset 2545 d10abc8c11fb
parent 2512 0231e4f467f2
child 2608 450c9b682a92
permissions -rw-r--r--
added AxClasses test;
     1 (*  Title:      HOL/List.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   1994 TU Muenchen
     5 
     6 The datatype of finite lists.
     7 *)
     8 
     9 List = Arith +
    10 
    11 datatype 'a list = "[]" ("[]") | "#" 'a ('a list) (infixr 65)
    12 
    13 consts
    14 
    15   "@"         :: ['a list, 'a list] => 'a list            (infixr 65)
    16   drop        :: [nat, 'a list] => 'a list
    17   filter      :: ['a => bool, 'a list] => 'a list
    18   flat        :: 'a list list => 'a list
    19   foldl       :: [['b,'a] => 'b, 'b, 'a list] => 'b
    20   hd          :: 'a list => 'a
    21   length      :: 'a list => nat
    22   set_of_list :: ('a list => 'a set)
    23   list_all    :: ('a => bool) => ('a list => bool)
    24   map         :: ('a=>'b) => ('a list => 'b list)
    25   mem         :: ['a, 'a list] => bool                    (infixl 55)
    26   nth         :: [nat, 'a list] => 'a
    27   take        :: [nat, 'a list] => 'a list
    28   tl,ttl      :: 'a list => 'a list
    29   rev         :: 'a list => 'a list
    30 
    31 syntax
    32   (* list Enumeration *)
    33   "@list"     :: args => 'a list                          ("[(_)]")
    34 
    35   (* Special syntax for filter *)
    36   "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_:_ ./ _])")
    37 
    38 translations
    39   "[x, xs]"     == "x#[xs]"
    40   "[x]"         == "x#[]"
    41   "[x:xs . P]"  == "filter (%x.P) xs"
    42 
    43 syntax (symbols)
    44   "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_\\<in>_ ./ _])")
    45 
    46 
    47 primrec hd list
    48   "hd([]) = (@x.False)"
    49   "hd(x#xs) = x"
    50 primrec tl list
    51   "tl([]) = (@x.False)"
    52   "tl(x#xs) = xs"
    53 primrec ttl list
    54   (* a "total" version of tl: *)
    55   "ttl([]) = []"
    56   "ttl(x#xs) = xs"
    57 primrec "op mem" list
    58   "x mem [] = False"
    59   "x mem (y#ys) = (if y=x then True else x mem ys)"
    60 primrec set_of_list list
    61   "set_of_list [] = {}"
    62   "set_of_list (x#xs) = insert x (set_of_list xs)"
    63 primrec list_all list
    64   list_all_Nil  "list_all P [] = True"
    65   list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
    66 primrec map list
    67   "map f [] = []"
    68   "map f (x#xs) = f(x)#map f xs"
    69 primrec "op @" list
    70   "[] @ ys = ys"
    71   "(x#xs)@ys = x#(xs@ys)"
    72 primrec rev list
    73   "rev([]) = []"
    74   "rev(x#xs) = rev(xs) @ [x]"
    75 primrec filter list
    76   "filter P [] = []"
    77   "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
    78 primrec foldl list
    79   "foldl f a [] = a"
    80   "foldl f a (x#xs) = foldl f (f a x) xs"
    81 primrec length list
    82   "length([]) = 0"
    83   "length(x#xs) = Suc(length(xs))"
    84 primrec flat list
    85   "flat([]) = []"
    86   "flat(x#xs) = x @ flat(xs)"
    87 primrec drop list
    88   drop_Nil  "drop n [] = []"
    89   drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
    90 primrec take list
    91   take_Nil  "take n [] = []"
    92   take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
    93 defs
    94   nth_def  "nth(n) == nat_rec hd (%m r xs. r(tl(xs))) n"
    95 end