# HG changeset patch # User Chris Cannam # Date 1638893984 0 # Tue Dec 07 16:19:44 2021 +0000 # Node ID a50862ffc9f0e8bb49d61aa41df9cb8a1862429a # Parent 655e71faf6ad2c46aa6369dead78f3dad3a00569 Use a datatype to make the types less interchangeable diff --git a/persistent-array-slice.sml b/persistent-array-slice.sml --- a/persistent-array-slice.sml +++ b/persistent-array-slice.sml @@ -75,30 +75,30 @@ fun isEmpty (S { array, start, count }) = count = 0 - fun array (s as S { array = { size, trie }, start, count }) = - { size = Word32.fromInt count, - trie = T.extractRange (trie, makeRange s) - } + fun array (s as S { array = A.A { size, trie }, start, count }) = + A.A { size = Word32.fromInt count, + trie = T.extractRange (trie, makeRange s) + } - fun foldl f acc (s as S { array = { size, trie }, start, count }) = + fun foldl f acc (s as S { array = A.A { size, trie }, start, count }) = T.foldlRange f acc (trie, makeRange s) - fun foldli f acc (s as S { array = { size, trie }, start, count }) = + fun foldli f acc (s as S { array = A.A { size, trie }, start, count }) = T.foldliRange (fn (w, x, acc) => f (Word32.toInt w, x, acc)) acc (trie, makeRange s) - fun foldr f acc (s as S { array = { size, trie }, start, count }) = + fun foldr f acc (s as S { array = A.A { size, trie }, start, count }) = T.foldrRange f acc (trie, makeRange s) - fun foldri f acc (s as S { array = { size, trie }, start, count }) = + fun foldri f acc (s as S { array = A.A { size, trie }, start, count }) = T.foldriRange (fn (w, x, acc) => f (Word32.toInt w, x, acc)) acc (trie, makeRange s) - fun app f (s as S { array = { size, trie }, start, count }) = + fun app f (s as S { array = A.A { size, trie }, start, count }) = T.foldliRange (fn (_, x, _) => f x) () (trie, makeRange s) - fun appi f (s as S { array = { size, trie }, start, count }) = + fun appi f (s as S { array = A.A { size, trie }, start, count }) = T.foldliRange (fn (w, x, _) => f (Word32.toInt w, x)) () (trie, makeRange s) diff --git a/persistent-array.sml b/persistent-array.sml --- a/persistent-array.sml +++ b/persistent-array.sml @@ -3,7 +3,7 @@ structure T = Word32TrieMap - type 'a array = { + datatype 'a array = A of { size : Word32.word, trie : 'a T.trie } @@ -23,69 +23,69 @@ val maxLen = Word32.toInt maxLenW - val empty : 'a array = { + val empty : 'a array = A { size = 0w0, trie = T.empty } - fun isEmpty ({ size, trie } : 'a array) = + fun isEmpty (A { size, trie } : 'a array) = size = 0w0 - fun length { size, trie } = + fun length (A { size, trie }) = Word32.toInt size - fun append ({ size, trie }, x) = + fun append (A { size, trie }, x) = let val _ = if size = maxLenW then raise Size else () val t = T.insert (trie, size, x) in - { size = size + 0w1, trie = t } + A { size = size + 0w1, trie = t } end - fun popEnd { size, trie } = + fun popEnd (A { size, trie }) = case T.find (trie, size - 0w1) of NONE => raise Size | SOME x => let val t = T.remove (trie, size - 0w1) in - ({ size = size - 0w1, trie = t }, x) + (A { size = size - 0w1, trie = t }, x) end - fun sub (v as { size, trie }, i) = + fun sub (v as A { size, trie }, i) = if i < 0 orelse i >= length v then raise Subscript else T.lookup (trie, Word32.fromInt i) - fun update (v as { size, trie }, i, x) = + fun update (v as A { size, trie }, i, x) = if i < 0 orelse i >= length v then raise Subscript else let val t = T.insert (trie, Word32.fromInt i, x) in - { size = size, trie = t } + A { size = size, trie = t } end - fun foldl f acc { size, trie } = + fun foldl f acc (A { size, trie }) = T.foldl f acc trie - fun foldli f acc { size, trie } = + fun foldli f acc (A { size, trie }) = T.foldli (fn (w, x, acc) => f (Word32.toInt w, x, acc)) acc trie - fun foldr f acc { size, trie } = + fun foldr f acc (A { size, trie }) = T.foldr f acc trie - fun foldri f acc { size, trie } = + fun foldri f acc (A { size, trie }) = T.foldri (fn (w, x, acc) => f (Word32.toInt w, x, acc)) acc trie - fun find f { size, trie } = + fun find f (A { size, trie }) = T.search f trie - fun findi f { size, trie } = + fun findi f (A { size, trie }) = Option.map (fn (w, x) => (Word32.toInt w, x)) (T.searchi (fn (w, x) => f (Word32.toInt w, x)) trie) - fun exists f { size, trie } = + fun exists f (A { size, trie }) = Option.isSome (T.search f trie) - fun all f { size, trie } = + fun all f (A { size, trie }) = not (Option.isSome (T.search (fn x => not (f x)) trie)) fun mapi f v =