Implementing RefMonads in Haskell without ST,IO
Tim Sweeney
tim@epicgames.com
Thu, 29 May 2003 22:48:05 -0500
This is a multi-part message in MIME format.
------=_NextPart_000_0EDB_01C32634.5E29ABD0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Given the common definition of RefMonad:
class Monad m =3D> RefMonad m r | m -> r where
newRef :: a -> m (r a)
readRef :: r a -> m a
writeRef :: r a -> a -> m ()
Is it possible to actually implement a working instance of RefMonad in =
Haskell, without making use of a built-in monad like IO or ST? If so, =
I'd love any tips -- I've been making good use of monads for a while, =
but can't figure this one out.
The Java programmer in me wants to implement RefMonad by passing around =
a function from integers (think of them as pointers or heap indices) to =
"objects", and in readRef, "cast" the "object" to the appropriate type =
"t". =20
If it's not possible to implement a typesafe RefMonad instance directly =
in Haskell, without making use of built-in imperative features like IO, =
then doesn't this refute the claims that monads implement imperative =
features functionally?
-Tim
------=_NextPart_000_0EDB_01C32634.5E29ABD0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1106" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>Given the common definition of=20
RefMonad:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>class Monad m =3D> RefMonad m r | m =
-> r=20
where</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> newRef :: a -> m =
(r=20
a)</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> readRef :: r a -> =
m=20
a</FONT></DIV>
<DIV><FONT face=3DArial size=3D2> writeRef :: r a =
-> a -> m=20
()</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>Is it possible to actually implement a =
working=20
instance of RefMonad in Haskell, without making use of a built-in monad =
like IO=20
or ST? If so, I'd love any tips -- I've been making good use of =
monads for=20
a while, but can't figure this one out.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>The Java programmer in me wants to =
implement=20
RefMonad by passing around a function from integers (think of them as =
pointers=20
or heap indices) to "objects", and in readRef, "cast" the "object" to =
the=20
appropriate type "t". </FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>If it's not possible to implement a =
typesafe=20
RefMonad instance directly in Haskell, without making use of built-in =
imperative=20
features like IO, then doesn't this refute the claims that monads =
implement=20
imperative features functionally?</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT> </DIV>
<DIV><FONT face=3DArial size=3D2>-Tim</FONT></DIV></BODY></HTML>
------=_NextPart_000_0EDB_01C32634.5E29ABD0--