caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pietro Abate <Pietro.Abate@anu.edu.au>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Functional design for a basic simulation pipe.
Date: Fri, 12 Oct 2007 00:20:14 +1000	[thread overview]
Message-ID: <20071011142014.GB9205@pulp.rsise.anu.edu.au> (raw)
In-Reply-To: <470E2A8D.4040704@inescporto.pt>

On Thu, Oct 11, 2007 at 02:52:13PM +0100, Hugo Ferreira wrote:
> > I'm not entirely sure this is correct. Note that I've used an exception
> > to get out of the loop just because I'm too lazy to wrap my head around
> > the exception monad. I'm also not sure that is the best way of using a
> > state monad... Any monad-experts out there to comment ?
> I am going to have to look at this very carefully (read: learn something
> about monads). Comment below.
> 
> > This is the code:
[...]
> > let rec exp inputstate =
> >     SM.bind inputstate (fun state ->
> >         let newevent = a state in
> >         let (newstate,newdata) = b newevent state in
> >         SM.bind (SM.modify (fun olddata -> newdata::olddata)) (fun _ ->
> >             if newstate = 0 then
> >                 SM.bind (SM.fetch) (fun data ->
> >                     raise ( Stop ( c (data) ))
> >                 )
> >             else exp (SM.return newstate)
> >         )
> >     )
> 
> I can however see that "exp" seems to be a loop. Cannot see however were
> you tie the latest state of "a" with the next use of "b" 8-(. I will
> really have to see how this stuff works.

There are two states. One is the state of the state monad, that is hidden and
that is basically the list of partial results. The other one is the state of
the function (your state), that is explicit.  the latest explicit state is the
variable "state" and the new state, result of the function b, is "newstate", that is
passed back in the loop with a recursive call. To avoid the (horrible)
exception in the middle of the computation, you can try to wrap the state monad
with and exception monad, so to make it more elegant. This is a simple
exception monad functor that you can use to compose the two monads
together.

module ExceptionMonadMake(T:sig type t end) =
struct
    type mt = Just of T.t| Nothing
    let return a = Just a
    let bind m f =
        match m with
        |Just v -> f v
        |Nothing -> Nothing
    let mzero = Nothing
    let mplus = function
        |Just v -> fun _ -> Just v
        |Nothing -> fun m -> m
    let trywith = mplus
    let lift f m = bind m f
end

Something that I haven't figure out yet is how to encode branching
computations using the same pattern to implement a finite state
machine.

pietro


  reply	other threads:[~2007-10-11 14:20 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-10  7:39 Hugo Ferreira
2007-10-10  8:34 ` [Caml-list] " skaller
2007-10-10 10:08   ` Hugo Ferreira
2007-10-10 10:31     ` Vincent Aravantinos
2007-10-10 10:56       ` Hugo Ferreira
2007-10-11 12:01         ` Pietro Abate
2007-10-11 13:52           ` Hugo Ferreira
2007-10-11 14:20             ` Pietro Abate [this message]
2007-10-10 15:00     ` skaller
2007-10-10 15:56       ` skaller
2007-10-11  6:57         ` Hugo Ferreira
2007-10-11  8:09           ` skaller
2007-10-11  9:54             ` Hugo Ferreira
2007-10-11 13:47               ` skaller
2007-10-11 11:17 ` Zheng Li
2007-10-11 13:48   ` [Caml-list] " Hugo Ferreira
2007-10-15 23:04     ` Zheng Li
2007-10-22  7:48       ` [Caml-list] " Hugo Ferreira

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20071011142014.GB9205@pulp.rsise.anu.edu.au \
    --to=pietro.abate@anu.edu.au \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).