src/HOL/List.thy
author wenzelm
Wed Nov 27 17:00:25 1996 +0100 (1996-11-27)
changeset 2262 c7ee913746fd
parent 1908 55d8e38262a8
child 2369 8100f00e8950
permissions -rw-r--r--
added symbols syntax;
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@1908
     9
      use set_of_list 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
paulson@1908
    18
  "@"         :: ['a list, 'a list] => 'a list            (infixr 65)
paulson@1908
    19
  drop        :: [nat, 'a list] => 'a list
paulson@1908
    20
  filter      :: ['a => bool, 'a list] => 'a list
paulson@1908
    21
  flat        :: 'a list list => 'a list
paulson@1908
    22
  foldl       :: [['b,'a] => 'b, 'b, 'a list] => 'b
paulson@1908
    23
  hd          :: 'a list => 'a
paulson@1908
    24
  length      :: 'a list => nat
paulson@1908
    25
  set_of_list :: ('a list => 'a set)
paulson@1908
    26
  list_all    :: ('a => bool) => ('a list => bool)
paulson@1908
    27
  map         :: ('a=>'b) => ('a list => 'b list)
paulson@1908
    28
  mem         :: ['a, 'a list] => bool                    (infixl 55)
paulson@1908
    29
  nth         :: [nat, 'a list] => 'a
paulson@1908
    30
  take        :: [nat, 'a list] => 'a list
paulson@1908
    31
  tl,ttl      :: 'a list => 'a list
paulson@1908
    32
  rev         :: 'a list => 'a list
clasohm@923
    33
clasohm@923
    34
syntax
clasohm@923
    35
  (* list Enumeration *)
wenzelm@2262
    36
  "@list"     :: args => 'a list                          ("[(_)]")
clasohm@923
    37
clasohm@923
    38
  (* Special syntax for list_all and filter *)
wenzelm@2262
    39
  "@Alls"     :: [idt, 'a list, bool] => bool             ("(2Alls _:_./ _)" 10)
wenzelm@2262
    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@1475
    45
  "[x:xs . P]"  == "filter (%x.P) xs"
clasohm@1475
    46
  "Alls x:xs.P" == "list_all (%x.P) xs"
clasohm@923
    47
wenzelm@2262
    48
syntax (symbols)
wenzelm@2262
    49
  "op #"      :: ['a, 'a list] => 'a list                 (infixr "\\<bullet>" 65)
wenzelm@2262
    50
  "op @"      :: ['a list, 'a list] => 'a list            (infixr "\\<circ>" 65)
wenzelm@2262
    51
  "op mem"    :: ['a, 'a list] => bool                    (infixl "\\<in>" 55)
wenzelm@2262
    52
  "@Alls"     :: [idt, 'a list, bool] => bool             ("(2\\<forall> _\\<in>_./ _)" 10)
wenzelm@2262
    53
  "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_\\<in>_ ./ _])")
wenzelm@2262
    54
wenzelm@2262
    55
clasohm@923
    56
primrec hd list
berghofe@1898
    57
  "hd([]) = (@x.False)"
berghofe@1898
    58
  "hd(x#xs) = x"
clasohm@923
    59
primrec tl list
berghofe@1898
    60
  "tl([]) = (@x.False)"
berghofe@1898
    61
  "tl(x#xs) = xs"
clasohm@923
    62
primrec ttl list
clasohm@923
    63
  (* a "total" version of tl: *)
berghofe@1898
    64
  "ttl([]) = []"
berghofe@1898
    65
  "ttl(x#xs) = xs"
clasohm@923
    66
primrec "op mem" list
berghofe@1898
    67
  "x mem [] = False"
berghofe@1898
    68
  "x mem (y#ys) = (if y=x then True else x mem ys)"
paulson@1908
    69
primrec set_of_list list
paulson@1908
    70
  "set_of_list [] = {}"
paulson@1908
    71
  "set_of_list (x#xs) = insert x (set_of_list xs)"
clasohm@923
    72
primrec list_all list
clasohm@923
    73
  list_all_Nil  "list_all P [] = True"
clasohm@923
    74
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
clasohm@923
    75
primrec map list
berghofe@1898
    76
  "map f [] = []"
berghofe@1898
    77
  "map f (x#xs) = f(x)#map f xs"
clasohm@923
    78
primrec "op @" list
berghofe@1898
    79
  "[] @ ys = ys"
berghofe@1898
    80
  "(x#xs)@ys = x#(xs@ys)"
lcp@1169
    81
primrec rev list
berghofe@1898
    82
  "rev([]) = []"
berghofe@1898
    83
  "rev(x#xs) = rev(xs) @ [x]"
clasohm@923
    84
primrec filter list
berghofe@1898
    85
  "filter P [] = []"
berghofe@1898
    86
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
clasohm@923
    87
primrec foldl list
berghofe@1898
    88
  "foldl f a [] = a"
berghofe@1898
    89
  "foldl f a (x#xs) = foldl f (f a x) xs"
clasohm@923
    90
primrec length list
berghofe@1898
    91
  "length([]) = 0"
berghofe@1898
    92
  "length(x#xs) = Suc(length(xs))"
clasohm@923
    93
primrec flat list
berghofe@1898
    94
  "flat([]) = []"
berghofe@1898
    95
  "flat(x#xs) = x @ flat(xs)"
nipkow@1419
    96
primrec drop list
nipkow@1419
    97
  drop_Nil  "drop n [] = []"
nipkow@1419
    98
  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
nipkow@1419
    99
primrec take list
nipkow@1419
   100
  take_Nil  "take n [] = []"
nipkow@1419
   101
  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
clasohm@923
   102
defs
berghofe@1824
   103
  nth_def  "nth(n) == nat_rec hd (%m r xs. r(tl(xs))) n"
clasohm@923
   104
end