src/HOL/List.thy
author wenzelm
Wed Nov 05 11:49:34 1997 +0100 (1997-11-05)
changeset 4151 5c19cd418c33
parent 4132 daff3c9987cc
child 4502 337c073de95e
permissions -rw-r--r--
adapted typed_print_translation;
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
nipkow@2512
     6
The datatype of finite lists.
clasohm@923
     7
*)
clasohm@923
     8
paulson@3367
     9
List = Divides +
clasohm@923
    10
clasohm@977
    11
datatype 'a list = "[]" ("[]") | "#" 'a ('a list) (infixr 65)
clasohm@923
    12
clasohm@923
    13
consts
paulson@1908
    14
  "@"         :: ['a list, 'a list] => 'a list            (infixr 65)
paulson@1908
    15
  filter      :: ['a => bool, 'a list] => 'a list
nipkow@2608
    16
  concat      :: 'a list list => 'a list
paulson@1908
    17
  foldl       :: [['b,'a] => 'b, 'b, 'a list] => 'b
nipkow@3896
    18
  hd, last    :: 'a list => 'a
nipkow@3465
    19
  set         :: 'a list => 'a set
paulson@1908
    20
  list_all    :: ('a => bool) => ('a list => bool)
paulson@1908
    21
  map         :: ('a=>'b) => ('a list => 'b list)
paulson@1908
    22
  mem         :: ['a, 'a list] => bool                    (infixl 55)
paulson@1908
    23
  nth         :: [nat, 'a list] => 'a
nipkow@2608
    24
  take, drop  :: [nat, 'a list] => 'a list
nipkow@2608
    25
  takeWhile,
nipkow@2608
    26
  dropWhile   :: ('a => bool) => 'a list => 'a list
nipkow@3896
    27
  tl, butlast :: 'a list => 'a list
paulson@1908
    28
  rev         :: 'a list => 'a list
oheimb@4132
    29
  zip	      :: "'a list => 'b list => ('a * 'b) list"
oheimb@4132
    30
  nodup	      :: "'a list => bool"
nipkow@3589
    31
  replicate   :: nat => 'a => 'a list
clasohm@923
    32
clasohm@923
    33
syntax
clasohm@923
    34
  (* list Enumeration *)
wenzelm@2262
    35
  "@list"     :: args => 'a list                          ("[(_)]")
clasohm@923
    36
nipkow@2512
    37
  (* Special syntax for filter *)
wenzelm@2262
    38
  "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_:_ ./ _])")
clasohm@923
    39
clasohm@923
    40
translations
clasohm@923
    41
  "[x, xs]"     == "x#[xs]"
clasohm@923
    42
  "[x]"         == "x#[]"
wenzelm@3842
    43
  "[x:xs . P]"  == "filter (%x. P) xs"
clasohm@923
    44
wenzelm@2262
    45
syntax (symbols)
wenzelm@2262
    46
  "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_\\<in>_ ./ _])")
wenzelm@2262
    47
wenzelm@2262
    48
paulson@3342
    49
consts
paulson@3342
    50
  lists        :: 'a set => 'a list set
paulson@3342
    51
paulson@3342
    52
  inductive "lists A"
paulson@3342
    53
  intrs
paulson@3342
    54
    Nil  "[]: lists A"
paulson@3342
    55
    Cons "[| a: A;  l: lists A |] ==> a#l : lists A"
paulson@3342
    56
paulson@3342
    57
paulson@3437
    58
(*Function "size" is overloaded for all datatypes.  Users may refer to the
paulson@3437
    59
  list version as "length".*)
paulson@3437
    60
syntax   length :: 'a list => nat
nipkow@3507
    61
translations  "length"  =>  "size:: _ list => nat"
paulson@3437
    62
clasohm@923
    63
primrec hd list
nipkow@3320
    64
  "hd([]) = arbitrary"
berghofe@1898
    65
  "hd(x#xs) = x"
clasohm@923
    66
primrec tl list
nipkow@3896
    67
  "tl([]) = []"
berghofe@1898
    68
  "tl(x#xs) = xs"
nipkow@3896
    69
primrec last list
nipkow@3896
    70
  "last [] = arbitrary"
nipkow@3896
    71
  "last(x#xs) = (if xs=[] then x else last xs)"
nipkow@3896
    72
primrec butlast list
nipkow@3896
    73
  "butlast [] = []"
nipkow@3896
    74
  "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
clasohm@923
    75
primrec "op mem" list
berghofe@1898
    76
  "x mem [] = False"
berghofe@1898
    77
  "x mem (y#ys) = (if y=x then True else x mem ys)"
nipkow@3465
    78
primrec set list
nipkow@3465
    79
  "set [] = {}"
nipkow@3465
    80
  "set (x#xs) = insert x (set xs)"
clasohm@923
    81
primrec list_all list
clasohm@923
    82
  list_all_Nil  "list_all P [] = True"
clasohm@923
    83
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
clasohm@923
    84
primrec map list
berghofe@1898
    85
  "map f [] = []"
berghofe@1898
    86
  "map f (x#xs) = f(x)#map f xs"
clasohm@923
    87
primrec "op @" list
berghofe@1898
    88
  "[] @ ys = ys"
berghofe@1898
    89
  "(x#xs)@ys = x#(xs@ys)"
lcp@1169
    90
primrec rev list
berghofe@1898
    91
  "rev([]) = []"
berghofe@1898
    92
  "rev(x#xs) = rev(xs) @ [x]"
clasohm@923
    93
primrec filter list
berghofe@1898
    94
  "filter P [] = []"
berghofe@1898
    95
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
clasohm@923
    96
primrec foldl list
berghofe@1898
    97
  "foldl f a [] = a"
berghofe@1898
    98
  "foldl f a (x#xs) = foldl f (f a x) xs"
nipkow@2608
    99
primrec concat list
nipkow@2608
   100
  "concat([]) = []"
nipkow@2608
   101
  "concat(x#xs) = x @ concat(xs)"
nipkow@1419
   102
primrec drop list
nipkow@1419
   103
  drop_Nil  "drop n [] = []"
nipkow@1419
   104
  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
nipkow@1419
   105
primrec take list
nipkow@1419
   106
  take_Nil  "take n [] = []"
nipkow@1419
   107
  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
pusch@2738
   108
primrec nth nat
pusch@2738
   109
  "nth 0 xs = hd xs"
pusch@2738
   110
  "nth (Suc n) xs = nth n (tl xs)"
nipkow@2608
   111
primrec takeWhile list
nipkow@2608
   112
  "takeWhile P [] = []"
nipkow@2608
   113
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
nipkow@2608
   114
primrec dropWhile list
nipkow@2608
   115
  "dropWhile P [] = []"
nipkow@3584
   116
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
oheimb@4132
   117
primrec zip list
oheimb@4132
   118
  "zip xs []     = []"
oheimb@4132
   119
  "zip xs (y#ys) = (hd xs,y)#zip (tl xs) ys"
oheimb@4132
   120
primrec nodup list
oheimb@4132
   121
  "nodup []     = True"
oheimb@4132
   122
  "nodup (x#xs) = (x ~: set xs & nodup xs)"
nipkow@3589
   123
primrec replicate nat
nipkow@3589
   124
replicate_0   "replicate 0 x       = []"
nipkow@3589
   125
replicate_Suc "replicate (Suc n) x = x # replicate n x"
paulson@3196
   126
clasohm@923
   127
end
nipkow@3507
   128
nipkow@3507
   129
ML
nipkow@3507
   130
nipkow@3507
   131
local
nipkow@3507
   132
nipkow@3507
   133
(* translating size::list -> length *)
nipkow@3507
   134
wenzelm@4151
   135
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
nipkow@3507
   136
      Syntax.const "length" $ t
wenzelm@4151
   137
  | size_tr' _ _ _ = raise Match;
nipkow@3507
   138
nipkow@3507
   139
in
nipkow@3507
   140
nipkow@3507
   141
val typed_print_translation = [("size", size_tr')];
nipkow@3507
   142
nipkow@3507
   143
end;