src/HOL/List.thy
author nipkow
Wed Jun 24 13:59:45 1998 +0200 (1998-06-24)
changeset 5077 71043526295f
parent 4643 1b40fcac5a09
child 5162 53e505c6019c
permissions -rw-r--r--
* HOL/List: new function list_update written xs[i:=v] that updates the i-th
list position. May also be iterated as in xs[i:=a,j:=b,...].
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
nipkow@4643
     9
List = WF_Rel +
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)
nipkow@4502
    23
  nth         :: ['a list, nat] => 'a			  (infixl "!" 100)
nipkow@5077
    24
  list_update :: 'a list => nat => 'a => 'a list
nipkow@2608
    25
  take, drop  :: [nat, 'a list] => 'a list
nipkow@2608
    26
  takeWhile,
nipkow@2608
    27
  dropWhile   :: ('a => bool) => 'a list => 'a list
nipkow@3896
    28
  tl, butlast :: 'a list => 'a list
paulson@1908
    29
  rev         :: 'a list => 'a list
oheimb@4132
    30
  zip	      :: "'a list => 'b list => ('a * 'b) list"
nipkow@4605
    31
  remdups     :: 'a list => 'a list
nipkow@4605
    32
  nodups      :: "'a list => bool"
nipkow@3589
    33
  replicate   :: nat => 'a => 'a list
clasohm@923
    34
nipkow@5077
    35
nonterminals
nipkow@5077
    36
  lupdbinds  lupdbind
nipkow@5077
    37
clasohm@923
    38
syntax
clasohm@923
    39
  (* list Enumeration *)
wenzelm@2262
    40
  "@list"     :: args => 'a list                          ("[(_)]")
clasohm@923
    41
nipkow@2512
    42
  (* Special syntax for filter *)
wenzelm@2262
    43
  "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_:_ ./ _])")
clasohm@923
    44
nipkow@5077
    45
  (* list update *)
nipkow@5077
    46
  "_lupdbind"      :: ['a, 'a] => lupdbind            ("(2_ :=/ _)")
nipkow@5077
    47
  ""               :: lupdbind => lupdbinds           ("_")
nipkow@5077
    48
  "_lupdbinds"     :: [lupdbind, lupdbinds] => lupdbinds ("_,/ _")
nipkow@5077
    49
  "_LUpdate"       :: ['a, lupdbinds] => 'a           ("_/[(_)]" [900,0] 900)
nipkow@5077
    50
clasohm@923
    51
translations
clasohm@923
    52
  "[x, xs]"     == "x#[xs]"
clasohm@923
    53
  "[x]"         == "x#[]"
wenzelm@3842
    54
  "[x:xs . P]"  == "filter (%x. P) xs"
clasohm@923
    55
nipkow@5077
    56
  "_LUpdate xs (_lupdbinds b bs)"  == "_LUpdate (_LUpdate xs b) bs"
nipkow@5077
    57
  "xs[i:=x]"                       == "list_update xs i x"
nipkow@5077
    58
wenzelm@2262
    59
syntax (symbols)
wenzelm@2262
    60
  "@filter"   :: [idt, 'a list, bool] => 'a list          ("(1[_\\<in>_ ./ _])")
wenzelm@2262
    61
wenzelm@2262
    62
paulson@3342
    63
consts
paulson@3342
    64
  lists        :: 'a set => 'a list set
paulson@3342
    65
paulson@3342
    66
  inductive "lists A"
paulson@3342
    67
  intrs
paulson@3342
    68
    Nil  "[]: lists A"
paulson@3342
    69
    Cons "[| a: A;  l: lists A |] ==> a#l : lists A"
paulson@3342
    70
paulson@3342
    71
paulson@3437
    72
(*Function "size" is overloaded for all datatypes.  Users may refer to the
paulson@3437
    73
  list version as "length".*)
paulson@3437
    74
syntax   length :: 'a list => nat
nipkow@3507
    75
translations  "length"  =>  "size:: _ list => nat"
paulson@3437
    76
clasohm@923
    77
primrec hd list
nipkow@3320
    78
  "hd([]) = arbitrary"
berghofe@1898
    79
  "hd(x#xs) = x"
clasohm@923
    80
primrec tl list
nipkow@3896
    81
  "tl([]) = []"
berghofe@1898
    82
  "tl(x#xs) = xs"
nipkow@3896
    83
primrec last list
nipkow@3896
    84
  "last [] = arbitrary"
nipkow@3896
    85
  "last(x#xs) = (if xs=[] then x else last xs)"
nipkow@3896
    86
primrec butlast list
nipkow@3896
    87
  "butlast [] = []"
nipkow@3896
    88
  "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
clasohm@923
    89
primrec "op mem" list
berghofe@1898
    90
  "x mem [] = False"
berghofe@1898
    91
  "x mem (y#ys) = (if y=x then True else x mem ys)"
nipkow@3465
    92
primrec set list
nipkow@3465
    93
  "set [] = {}"
nipkow@3465
    94
  "set (x#xs) = insert x (set xs)"
clasohm@923
    95
primrec list_all list
clasohm@923
    96
  list_all_Nil  "list_all P [] = True"
clasohm@923
    97
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
clasohm@923
    98
primrec map list
berghofe@1898
    99
  "map f [] = []"
berghofe@1898
   100
  "map f (x#xs) = f(x)#map f xs"
clasohm@923
   101
primrec "op @" list
berghofe@1898
   102
  "[] @ ys = ys"
berghofe@1898
   103
  "(x#xs)@ys = x#(xs@ys)"
lcp@1169
   104
primrec rev list
berghofe@1898
   105
  "rev([]) = []"
berghofe@1898
   106
  "rev(x#xs) = rev(xs) @ [x]"
clasohm@923
   107
primrec filter list
berghofe@1898
   108
  "filter P [] = []"
berghofe@1898
   109
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
clasohm@923
   110
primrec foldl list
berghofe@1898
   111
  "foldl f a [] = a"
berghofe@1898
   112
  "foldl f a (x#xs) = foldl f (f a x) xs"
nipkow@2608
   113
primrec concat list
nipkow@2608
   114
  "concat([]) = []"
nipkow@2608
   115
  "concat(x#xs) = x @ concat(xs)"
nipkow@1419
   116
primrec drop list
nipkow@1419
   117
  drop_Nil  "drop n [] = []"
nipkow@1419
   118
  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
nipkow@1419
   119
primrec take list
nipkow@1419
   120
  take_Nil  "take n [] = []"
nipkow@1419
   121
  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
pusch@2738
   122
primrec nth nat
nipkow@4502
   123
  "xs!0 = hd xs"
nipkow@4502
   124
  "xs!(Suc n) = (tl xs)!n"
nipkow@5077
   125
primrec list_update list
nipkow@5077
   126
 "    [][i:=v] = []"
nipkow@5077
   127
 "(x#xs)[i:=v] = (case i of 0     => v # xs 
nipkow@5077
   128
			  | Suc j => x # xs[j:=v])"
nipkow@2608
   129
primrec takeWhile list
nipkow@2608
   130
  "takeWhile P [] = []"
nipkow@2608
   131
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
nipkow@2608
   132
primrec dropWhile list
nipkow@2608
   133
  "dropWhile P [] = []"
nipkow@3584
   134
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
oheimb@4132
   135
primrec zip list
oheimb@4132
   136
  "zip xs []     = []"
oheimb@4132
   137
  "zip xs (y#ys) = (hd xs,y)#zip (tl xs) ys"
nipkow@4605
   138
primrec nodups list
nipkow@4605
   139
  "nodups []     = True"
nipkow@4605
   140
  "nodups (x#xs) = (x ~: set xs & nodups xs)"
nipkow@4605
   141
primrec remdups list
nipkow@4605
   142
  "remdups [] = []"
nipkow@4605
   143
  "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
nipkow@3589
   144
primrec replicate nat
nipkow@3589
   145
replicate_0   "replicate 0 x       = []"
nipkow@3589
   146
replicate_Suc "replicate (Suc n) x = x # replicate n x"
paulson@3196
   147
clasohm@923
   148
end
nipkow@3507
   149
nipkow@3507
   150
ML
nipkow@3507
   151
nipkow@3507
   152
local
nipkow@3507
   153
nipkow@3507
   154
(* translating size::list -> length *)
nipkow@3507
   155
wenzelm@4151
   156
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
nipkow@3507
   157
      Syntax.const "length" $ t
wenzelm@4151
   158
  | size_tr' _ _ _ = raise Match;
nipkow@3507
   159
nipkow@3507
   160
in
nipkow@3507
   161
nipkow@3507
   162
val typed_print_translation = [("size", size_tr')];
nipkow@3507
   163
nipkow@3507
   164
end;