need help w/ monad comprehension syntax

Konst Sushenko konsu@microsoft.com
Thu, 15 Feb 2001 17:11:20 -0800


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C097B5.5F16060C
Content-Type: text/plain;
	charset="iso-8859-1"

hello,
 
i am having trouble getting my program below to work.
i think i implemented the monad methods correctly, but
the function 'g' does not type as i would expect. Hugs
thinks that it is just a list (if i remove the explicit
typing). i want it to be functionally identical to the
function 'h'.
 
what am i missing?
 
thanks
konst
 
 
> newtype State s a = ST (s -> (a,s))
>
> unST (ST m) = m
>
> instance Functor (State s) where
>     fmap f m = ST (\s -> let (a,s') = unST m s in (f a, s'))
>
> instance Monad (State s) where
>     return a = ST (\s -> (a,s))
>     m >>= f  = ST (\s -> let (a,s') = unST m s in unST (f a) s')
>
> --g :: State String Char
> g = [ x | x <- return 'a' ]
>
> h :: State String Char
> h = return 'a'
 

------_=_NextPart_001_01C097B5.5F16060C
Content-Type: text/html;
	charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.50.4208.1700" name=GENERATOR></HEAD>
<BODY>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2>hello,</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>i am having 
trouble&nbsp;getting my program below to work.</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>i think i 
implemented the monad methods correctly, but</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>the function 
'g' does not type as i would expect. Hugs</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>thinks that 
it is just a list (if i remove the explicit</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>typing). i 
want it to be functionally identical to the</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>function 
'h'.</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2></FONT></SPAN><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>what am i 
missing?</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2>thanks</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2>konst</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>&gt; newtype 
State s a = ST (s -&gt; (a,s))</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2><SPAN 
class=628090501-16022001>&gt;</SPAN></FONT></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>&gt; unST 
(ST m) = m</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2><SPAN 
class=628090501-16022001>&gt;</SPAN></FONT></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>&gt; 
instance Functor (State s) where<BR>&gt; &nbsp;&nbsp;&nbsp; fmap f m = ST (\s 
-&gt; let (a,s') = unST m s in (f a, s'))</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2><SPAN 
class=628090501-16022001>&gt;</SPAN></FONT></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>&gt; 
instance Monad (State s) where<BR>&gt; &nbsp;&nbsp;&nbsp; return a = ST (\s 
-&gt; (a,s))<BR>&gt; &nbsp;&nbsp;&nbsp; m &gt;&gt;= f&nbsp; = ST (\s -&gt; let 
(a,s') = unST m s in unST (f a) s')</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2><SPAN 
class=628090501-16022001>&gt;</SPAN></FONT></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>&gt; --g :: 
State String Char<BR>&gt; g = [ x | x &lt;- return 'a' ]</FONT></SPAN></DIV>
<DIV><FONT face="Courier New" size=2><SPAN 
class=628090501-16022001>&gt;</SPAN></FONT></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" size=2>&gt; h :: 
State String Char<BR>&gt; h = return 'a'</FONT></SPAN></DIV>
<DIV><SPAN class=628090501-16022001><FONT face="Courier New" 
size=2></FONT></SPAN>&nbsp;</DIV></BODY></HTML>

------_=_NextPart_001_01C097B5.5F16060C--