There was a mistake in the trace, please ignore the previous one and look at this. Again your viewing window should be this wide:<br><font face="courier new,monospace"><-----------------------------------------------------------------><br>
(liftM2 (&&) (< 0.5) (> -0.5))<br>=> do {x1 <- (< 0.5);<br> x2 <- (> -0.5);<br> return ((&&) x1 x2)}<br><br>=> (< 0.5) >>= \x1<br> (> -0.5) >>= \x2<br>
return ((&&) x1 x2)<br><br>=> \r ->(\x1 -> <br> (> -0.5) >>= \x2<br> return ((&&) x1 x2))<br> ((< 0.5) r)<br> r<br><br>=> \r -> ((> -0.5) >>= \x2<br>
return ((&&) ((< 0.5) r) x2))<br> r<br><br>=> \r -> (\r' -> (\x2 -> <br> return ((&&) (const (< 0.5) r) x2))<br> ((> -0.5) r')<br>
r')<br> r <br>=> \r -> (\r' -> (return ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r<br>=> \r -> (\r' -> (const ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r<br>
=> \r -> (\r' -> ((&&) ((< 0.5) r) ((> -0.5) r'))) r<br>=> \r -> (\r' -> ((r < 0.5) && (r' > -0.5))) r<br><br></font><br><div class="gmail_quote">On Mon, Oct 19, 2009 at 11:42 AM, aditya siram <span dir="ltr"><<a href="mailto:aditya.siram@gmail.com">aditya.siram@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">This one had me puzzled too - so did a traced through the program below. Please make sure your viewing window is at least this wide:<br>
<font face="courier new,monospace"><----------------------------------------------------------------></font><br>
<br>It was more helpful to think of this as using the ((->) r) instance of Monad. It is defined in ./libraries/base/Control/Monad/Instances.hs in your GHC source as:<br><span style="font-family: courier new,monospace;">>instance Monad ((->) r) where</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">> return = const</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">> f >>= k = \ r -> k (f r) r</span><br style="font-family: courier new,monospace;">
<br>And const just returns its first argument like:<br>const 1 3 => 1<br>const "hello" "world" => "hello"<br><br>And liftM2 is defined in ./libraries/base/Control/Monad.hs as :<br><span style="font-family: courier new,monospace;">>liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">>liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }</span><br><br>So a trace of the program goes like this:<div class="im"><br> <br><span style="font-family: courier new,monospace;">(liftM2 (&&) (< 0.5) (> -0.5))</span><br style="font-family: courier new,monospace;">
</div><span style="font-family: courier new,monospace;">=> do {x1 <- (< 0.5);</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> x2 <- (> -0.5);</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return ((&&) x1 x2)}</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">=> (< 0.5) >>= \x1</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (> -0.5) >>= \x2</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return ((&&) x1 x2)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">=> \r ->(\x1 -> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (> -0.5) >>= \x2</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return ((&&) x1 x2))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> ((< 0.5) r)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> r</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">=> \r -> (return (> -0.5) >>= \x2</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> return ((&&) ((< 0.5) r) x2))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> r</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">=> \r -> (\r' -> (\x2 -> </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> return ((&&) (const (< 0.5) r) x2))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ((> -0.5) r')</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> r')</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> r </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">=> \r -> (\r' -> (return ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">=> \r -> (\r' -> (const ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">=> \r -> (\r' -> ((&&) ((< 0.5) r) ((> -0.5) r'))) r</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">=> \r -> (\r' -> ((r < 0.5) && (r' > -0.5))) r</span><br><br>hope this helps,<br>-deech<div><div></div><div class="h5"><br><br><div class="gmail_quote">
On Mon, Oct 19, 2009 at 10:24 AM, Jordan Cooper <span dir="ltr"><<a href="mailto:nefigah@gmail.com" target="_blank">nefigah@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Whoa... how on earth does this work? How does it interpret the<br>
sections as Reader monads?<br>
<div><br>
> That's a job for the reader monad.<br>
><br>
><br>
> Lambda Fu, form 53 - silent reader of truth<br>
><br>
> import Control.Monad<br>
> import Control.Monad.Reader<br>
><br>
</div>> filter (liftM2 (&&) (< 0.5) (> -0.5)) xs<br>
><br>
><br>
><br>
> Regards,<br>
> apfelmus<br>
<div><div></div><div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br>
</div></div></blockquote></div><br>