caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oleg <oleg@okmij.org>
To: choeger@umpa-net.de
Cc: caml-list@inria.fr, yminsky@janestreet.com, anil@recoil.org
Subject: Re: [Caml-list] Transforming side-effects to a monad
Date: Tue, 9 May 2017 22:40:51 +0900	[thread overview]
Message-ID: <20170509134051.GA3873@Magus.localnet> (raw)
In-Reply-To: <5af3b33f-d4eb-05dc-d774-b42f63275776@umpa-net.de>


Back on Mar 24, Christoph Höger wrote:

> Assume a simple OCaml program with two primitives that can cause
> side-effects:

> let counter = ref 0
> let incr x = counter := !counter + x ; !counter
> let put n = counter := n; !counter
> put (5 + let f x = incr x in f 3)

> This example can be transformed into a pure program using a counter
> monad (using ppx_monadic syntax):

> do_;
>   i <-- let f x = incr x in f 3 ;
>   p <-- put (5 + i)
>   return p

> For a suitable definition of bind and return, both programs behave
> equivalently. My question is: How can one automatically translate a
> program of the former kind to the latter? 

More recently (Apr 29), Yaron Minsky, contrasting his view with the
moderate position by Anil Madhavapeddy, spoke very highly about
monads. 

One really wonders why this obsession with monads. Why to use monadic
encoding if effects can be expressed directly? Is really

> do_;
>   i <-- let f x = incr x in f 3 ;
>   p <-- put (5 + i)
>   return p

so much better than 
        let res = put (int 5 + let f x = incr x in f 3)

?

One can say that the do-notation lets us use the other ways to
implement counters. Well, so does the direct notation:
        let res = put (int 5 + let f x = incr x in f 3)
This is truly OCaml code, with no PPX or other pre-processors.

It all depends on how the `primitives' int, put, incr are
defined. They can use a reference cell or pass the state or talk to a
remote computer via some RPC. If we abstract over the primitives, the
same expression can be evaluated with different models of
`counters'. Incidentally, we did not have to abstract over `let' in
this example. And we did not have to abstract over functions. Very
often our DSL can be kept to first order. Embedding into expressive
OCaml compensates. Incidentally, types really help to tell which
expression belongs to which `level' -- effectful DSL or OCaml (which
acts as a higher-order, typed `macro' language).

The recent article
        http://okmij.org/ftp/tagless-final/nondet-effect.html

presents quite a bit more challenging example, of
non-determinism. Once can write literally Curry (*) code in OCaml --
with no PPX or other preprocessors, using multiple interpretations of
non-determinism.  All works in vanilla OCaml 4.04. We don't have to
put up with functors: first-class modules really help. The modular
implicits will help even more, by putting the `implementation'
argument out of sight.

(*) Curry is a functional-logic programming language, with built-in
non-determinism

  parent reply	other threads:[~2017-05-09 13:39 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-23 19:56 Christoph Höger
2017-03-23 20:59 ` Jeremy Yallop
2017-05-09 13:40 ` Oleg [this message]
2017-05-09 17:15   ` Yaron Minsky

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=20170509134051.GA3873@Magus.localnet \
    --to=oleg@okmij.org \
    --cc=anil@recoil.org \
    --cc=caml-list@inria.fr \
    --cc=choeger@umpa-net.de \
    --cc=yminsky@janestreet.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;
as well as URLs for NNTP newsgroup(s).