From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA32620 for caml-red; Sun, 3 Sep 2000 22:20:36 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA13153 for ; Fri, 1 Sep 2000 19:48:29 +0200 (MET DST) Received: from miss.wu-wien.ac.at (miss.wu-wien.ac.at [137.208.107.17]) by nez-perce.inria.fr (8.10.0/8.10.0) with ESMTP id e81HmSP18114 for ; Fri, 1 Sep 2000 19:48:28 +0200 (MET DST) Received: (from mottl@localhost) by miss.wu-wien.ac.at (8.9.0/8.9.0) id TAA26730; Fri, 1 Sep 2000 19:48:21 +0200 (MET DST) Date: Fri, 1 Sep 2000 19:48:21 +0200 From: Markus Mottl To: Dave Berry Cc: OCAML Subject: Re: Language Design Message-ID: <20000901194821.A17888@miss.wu-wien.ac.at> References: <3145774E67D8D111BE6E00C0DF418B662D78C0@nt.kal.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2i In-Reply-To: <3145774E67D8D111BE6E00C0DF418B662D78C0@nt.kal.com>; from dave@kal.com on Fri, Sep 01, 2000 at 12:57:13 +0100 Sender: weis@pauillac.inria.fr On Fri, 01 Sep 2000, Dave Berry wrote: > Given a value in a monad, e.g. IO v, how can I remove v from the Monad? > Surely this would be required to seamlessly integrate stateful and > functional code? Well, that's the idea behind monads that you (normally) shouldn't: you must not be able to run a function with the same arguments and get a different result. That's the way I/O-monads work in e.g. Haskell. However, there are different monads, e.g. "state monads", which allow for a function that takes a state, the computation (in the monad) and runs the computation on (in) the given state, returning the result as "normal" value. This does not circumvent referential transparancy unless running the computation in the monad in the same state can yield different results (e.g. you use I/O, random numbers, etc.). You can find an OCaml-implementation of state monads here (with an example application, namely an interpreter for a simple imperative language): http://miss.wu-wien.ac.at/~mottl/ocaml_sources/IMP-1.0-2/monad E.g. the following program: --------------------------------------------------------------------------- open State_monad open Printf let test = fetchST >>= fun state -> unitST (printf "First state: %d\n" state) >> assignST 2 >> unitST "is 42" >>= fun answer -> unitST (printf "Answer %s\n" answer) >> fetchST >>= fun state -> unitST (printf "Second state: %d\n" state) >> assignST 3 let _ = printf "State in monad: %d\n" (initST 1 (test >> fetchST)) --------------------------------------------------------------------------- prints: First state: 1 Answer is 42 Second state: 2 State in monad: 3 As you seen in the code, the state is propagated implicitely. Only when you want to "look" at it, you use "fetchST", and you use "assignST" to overwrite it. "initST" does what you asked for: it "runs" the computation in "test" starting with a specific state (the integer 1) and returns whatever the monad wants to return. In this case we force the monad to return the internal state by attaching "fetchST" to "test". Best regards, Markus Mottl -- Markus Mottl, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl