[MLton-commit] r4353

Matthew Fluet MLton@mlton.org
Sun, 12 Feb 2006 10:36:40 -0800


Refactoring arrays-and-vectors
----------------------------------------------------------------------

U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/array.sig
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/array.sml
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/mono-array.sig
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/sequence.fun
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/sequence.sig
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/slice.sig
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector-slice.sig
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector.sig
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector.sml
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/build/sources.mlb
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/integer/int-inf0.sml
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/primitive/prim-seq.sml
U   mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/primitive/prim2.sml

----------------------------------------------------------------------

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/array.sig
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/array.sig	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/array.sig	2006-02-12 18:36:38 UTC (rev 4353)
@@ -40,7 +40,6 @@
 
       structure ArraySlice: ARRAY_SLICE_EXTRA 
 
-      val rawArray: int -> 'a array
       val unsafeSub: 'a array * int -> 'a
       val unsafeUpdate: 'a array * int * 'a -> unit
 
@@ -48,9 +47,4 @@
       val duplicate: 'a array -> 'a array
       val toList: 'a array -> 'a list
       val unfoldi: int * 'a * (int * 'a -> 'b * 'a) -> 'b array
-
-      (* Deprecated *)
-      val checkSlice: 'a array * int * int option -> int
-      (* Deprecated *)
-      val checkSliceMax: int * int option * int -> int
    end

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/array.sml
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/array.sml	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/array.sml	2006-02-12 18:36:38 UTC (rev 4353)
@@ -13,28 +13,43 @@
                               val fromArray = fn a => a
                               val isMutable = true
                               val length = Primitive.Array.length
-                              val sub = Primitive.Array.sub)
+                              val subUnsafe = Primitive.Array.subUnsafe)
       open A
-      open Primitive.Int
 
+      val op +? = Int.+?
+      val op + = Int.+
+      val op -? = Int.-?
+      val op - = Int.-
+      val op < = Int.<
+      val op <= = Int.<=
+      val op > = Int.>
+      val op >= = Int.>=
+
+      fun wrap2 f = fn (i, x) => f (SeqIndex.toIntUnsafe i, x)
+
       type 'a array = 'a array
       type 'a vector = 'a Vector.vector
 
       structure ArraySlice =
          struct
             open Slice
+            fun update' (arr, i, x) = 
+               updateMk' Primitive.Array.updateUnsafe (arr, i, x)
             fun update (arr, i, x) = 
-               update' Primitive.Array.update (arr, i, x)
+               updateMk Primitive.Array.updateUnsafe (arr, i, x)
+            fun unsafeUpdate' (arr, i, x) = 
+               unsafeUpdateMk' Primitive.Array.updateUnsafe (arr, i, x)
             fun unsafeUpdate (arr, i, x) = 
-               unsafeUpdate' Primitive.Array.update (arr, i, x)
-            fun vector sl = create Vector.tabulate (fn x => x) sl
-            fun modifyi f sl = 
-               appi (fn (i, x) => unsafeUpdate (sl, i, f (i, x))) sl
+               unsafeUpdateMk Primitive.Array.updateUnsafe (arr, i, x)
+            fun vector sl = create Vector.tabulate' (fn x => x) sl
+            fun modifyi' f sl =
+               appi' (fn (i, x) => unsafeUpdate' (sl, i, f (i, x))) sl
+            fun modifyi f sl = modifyi' (wrap2 f) sl
             fun modify f sl = modifyi (f o #2) sl
             local
-               fun make (length, sub) {src, dst, di} =
-                  modifyi (fn (i, _) => sub (src, i)) 
-                          (slice (dst, di, SOME (length src)))
+               fun make (length, sub') {src, dst, di} =
+                  modifyi' (fn (i, _) => sub' (src, i)) 
+                           (slice (dst, di, SOME (length src)))
             in
                fun copy (arg as {src, dst, di}) =
                   let val (src', si', len') = base src
@@ -42,25 +57,25 @@
                     if src' = dst andalso si' < di andalso si' +? len' >= di
                        then let val sl = slice (dst, di, SOME (length src))
                             in 
-                               foldri (fn (i, _, _) => 
-                                       unsafeUpdate (sl, i, unsafeSub (src, i)))
+                               foldri' (fn (i, _, _) => 
+                                        unsafeUpdate' (sl, i, unsafeSub' (src, i)))
                                () sl
                             end
-                    else make (length, unsafeSub) arg
+                    else make (length, unsafeSub') arg
                   end
 
                fun copyVec arg =
-                  make (Vector.VectorSlice.length, Vector.VectorSlice.unsafeSub) arg
+                  make (Vector.VectorSlice.length, Vector.VectorSlice.unsafeSub') arg
             end
          end
 
-      val rawArray = Primitive.Array.array
       val array = new
 
       local
         fun make f arr = f (ArraySlice.full arr)
       in
         fun vector arr = make (ArraySlice.vector) arr
+        fun modifyi' f = make (ArraySlice.modifyi' f)
         fun modifyi f = make (ArraySlice.modifyi f)
         fun modify f = make (ArraySlice.modify f)
         fun copy {src, dst, di} = ArraySlice.copy {src = ArraySlice.full src,
@@ -69,9 +84,8 @@
                                                          dst = dst, di = di}
       end
 
-      val unsafeSub = Primitive.Array.sub
-      fun update (arr, i, x) = update' Primitive.Array.update (arr, i, x)
-      val unsafeUpdate = Primitive.Array.update
+      fun update (arr, i, x) = updateMk Primitive.Array.updateUnsafe (arr, i, x)
+      fun unsafeUpdate (arr, i, x) = unsafeUpdateMk Primitive.Array.updateUnsafe (arr, i, x)
    end
 structure ArraySlice: ARRAY_SLICE_EXTRA = Array.ArraySlice
 

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/mono-array.sig
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/mono-array.sig	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/mono-array.sig	2006-02-12 18:36:38 UTC (rev 4353)
@@ -42,7 +42,6 @@
       val concat: array list -> array
       val duplicate: array -> array
       val fromPoly: elem Array.array -> array
-      val rawArray: int -> array
       val toList: array -> elem list
       val toPoly: array -> elem Array.array
       val unfoldi: int * 'a * (int * 'a -> elem * 'a) -> array

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/sequence.fun
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/sequence.fun	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/sequence.fun	2006-02-12 18:36:38 UTC (rev 4353)
@@ -12,75 +12,105 @@
                         (* fromArray should be constant time. *)
                         val fromArray: 'a elt array -> 'a sequence 
                         val isMutable: bool
-                        val length: 'a sequence -> int
-                        val sub: 'a sequence * int -> 'a elt
+                        val length: 'a sequence -> SeqIndex.int
+                        val subUnsafe: 'a sequence * SeqIndex.int -> 'a elt
                      end
                   ): SEQUENCE =
    struct
-      open S
-
       structure Array = Primitive.Array
 
