TFL/utils.sml
author wenzelm
Fri Mar 07 15:30:23 1997 +0100 (1997-03-07)
changeset 2768 bc6d915b8019
parent 2467 357adb429fda
child 3191 14bd6e5985f1
permissions -rw-r--r--
renamed SYSTEM to RAW_ML_SYSTEM;
paulson@2112
     1
(*---------------------------------------------------------------------------
paulson@2112
     2
 * Some common utilities. This strucuture is parameterized over
paulson@2112
     3
 * "int_to_string" because there is a difference between the string 
paulson@2112
     4
 * operations of SML/NJ versions 93 and 109.
paulson@2112
     5
 *---------------------------------------------------------------------------*)
paulson@2112
     6
paulson@2112
     7
functor UTILS (val int_to_string : int -> string) :Utils_sig = 
paulson@2112
     8
struct
paulson@2112
     9
paulson@2112
    10
(* Standard exception for TFL. *)
paulson@2112
    11
exception ERR of {module:string,func:string, mesg:string};
paulson@2112
    12
fun UTILS_ERR{func,mesg} = ERR{module = "Utils",func=func,mesg=mesg};
paulson@2112
    13
paulson@2112
    14
local 
paulson@2112
    15
fun info_string s {module,func,mesg} =
paulson@2112
    16
       (s^" at "^module^"."^func^":\n"^mesg^"\n")
paulson@2112
    17
in
paulson@2112
    18
val ERR_string = info_string "Exception raised"
paulson@2112
    19
val MESG_string = info_string "Message"
paulson@2112
    20
end;
paulson@2112
    21
paulson@2467
    22
fun Raise (e as ERR sss) = (prs (ERR_string sss); raise e)
paulson@2112
    23
  | Raise e = raise e;
paulson@2112
    24
paulson@2112
    25
paulson@2112
    26
(* option type *)
paulson@2112
    27
datatype 'a option = SOME of 'a | NONE
paulson@2112
    28
paulson@2112
    29
paulson@2112
    30
(* Simple combinators *)
paulson@2112
    31
paulson@2112
    32
infix 3 ##
paulson@2112
    33
fun f ## g = (fn (x,y) => (f x, g y))
paulson@2112
    34
paulson@2112
    35
fun W f x = f x x
paulson@2112
    36
fun C f x y = f y x
paulson@2112
    37
fun I x = x
paulson@2112
    38
paulson@2112
    39
fun curry f x y = f(x,y)
paulson@2112
    40
fun uncurry f (x,y) = f x y
paulson@2112
    41
paulson@2112
    42
fun fst(x,y) = x
paulson@2112
    43
fun snd(x,y) = y;
paulson@2112
    44
paulson@2112
    45
val concat = curry (op ^)
paulson@2112
    46
fun quote s = "\""^s^"\"";
paulson@2112
    47
paulson@2112
    48
fun map2 f L1 L2 =
paulson@2112
    49
 let fun mp2 [] [] L = rev L
paulson@2112
    50
       | mp2 (a1::rst1) (a2::rst2) L = mp2 rst1 rst2 (f a1 a2::L)
paulson@2112
    51
       | mp2 _ _ _ = raise UTILS_ERR{func="map2",mesg="different length lists"}
paulson@2112
    52
   in mp2 L1 L2 []
paulson@2112
    53
   end;
paulson@2112
    54
paulson@2112
    55
paulson@2112
    56
fun itlist f L base_value =
paulson@2112
    57
   let fun it [] = base_value
paulson@2112
    58
         | it (a::rst) = f a (it rst)
paulson@2112
    59
   in it L 
paulson@2112
    60
   end;
paulson@2112
    61
paulson@2112
    62
fun rev_itlist f =
paulson@2112
    63
   let fun rev_it [] base = base
paulson@2112
    64
         | rev_it (a::rst) base = rev_it rst (f a base)
paulson@2112
    65
   in rev_it
paulson@2112
    66
   end;
paulson@2112
    67
paulson@2112
    68
fun end_itlist f =
paulson@2112
    69
   let fun endit [] = raise UTILS_ERR{func="end_itlist", mesg="list too short"}
paulson@2112
    70
         | endit alist = 
paulson@2112
    71
            let val (base::ralist) = rev alist
paulson@2112
    72
            in itlist f (rev ralist) base
paulson@2112
    73
            end
paulson@2112
    74
   in endit
paulson@2112
    75
   end;
paulson@2112
    76
paulson@2112
    77
fun itlist2 f L1 L2 base_value =
paulson@2112
    78
 let fun it ([],[]) = base_value
paulson@2112
    79
       | it ((a::rst1),(b::rst2)) = f a b (it (rst1,rst2))
paulson@2112
    80
       | it _ = raise UTILS_ERR{func="itlist2",mesg="different length lists"}
paulson@2112
    81
 in  it (L1,L2)
paulson@2112
    82
 end;
paulson@2112
    83
paulson@2112
    84
fun filter p L = itlist (fn x => fn y => if (p x) then x::y else y) L []
paulson@2112
    85
paulson@2112
    86
fun mapfilter f alist = itlist (fn i=>fn L=> (f i::L) handle _ => L) alist [];
paulson@2112
    87
paulson@2112
    88
fun pluck p  =
paulson@2112
    89
  let fun remv ([],_) = raise UTILS_ERR{func="pluck",mesg = "item not found"}
paulson@2112
    90
        | remv (h::t, A) = if p h then (h, rev A @ t) else remv (t,h::A)
paulson@2112
    91
  in fn L => remv(L,[])
paulson@2112
    92
  end;
paulson@2112
    93
paulson@2112
    94
fun front_back [] = raise UTILS_ERR{func="front_back",mesg="empty list"}
paulson@2112
    95
  | front_back [x] = ([],x)
