src/HOL/List.thy
author paulson
Mon Oct 07 10:28:44 1996 +0200 (1996-10-07)
changeset 2056 93c093620c28
parent 1908 55d8e38262a8
child 2262 c7ee913746fd
permissions -rw-r--r--
Removed commands made redundant by new one-point rules
     1 (*  Title:      HOL/List.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   1994 TU Muenchen
     5 
     6 Definition of type 'a list as a datatype. This allows primrec to work.
     7 
     8 TODO: delete list_all from this theory and from ex/Sorting, etc.
     9       use set_of_list instead
    10 *)
    11 
    12 List = Arith +
    13 
    14 datatype 'a list = "[]" ("[]") | "#" 'a ('a list) (infixr 65)
    15 
    16 consts
    17 
    18   "@"         :: ['a list, 'a list] => 'a list            (infixr 65)
    19   drop        :: [nat, 'a list] => 'a list
    20   filter      :: ['a => bool, 'a list] => 'a list
    21   flat        :: 'a list list => 'a list
    22   foldl       :: [['b,'a] => 'b, 'b, 'a list] => 'b
    23   hd          :: 'a list => 'a
    24   length      :: 'a list => nat
    25   set_of_list :: ('a list => 'a set)
    26   list_all    :: ('a => bool) => ('a list => bool)
    27   map         :: ('a=>'b) => ('a list => 'b list)
    28   mem         :: ['a, 'a list] => bool                    (infixl 55)
    29   nth         :: [nat, 'a list] => 'a
    30   take        :: [nat, 'a list] => 'a list
    31   tl,ttl      :: 'a list => 'a list
    32   rev         :: 'a list => 'a list
    33 
    34 syntax
    35   (* list Enumeration *)
    36   "@list"     :: args => 'a list                        ("[(_)]")
    37 
    38   (* Special syntax for list_all and filter *)
    39   "@Alls"     :: [idt, 'a list, bool] => bool         ("(2Alls _:_./ _)" 10)
    40   "@filter"   :: [idt, 'a list, bool] => 'a list      ("(1[_:_ ./ _])")
    41 
    42 translations
    43   "[x, xs]"     == "x#[xs]"
    44   "[x]"         == "x#[]"
    45 
    46   "[x:xs . P]"  == "filter (%x.P) xs"
    47   "Alls x:xs.P" == "list_all (%x.P) xs"
    48 
    49 primrec hd list
    50   "hd([]) = (@x.False)"
    51   "hd(x#xs) = x"
    52 primrec tl list
    53   "tl([]) = (@x.False)"
    54   "tl(x#xs) = xs"
    55 primrec ttl list
    56   (* a "total" version of tl: *)
    57   "ttl([]) = []"
    58   "ttl(x#xs) = xs"
    59 primrec "op mem" list
    60   "x mem [] = False"
    61   "x mem (y#ys) = (if y=x then True else x mem ys)"
    62 primrec set_of_list list
    63   "set_of_list [] = {}"
    64   "set_of_list (x#xs) = insert x (set_of_list xs)"
    65 primrec list_all list
    66   list_all_Nil  "list_all P [] = True"
    67   list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
    68 primrec map list
    69   "map f [] = []"
    70   "map f (x#xs) = f(x)#map f xs"
    71 primrec "op @" list
    72   "[] @ ys = ys"
    73   "(x#xs)@ys = x#(xs@ys)"
    74 primrec rev list
    75   "rev([]) = []"
    76   "rev(x#xs) = rev(xs) @ [x]"
    77 primrec filter list
    78   "filter P [] = []"
    79   "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
    80 primrec foldl list
    81   "foldl f a [] = a"
    82   "foldl f a (x#xs) = foldl f (f a x) xs"
    83 primrec length list
    84   "length([]) = 0"
    85   "length(x#xs) = Suc(length(xs))"
    86 primrec flat list
    87   "flat([]) = []"
    88   "flat(x#xs) = x @ flat(xs)"
    89 primrec drop list
    90   drop_Nil  "drop n [] = []"
    91   drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
    92 primrec take list
    93   take_Nil  "take n [] = []"
    94   take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
    95 defs
    96   nth_def  "nth(n) == nat_rec hd (%m r xs. r(tl(xs))) n"
    97 end
    98