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