TFL/utils.ML
author obua
Fri, 16 Sep 2005 21:02:15 +0200
changeset 17440 df77edc4f5d0
parent 16854 fdd362b7e980
permissions -rw-r--r--
fixed HOL-light/Isabelle syntax incompatability via more protect_xxx functions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      TFL/utils.ML
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     4
    Copyright   1997  University of Cambridge
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     5
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     6
Basic utilities.
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     7
*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     8
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
     9
signature UTILS =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    10
sig
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    11
  exception ERR of {module: string, func: string, mesg: string}
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    12
  val C: ('a -> 'b -> 'c) -> 'b -> 'a -> 'c
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    13
  val end_itlist: ('a -> 'a -> 'a) -> 'a list -> 'a
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    14
  val itlist2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    15
  val pluck: ('a -> bool) -> 'a list -> 'a * 'a list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    16
  val zip3: 'a list -> 'b list -> 'c list -> ('a*'b*'c) list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    17
  val take: ('a -> 'b) -> int * 'a list -> 'b list
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    18
end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    19
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    20
structure Utils: UTILS =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    21
struct
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    22
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    23
(*standard exception for TFL*)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    24
exception ERR of {module: string, func: string, mesg: string};
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    25
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    26
fun UTILS_ERR func mesg = ERR {module = "Utils", func = func, mesg = mesg};
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    27
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    28
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    29
fun C f x y = f y x
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    30
16854
fdd362b7e980 removed itlist, rev_itlist -- use fold_rev, fold instead;
wenzelm
parents: 10769
diff changeset
    31
fun end_itlist f [] = raise (UTILS_ERR "end_itlist" "list too short")
fdd362b7e980 removed itlist, rev_itlist -- use fold_rev, fold instead;
wenzelm
parents: 10769
diff changeset
    32
  | end_itlist f [x] = x 
fdd362b7e980 removed itlist, rev_itlist -- use fold_rev, fold instead;
wenzelm
parents: 10769
diff changeset
    33
  | end_itlist f (x :: xs) = f x (end_itlist f xs);
10769
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    34
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    35
fun itlist2 f L1 L2 base_value =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    36
 let fun it ([],[]) = base_value
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    37
       | it ((a::rst1),(b::rst2)) = f a b (it (rst1,rst2))
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    38
       | it _ = raise UTILS_ERR "itlist2" "different length lists"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    39
 in  it (L1,L2)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    40
 end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    41
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    42
fun pluck p  =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    43
  let fun remv ([],_) = raise UTILS_ERR "pluck" "item not found"
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    44
        | remv (h::t, A) = if p h then (h, rev A @ t) else remv (t,h::A)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    45
  in fn L => remv(L,[])
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    46
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    47
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    48
fun take f =
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    49
  let fun grab(0,L) = []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    50
        | grab(n, x::rst) = f x::grab(n-1,rst)
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    51
  in grab
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    52
  end;
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    53
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    54
fun zip3 [][][] = []
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    55
  | zip3 (x::l1) (y::l2) (z::l3) = (x,y,z)::zip3 l1 l2 l3
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    56
  | zip3 _ _ _ = raise UTILS_ERR "zip3" "different lengths";
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    57
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    58
70b9b0cfe05f renamed .sml files to .ML;
wenzelm
parents:
diff changeset
    59
end;