Lazy bind...
Martin Norbäck
d95mback@dtek.chalmers.se
05 Aug 2002 12:35:05 +0200
--=-3Q3Va/qpUoqD/k1D2Cod
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
fre 2002-08-02 klockan 14.57 skrev Lauri Alanko:
> On Tue, Jul 30, 2002 at 01:41:43PM +0100, MR K P SCHUPKE wrote:
> > Is there any way to do a lazy bind operation, something like
> >=20
> > a <- $ getLine
> > return (Constructor $ a)
> >=20
> > this works for computations in the IO monad :-
> >=20
> > a <- unsafeInterleaveIO getLine
> > return (Constructor $ a)
> >=20
> > but I need to do this for a general monad M.
>=20
> I don't think this is possible. In general, you have to write separate
> strict and lazy binding operations. For instance, there are both ST and
> LazyST for state transformer monads, and the latter's >>=3D operation is
> lazy.
>=20
> Here's a concrete example for the basic state-carrying monad. (Not tested=
)
>=20
> newtype State s a =3D State { unState :: s -> (a, s) }
> getState =3D State (\s -> (s, s))
> setState x =3D State (\s -> ((), x))
> -- Monad.return
> returnState a =3D State (\s -> (a, s))
> -- strict Monad.>>=3D
> State m `strictBindState` f=20
> =3D State (\s -> case m s of (a, s') -> unState (f a) s')
> -- lazy Monad.>>=3D
> State m `lazyBindState` f=20
> =3D State (\s -> case m s of ~(a, s) -> unState (f a) s')
>=20
> As you can see, the difference between lazy and strict binding is an
> inherent part of the definition of >>=3D, so there's no way to automatize
> it. This is kind of a shame, for I too sometimes need both lazy and
> strict versions of a monad transformer, but this requires two separate
> types with different monad instances...
Perhaps just some more syntactic sugar would be in order. Say something
like this:
infixl 1 >>=3D$
class Monad m =3D> LazyMonad m where
(>=3D=3D$) :: m a -> (a -> m b) -> m b
and then make
a <-$ m
use >=3D=3D$ instead of >>=3D.
The operator names are just meant as an example of course.
Regards,
Martin
--=20
Martin Norb=E4ck d95mback@dtek.chalmers.se =20
Kapplandsgatan 40 +46 (0)708 26 33 60 =20
S-414 78 G=D6TEBORG http://www.dtek.chalmers.se/~d95mback/
SWEDEN OpenPGP ID: 3FA8580B
--=-3Q3Va/qpUoqD/k1D2Cod
Content-Type: application/pgp-signature; name=signature.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: För information se http://www.gnupg.org/
iD8DBQA9TlTZkXyAGj+oWAsRAmQYAJ9s8Vzlu3Yw52oQtpCvSj8onQqpCwCfQviR
YOpi40t/X7c3xL9vQZKSvMw=
=szM9
-----END PGP SIGNATURE-----
--=-3Q3Va/qpUoqD/k1D2Cod--