src/HOL/List.thy
author clasohm
Wed Mar 13 11:55:25 1996 +0100 (1996-03-13 ago)
changeset 1574 5a63ab90ee8a
parent 1475 7f5a4cd08209
child 1812 debfc40b7756
permissions -rw-r--r--
modified primrec so it can be used in MiniML/Type.thy
     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 *)
     9 
    10 List = Arith +
    11 
    12 datatype 'a list = "[]" ("[]") | "#" 'a ('a list) (infixr 65)
    13 
    14 consts
    15 
    16   "@"       :: ['a list, 'a list] => 'a list            (infixr 65)
    17   drop      :: [nat, 'a list] => 'a list
    18   filter    :: ['a => bool, 'a list] => 'a list
    19   flat      :: 'a list list => 'a list
    20   foldl     :: [['b,'a] => 'b, 'b, 'a list] => 'b
    21   hd        :: 'a list => 'a
    22   length    :: 'a list => nat
    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 list_all and filter *)
    36   "@Alls"       :: [idt, 'a list, bool] => bool ("(2Alls _:_./ _)" 10)
    37   "@filter"     :: [idt, 'a list, bool] => 'a list      ("(1[_:_ ./ _])")
    38 
    39 translations
    40   "[x, xs]"     == "x#[xs]"
    41   "[x]"         == "x#[]"
    42 
    43   "[x:xs . P]"  == "filter (%x.P) xs"
    44   "Alls x:xs.P" == "list_all (%x.P) xs"
    45 
    46 primrec hd list
    47   hd_Nil  "hd([]) = (@x.False)"
    48   hd_Cons "hd(x#xs) = x"
    49 primrec tl list
    50   tl_Nil  "tl([]) = (@x.False)"
    51   tl_Cons "tl(x#xs) = xs"
    52 primrec ttl list
    53   (* a "total" version of tl: *)
    54   ttl_Nil  "ttl([]) = []"
    55   ttl_Cons "ttl(x#xs) = xs"
    56 primrec "op mem" list
    57   mem_Nil  "x mem [] = False"
    58   mem_Cons "x mem (y#ys) = (if y=x then True else x mem ys)"
    59 primrec list_all list
    60   list_all_Nil  "list_all P [] = True"
    61   list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
    62 primrec map list
    63   map_Nil  "map f [] = []"
    64   map_Cons "map f (x#xs) = f(x)#map f xs"
    65 primrec "op @" list
    66   append_Nil  "[] @ ys = ys"
    67   append_Cons "(x#xs)@ys = x#(xs@ys)"
    68 primrec rev list
    69   rev_Nil  "rev([]) = []"
    70   rev_Cons "rev(x#xs) = rev(xs) @ [x]"
    71 primrec filter list
    72   filter_Nil  "filter P [] = []"
    73   filter_Cons "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
    74 primrec foldl list
    75   foldl_Nil  "foldl f a [] = a"
    76   foldl_Cons "foldl f a (x#xs) = foldl f (f a x) xs"
    77 primrec length list
    78   length_Nil  "length([]) = 0"
    79   length_Cons "length(x#xs) = Suc(length(xs))"
    80 primrec flat list
    81   flat_Nil  "flat([]) = []"
    82   flat_Cons "flat(x#xs) = x @ flat(xs)"
    83 primrec drop list
    84   drop_Nil  "drop n [] = []"
    85   drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
    86 primrec take list
    87   take_Nil  "take n [] = []"
    88   take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
    89 defs
    90   nth_def  "nth(n) == nat_rec n hd (%m r xs. r(tl(xs)))"
    91 end