[Haskell] Fast unboxed references for the IO and ST monads

Bulat Ziganshin bulat.ziganshin at gmail.com
Sun Feb 19 05:43:23 EST 2006


Hello oleg,

Wednesday, February 08, 2006, 8:37:55 AM, you wrote:

>> I suggest you follow the same scheme as the unboxed array types, and
>> have IOURef/STURef types, parameterised over the element type.  Of
>> course, we should have instances for all of the primitive numeric types
>> plus Ptr, ForeignPtr, StablePtr, Bool.

opc> Perhaps it may be worth to introduce a class Unpackable as described
opc> at the end of
opc>   http://www.haskell.org/pipermail/haskell-cafe/2004-July/006400.html
opc> so we may define STUArray as an instance of MArray, and other similar
opc> unpackable things easier?

i just implemented this neat idea. this allowed to define all sorts of
unboxed arrays and references in just 300 lines of code!

for those who are not seen the discussion, it's a brief presentation of
implemented facilities:

1. Types IOURef/STURef represent fast alternative to the IORef/STRef
types. These "unboxed references" may be used only for simple
datatypes, including Bool, Char, Int..Int64, Word..Word64, Float,
Double, Ptr, FunPtr, StablePtr, but for these types they are guarantee
maximum speed! The unboxed references completely replaces the "Fast
Mutable Ints/Words/Bools" modules used before. To use unboxed
references instead of boxed ones, you just need to replace IORef/STRef
in type declarations to the IOURef/STURef and add the same "U" letter
to the names of operations that use these references:

main = do (i :: IORef Int) <- newIORef 1
          readIORef i >>= print
          writeIORef i 2

will become:

main = do (i :: IOURef Int) <- newIOURef 1
          readIOURef i >>= print
          writeIOURef i 2

The library includes compatibility code that allows to use
IOURef/STURef on other Haskell compilers. That allows to debug code
that use the unboxed references with Hugs/NHC, and use for the
final optimized compilation GHC/JHC.

2. Classes MRef and Var can be used to write monad-independent code
that use mutable variables. Class MRef represents monad-independent
boxed references, while class Var represents the unboxed ones. You
can find examples of their usage in the Streams library. These classes
uses functional dependencies, so they are available only in GHC/Hugs.

3. Module GHC.Unboxed also contains implementation of UnboxedArray,
UnboxedSTArray and UnboxedIOArray type constructors, what is a full
analogues of UArray, STUArray and IOUArray, only much easier to
implement. These definitions follow the scheme proposed by Oleg:
"instance (Unboxed e) => IArray UnboxedArray e" and so on.

4. You can define new unboxed types. For example, the code that
defines "instance Unboxed Bool" can be reused without any
modifications for any user-defined enumeration.

-- 
Best regards,
 Bulat                            mailto:bulatz at HotPOP.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Variables.hs
Type: application/octet-stream
Size: 7400 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell/attachments/20060219/adc51045/Variables-0001.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Unboxed.hs
Type: application/octet-stream
Size: 13349 bytes
Desc: not available
Url : http://www.haskell.org//pipermail/haskell/attachments/20060219/adc51045/Unboxed-0001.obj


More information about the Haskell mailing list