-      open Int
+      val op +? = SeqIndex.+?
+      val op + = SeqIndex.+
+      val op -? = SeqIndex.-?
+      val op - = SeqIndex.-
+      val op < = SeqIndex.<
+      val op <= = SeqIndex.<=
+      val op > = SeqIndex.>
+      val op >= = SeqIndex.>=
+      val ltu = SeqIndex.ltu
+      val leu = SeqIndex.leu
+      val gtu = SeqIndex.gtu
+      val geu = SeqIndex.geu
 
-      val maxLen = Array.maxLen
+      fun wrap1 f = fn (i) => f (SeqIndex.toIntUnsafe i)
+      fun wrap2 f = fn (i, x) => f (SeqIndex.toIntUnsafe i, x)
+      fun wrap3 f = fn (i, x, y) => f (SeqIndex.toIntUnsafe i, x, y)
 
-      fun array n =
-         if not isMutable andalso n = 0
+      type 'a sequence = 'a S.sequence
+      type 'a elt = 'a S.elt
+
+      (* 
+       * In general, *' values are in terms of SeqIndex.int,
+       * while * values are in terms of Int.int.
+       *)
+
+      local
+         fun doit (toInt, fromInt, maxInt') =
+            (Array.maxLen', toInt Array.maxLen') 
+            handle Overflow => (fromInt maxInt', maxInt')
+         structure S =
+            Int_ChooseInt
+            (type 'a t = SeqIndex.int * 'a
+             val fInt8 = doit (SeqIndex.toInt8, SeqIndex.fromInt8,
+                               Primitive.Int8.maxInt')
+             val fInt16 = doit (SeqIndex.toInt16, SeqIndex.fromInt16,
+                                Primitive.Int16.maxInt')
+             val fInt32 = doit (SeqIndex.toInt32, SeqIndex.fromInt32,
+                                Primitive.Int32.maxInt')
+             val fInt64 = doit (SeqIndex.toInt64, SeqIndex.fromInt64,
+                                Primitive.Int64.maxInt')
+             val fIntInf = (Array.maxLen', SeqIndex.toIntInf Array.maxLen'))
+      in
+         val (maxLen', maxLen) = S.f
+      end
+
+      fun fromIntForLength n =
+         if Primitive.Controls.safe
+            then (SeqIndex.fromInt n) handle Overflow => raise Size
+            else SeqIndex.fromIntUnsafe n
+
+      fun length' s = S.length s
+      fun length s = 
+         if Primitive.Controls.safe
+            then (SeqIndex.toInt (length' s))
+                 handle Overflow => raise Fail "Sequence.length"
+            else SeqIndex.toIntUnsafe (length' s)
+
+      fun array' n =
+         if not S.isMutable andalso n = 0
             then Array.array0Const ()
-         else Array.array n
+            else if Primitive.Controls.safe
+                    andalso (n < 0 orelse n > maxLen')
+                    then raise Size
+                    else Array.arrayUnsafe n
+      fun array n = array' (fromIntForLength n)
 
-      fun seq0 () = fromArray (array 0)
+      fun seq0 () = S.fromArray (array' 0)
 
-      fun unfoldi (n, b, f) =
+      fun unfoldi' (n, b, f) =
          let
-            val a = array n
+            val a = array' n
             fun loop (i, b)  =
                if i >= n
                   then ()
                else
                   let
                      val (x, b') = f (i, b)
-                     val _ = Array.update (a, i, x)
+                     val _ = Array.updateUnsafe (a, i, x)
                   in
                      loop (i +? 1, b')
                   end
             val _ = loop (0, b)
          in
-            fromArray a
+            S.fromArray a
          end
+      fun unfoldi (n, b, f) = unfoldi' (fromIntForLength n, b, wrap2 f)
+      fun unfold (n, b, f) = unfoldi (n, b, f o #2)
 
-      (* Tabulate depends on the fact that the runtime system fills in the array
-       * with reasonable bogus values.
-       *)
+      fun tabulate' (n, f) =
+         unfoldi' (n, (), fn (i, ()) => (f i, ()))
       fun tabulate (n, f) =
-(*
-         if !Primitive.usesCallcc
-            then
-               (* This code is careful to use a list to accumulate the 
-                * components of the array in case f uses callcc.
-                *)
-               let
-                  fun loop (i, l) =
-                     if i >= n
-                        then l
-                     else loop (i + 1, f i :: l)
-                  val l = loop (0, [])
-                  val a = array n
-                  fun loop (l, i) =
-                     case l of
-                        [] => ()
-                      | x :: l =>
-                           let val i = i -? 1
-                           in Array.update (a, i, x)
-                              ; loop (l, i)
-                           end
-               in loop (l, n)
-                  ; fromArray a
-               end
-         else
-*)
-            unfoldi (n, (), fn (i, ()) => (f i, ()))
+         unfoldi (n, (), fn (i, ()) => (f i, ()))
 
       fun new (n, x) = tabulate (n, fn _ => x)
 
@@ -88,116 +118,177 @@
          let
             val a = array (List.length l)
             val _ =
-               List.foldl (fn (c, i) => (Array.update (a, i, c) ; i +? 1)) 0 l
+               List.foldl (fn (x, i) => (Array.updateUnsafe (a, i, x) ; (i +? 1))) 0 l
          in
-            fromArray a
+            S.fromArray a
          end
 
       structure Slice =
          struct
-            type 'a sequence = 'a sequence
-            type 'a elt = 'a elt
-            datatype 'a t = T of {seq: 'a sequence, start: int, len: int}
+            type 'a sequence = 'a S.sequence
+            type 'a elt = 'a S.elt
+            datatype 'a t = T of {seq: 'a sequence, 
+                                  start: SeqIndex.int, len: SeqIndex.int}
             type 'a slice = 'a t
 
-            fun length (T {len, ...}) = len
-            fun unsafeSub (T {seq, start, ...}, i) =
-               S.sub (seq, start +? i)
-            fun sub (sl as T {len, ...}, i) =
-               if Primitive.Controls.safe andalso Int.geu (i, len)
+            fun length' (T {len, ...}) = len
+            fun length sl = 
+               if Primitive.Controls.safe
+                  then (SeqIndex.toInt (length' sl)) 
+                       handle Overflow => raise Fail "Sequence.Slice.length"
+                  else SeqIndex.toIntUnsafe (length' sl)
+            fun unsafeSub' (T {seq, start, ...}, i) =
+               S.subUnsafe (seq, start +? i)
+            fun unsafeSub (sl, i) =
+               unsafeSub' (sl, SeqIndex.fromIntUnsafe i)
+            fun sub' (sl as T {len, ...}, i) =
+               if Primitive.Controls.safe andalso geu (i, len)
                   then raise Subscript
-               else unsafeSub (sl, i)
-            fun unsafeUpdate' update (T {seq, start, ...}, i, x) =
-               update (seq, start +? i, x)
-            fun update' update (sl as T {len, ...}, i, x) =
-               if Primitive.safe andalso Int.geu (i, len)
+                  else unsafeSub' (sl, i)
+            fun sub (sl, i) =
+               if Primitive.Controls.safe
+                  then let
+                          val i = 
+                             (SeqIndex.fromInt i)
+                             handle Overflow => raise Subscript
+                       in
+                          sub' (sl, i)
+                       end
+                  else unsafeSub (sl, i)
+            fun unsafeUpdateMk' updateUnsafe (T {seq, start, ...}, i, x) =
+               updateUnsafe (seq, start +? i, x)
+            fun unsafeUpdateMk updateUnsafe (sl, i, x) =
+               unsafeUpdateMk' updateUnsafe (sl, SeqIndex.fromIntUnsafe i, x)
+            fun updateMk' updateUnsafe (sl as T {len, ...}, i, x) =
+               if Primitive.Controls.safe andalso geu (i, len)
                   then raise Subscript
-               else unsafeUpdate' update (sl, i, x)
+                  else unsafeUpdateMk' updateUnsafe (sl, i, x)
+            fun updateMk updateUnsafe (sl, i, x) =
+               if Primitive.Controls.safe
+                  then let
+                          val i =
+                             (SeqIndex.fromInt i)
+                             handle Overflow => raise Subscript
+                       in
+                          updateMk' updateUnsafe (sl, i, x)
+                       end
+                  else unsafeUpdateMk updateUnsafe (sl, i, x)
             fun full (seq: 'a sequence) : 'a slice = 
                T {seq = seq, start = 0, len = S.length seq}
-            fun subslice (T {seq, start, len}, start', len') = 
-               case len' of
-                  NONE => if Primitive.safe andalso
-                             (start' < 0 orelse start' > len)
-                             then raise Subscript
-                          else T {seq = seq,
-                                  start = start +? start',
-                                  len = len -? start'}
-                | SOME len' => if Primitive.safe andalso
-                                  (start' < 0 orelse start' > len orelse
-                                   len' < 0 orelse len' > len -? start')
-                                  then raise Subscript
-                               else T {seq = seq,
-                                       start = start +? start',
-                                       len = len'}
-            fun unsafeSubslice (T {seq, start, len}, start', len') = 
+            fun unsafeSubslice' (T {seq, start, len}, start', len') = 
                T {seq = seq, 
                   start = start +? start',
                   len = (case len' of
                             NONE => len -? start'
                           | SOME len' => len')}
+            fun unsafeSubslice (sl, start, len) =
+               unsafeSubslice' 
+               (sl, SeqIndex.fromIntUnsafe start, 
+                Option.map SeqIndex.fromIntUnsafe len)
+            fun unsafeSlice' (seq, start, len) =
+               unsafeSubslice' (full seq, start, len)
+            fun unsafeSlice (seq, start, len) =
+               unsafeSubslice  (full seq, start, len)
+            fun subslice' (T {seq, start, len}, start', len') =
+               case len' of
+                  NONE => 
+                     if Primitive.Controls.safe 
+                        andalso gtu (start', len)
+                        then raise Subscript
+                        else T {seq = seq,
+                                start = start +? start',
+                                len = len -? start'}
+                | SOME len' => 
+                     if Primitive.Controls.safe
+                        andalso (gtu (start', len)
+                                 orelse gtu (len', len -? start'))
+                        then raise Subscript
+                        else T {seq = seq,
+                                start = start +? start',
+                                len = len'}
+            fun subslice (sl, start, len) =
+               if Primitive.Controls.safe
+                  then (subslice' (sl, 
+                                   SeqIndex.fromInt start,
+                                   Option.map SeqIndex.fromInt len))
+                       handle Overflow => raise Subscript
+                  else unsafeSubslice (sl, start, len)
+            fun slice' (seq: 'a sequence, start, len) =
+               subslice' (full seq, start, len)
             fun slice (seq: 'a sequence, start, len) =
                subslice (full seq, start, len)
-            fun unsafeSlice (seq: 'a sequence, start, len) =
-               unsafeSubslice (full seq, start, len)
-            fun base (T {seq, start, len}) = (seq, start, len)
+            fun base' (T {seq, start, len}) = 
+               (seq, start, len)
+            fun base (T {seq, start, len}) = 
+               (seq, SeqIndex.toIntUnsafe start, SeqIndex.toIntUnsafe len)
             fun isEmpty sl = length sl = 0
             fun getItem (sl as T {seq, start, len}) =
                if isEmpty sl
                   then NONE
-               else SOME (S.sub (seq, start), 
+               else SOME (S.subUnsafe (seq, start), 
                           T {seq = seq, 
                              start = start +? 1, 
                              len = len -? 1})
-            fun foldli f b (T {seq, start, len}) =
+            fun foldli' f b (T {seq, start, len}) =
                let
                   val min = start
+                  val len = len -? 1
                   val max = start +? len
                   fun loop (i, b) =
-                     if i >= max then b
-                     else loop (i +? 1, f (i -? min, S.sub (seq, i), b))
+                     if i > max then b
+                     else loop (i +? 1, f (i -? min, S.subUnsafe (seq, i), b))
                in loop (min, b)
                end
-            fun foldri f b (T {seq, start, len}) =
+            fun foldli f b sl = foldli' (wrap3 f) b sl
+            fun foldri' f b (T {seq, start, len}) =
                let
                   val min = start
+                  val len = len -? 1
                   val max = start +? len
                   fun loop (i, b) =
                      if i < min then b
-                     else loop (i -? 1, f (i -? min, S.sub (seq, i), b))
-               in loop (max -? 1, b)
+                     else loop (i -? 1, f (i -? min, S.subUnsafe (seq, i), b))
+               in loop (max, b)
                end
+            fun foldri f b sl = foldri' (wrap3 f) b sl
             local
                fun make foldi f b sl = foldi (fn (_, x, b) => f (x, b)) b sl
             in
-               fun foldl f = make foldli f
-               fun foldr f = make foldri f
+               fun foldl f = make foldli' f
+               fun foldr f = make foldri' f
             end
-            fun appi f sl = foldli (fn (i, x, ()) => f (i, x)) () sl
+            fun appi' f sl = foldli' (fn (i, x, ()) => f (i, x)) () sl
+            fun appi f sl = appi' (wrap2 f) sl
             fun app f sl = appi (f o #2) sl
-            fun createi tabulate f (T {seq, start, len}) =
-               tabulate (len, fn i => f (i, S.sub (seq, start +? i)))
-            fun create tabulate f sl = createi tabulate (f o #2) sl
-            fun mapi f sl = createi tabulate f sl
+            fun createi' tabulate' f (T {seq, start, len}) =
+               tabulate' (len, fn i => f (i, S.subUnsafe (seq, start +? i)))
+            fun createi tabulate' f sl = createi' tabulate' (wrap2 f) sl
+            fun create tabulate' f sl = createi tabulate' (f o #2) sl
+            fun mapi' f sl = createi' tabulate' f sl
+            fun mapi f sl = mapi' (wrap2 f) sl
             fun map f sl = mapi (f o #2) sl
-            fun findi p (T {seq, start, len}) = 
+            fun findi' p (T {seq, start, len}) = 
                let
                   val min = start
+                  val len = len -? 1
                   val max = start +? len
                   fun loop i =
-                     if i >= max
+                     if i > max
                         then NONE
-                     else let val z = (i -? min, S.sub (seq, i))
+                     else let val z = (i -? min, S.subUnsafe (seq, i))
                           in if p z
                                 then SOME z
                              else loop (i +? 1)
                           end
                in loop min
                end
+            fun findi p sl = Option.map (wrap2 (fn z => z)) (findi' (wrap2 p) sl)
             fun find p sl = Option.map #2 (findi (p o #2) sl)
-            fun existsi p sl = Option.isSome (findi p sl)
+            fun existsi' p sl = Option.isSome (findi' p sl)
+            fun existsi p sl = existsi' (wrap2 p) sl
             fun exists p sl = existsi (p o #2) sl
-            fun alli p sl = not (existsi (not o p) sl)
+            fun alli' p sl = not (existsi' (not o p) sl)
+            fun alli p sl = alli' (wrap2 p) sl
             fun all p sl = alli (p o #2) sl
             fun collate cmp (T {seq = seq1, start = start1, len = len1},
                              T {seq = seq2, start = start2, len = len2}) =
@@ -212,31 +303,34 @@
                       | (true, false) => LESS
                       | (false, true) => GREATER
                       | (false, false) => 
-                           (case cmp (S.sub (seq1, i), S.sub (seq2, j)) of
+                           (case cmp (S.subUnsafe (seq1, i), 
+                                      S.subUnsafe (seq2, j)) of
                               EQUAL => loop (i +? 1, j +? 1)
                             | ans => ans)
                in loop (min1, min2)
                end
             fun sequence (sl as T {seq, start, len}): 'a sequence =
-               if isMutable orelse (start <> 0 orelse len <> S.length seq)
+               if S.isMutable orelse (start <> 0 orelse len <> S.length seq)
                   then map (fn x => x) sl
                else seq
             fun append (sl1: 'a slice, sl2: 'a slice): 'a sequence =
-               if length sl1 = 0
+               if length' sl1 = 0
                   then sequence sl2
-               else if length sl2 = 0
+               else if length' sl2 = 0
                   then sequence sl1
                else
                   let
-                     val l1 = length sl1
-                     val l2 = length sl2
-                     val n = l1 + l2 handle Overflow => raise Size
+                     val l1 = length' sl1
+                     val l2 = length' sl2
+                     val n = (l1 + l2) handle Overflow => raise Size
                   in
-                     unfoldi (n, (0, sl1),
+                     unfoldi' (n, (0, sl1),
                               fn (_, (i, sl)) =>
-                                  if i < length sl
-                                     then (unsafeSub (sl, i), (i +? 1, sl))
-                                  else (unsafeSub (sl2, 0), (1, sl2)))
+                                  if SeqIndex.< (i, length' sl)
+                                     then (unsafeSub' (sl, i), 
+                                           (i +? 1, sl))
+                                  else (unsafeSub' (sl2, 0), 
+                                        (1, sl2)))
                   end
             fun concat (sls: 'a slice list): 'a sequence =
                case sls of
@@ -244,17 +338,19 @@
                 | [sl] => sequence sl
                 | sls' as sl::sls =>
                      let
-                        val n = List.foldl (fn (sl, s) => s + length sl) 0 sls'
-                                handle Overflow => raise Size
+                        val n = 
+                           (List.foldl (fn (sl, s) => s +? length' sl) 0 sls')
+                           handle Overflow => raise Size
                      in
-                        unfoldi (n, (0, sl, sls),
-                                 fn (_, ac) =>
-                                 let
-                                    fun loop (i, sl, sls) =
-                                       if i < length sl
-                                          then (unsafeSub (sl, i), (i +? 1, sl, sls))
+                        unfoldi' (n, (0, sl, sls),
+                                  fn (_, ac) =>
+                                  let
+                                     fun loop (i, sl, sls) =
+                                        if SeqIndex.< (i, length' sl)
+                                          then (unsafeSub' (sl, i), 
+                                                (i +? 1, sl, sls))
                                        else case sls of
-                                               [] => raise Fail "concat bug"
+                                               [] => raise Fail "Sequence.Slice.concat"
                                              | sl :: sls => loop (0, sl, sls)
                                  in loop ac
                                  end)
@@ -270,26 +366,41 @@
                                   (sequence sl) sls
                end
             fun triml k =
-               if Primitive.safe andalso k < 0
+               if Primitive.Controls.safe andalso Int.< (k, 0)
                   then raise Subscript
                else
                   (fn (T {seq, start, len}) =>
-                   if k > len
-                      then unsafeSlice (seq, start +? len, SOME 0)
-                   else unsafeSlice (seq, start +? k, SOME (len -? k)))
+                   let
+                      val k = 
+                         if Primitive.Controls.safe
+                            then SeqIndex.fromInt k
+                            else SeqIndex.fromIntUnsafe k
+                   in
+                      if SeqIndex.> (k, len)
+                         then unsafeSlice' (seq, start +? len, SOME 0)
+                         else unsafeSlice' (seq, start +? k, SOME (len -? k))
+                   end handle Overflow => unsafeSlice' (seq, start +? len, SOME 0))
             fun trimr k =
-               if Primitive.safe andalso k < 0
+               if Primitive.Controls.safe andalso Int.< (k, 0)
                   then raise Subscript
                else 
                   (fn (T {seq, start, len}) =>
-                   unsafeSlice (seq, start,
-                                SOME (if k > len then 0 else len -? k)))
+                   let
+                      val k =
+                         if Primitive.Controls.safe
+                            then SeqIndex.fromInt k
+                            else SeqIndex.fromIntUnsafe k
+                   in
+                      if SeqIndex.> (k, len)
+                         then unsafeSlice' (seq, start, SOME 0)
+                         else unsafeSlice' (seq, start, SOME (len -? k))
+                   end handle Overflow => unsafeSlice' (seq, start, SOME 0))
             fun isSubsequence (eq: 'a elt * 'a elt -> bool)
                               (seq: 'a sequence)
                               (sl: 'a slice) =
                let
                   val n = S.length seq
-                  val n' = length sl
+                  val n' = length' sl
                in
                   if n <= n'
                      then let
@@ -299,7 +410,8 @@
                                    then false
                                 else if j >= n
                                    then true
-                                else if eq (S.sub (seq, j), unsafeSub (sl, i +? j))
+                                else if eq (S.subUnsafe (seq, j), 
+                                            unsafeSub' (sl, i +? j))
                                    then loop (i, j +? 1)
                                 else loop (i +? 1, 0)
                           in
@@ -312,14 +424,15 @@
                          (sl: 'a slice) =
                let
                   val n = S.length seq
-                  val n' = length sl
+                  val n' = length' sl
                in
                   if n <= n'
                      then let
                              fun loop (j) =
                                 if j >= n
                                    then true
-                                else if eq (S.sub (seq, j), unsafeSub (sl, j))
+                                else if eq (S.subUnsafe (seq, j), 
+                                            unsafeSub' (sl, j))
                                    then loop (j +? 1)
                                 else false
                           in
@@ -332,7 +445,7 @@
                          (sl: 'a slice) =
                let
                   val n = S.length seq
-                  val n' = length sl
+                  val n' = length' sl
                in
                   if n <= n'
                      then let
@@ -340,7 +453,8 @@
                              fun loop (j) =
                                 if j >= n
                                    then true
-                                else if eq (S.sub (seq, j), unsafeSub (sl, n'' +? j))
+                                else if eq (S.subUnsafe (seq, j), 
+                                            unsafeSub' (sl, n'' +? j))
                                    then loop (j +? 1)
                                 else false
                           in
@@ -348,35 +462,40 @@
                           end
                   else false
                end
-            fun split (T {seq, start, len}, i) =
-               (unsafeSlice (seq, start, SOME (i -? start)),
-                unsafeSlice (seq, i, SOME (len -? (i -? start))))
+            fun split' (T {seq, start, len}, i) =
+               (unsafeSlice' (seq, start, SOME (i -? start)),
+                unsafeSlice' (seq, i, SOME (len -? (i -? start))))
             fun splitl f (sl as T {seq, start, len}) =
                let
                   val stop = start +? len
                   fun loop i =
                      if i >= stop
                         then i
-                     else if f (S.sub (seq, i))
+                     else if f (S.subUnsafe (seq, i))
                              then loop (i +? 1)
                           else i
-               in split (sl, loop start)
+               in split' (sl, loop start)
                end
             fun splitr f (sl as T {seq, start, len}) =
                let
                   fun loop i =
                      if i < start
                         then start
-                     else if f (S.sub (seq, i))
+                     else if f (S.subUnsafe (seq, i))
                              then loop (i -? 1)
                           else i +? 1
-               in split (sl, loop (start +? len -? 1))
+               in split' (sl, loop (start +? len -? 1))
                end
-            fun splitAt (T {seq, start, len}, i) =
-               if Primitive.safe andalso Int.gtu (i, len)
+            fun splitAt' (T {seq, start, len}, i) =
+               if Primitive.Controls.safe andalso SeqIndex.gtu (i, len)
                   then raise Subscript
-               else (unsafeSlice (seq, start, SOME i),
-                     unsafeSlice (seq, start +? i, SOME (len -? i)))
+               else (unsafeSlice' (seq, start, SOME i),
+                     unsafeSlice' (seq, start +? i, SOME (len -? i)))
+            fun splitAt (sl, i) =
+               if Primitive.Controls.safe
+                  then (splitAt' (sl, SeqIndex.fromInt i))
+                       handle Overflow => raise Subscript
+                  else splitAt' (sl, SeqIndex.fromIntUnsafe i)
             fun dropl p s = #2 (splitl p s)
             fun dropr p s = #1 (splitr p s)
             fun takel p s = #1 (splitl p s)
@@ -395,21 +514,21 @@
                              fun loop' j =
                                 if j >= len'
                                    then i
-                                else if eq (S.sub (seq, i +? j), 
-                                            S.sub (seq', j))
+                                else if eq (S.subUnsafe (seq, i +? j), 
+                                            S.subUnsafe (seq', j))
                                         then loop' (j +? 1)
                                      else loop (i +? 1)
                           in loop' 0
                           end
-               in split (sl, loop start)
+               in split' (sl, loop start)
                end
             fun span (eq: 'a sequence * 'a sequence -> bool)
                      (T {seq, start, ...},
                       T {seq = seq', start = start', len = len'}) =
-               if Primitive.safe andalso 
+               if Primitive.Controls.safe andalso 
                   (not (eq (seq, seq')) orelse start' +? len' < start)
                   then raise Span
-               else unsafeSlice (seq, start, SOME ((start' +? len') -? start))
+               else unsafeSlice' (seq, start, SOME ((start' +? len') -? start))
             fun translate f (sl: 'a slice) =
                concat (List.rev (foldl (fn (c, l) => (full (f c)) :: l) [] sl))
             local
@@ -420,7 +539,7 @@
                         if i >= max
                            then List.rev (finish (seq, start, i, sls))
                         else
-                           if p (S.sub (seq, i))
+                           if p (S.subUnsafe (seq, i))
                               then loop (i +? 1, i +? 1, finish (seq, start, i, sls))
                            else loop (i +? 1, start, sls)
                   in loop (start, start, []) 
@@ -431,12 +550,12 @@
                         if start = stop
                            then sls
                         else
-                           (unsafeSlice (seq, start, SOME (stop -? start)))
+                           (unsafeSlice' (seq, start, SOME (stop -? start)))
                            :: sls)
                        p sl
                fun fields p sl = 
                   make (fn (seq, start, stop, sls) =>
-                        (unsafeSlice (seq, start, SOME (stop -? start)))
+                        (unsafeSlice' (seq, start, SOME (stop -? start)))
                         :: sls)
                        p sl
             end
@@ -448,23 +567,38 @@
         fun make2 f (seq1, seq2) = f (Slice.full seq1, Slice.full seq2)
       in
         fun sub (seq, i) = Slice.sub (Slice.full seq, i)
+        fun sub' (seq, i) = Slice.sub' (Slice.full seq, i)
         fun unsafeSub (seq, i) = Slice.unsafeSub (Slice.full seq, i)
-        fun update' update (seq, i, x) = 
-           Slice.update' update (Slice.full seq, i, x)
+        fun unsafeSub' (seq, i) = Slice.unsafeSub' (Slice.full seq, i)
+        fun updateMk updateUnsafe (seq, i, x) = 
+           Slice.updateMk updateUnsafe (Slice.full seq, i, x)
+        fun updateMk' updateUnsafe (seq, i, x) = 
+           Slice.updateMk' updateUnsafe (Slice.full seq, i, x)
+        fun unsafeUpdateMk updateUnsafe (seq, i, x) = 
+           Slice.unsafeUpdateMk updateUnsafe (Slice.full seq, i, x)
+        fun unsafeUpdateMk' updateUnsafe (seq, i, x) = 
+           Slice.unsafeUpdateMk' updateUnsafe (Slice.full seq, i, x)
         fun append seqs = make2 Slice.append seqs
         fun concat seqs = Slice.concat (List.map Slice.full seqs)
+        fun appi' f = make (Slice.appi' f)
         fun appi f = make (Slice.appi f)
         fun app f = make (Slice.app f)
+        fun mapi' f = make (Slice.mapi' f)
         fun mapi f = make (Slice.mapi f)
         fun map f = make (Slice.map f)
+        fun foldli' f b = make (Slice.foldli' f b)
         fun foldli f b = make (Slice.foldli f b)
+        fun foldl f b = make (Slice.foldl f b)
+        fun foldri' f b = make (Slice.foldri' f b)
         fun foldri f b = make (Slice.foldri f b)
-        fun foldl f b = make (Slice.foldl f b)
         fun foldr f b = make (Slice.foldr f b)
+        fun findi' p = make (Slice.findi' p)
         fun findi p = make (Slice.findi p)
         fun find p = make (Slice.find p)
+        fun existsi' p = make (Slice.existsi' p)
         fun existsi p = make (Slice.existsi p)
         fun exists p = make (Slice.exists p)
+        fun alli' p = make (Slice.alli' p)
         fun alli p = make (Slice.alli p)
         fun all p = make (Slice.all p)
         fun collate cmp = make2 (Slice.collate cmp)
@@ -475,23 +609,10 @@
         fun translate f = make (Slice.translate f)
         fun tokens f seq = List.map Slice.sequence (make (Slice.tokens f) seq)
         fun fields f seq = List.map Slice.sequence (make (Slice.fields f) seq)
-        fun createi tabulate f seq = make (Slice.createi tabulate f) seq
-        fun create tabulate f seq = make (Slice.create tabulate f) seq
+        fun createi' tabulate' f seq = make (Slice.createi' tabulate' f) seq
+        fun createi tabulate' f seq = make (Slice.createi tabulate' f) seq
+        fun create tabulate' f seq = make (Slice.create tabulate' f) seq
         fun duplicate seq = make Slice.sequence seq
         fun toList seq = make Slice.toList seq
       end
-    
-      (* Deprecated *)
-      fun checkSliceMax (start: int, num: int option, max: int): int =
-         case num of
-            NONE => if Primitive.safe andalso (start < 0 orelse start > max)
-                       then raise Subscript
-                    else max
-          | SOME num =>
-               if Primitive.safe
-                  andalso (start < 0 orelse num < 0 orelse start > max -? num)
-                  then raise Subscript
-               else start +? num
-      (* Deprecated *)
-      fun checkSlice (s, i, opt) = checkSliceMax (i, opt, length s)
    end

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/sequence.sig
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/sequence.sig	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/sequence.sig	2006-02-12 18:36:38 UTC (rev 4353)
@@ -16,28 +16,45 @@
 
       val maxLen: int
       val fromList: 'a elt list -> 'a sequence
+      val tabulate': SeqIndex.int * (SeqIndex.int -> 'a elt) -> 'a sequence
       val tabulate: int * (int -> 'a elt) -> 'a sequence
+      val length': 'a sequence -> SeqIndex.int
       val length: 'a sequence -> int
+      val sub': 'a sequence * SeqIndex.int -> 'a elt
       val sub: 'a sequence * int -> 'a elt
+      val unsafeSub': 'a sequence * SeqIndex.int -> 'a elt
       val unsafeSub: 'a sequence * int -> 'a elt
-      (* update':
-       * ('a sequence * int * 'a elt -> unit)  should be an unsafe update. 
+      (* updateMk',updateMk,unsafeUpdateMk',unsafeUpdateMk:
+       * ('a sequence * SeqIndex.int * 'a elt -> unit)  should be an unsafe update. 
        *)
-      val update': ('a sequence * int * 'a elt -> unit) ->
-                   ('a sequence * int * 'a elt) -> unit
+      val updateMk': ('a sequence * SeqIndex.int * 'a elt -> unit) ->
+                     ('a sequence * SeqIndex.int * 'a elt) -> unit
+      val updateMk: ('a sequence * SeqIndex.int * 'a elt -> unit) ->
+                    ('a sequence * int * 'a elt) -> unit
+      val unsafeUpdateMk': ('a sequence * SeqIndex.int * 'a elt -> unit) ->
+                           ('a sequence * SeqIndex.int * 'a elt) -> unit
+      val unsafeUpdateMk: ('a sequence * SeqIndex.int * 'a elt -> unit) ->
+                          ('a sequence * int * 'a elt) -> unit
       val concat: 'a sequence list -> 'a sequence 
+      val appi': (SeqIndex.int * 'a elt -> unit) -> 'a sequence -> unit 
       val appi: (int * 'a elt -> unit) -> 'a sequence -> unit 
       val app: ('a elt -> unit) -> 'a sequence -> unit 
+      val mapi' : (SeqIndex.int * 'a elt -> 'b elt) -> 'a sequence -> 'b sequence 
       val mapi : (int * 'a elt -> 'b elt) -> 'a sequence -> 'b sequence 
       val map: ('a elt -> 'b elt) -> 'a sequence -> 'b sequence 
+      val foldli': (SeqIndex.int * 'a elt * 'b -> 'b) -> 'b -> 'a sequence -> 'b 
       val foldli: (int * 'a elt * 'b -> 'b) -> 'b -> 'a sequence -> 'b 
+      val foldl: ('a elt * 'b -> 'b) -> 'b -> 'a sequence -> 'b 
+      val foldri': (SeqIndex.int * 'a elt * 'b -> 'b) -> 'b -> 'a sequence -> 'b 
       val foldri: (int * 'a elt * 'b -> 'b) -> 'b -> 'a sequence -> 'b 
-      val foldl: ('a elt * 'b -> 'b) -> 'b -> 'a sequence -> 'b 
       val foldr: ('a elt * 'b -> 'b) -> 'b -> 'a sequence -> 'b
+      val findi': (SeqIndex.int * 'a elt -> bool) -> 'a sequence -> (SeqIndex.int * 'a elt) option
       val findi: (int * 'a elt -> bool) -> 'a sequence -> (int * 'a elt) option
       val find: ('a elt -> bool) -> 'a sequence -> 'a elt option
+      val existsi': (SeqIndex.int * 'a elt -> bool) -> 'a sequence -> bool
       val existsi: (int * 'a elt -> bool) -> 'a sequence -> bool
       val exists: ('a elt -> bool) -> 'a sequence -> bool
+      val alli': (SeqIndex.int * 'a elt -> bool) -> 'a sequence -> bool
       val alli: (int * 'a elt -> bool) -> 'a sequence -> bool
       val all: ('a elt -> bool) -> 'a sequence -> bool
       val collate: ('a elt * 'a elt -> order) -> 'a sequence * 'a sequence -> order
@@ -53,20 +70,19 @@
 
       (* Extra *)
       val append: 'a sequence * 'a sequence -> 'a sequence
-      (* createi,create:
-       * (int * (int -> 'b elt) -> 'c)  should be a tabulate function.
+      (* createi',createi,create:
+       * (SeqIndex.int * (SeqIndex.int -> 'b elt) -> 'c)  should be a tabulate' function.
        *)
-      val createi: (int * (int -> 'b elt) -> 'c) ->
+      val createi': (SeqIndex.int * (SeqIndex.int -> 'b elt) -> 'c) ->
+                    (SeqIndex.int * 'a elt -> 'b elt) -> 'a sequence -> 'c
+      val createi: (SeqIndex.int * (SeqIndex.int -> 'b elt) -> 'c) ->
                    (int * 'a elt -> 'b elt) -> 'a sequence -> 'c
-      val create: (int * (int -> 'b elt) -> 'c) ->
+      val create: (SeqIndex.int * (SeqIndex.int -> 'b elt) -> 'c) ->
                   ('a elt -> 'b elt) -> 'a sequence -> 'c
       val duplicate: 'a sequence -> 'a sequence
       val new: int * 'a elt -> 'a sequence
       val toList: 'a sequence -> 'a elt list
+      val unfoldi': SeqIndex.int * 'a * (SeqIndex.int * 'a -> 'b elt * 'a) -> 'b sequence
       val unfoldi: int * 'a * (int * 'a -> 'b elt * 'a) -> 'b sequence
-
-      (* Deprecated *)
-      val checkSlice: 'a sequence * int * int option -> int
-      (* Deprecated *)
-      val checkSliceMax: int * int option * int -> int
+      val unfold: int * 'a * ('a -> 'b elt * 'a) -> 'b sequence
    end

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/slice.sig
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/slice.sig	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/slice.sig	2006-02-12 18:36:38 UTC (rev 4353)
@@ -11,37 +11,56 @@
       type 'a sequence
       type 'a elt
       type 'a slice
+      val length': 'a slice -> SeqIndex.int
       val length: 'a slice -> int
+      val sub': 'a slice * SeqIndex.int -> 'a elt
       val sub: 'a slice * int -> 'a elt
+      val unsafeSub': 'a slice * SeqIndex.int -> 'a elt
       val unsafeSub: 'a slice * int -> 'a elt
-      (* update',unsafeUpdate':
-       * ('a sequence * int * 'a elt -> unit)  should be an unsafe update. 
+      (* updateMk',updateMk,unsafeUpdateMk',unsafeUpdateMk:
+       * ('a sequence * SeqIndex.int * 'a elt -> unit)  should be an unsafe update. 
        *)
-      val update': ('a sequence * int * 'a elt -> unit) ->
-                   ('a slice * int * 'a elt) -> unit
-      val unsafeUpdate': ('a sequence * int * 'a elt -> unit) ->
-                         ('a slice * int * 'a elt) -> unit
+      val updateMk': ('a sequence * SeqIndex.int * 'a elt -> unit) ->
+                     ('a slice * SeqIndex.int * 'a elt) -> unit
+      val updateMk: ('a sequence * SeqIndex.int * 'a elt -> unit) ->
+                    ('a slice * int * 'a elt) -> unit
+      val unsafeUpdateMk': ('a sequence * SeqIndex.int * 'a elt -> unit) ->
+                           ('a slice * SeqIndex.int * 'a elt) -> unit
+      val unsafeUpdateMk: ('a sequence * SeqIndex.int * 'a elt -> unit) ->
+                          ('a slice * int * 'a elt) -> unit
       val full: 'a sequence -> 'a slice
+      val slice': 'a sequence * SeqIndex.int * SeqIndex.int option -> 'a slice
       val slice: 'a sequence * int * int option -> 'a slice
+      val unsafeSlice': 'a sequence * SeqIndex.int * SeqIndex.int option -> 'a slice
       val unsafeSlice: 'a sequence * int * int option -> 'a slice
+      val subslice': 'a slice * SeqIndex.int * SeqIndex.int option -> 'a slice
       val subslice: 'a slice * int * int option -> 'a slice
+      val unsafeSubslice': 'a slice * SeqIndex.int * SeqIndex.int option -> 'a slice
       val unsafeSubslice: 'a slice * int * int option -> 'a slice
+      val base': 'a slice -> 'a sequence * SeqIndex.int * SeqIndex.int
       val base: 'a slice -> 'a sequence * int * int
       val concat: 'a slice list -> 'a sequence
       val isEmpty: 'a slice -> bool
       val getItem: 'a slice -> ('a elt * 'a slice) option
+      val appi': (SeqIndex.int * 'a elt -> unit) -> 'a slice -> unit
       val appi: (int * 'a elt -> unit) -> 'a slice -> unit
       val app: ('a elt -> unit) -> 'a slice -> unit
+      val mapi': (SeqIndex.int * 'a elt -> 'b elt) -> 'a slice -> 'b sequence
       val mapi: (int * 'a elt -> 'b elt) -> 'a slice -> 'b sequence
       val map: ('a elt -> 'b elt) -> 'a slice -> 'b sequence
+      val foldli': (SeqIndex.int * 'a elt * 'b -> 'b) -> 'b -> 'a slice -> 'b
       val foldli: (int * 'a elt * 'b -> 'b) -> 'b -> 'a slice -> 'b
+      val foldl: ('a elt * 'b -> 'b) -> 'b -> 'a slice -> 'b
+      val foldri': (SeqIndex.int * 'a elt * 'b -> 'b) -> 'b -> 'a slice -> 'b
       val foldri: (int * 'a elt * 'b -> 'b) -> 'b -> 'a slice -> 'b
-      val foldl: ('a elt * 'b -> 'b) -> 'b -> 'a slice -> 'b
       val foldr: ('a elt * 'b -> 'b) -> 'b -> 'a slice -> 'b
+      val findi': (SeqIndex.int * 'a elt -> bool) -> 'a slice -> (SeqIndex.int * 'a elt) option
       val findi: (int * 'a elt -> bool) -> 'a slice -> (int * 'a elt) option
       val find: ('a elt -> bool) -> 'a slice -> 'a elt option
+      val existsi': (SeqIndex.int * 'a elt -> bool) -> 'a slice -> bool
       val existsi: (int * 'a elt -> bool) -> 'a slice -> bool
       val exists: ('a elt -> bool) -> 'a slice -> bool
+      val alli': (SeqIndex.int * 'a elt -> bool) -> 'a slice -> bool
       val alli: (int * 'a elt -> bool) -> 'a slice -> bool
       val all: ('a elt -> bool) -> 'a slice -> bool
       val collate: ('a elt * 'a elt -> order) -> 'a slice * 'a slice -> order
@@ -55,6 +74,7 @@
       val isSuffix: ('a elt * 'a elt -> bool) -> 'a sequence -> 'a slice -> bool
       val splitl: ('a elt -> bool) -> 'a slice -> 'a slice * 'a slice
       val splitr: ('a elt -> bool) -> 'a slice -> 'a slice * 'a slice
+      val splitAt': 'a slice * SeqIndex.int -> 'a slice * 'a slice
       val splitAt: 'a slice * int -> 'a slice * 'a slice
       val dropl: ('a elt -> bool) -> 'a slice -> 'a slice
       val dropr: ('a elt -> bool) -> 'a slice -> 'a slice
@@ -72,12 +92,14 @@
 
       (* Extra *)
       val append: 'a slice * 'a slice -> 'a sequence
-      (* createi,create:
-       * (int * (int -> 'b elt) -> 'c)  should be a tabulate function.
+      (* createi',createi,create:
+       * (SeqIndex.int * (SeqIndex.int -> 'b elt) -> 'c)  should be a tabulate' function.
        *)
-      val createi: (int * (int -> 'b elt) -> 'c) ->
+      val createi': (SeqIndex.int * (SeqIndex.int -> 'b elt) -> 'c) ->
+                    (SeqIndex.int * 'a elt -> 'b elt) -> 'a slice -> 'c
+      val createi: (SeqIndex.int * (SeqIndex.int -> 'b elt) -> 'c) ->
                    (int * 'a elt -> 'b elt) -> 'a slice -> 'c
-      val create: (int * (int -> 'b elt) -> 'c) ->
+      val create: (SeqIndex.int * (SeqIndex.int -> 'b elt) -> 'c) ->
                   ('a elt -> 'b elt) -> 'a slice -> 'c
       val toList: 'a slice -> 'a elt list
       val sequence: 'a slice -> 'a sequence

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector-slice.sig
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector-slice.sig	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector-slice.sig	2006-02-12 18:36:38 UTC (rev 4353)
@@ -5,7 +5,6 @@
 
 signature VECTOR_SLICE_GLOBAL =
    sig
-
    end
 
 signature VECTOR_SLICE =
@@ -43,8 +42,11 @@
    sig
       include VECTOR_SLICE
 
+      val unsafeSub': 'a slice * SeqIndex.int -> 'a
       val unsafeSub: 'a slice * int -> 'a
+      val unsafeSlice': 'a Vector.vector * SeqIndex.int * SeqIndex.int option -> 'a slice
       val unsafeSlice: 'a Vector.vector * int * int option -> 'a slice
+      val unsafeSubslice': 'a slice * SeqIndex.int * SeqIndex.int option -> 'a slice
       val unsafeSubslice: 'a slice * int * int option -> 'a slice
 
       (* Used to implement Substring/String functions *)

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector.sig
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector.sig	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector.sig	2006-02-12 18:36:38 UTC (rev 4353)
@@ -34,6 +34,7 @@
       include VECTOR
       structure VectorSlice: VECTOR_SLICE_EXTRA 
 
+      val fromArray: 'a array -> 'a vector
       val unsafeSub: 'a vector * int -> 'a
 
       (* Used to implement Substring/String functions *)
@@ -47,11 +48,8 @@
 
       val append: 'a vector * 'a vector -> 'a vector
       val duplicate: 'a vector -> 'a vector
-      val fromArray: 'a array -> 'a vector
+      val tabulate': SeqIndex.int * (SeqIndex.int -> 'a) -> 'a vector 
       val toList: 'a vector -> 'a list
       val unfoldi: int * 'a * (int * 'a -> 'b * 'a) -> 'b vector
       val vector: int * 'a -> 'a vector
-
-      (* Deprecated *)
-      val checkSlice: 'a vector * int * int option -> int
    end

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector.sml
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector.sml	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/arrays-and-vectors/vector.sml	2006-02-12 18:36:38 UTC (rev 4353)
@@ -13,7 +13,7 @@
                               val fromArray = Primitive.Vector.fromArray
                               val isMutable = false
                               val length = Primitive.Vector.length
-                              val sub = Primitive.Vector.sub)
+                              val subUnsafe = Primitive.Vector.subUnsafe)
       open V
 
       type 'a vector = 'a vector
@@ -30,13 +30,31 @@
          end
 
       fun update (v, i, x) = 
-        tabulate (length v,
-                  fn j => if i = j 
-                             then x
-                          else unsafeSub (v, j))
+         let
+            fun doit i =
+               tabulate' (length' v,
+                          fn j => if i = j 
+                                     then x 
+                                     else unsafeSub' (v, j))
+         in 
+            if Primitive.Controls.safe
+               then
+                  let
+                     val i = 
+                        (SeqIndex.fromInt i)
+                        handle Overflow => raise Subscript
+                  in
+                     if SeqIndex.geu (i, length' v)
+                        then raise Subscript
+                        else doit i
+                  end 
+               else let
+                       val i = SeqIndex.fromIntUnsafe i
+                    in 
+                       doit i
+                    end
+         end
 
-      val unsafeSub = Primitive.Vector.sub
-
       val isSubvector = isSubsequence
 
       val fromArray = Primitive.Vector.fromArray

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/build/sources.mlb
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/build/sources.mlb	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/build/sources.mlb	2006-02-12 18:36:38 UTC (rev 4353)
@@ -62,27 +62,28 @@
    ../list/list-pair.sml
    ../arrays-and-vectors/slice.sig
    ../arrays-and-vectors/sequence.sig
+   ../arrays-and-vectors/seq-index1.sml
+   ../arrays-and-vectors/sequence.fun
+   ../arrays-and-vectors/vector-slice.sig
+   ../arrays-and-vectors/vector.sig
+   ../arrays-and-vectors/vector.sml
+   ../arrays-and-vectors/array-slice.sig
+   ../arrays-and-vectors/array.sig
+   ../arrays-and-vectors/array.sml
+   ../arrays-and-vectors/array2.sig
 (*
-      ../arrays-and-vectors/sequence.fun
+   ../arrays-and-vectors/array2.sml
 *)
-      ../arrays-and-vectors/vector-slice.sig
-      ../arrays-and-vectors/vector.sig
+   ../arrays-and-vectors/mono-vector-slice.sig
+   ../arrays-and-vectors/mono-vector.sig
+   ../arrays-and-vectors/mono-vector.fun
+   ../arrays-and-vectors/mono-array-slice.sig
+   ../arrays-and-vectors/mono-array.sig
+   ../arrays-and-vectors/mono-array.fun
 (*
-      ../arrays-and-vectors/vector.sml
-      ../arrays-and-vectors/array-slice.sig
-      ../arrays-and-vectors/array.sig
-      ../arrays-and-vectors/array.sml
-      ../arrays-and-vectors/array2.sig
-      ../arrays-and-vectors/array2.sml
-      ../arrays-and-vectors/mono-vector-slice.sig
-      ../arrays-and-vectors/mono-vector.sig
-      ../arrays-and-vectors/mono-vector.fun
-      ../arrays-and-vectors/mono-array-slice.sig
-      ../arrays-and-vectors/mono-array.sig
-      ../arrays-and-vectors/mono-array.fun
-      ../arrays-and-vectors/mono-array2.sig
-      ../arrays-and-vectors/mono-array2.fun
-      ../arrays-and-vectors/mono.sml
+   ../arrays-and-vectors/mono-array2.sig
+   ../arrays-and-vectors/mono-array2.fun
+   ../arrays-and-vectors/mono.sml
       ../../text/string0.sml
       ../../text/char0.sml
       ../../misc/reader.sig

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/integer/int-inf0.sml
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/integer/int-inf0.sml	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/integer/int-inf0.sml	2006-02-12 18:36:38 UTC (rev 4353)
@@ -238,7 +238,7 @@
                                 end
                        val (n, acc) = 
                           loop (w, 1, [(0, if isneg then 0w1 else 0w0)])
-                       val a = A.array n
+                       val a = A.arrayUnsafe n
                        fun loop acc =
                           case acc of
                              [] => ()

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/primitive/prim-seq.sml
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/primitive/prim-seq.sml	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/primitive/prim-seq.sml	2006-02-12 18:36:38 UTC (rev 4353)
@@ -16,10 +16,6 @@
    struct
       open Array
       val arrayUnsafe = _prim "Array_array": SeqIndex.int -> 'a array;
-      fun array n =
-         if Controls.safe andalso SeqIndex.< (n, 0)
-            then raise Exn.Size
-            else arrayUnsafe n
       val array0Const = _prim "Array_array0Const": unit -> 'a array;
       val length = _prim "Array_length": 'a array -> SeqIndex.int;
       (* There is no maximum length on arrays, so maxLen' = SeqIndex.maxInt'. *)

Modified: mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/primitive/prim2.sml
===================================================================
--- mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/primitive/prim2.sml	2006-02-10 03:21:00 UTC (rev 4352)
+++ mlton/branches/on-20050822-x86_64-branch/basis-library.refactor/primitive/prim2.sml	2006-02-12 18:36:38 UTC (rev 4353)
@@ -48,7 +48,6 @@
 (* Install an emergency suffix. *)
 local
    structure P = Primitive
-   structure PFFI = PrimitiveFFI
    val _ =
       P.TopLevel.setSuffix
       (fn () =>