TFL/utils.sml
author wenzelm
Tue, 18 Jul 2000 21:09:18 +0200
changeset 9386 8800603d99f6
parent 3391 5e45dd3b64e9
child 9867 bf8300fa4238
permissions -rw-r--r--
theorems foo.splits = foo.split foo.split_asm;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3302
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     1
(*  Title:      TFL/utils
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     2
    ID:         $Id$
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     3
    Author:     Konrad Slind, Cambridge University Computer Laboratory
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     4
    Copyright   1997  University of Cambridge
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
     5
3302
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     6
Basic utilities
404fe31fd8d2 New headers and other minor changes
paulson
parents: 3245
diff changeset
     7
*)
3245
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3191
diff changeset
     8
241838c01caf Removal of redundant code (unused or already present in Isabelle.
paulson
parents: 3191
diff changeset
     9
structure Utils = 
2112
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    10
struct
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    11
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    12
(* Standard exception for TFL. *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    13
exception ERR of {module:string,func:string, mesg:string};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    14
fun UTILS_ERR{func,mesg} = ERR{module = "Utils",func=func,mesg=mesg};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    15
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    16
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    17
(* Simple combinators *)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    18
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    19
fun C f x y = f y x
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    20
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    21
fun itlist f L base_value =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    22
   let fun it [] = base_value
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    23
         | it (a::rst) = f a (it rst)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    24
   in it L 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    25
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    26
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    27
fun rev_itlist f =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    28
   let fun rev_it [] base = base
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    29
         | rev_it (a::rst) base = rev_it rst (f a base)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    30
   in rev_it
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    31
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    32
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    33
fun end_itlist f =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    34
   let fun endit [] = raise UTILS_ERR{func="end_itlist", mesg="list too short"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    35
         | endit alist = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    36
            let val (base::ralist) = rev alist
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    37
            in itlist f (rev ralist) base
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    38
            end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    39
   in endit
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    40
   end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    41
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    42
fun itlist2 f L1 L2 base_value =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    43
 let fun it ([],[]) = base_value
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    44
       | it ((a::rst1),(b::rst2)) = f a b (it (rst1,rst2))
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    45
       | it _ = raise UTILS_ERR{func="itlist2",mesg="different length lists"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    46
 in  it (L1,L2)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    47
 end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    48
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    49
fun pluck p  =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    50
  let fun remv ([],_) = raise UTILS_ERR{func="pluck",mesg = "item not found"}
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    51
        | remv (h::t, A) = if p h then (h, rev A @ t) else remv (t,h::A)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    52
  in fn L => remv(L,[])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    53
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    54
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    55
fun take f =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    56
  let fun grab(0,L) = []
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    57
        | grab(n, x::rst) = f x::grab(n-1,rst)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    58
  in grab
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    59
  end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    60
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    61
fun zip3 [][][] = []
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    62
  | zip3 (x::l1) (y::l2) (z::l3) = (x,y,z)::zip3 l1 l2 l3
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    63
  | zip3 _ _ _ = raise UTILS_ERR{func="zip3",mesg="different lengths"};
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    64
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    65
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    66
fun can f x = (f x ; true) handle _ => false;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    67
fun holds P x = P x handle _ => false;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    68
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    69
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    70
fun sort R = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    71
let fun part (m, []) = ([],[])
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    72
      | part (m, h::rst) =
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    73
         let val (l1,l2) = part (m,rst)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    74
         in if (R h m) then (h::l1, l2)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    75
                       else (l1,  h::l2) end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    76
    fun qsort [] = []
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    77
      | qsort (h::rst) = 
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    78
        let val (l1,l2) = part(h,rst)
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    79
        in qsort l1@ [h] @qsort l2
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    80
        end
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    81
in qsort
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    82
end;
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    83
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    84
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    85
3902e9af752f Konrad Slind's TFL
paulson
parents:
diff changeset
    86
end; (* Utils *)