src/HOL/List.thy
author lcp
Thu Jun 29 16:33:17 1995 +0200 (1995-06-29)
changeset 1169 5873833cf37f
parent 977 5d57287e5e1e
child 1327 6c29cfab679c
permissions -rw-r--r--
Added function rev and its properties length_rev, etc.
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
clasohm@923
     8
*)
clasohm@923
     9
clasohm@923
    10
List = Arith +
clasohm@923
    11
clasohm@977
    12
datatype 'a list = "[]" ("[]") | "#" 'a ('a list) (infixr 65)
clasohm@923
    13
clasohm@923
    14
consts
clasohm@923
    15
clasohm@923
    16
  null      :: "'a list => bool"
clasohm@923
    17
  hd        :: "'a list => 'a"
clasohm@923
    18
  tl,ttl    :: "'a list => 'a list"
clasohm@923
    19
  mem       :: "['a, 'a list] => bool"			(infixl 55)
clasohm@923
    20
  list_all  :: "('a => bool) => ('a list => bool)"
clasohm@923
    21
  map       :: "('a=>'b) => ('a list => 'b list)"
clasohm@923
    22
  "@"	    :: "['a list, 'a list] => 'a list"		(infixr 65)
lcp@1169
    23
  rev       :: "'a list => 'a list"
clasohm@923
    24
  filter    :: "['a => bool, 'a list] => 'a list"
clasohm@923
    25
  foldl     :: "[['b,'a] => 'b, 'b, 'a list] => 'b"
clasohm@923
    26
  length    :: "'a list => nat"
clasohm@923
    27
  flat      :: "'a list list => 'a list"
clasohm@923
    28
  nth       :: "[nat, 'a list] => 'a"
clasohm@923
    29
clasohm@923
    30
syntax
clasohm@923
    31
  (* list Enumeration *)
clasohm@923
    32
  "@list"   :: "args => 'a list"                        ("[(_)]")
clasohm@923
    33
clasohm@923
    34
  (* Special syntax for list_all and filter *)
clasohm@923
    35
  "@Alls"	:: "[idt, 'a list, bool] => bool"	("(2Alls _:_./ _)" 10)
clasohm@923
    36
  "@filter"	:: "[idt, 'a list, bool] => 'a list"	("(1[_:_ ./ _])")
clasohm@923
    37
clasohm@923
    38
translations
clasohm@923
    39
  "[x, xs]"     == "x#[xs]"
clasohm@923
    40
  "[x]"         == "x#[]"
clasohm@923
    41
clasohm@923
    42
  "[x:xs . P]"	== "filter (%x.P) xs"
clasohm@923
    43
  "Alls x:xs.P"	== "list_all (%x.P) xs"
clasohm@923
    44
clasohm@923
    45
primrec null list
clasohm@923
    46
  null_Nil "null([]) = True"
clasohm@923
    47
  null_Cons "null(x#xs) = False"
clasohm@923
    48
primrec hd list
clasohm@923
    49
  hd_Nil  "hd([]) = (@x.False)"
clasohm@923
    50
  hd_Cons "hd(x#xs) = x"
clasohm@923
    51
primrec tl list
clasohm@923
    52
  tl_Nil  "tl([]) = (@x.False)"
clasohm@923
    53
  tl_Cons "tl(x#xs) = xs"
clasohm@923
    54
primrec ttl list
clasohm@923
    55
  (* a "total" version of tl: *)
clasohm@923
    56
  ttl_Nil  "ttl([]) = []"
clasohm@923
    57
  ttl_Cons "ttl(x#xs) = xs"
clasohm@923
    58
primrec "op mem" list
clasohm@923
    59
  mem_Nil  "x mem [] = False"
clasohm@965
    60
  mem_Cons "x mem (y#ys) = (if y=x then True else x mem ys)"
clasohm@923
    61
primrec list_all list
clasohm@923
    62
  list_all_Nil  "list_all P [] = True"
clasohm@923
    63
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
clasohm@923
    64
primrec map list
clasohm@923
    65
  map_Nil  "map f [] = []"
clasohm@923
    66
  map_Cons "map f (x#xs) = f(x)#map f xs"
clasohm@923
    67
primrec "op @" list
clasohm@923
    68
  append_Nil  "[] @ ys = ys"
clasohm@923
    69
  append_Cons "(x#xs)@ys = x#(xs@ys)"
lcp@1169
    70
primrec rev list
lcp@1169
    71
  rev_Nil  "rev([]) = []"
lcp@1169
    72
  rev_Cons "rev(x#xs) = rev(xs) @ [x]"
clasohm@923
    73
primrec filter list
clasohm@923
    74
  filter_Nil  "filter P [] = []"
clasohm@965
    75
  filter_Cons "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
clasohm@923
    76
primrec foldl list
clasohm@923
    77
  foldl_Nil  "foldl f a [] = a"
clasohm@923
    78
  foldl_Cons "foldl f a (x#xs) = foldl f (f a x) xs"
clasohm@923
    79
primrec length list
clasohm@923
    80
  length_Nil  "length([]) = 0"
clasohm@923
    81
  length_Cons "length(x#xs) = Suc(length(xs))"
clasohm@923
    82
primrec flat list
clasohm@923
    83
  flat_Nil  "flat([]) = []"
clasohm@923
    84
  flat_Cons "flat(x#xs) = x @ flat(xs)"
clasohm@923
    85
defs
clasohm@923
    86
  nth_def "nth(n) == nat_rec n hd (%m r xs. r(tl(xs)))"
clasohm@923
    87
end