From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 98591BC6B for ; Thu, 11 Oct 2007 16:20:20 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao8CADrNDUeWy9CN/2dsb2JhbAA X-IronPort-AV: E=Sophos;i="4.21,260,1188770400"; d="scan'208";a="17849506" Received: from unknown (HELO riemann.rsise.anu.edu.au) ([150.203.208.141]) by mail4-smtp-sop.national.inria.fr with ESMTP; 11 Oct 2007 16:20:18 +0200 Received: from localhost (localhost [127.0.0.1]) by riemann.rsise.anu.edu.au (Postfix) with ESMTP id BEC786BA51 for ; Fri, 12 Oct 2007 00:20:14 +1000 (EST) X-Virus-Scanned: Debian amavisd-new at rsise.anu.edu.au Received: from riemann.rsise.anu.edu.au ([127.0.0.1]) by localhost (backend2.rsise.anu.edu.au [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Aq4RTEUpNKIz for ; Fri, 12 Oct 2007 00:20:14 +1000 (EST) Received: from pulp.rsise.anu.edu.au (pulp.rsise.anu.edu.au [150.203.208.49]) by riemann.rsise.anu.edu.au (Postfix) with ESMTP for ; Fri, 12 Oct 2007 00:20:14 +1000 (EST) Received: by pulp.rsise.anu.edu.au (Postfix, from userid 1560) id 90CB4A0C4FE; Fri, 12 Oct 2007 00:20:14 +1000 (EST) Date: Fri, 12 Oct 2007 00:20:14 +1000 From: Pietro Abate To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Functional design for a basic simulation pipe. Message-ID: <20071011142014.GB9205@pulp.rsise.anu.edu.au> References: <470C8199.4080708@inescporto.pt> <1192005274.6285.4.camel@rosella.wigram> <470CA488.1070804@inescporto.pt> <3EDA3D93-DE9A-4336-B65E-03593A3B9723@yahoo.fr> <470CAFC2.2020406@inescporto.pt> <20071011120139.GA8588@pulp.rsise.anu.edu.au> <470E2A8D.4040704@inescporto.pt> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <470E2A8D.4040704@inescporto.pt> User-Agent: Mutt/1.5.13 (2006-08-11) X-Spam: no; 0.00; 0100,:01 monads:01 recursive:01 functor:01 monads:01 sig:01 struct:01 branching:01 computations:01 wrote:01 partial:01 rec:01 encode:01 exception:01 exception:01 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