<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 12 (filtered medium)">
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
        {page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1" />
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US link=blue vlink=purple>

<div class=Section1>

<p class=MsoNormal>Hello,<o:p></o:p></p>

<p class=MsoNormal>I am trying to simulate a client server traffic using
recursive lazy evaluation. I am trying to do that in a recursive writer monad. <o:p></o:p></p>

<p class=MsoNormal>Following code is my attempt to simulate client server
interaction and collect its transcript:<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>{-# OPTIONS -fglasgow-exts #-}<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>module Main where<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'><o:p>&nbsp;</o:p></p>

<p class=MsoNormal style='margin-left:.5in'>import Control.Monad.Writer.Lazy<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>simulation:: Writer [String] ()<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>simulation = mdo<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
a &lt;- server cr<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
cr &lt;- client $ take 10 a<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return ()<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'><o:p>&nbsp;</o:p></p>

<p class=MsoNormal style='margin-left:.5in'>server:: [Integer] -&gt; Writer
[String] [Integer]<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>server (a:as) = do<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp; tell [&quot;server &quot; ++
show a]<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp; rs &lt;- server as<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp; return ((a*2):rs)<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'><o:p>&nbsp;</o:p></p>

<p class=MsoNormal style='margin-left:.5in'>server [] = return []<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'><o:p>&nbsp;</o:p></p>

<p class=MsoNormal style='margin-left:.5in'>client:: [Integer] -&gt; Writer
[String] [Integer]<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>client as = do<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp; dc &lt;- doClient as<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp; return (0:dc)<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp;&nbsp;&nbsp; where <o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
doClient (a:as) = do<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
tell [&quot;Client &quot; ++ show a]<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
as' &lt;- doClient as<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return ((a+1):as')<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
doClient [] = return []<o:p></o:p></p>

<p class=MsoNormal style='margin-left:.5in'><o:p>&nbsp;</o:p></p>

<p class=MsoNormal style='margin-left:.5in'>main = return $ snd $ runWriter
simulation<o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal>The problem that I see is that the transcript collected contains
first all output from the server, and then output from the client.<o:p></o:p></p>

<p class=MsoNormal>Here is an example of output that I see: <o:p></o:p></p>

<p class=MsoNormal>:[&quot;server 0&quot;,&quot;server 1&quot;,&quot;server
3&quot;,&quot;server 7&quot;,&quot;server 15&quot;,&quot;server
31&quot;,&quot;server 63&quot;,&quot;server 127&quot;,&quot;server
255&quot;,&quot;server 511&quot;,&quot;server 1023&quot;,&quot;Client
0&quot;,&quot;Client 2&quot;,&quot;Client 6&quot;,&quot;Client
14&quot;,&quot;Client 30&quot;,&quot;Client 62&quot;,&quot;Client
126&quot;,&quot;Client 254&quot;,&quot;Client 510&quot;,&quot;Client
1022&quot;]<o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal>I would like to collect the output like:<o:p></o:p></p>

<p class=MsoNormal>:[&quot;client 0&quot;,&quot;server 0&#8221;, &#8220;client 1&#8221;,&#8230;]<o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal>This would allow me to remove the ending condition in simulation
(take 10), and instead rely fully on lazy evaluation to collect as many
simulation steps as needed by my computation. &nbsp;<o:p></o:p></p>

<p class=MsoNormal>I am still relatively new to the concepts of recursive
monadic computations, so I would appreciate any suggestions from experts on
this mailing list. <o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal>Thank you<o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

<p class=MsoNormal>Jan<o:p></o:p></p>

<p class=MsoNormal><o:p>&nbsp;</o:p></p>

</div>

</body>

</html>