src/HOL/List.thy
author oheimb
Fri Jul 14 16:28:49 2000 +0200 (2000-07-14)
changeset 9341 40bab6613000
parent 9336 9ae89b9ce206
child 9355 1b2cd40579c6
permissions -rw-r--r--
tuned syntax
     1 (*  Title:      HOL/List.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   1994 TU Muenchen
     5 
     6 The datatype of finite lists.
     7 *)
     8 
     9 List = PreList +
    10 
    11 datatype 'a list = Nil ("[]") | Cons 'a ('a list) (infixr "#" 65)
    12 
    13 consts
    14   "@"         :: ['a list, 'a list] => 'a list            (infixr 65)
    15   filter      :: ['a => bool, 'a list] => 'a list
    16   concat      :: 'a list list => 'a list
    17   foldl       :: [['b,'a] => 'b, 'b, 'a list] => 'b
    18   foldr       :: [['a,'b] => 'b, 'a list, 'b] => 'b
    19   hd, last    :: 'a list => 'a
    20   set         :: 'a list => 'a set
    21   list_all    :: ('a => bool) => ('a list => bool)
    22   list_all2   :: ('a => 'b => bool) => 'a list => 'b list => bool
    23   map         :: ('a=>'b) => ('a list => 'b list)
    24   mem         :: ['a, 'a list] => bool                    (infixl 55)
    25   nth         :: ['a list, nat] => 'a			  (infixl "!" 100)
    26   list_update :: 'a list => nat => 'a => 'a list
    27   take, drop  :: [nat, 'a list] => 'a list
    28   takeWhile,
    29   dropWhile   :: ('a => bool) => 'a list => 'a list
    30   tl, butlast :: 'a list => 'a list
    31   rev         :: 'a list => 'a list
    32   zip	      :: "'a list => 'b list => ('a * 'b) list"
    33   upt         :: nat => nat => nat list                   ("(1[_../_'(])")
    34   remdups     :: 'a list => 'a list
    35   null, nodups :: "'a list => bool"
    36   replicate   :: nat => 'a => 'a list
    37 
    38 nonterminals
    39   lupdbinds  lupdbind
    40 
    41 syntax
    42   (* list Enumeration *)
    43   "@list"     :: args => 'a list                          ("[(_)]")
    44 
    45   (* Special syntax for filter *)
    46   "@filter"   :: [pttrn, 'a list, bool] => 'a list        ("(1[_:_./ _])")
    47 
    48   (* list update *)
    49   "_lupdbind"      :: ['a, 'a] => lupdbind            ("(2_ :=/ _)")
    50   ""               :: lupdbind => lupdbinds           ("_")
    51   "_lupdbinds"     :: [lupdbind, lupdbinds] => lupdbinds ("_,/ _")
    52   "_LUpdate"       :: ['a, lupdbinds] => 'a           ("_/[(_)]" [900,0] 900)
    53 
    54   upto        :: nat => nat => nat list                   ("(1[_../_])")
    55 
    56 translations
    57   "[x, xs]"     == "x#[xs]"
    58   "[x]"         == "x#[]"
    59   "[x:xs . P]"  == "filter (%x. P) xs"
    60 
    61   "_LUpdate xs (_lupdbinds b bs)"  == "_LUpdate (_LUpdate xs b) bs"
    62   "xs[i:=x]"                       == "list_update xs i x"
    63 
    64   "[i..j]" == "[i..(Suc j)(]"
    65 
    66 
    67 syntax (symbols)
    68   "@filter"   :: [pttrn, 'a list, bool] => 'a list        ("(1[_\\<in>_ ./ _])")
    69 
    70 
    71 consts
    72   lists        :: 'a set => 'a list set
    73 
    74   inductive "lists A"
    75   intrs
    76     Nil  "[]: lists A"
    77     Cons "[| a: A;  l: lists A |] ==> a#l : lists A"
    78 
    79 
    80 (*Function "size" is overloaded for all datatypes.  Users may refer to the
    81   list version as "length".*)
    82 syntax   length :: 'a list => nat
    83 translations  "length"  =>  "size:: _ list => nat"
    84 
    85 primrec
    86   "hd(x#xs) = x"
    87 primrec
    88   "tl([])   = []"
    89   "tl(x#xs) = xs"
    90 primrec
    91   "null([])   = True"
    92   "null(x#xs) = False"
    93 primrec
    94   "last(x#xs) = (if xs=[] then x else last xs)"
    95 primrec
    96   "butlast []    = []"
    97   "butlast(x#xs) = (if xs=[] then [] else x#butlast xs)"
    98 primrec
    99   "x mem []     = False"
   100   "x mem (y#ys) = (if y=x then True else x mem ys)"
   101 primrec
   102   "set [] = {}"
   103   "set (x#xs) = insert x (set xs)"
   104 primrec
   105   list_all_Nil  "list_all P [] = True"
   106   list_all_Cons "list_all P (x#xs) = (P(x) & list_all P xs)"
   107 primrec
   108   "map f []     = []"
   109   "map f (x#xs) = f(x)#map f xs"
   110 primrec
   111   append_Nil  "[]    @ys = ys"
   112   append_Cons "(x#xs)@ys = x#(xs@ys)"
   113 primrec
   114   "rev([])   = []"
   115   "rev(x#xs) = rev(xs) @ [x]"
   116 primrec
   117   "filter P []     = []"
   118   "filter P (x#xs) = (if P x then x#filter P xs else filter P xs)"
   119 primrec
   120   foldl_Nil  "foldl f a [] = a"
   121   foldl_Cons "foldl f a (x#xs) = foldl f (f a x) xs"
   122 primrec
   123   "foldr f [] a     = a"
   124   "foldr f (x#xs) a = f x (foldr f xs a)"
   125 primrec
   126   "concat([])   = []"
   127   "concat(x#xs) = x @ concat(xs)"
   128 primrec
   129   drop_Nil  "drop n [] = []"
   130   drop_Cons "drop n (x#xs) = (case n of 0 => x#xs | Suc(m) => drop m xs)"
   131   (* Warning: simpset does not contain this definition but separate theorems 
   132      for n=0 / n=Suc k*)
   133 primrec
   134   take_Nil  "take n [] = []"
   135   take_Cons "take n (x#xs) = (case n of 0 => [] | Suc(m) => x # take m xs)"
   136   (* Warning: simpset does not contain this definition but separate theorems 
   137      for n=0 / n=Suc k*)
   138 primrec 
   139   nth_Cons  "(x#xs)!n = (case n of 0 => x | (Suc k) => xs!k)"
   140   (* Warning: simpset does not contain this definition but separate theorems 
   141      for n=0 / n=Suc k*)
   142 primrec
   143  "    [][i:=v] = []"
   144  "(x#xs)[i:=v] = (case i of 0     => v # xs 
   145 			  | Suc j => x # xs[j:=v])"
   146 primrec
   147   "takeWhile P []     = []"
   148   "takeWhile P (x#xs) = (if P x then x#takeWhile P xs else [])"
   149 primrec
   150   "dropWhile P []     = []"
   151   "dropWhile P (x#xs) = (if P x then dropWhile P xs else x#xs)"
   152 primrec
   153   "zip xs []     = []"
   154   "zip xs (y#ys) = (case xs of [] => [] | z#zs => (z,y)#zip zs ys)"
   155   (* Warning: simpset does not contain this definition but separate theorems 
   156      for xs=[] / xs=z#zs *)
   157 primrec
   158   upt_0   "[i..0(] = []"
   159   upt_Suc "[i..(Suc j)(] = (if i <= j then [i..j(] @ [j] else [])"
   160 primrec
   161   "nodups []     = True"
   162   "nodups (x#xs) = (x ~: set xs & nodups xs)"
   163 primrec
   164   "remdups [] = []"
   165   "remdups (x#xs) = (if x : set xs then remdups xs else x # remdups xs)"
   166 primrec
   167   replicate_0   "replicate  0      x = []"
   168   replicate_Suc "replicate (Suc n) x = x # replicate n x"
   169 defs
   170  list_all2_def
   171  "list_all2 P xs ys == length xs = length ys & (!(x,y):set(zip xs ys). P x y)"
   172 
   173 
   174 (** Lexicographic orderings on lists **)
   175 
   176 consts
   177  lexn :: "('a * 'a)set => nat => ('a list * 'a list)set"
   178 primrec
   179 "lexn r 0       = {}"
   180 "lexn r (Suc n) = (prod_fun (split op#) (split op#) `` (r <*lex*> lexn r n)) Int
   181                   {(xs,ys). length xs = Suc n & length ys = Suc n}"
   182 
   183 constdefs
   184   lex :: "('a * 'a)set => ('a list * 'a list)set"
   185     "lex r == UN n. lexn r n"
   186 
   187   lexico :: "('a * 'a)set => ('a list * 'a list)set"
   188     "lexico r == inv_image (less_than <*lex*> lex r) (%xs. (length xs, xs))"
   189 
   190   sublist :: "['a list, nat set] => 'a list"
   191     "sublist xs A == map fst (filter (%p. snd p : A) (zip xs [0..size xs(]))"
   192 
   193 end
   194 
   195 ML
   196 
   197 local
   198 
   199 (* translating size::list -> length *)
   200 
   201 fun size_tr' _ (Type ("fun", (Type ("list", _) :: _))) [t] =
   202       Syntax.const "length" $ t
   203   | size_tr' _ _ _ = raise Match;
   204 
   205 in
   206 
   207 val typed_print_translation = [("size", size_tr')];
   208 
   209 end;