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