Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Markus Mottl <mottl@miss.wu-wien.ac.at>
To: Dave Berry <dave@kal.com>
Cc: OCAML <caml-list@inria.fr>
Subject: Re: Language Design
Date: Fri, 1 Sep 2000 19:48:21 +0200	[thread overview]
Message-ID: <20000901194821.A17888@miss.wu-wien.ac.at> (raw)
In-Reply-To: <3145774E67D8D111BE6E00C0DF418B662D78C0@nt.kal.com>; from dave@kal.com on Fri, Sep 01, 2000 at 12:57:13 +0100

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



  reply	other threads:[~2000-09-03 20:20 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-09-01 11:57 Dave Berry
2000-09-01 17:48 ` Markus Mottl [this message]
2000-09-01 19:12 ` Marcin 'Qrczak' Kowalczyk
     [not found]   ` <39B5DD81.E2500203@maxtal.com.au>
2000-09-06  6:33     ` Marcin 'Qrczak' Kowalczyk
  -- strict thread matches above, loose matches on Subject: below --
2000-08-21 21:44 David McClain
2000-08-23  5:55 ` John Max Skaller
2000-08-24  9:12   ` Francois Pottier
2000-08-24 20:16     ` John Max Skaller
2000-08-25  9:52       ` Andreas Rossberg
2000-08-27 22:00         ` John Max Skaller
2000-08-28 23:11           ` Daan Leijen
2000-08-25 15:41       ` Jerome Vouillon
2000-08-27 22:21         ` John Max Skaller

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=20000901194821.A17888@miss.wu-wien.ac.at \
    --to=mottl@miss.wu-wien.ac.at \
    --cc=caml-list@inria.fr \
    --cc=dave@kal.com \
    /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