TFL/utils.ML
author nipkow
Wed May 26 14:57:06 2004 +0200 (2004-05-26)
changeset 14804 8de39d3e8eb6
parent 10769 70b9b0cfe05f
child 16854 fdd362b7e980
permissions -rw-r--r--
Corrected printer bug for bounded quantifiers Q x<=y. P
wenzelm@10769
     1
(*  Title:      TFL/utils.ML
wenzelm@10769
     2
    ID:         $Id$
wenzelm@10769
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
wenzelm@10769
     4
    Copyright   1997  University of Cambridge
wenzelm@10769
     5
wenzelm@10769
     6
Basic utilities.
wenzelm@10769
     7
*)
wenzelm@10769
     8
wenzelm@10769
     9
signature UTILS =
wenzelm@10769
    10
sig
wenzelm@10769
    11
  exception ERR of {module: string, func: string, mesg: string}
wenzelm@10769
    12
  val C: ('a -> 'b -> 'c) -> 'b -> 'a -> 'c
wenzelm@10769
    13
  val itlist: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
wenzelm@10769
    14
  val rev_itlist: ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
wenzelm@10769
    15
  val end_itlist: ('a -> 'a -> 'a) -> 'a list -> 'a
wenzelm@10769
    16
  val itlist2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
wenzelm@10769
    17
  val pluck: ('a -> bool) -> 'a list -> 'a * 'a list
wenzelm@10769
    18
  val zip3: 'a list -> 'b list -> 'c list -> ('a*'b*'c) list
wenzelm@10769
    19
  val take: ('a -> 'b) -> int * 'a list -> 'b list
wenzelm@10769
    20
end;
wenzelm@10769
    21
wenzelm@10769
    22
structure Utils: UTILS =
wenzelm@10769
    23
struct
wenzelm@10769
    24
wenzelm@10769
    25
(*standard exception for TFL*)
wenzelm@10769
    26
exception ERR of {module: string, func: string, mesg: string};
wenzelm@10769
    27
wenzelm@10769
    28
fun UTILS_ERR func mesg = ERR {module = "Utils", func = func, mesg = mesg};
wenzelm@10769
    29
wenzelm@10769
    30
wenzelm@10769
    31
fun C f x y = f y x
wenzelm@10769
    32
wenzelm@10769
    33
fun itlist f L base_value =
wenzelm@10769
    34
   let fun it [] = base_value
wenzelm@10769
    35
         | it (a::rst) = f a (it rst)
wenzelm@10769
    36
   in it L
wenzelm@10769
    37
   end;
wenzelm@10769
    38
wenzelm@10769
    39
fun rev_itlist f =
wenzelm@10769
    40
   let fun rev_it [] base = base
wenzelm@10769
    41
         | rev_it (a::rst) base = rev_it rst (f a base)
wenzelm@10769
    42
   in rev_it
wenzelm@10769
    43
   end;
wenzelm@10769
    44
wenzelm@10769
    45
fun end_itlist f =
wenzelm@10769
    46
   let fun endit [] = raise UTILS_ERR "end_itlist" "list too short"
wenzelm@10769
    47
         | endit alist =
wenzelm@10769
    48
            let val (base::ralist) = rev alist
wenzelm@10769
    49
            in itlist f (rev ralist) base
wenzelm@10769
    50
            end
wenzelm@10769
    51
   in endit
wenzelm@10769
    52
   end;
wenzelm@10769
    53
wenzelm@10769
    54
fun itlist2 f L1 L2 base_value =
wenzelm@10769
    55
 let fun it ([],[]) = base_value
wenzelm@10769
    56
       | it ((a::rst1),(b::rst2)) = f a b (it (rst1,rst2))
wenzelm@10769
    57
       | it _ = raise UTILS_ERR "itlist2" "different length lists"
wenzelm@10769
    58
 in  it (L1,L2)
wenzelm@10769
    59
 end;
wenzelm@10769
    60
wenzelm@10769
    61
fun pluck p  =
wenzelm@10769
    62
  let fun remv ([],_) = raise UTILS_ERR "pluck" "item not found"
wenzelm@10769
    63
        | remv (h::t, A) = if p h then (h, rev A @ t) else remv (t,h::A)
wenzelm@10769
    64
  in fn L => remv(L,[])
wenzelm@10769
    65
  end;
wenzelm@10769
    66
wenzelm@10769
    67
fun take f =
wenzelm@10769
    68
  let fun grab(0,L) = []
wenzelm@10769
    69
        | grab(n, x::rst) = f x::grab(n-1,rst)
wenzelm@10769
    70
  in grab
wenzelm@10769
    71
  end;
wenzelm@10769
    72
wenzelm@10769
    73
fun zip3 [][][] = []
wenzelm@10769
    74
  | zip3 (x::l1) (y::l2) (z::l3) = (x,y,z)::zip3 l1 l2 l3
wenzelm@10769
    75
  | zip3 _ _ _ = raise UTILS_ERR "zip3" "different lengths";
wenzelm@10769
    76
wenzelm@10769
    77
wenzelm@10769
    78
end;