paulson@2112
    96
  | front_back (h::t) = 
paulson@2112
    97
       let val (L,b) = front_back t
paulson@2112
    98
       in (h::L,b)
paulson@2112
    99
       end;
paulson@2112
   100
paulson@2112
   101
fun take f =
paulson@2112
   102
  let fun grab(0,L) = []
paulson@2112
   103
        | grab(n, x::rst) = f x::grab(n-1,rst)
paulson@2112
   104
  in grab
paulson@2112
   105
  end;
paulson@2112
   106
paulson@2112
   107
fun all p =
paulson@2112
   108
   let fun every [] = true
paulson@2112
   109
         | every (a::rst) = (p a) andalso every rst       
paulson@2112
   110
   in every 
paulson@2112
   111
   end;
paulson@2112
   112
paulson@2112
   113
fun exists p =
paulson@2112
   114
   let fun ex [] = false
paulson@2112
   115
         | ex (a::rst) = (p a) orelse ex rst       
paulson@2112
   116
   in ex 
paulson@2112
   117
   end;
paulson@2112
   118
paulson@2112
   119
fun zip [] [] = []
paulson@2112
   120
  | zip (a::b) (c::d) = (a,c)::(zip b d)
paulson@2112
   121
  | zip _ _ = raise UTILS_ERR{func = "zip",mesg = "different length lists"};
paulson@2112
   122
paulson@2112
   123
fun unzip L = itlist (fn (x,y) => fn (l1,l2) =>((x::l1),(y::l2))) L ([],[]);
paulson@2112
   124
paulson@2112
   125
fun zip3 [][][] = []
paulson@2112
   126
  | zip3 (x::l1) (y::l2) (z::l3) = (x,y,z)::zip3 l1 l2 l3
paulson@2112
   127
  | zip3 _ _ _ = raise UTILS_ERR{func="zip3",mesg="different lengths"};
paulson@2112
   128
paulson@2112
   129
paulson@2112
   130
fun can f x = (f x ; true) handle _ => false;
paulson@2112
   131
fun holds P x = P x handle _ => false;
paulson@2112
   132
paulson@2112
   133
paulson@2112
   134
fun assert p x = 
paulson@2112
   135
 if (p x) then x else raise UTILS_ERR{func="assert", mesg="predicate not true"}
paulson@2112
   136
paulson@2112
   137
fun assoc1 eq item =
paulson@2112
   138
   let fun assc ((entry as (key,_))::rst) = 
paulson@2112
   139
             if eq(item,key) then SOME entry else assc rst
paulson@2112
   140
         | assc [] = NONE
paulson@2112
   141
    in assc
paulson@2112
   142
    end;
paulson@2112
   143
paulson@2112
   144
(* Set ops *)
paulson@2112
   145
nonfix mem union;  (* Gag Barf Choke *)
paulson@2112
   146
fun mem eq_func i =
paulson@2112
   147
   let val eqi = eq_func i
paulson@2112
   148
       fun mm [] = false
paulson@2112
   149
         | mm (a::rst) = eqi a orelse mm rst
paulson@2112
   150
   in mm
paulson@2112
   151
   end;
paulson@2112
   152
paulson@2112
   153
fun union eq_func =
paulson@2112
   154
   let val mem = mem eq_func
paulson@2112
   155
       fun un S1 []  = S1
paulson@2112
   156
         | un [] S1  = S1
paulson@2112
   157
         | un (a::rst) S2 = if (mem a S2) then (un rst S2) else (a::un rst S2)
paulson@2112
   158
   in un
paulson@2112
   159
   end;
paulson@2112
   160
paulson@2112
   161
fun mk_set eq_func =
paulson@2112
   162
   let val mem = mem eq_func
paulson@2112
   163
       fun mk [] = []
paulson@2112
   164
         | mk (a::rst) = if (mem a rst) then mk rst else a::(mk rst)
paulson@2112
   165
   in mk
paulson@2112
   166
   end;
paulson@2112
   167
paulson@2112
   168
(* Union of a family of sets *)
paulson@2112
   169
fun Union eq_func set_o_sets = itlist (union eq_func) set_o_sets [];
paulson@2112
   170
paulson@2112
   171
fun intersect eq_func S1 S2 = mk_set eq_func (filter (C (mem eq_func) S2) S1);
paulson@2112
   172
paulson@2112
   173
(* All the elements in the first set that are not also in the second set. *)
paulson@2112
   174
fun set_diff eq_func S1 S2 = filter (fn x => not (mem eq_func x S2)) S1
paulson@2112
   175
paulson@2112
   176
fun set_eq eq_func S1 S2 = 
paulson@2112
   177
   null(set_diff eq_func S1 S2) andalso null(set_diff eq_func S2 S1);
paulson@2112
   178
paulson@2112
   179
paulson@2112
   180
fun sort R = 
paulson@2112
   181
let fun part (m, []) = ([],[])
paulson@2112
   182
      | part (m, h::rst) =
paulson@2112
   183
         let val (l1,l2) = part (m,rst)
paulson@2112
   184
         in if (R h m) then (h::l1, l2)
paulson@2112
   185
                       else (l1,  h::l2) end
paulson@2112
   186
    fun qsort [] = []
paulson@2112
   187
      | qsort (h::rst) = 
paulson@2112
   188
        let val (l1,l2) = part(h,rst)
paulson@2112
   189
        in qsort l1@ [h] @qsort l2
paulson@2112
   190
        end
paulson@2112
   191
in qsort
paulson@2112
   192
end;
paulson@2112
   193
paulson@2112
   194
paulson@2112
   195
val int_to_string = int_to_string;
paulson@2112
   196
paulson@2112
   197
end; (* Utils *)