src/HOL/Tools/TFL/utils.ML
author wenzelm
Mon, 05 Aug 2013 15:03:52 +0200
changeset 52861 e93d73b51fd0
parent 41589 bbd861837ebc
child 60329 e85ed7a36b2f
permissions -rw-r--r--
commands with overlay remain visible, to avoid loosing printed output;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23150
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     1
(*  Title:      HOL/Tools/TFL/utils.ML
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     2
    Author:     Konrad Slind, Cambridge University Computer Laboratory
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     3
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     4
Basic utilities.
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     5
*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     6
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     7
signature UTILS =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     8
sig
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
     9
  exception ERR of {module: string, func: string, mesg: string}
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    10
  val C: ('a -> 'b -> 'c) -> 'b -> 'a -> 'c
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    11
  val end_itlist: ('a -> 'a -> 'a) -> 'a list -> 'a
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    12
  val itlist2: ('a -> 'b -> 'c -> 'c) -> 'a list -> 'b list -> 'c -> 'c
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    13
  val pluck: ('a -> bool) -> 'a list -> 'a * 'a list
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    14
  val zip3: 'a list -> 'b list -> 'c list -> ('a*'b*'c) list
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    15
  val take: ('a -> 'b) -> int * 'a list -> 'b list
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    16
end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    17
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    18
structure Utils: UTILS =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    19
struct
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    20
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    21
(*standard exception for TFL*)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    22
exception ERR of {module: string, func: string, mesg: string};
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    23
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    24
fun UTILS_ERR func mesg = ERR {module = "Utils", func = func, mesg = mesg};
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    25
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    26
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    27
fun C f x y = f y x
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    28
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    29
fun end_itlist f [] = raise (UTILS_ERR "end_itlist" "list too short")
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    30
  | end_itlist f [x] = x 
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    31
  | end_itlist f (x :: xs) = f x (end_itlist f xs);
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    32
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    33
fun itlist2 f L1 L2 base_value =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    34
 let fun it ([],[]) = base_value
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    35
       | it ((a::rst1),(b::rst2)) = f a b (it (rst1,rst2))
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    36
       | it _ = raise UTILS_ERR "itlist2" "different length lists"
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    37
 in  it (L1,L2)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    38
 end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    39
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    40
fun pluck p  =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    41
  let fun remv ([],_) = raise UTILS_ERR "pluck" "item not found"
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    42
        | remv (h::t, A) = if p h then (h, rev A @ t) else remv (t,h::A)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    43
  in fn L => remv(L,[])
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    44
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    45
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    46
fun take f =
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    47
  let fun grab(0,L) = []
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    48
        | grab(n, x::rst) = f x::grab(n-1,rst)
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    49
  in grab
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    50
  end;
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    51
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    52
fun zip3 [][][] = []
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    53
  | zip3 (x::l1) (y::l2) (z::l3) = (x,y,z)::zip3 l1 l2 l3
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    54
  | zip3 _ _ _ = raise UTILS_ERR "zip3" "different lengths";
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    55
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    56
073a65f0bc40 moved TFL files to canonical place;
wenzelm
parents:
diff changeset
    57
end;