[Haskell] Simulating client server communication with recursive monads

Jan Stranik janstranik at yahoo.de
Mon Jan 14 19:02:43 EST 2008


Hello,

I am trying to simulate a client server traffic using recursive lazy
evaluation. I am trying to do that in a recursive writer monad. 

Following code is my attempt to simulate client server interaction and
collect its transcript:

{-# OPTIONS -fglasgow-exts #-}

module Main where

 

import Control.Monad.Writer.Lazy

simulation:: Writer [String] ()

simulation = mdo

        a <- server cr

        cr <- client $ take 10 a

        return ()

 

server:: [Integer] -> Writer [String] [Integer]

server (a:as) = do

  tell ["server " ++ show a]

  rs <- server as

  return ((a*2):rs)

 

server [] = return []

 

client:: [Integer] -> Writer [String] [Integer]

client as = do

  dc <- doClient as

  return (0:dc)

    where 

      doClient (a:as) = do

              tell ["Client " ++ show a]

              as' <- doClient as

              return ((a+1):as')

      doClient [] = return []

 

main = return $ snd $ runWriter simulation

 

The problem that I see is that the transcript collected contains first all
output from the server, and then output from the client.

Here is an example of output that I see: 

:["server 0","server 1","server 3","server 7","server 15","server
31","server 63","server 127","server 255","server 511","server 1023","Client
0","Client 2","Client 6","Client 14","Client 30","Client 62","Client
126","Client 254","Client 510","Client 1022"]

 

I would like to collect the output like:

:["client 0","server 0", "client 1",.]

 

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.  

I am still relatively new to the concepts of recursive monadic computations,
so I would appreciate any suggestions from experts on this mailing list. 

 

Thank you

 

Jan

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell/attachments/20080114/4ff454e6/attachment-0001.htm


More information about the Haskell mailing list