src/HOL/List.thy
author nipkow
Fri Sep 04 11:01:59 1998 +0200 (1998-09-04)
changeset 5427 26c9a7c0b36b
parent 5425 157c6663dedd
child 5443 e2459d18ff47
permissions -rw-r--r--
Arith: less_diff_conv
List: [i..j]
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@5427
     9
List = Datatype + 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@5427
    31
  upt         :: nat => nat => nat list                   ("(1[_../_'(])")
nipkow@4605
    32
  remdups     :: 'a list => 'a list
nipkow@4605
    33
  nodups      :: "'a list => bool"
nipkow@3589
    34
  replicate   :: nat => 'a => 'a list
clasohm@923
    35
nipkow@5077
    36
nonterminals
nipkow@5077
    37
  lupdbinds  lupdbind
nipkow@5077
    38
clasohm@923
    39
syntax
clasohm@923
    40
  (* list Enumeration *)
wenzelm@2262
    41
  "@list"     :: args => 'a list                          ("[(_)]")
clasohm@923
    42
nipkow@2512
    43
  (* Special syntax for filter *)
oheimb@5295
    44
  "@filter"   :: [pttrn, 'a list, bool] => 'a list        ("(1[_:_ ./ _])")
clasohm@923
    45
nipkow@5077
    46
  (* list update *)
nipkow@5077
    47
  "_lupdbind"      :: ['a, 'a] => lupdbind            ("(2_ :=/ _)")
nipkow@5077
    48
  ""               :: lupdbind => lupdbinds           ("_")
nipkow@5077
    49
  "_lupdbinds"     :: [lupdbind, lupdbinds] => lupdbinds ("_,/ _")
nipkow@5077
    50
  "_LUpdate"       :: ['a, lupdbinds] => 'a           ("_/[(_)]" [900,0] 900)
nipkow@5077
    51
nipkow@5427
    52
  upto        :: nat => nat => nat list                   ("(1[_../_])")
nipkow@5427
    53
clasohm@923
    54
translations
clasohm@923
    55
  "[x, xs]"     == "x#[xs]"
clasohm@923
    56
  "[x]"         == "x#[]"
wenzelm@3842
    57
  "[x:xs . P]"  == "filter (%x. P) xs"
clasohm@923
    58
nipkow@5077
    59
  "_LUpdate xs (_lupdbinds b bs)"  == "_LUpdate (_LUpdate xs b) bs"
nipkow@5077
    60
  "xs[i:=x]"                       == "list_update xs i x"
nipkow@5077
    61
nipkow@5427
    62
  "[i..j]" == "[i..(Suc j)(]"
nipkow@5427
    63
nipkow@5427
    64
wenzelm@2262
    65
syntax (symbols)
oheimb@5295
    66
  "@filter"   :: [pttrn, 'a list, bool] => 'a list        ("(1[_\\<in>_ ./ _])")
wenzelm@2262
    67
wenzelm@2262
    68
paulson@3342
    69
consts
paulson@3342
    70
  lists        :: 'a set => 'a list set
paulson@3342
    71
paulson@3342
    72
  inductive "lists A"
paulson@3342
    73
  intrs
paulson@3342
    74
    Nil  "[]: lists A"
paulson@3342
    75
    Cons "[| a: A;  l: lists A |] ==> a#l : lists A"
paulson@3342
    76
paulson@3342
    77
paulson@3437
    78
(*Function "size" is overloaded for all datatypes.  Users may refer to the
paulson@3437
    79
  list version as "length".*)
paulson@3437
    80
syntax   length :: 'a list => nat
nipkow@3507
    81
translations  "length"  =>  "size:: _ list => nat"
paulson@3437
    82
berghofe@5183
    83
primrec
nipkow@3320
    84
  "hd([]) = arbitrary"
berghofe@1898
    85
  "hd(x#xs) = x"
berghofe@5183
    86
primrec
nipkow@3896
    87
  "tl([]) = []"
berghofe@1898
    88
  "tl(x#xs) = xs"
berghofe@5183
    89
primrec
nipkow@3896
    90
  "last [] = arbitrary"
nipkow@3896
    91
  "last(x#xs) = (if xs=[] then x else last xs)"
berghofe@5183
    92
primrec
nipkow@3896
    93
  "butlast [] = []"
nipkow@3896
    94
  "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
berghofe@5183
    95
primrec
berghofe@1898
    96
  "x mem [] = False"
berghofe@1898
    97
  "x mem (y#ys) = (if y=x then True else x mem ys)"
berghofe@5183
    98
primrec
nipkow@3465
    99
  "set [] = {}"
nipkow@3465
   100
  "set (x#xs) = insert x (set xs)"
berghofe@5183
   101
primrec
clasohm@923
   102
  list_all_Nil  "list_all P [] = True"
clasohm@923
   103
  list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
berghofe@5183
   104
primrec
berghofe@1898
   105
  "map f [] = []"
berghofe@1898
   106
  "map f (x#xs) = f(x)#map f xs"
berghofe@5183
   107
primrec
berghofe@5183
   108
  append_Nil  "[]    @ys = ys"
berghofe@5183
   109
  append_Cons "(x#xs)@ys = x#(xs@ys)"
berghofe@5183
   110
primrec
berghofe@1898
   111
  "rev([]) = []"
berghofe@1898
   112
  "rev(x#xs) = rev(xs) @ [x]"
berghofe@5183
   113
primrec
berghofe@1898
   114
  "filter P [] = []"
berghofe@1898
   115
  "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
berghofe@5183
   116
primrec
berghofe@1898
   117
  "foldl f a [] = a"
berghofe@1898
   118
  "foldl f a (x#xs) = foldl f (f a x) xs"
berghofe@5183
   119
primrec
nipkow@2608
   120
  "concat([]) = []"
nipkow@2608
   121
  "concat(x#xs) = x @ concat(xs)"
berghofe@5183
   122
primrec
nipkow@1419
   123
  drop_Nil  "drop n [] = []"
nipkow@1419
   124
  drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
berghofe@5183
   125
primrec
nipkow@1419
   126
  take_Nil  "take n [] = []"
nipkow@1419
   127
  take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
berghofe@5183
   128
primrec
nipkow@4502
   129
  "xs!0 = hd xs"
nipkow@4502
   130
  "xs!(Suc n) = (tl xs)!n"
berghofe@5183
   131
primrec
nipkow@5077
   132
 "    [][i:=v] = []"
nipkow@5077
   133
 "(x#xs)[i:=v] = (case i of 0     => v # xs 
nipkow@5077
   134
			  | Suc j => x # xs[j:=v])"
berghofe@5183
   135
primrec
nipkow@2608
   136
  "takeWhile P [] = []"
nipkow@2608
   137
  "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
berghofe@5183
   138
primrec
nipkow@2608
   139
  "dropWhile P [] = []"
nipkow@3584
   140
  "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
berghofe@5183
   141
primrec
oheimb@4132
   142
  "zip xs []     = []"
oheimb@4132
   143
  "zip xs (y#ys) = (hd xs,y)#zip (tl xs) ys"
nipkow@5427
   144
primrec
nipkow@5427
   145
  "[i..0(] = []"
nipkow@5427
   146
  "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
berghofe@5183
   147
primrec
nipkow@4605
   148
  "nodups []     = True"
nipkow@4605
   149
  "nodups (x#xs) = (x ~: set xs & nodups xs)"
berghofe@5183
   150
primrec
nipkow@4605
   151
  "remdups [] = []"
nipkow@4605
   152
  "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
berghofe@5183
   153
primrec
berghofe@5183
   154
  replicate_0   "replicate 0 x       = []"
berghofe@5183
   155
  replicate_Suc "replicate (Suc n) x = x # replicate n x"
paulson@3196
   156
nipkow@5281
   157
(** Lexcicographic orderings on lists **)
nipkow@5281
   158
nipkow@5281
   159
consts
nipkow@5281
   160
 lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
nipkow@5281
   161
primrec
nipkow@5281
   162
"lexn r 0       = {}"
nipkow@5281
   163
"lexn r (Suc n) = (prod_fun (split op#) (split op#) `` (r ** lexn r n)) Int
nipkow@5281
   164
                  {(xs,ys). length xs = Suc n & length ys = Suc n}"
nipkow@5281
   165
nipkow@5281
   166
constdefs
nipkow@5281
   167
 lex :: "('a * 'a)set => ('a list * 'a list)set"
nipkow@5281
   168
"lex r == UN n. lexn r n"
nipkow@5281
   169
nipkow@5281
   170
 lexico :: "('a * 'a)set => ('a list * 'a list)set"
nipkow@5281
   171
"lexico r == inv_image (less_than ** lex r) (%xs. (length xs, xs))"
nipkow@5281
   172
clasohm@923
   173
end
nipkow@3507
   174
nipkow@3507
   175
ML
nipkow@3507
   176
nipkow@3507
   177
local
nipkow@3507
   178
nipkow@3507
   179
(* translating size::list -> length *)
nipkow@3507
   180
wenzelm@4151
   181
fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
nipkow@3507
   182
      Syntax.const "length" $ t
wenzelm@4151
   183
  | size_tr' _ _ _ = raise Match;
nipkow@3507
   184
nipkow@3507
   185
in
nipkow@3507
   186
nipkow@3507
   187
val typed_print_translation = [("size", size_tr')];
nipkow@3507
   188
nipkow@3507
   189
end;