From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id B49657FA5E for ; Tue, 9 May 2017 15:39:00 +0200 (CEST) Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=oleg@okmij.org; spf=Pass smtp.mailfrom=oleg@okmij.org; spf=None smtp.helo=postmaster@mail1.g3.pair.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of oleg@okmij.org) identity=pra; client-ip=66.39.3.114; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="oleg@okmij.org"; x-sender="oleg@okmij.org"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of oleg@okmij.org designates 66.39.3.114 as permitted sender) identity=mailfrom; client-ip=66.39.3.114; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="oleg@okmij.org"; x-sender="oleg@okmij.org"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@mail1.g3.pair.com) identity=helo; client-ip=66.39.3.114; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="oleg@okmij.org"; x-sender="postmaster@mail1.g3.pair.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3ADLgYWRz1BmRoK0zXCy+O+j09IxM/srCxBDY+r6Qd?= =?us-ascii?q?1eMXIJqq85mqBkHD//Il1AaPBtSHraocw8Pt8InYEVQa5piAtH1QOLdtbDQizf?= =?us-ascii?q?ssogo7HcSeAlf6JvO5JwYzHcBFSUM3tyrjaRsdF8nxfUDdrWOv5jAOBBr/KRB1?= =?us-ascii?q?JuPoEYLOksi7ze6/9pncbglSmDaxfa55IQmrownWqsQYm5ZpJLwryhvOrHtIeu?= =?us-ascii?q?BWyn1tKFmOgRvy5dq+8YB6/ShItP0v68BPUaPhf6QlVrNYFygpM3o05MLwqxbO?= =?us-ascii?q?SxaE62YGXWUXlhpIBBXF7A3/U5zsvCb2qvZx1S+HNsDwULs6Wymt771zRRHmiC?= =?us-ascii?q?kJKSM3/mLYhcNska1VrxCuqBJkzo7IY4yYLuZycr/HcN4YQ2dKQ8ZfVzZGAoO5?= =?us-ascii?q?d4YBFu4BMvhXr4LgoFYOqh++ChejBO70zT9IhXr23aw40+g7FgHGxgMgEssIvX?= =?us-ascii?q?/Jotv1M6ISXvq0zKnO1TjDaPBW2TL86IjKaBwhpuuMXb12fMHMykcvDxvIg1SR?= =?us-ascii?q?pID/MT6Y0v4BvmaB4+Z9SO6ihHYrpxx/rzSz3MshhIbEipgIxl3A6yl12og4KN?= =?us-ascii?q?K+RUVme9CrCoFQuDufN4ZuQsMtXWVouCEix70Do5G7fSwKxIk9yx7QZfGLaZCI?= =?us-ascii?q?7QjiVOaLOzt3mHVleLenixaz90iv1PH8W9G13VtLtCZInN3BumoD2hDJ98SLV/?= =?us-ascii?q?Rw8l+k2TmV1gDT7u9EIVozlareM5MhxbowmYQcsUnYGS/2m0D2jK6Udkk+/eio?= =?us-ascii?q?8evnbq38ppCAL490lh3+MqM2l8OjG+s4NwwOU3Gf+eS9z73j4Vb0QK5Kj/0ziq?= =?us-ascii?q?nWqorWJcUdpq6jAg9ayJwv6xilD2Tu7NNNlnkAIV1IYjqaipDgflfDPLb0AKSR?= =?us-ascii?q?mVOpxRBvyv+OarrlD5rlKXvGkbWneqxyvR0PgDEvxMxSssoHQooKJ+j+Dxf8?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0CkBAAYxRFZgHIDJ0JbHgYMGQYMhAyBD?= =?us-ascii?q?INpiwuQZJgBLIV4AoRpQhUBAQEBAQEBAQEBARIBAQsLCQgmMYIzIIJDAQICASM?= =?us-ascii?q?PAUYFFhoCBSECEQWKdQgOsmCCJop2AQEBBwIBJYELhVOEeoJ8gjOCPi6CMQWRJ?= =?us-ascii?q?oxfgm6ELoMziD0NglmBB44LlEA1gSxPLgiFOhyBcmeIdQEBAQ?= X-IPAS-Result: =?us-ascii?q?A0CkBAAYxRFZgHIDJ0JbHgYMGQYMhAyBDINpiwuQZJgBLIV?= =?us-ascii?q?4AoRpQhUBAQEBAQEBAQEBARIBAQsLCQgmMYIzIIJDAQICASMPAUYFFhoCBSECE?= =?us-ascii?q?QWKdQgOsmCCJop2AQEBBwIBJYELhVOEeoJ8gjOCPi6CMQWRJoxfgm6ELoMziD0?= =?us-ascii?q?NglmBB44LlEA1gSxPLgiFOhyBcmeIdQEBAQ?= X-IronPort-AV: E=Sophos;i="5.38,314,1491256800"; d="scan'208";a="222990090" Received: from mail1.g3.pair.com ([66.39.3.114]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 May 2017 15:38:59 +0200 Received: from mail1.g3.pair.com (localhost [127.0.0.1]) by mail1.g3.pair.com (Postfix) with ESMTP id 0A8E13FAF76; Tue, 9 May 2017 09:38:57 -0400 (EDT) Received: from Magus.localnet (g.sf.ecei.tohoku.ac.jp [130.34.192.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail1.g3.pair.com (Postfix) with ESMTPSA id CD655582C39; Tue, 9 May 2017 09:38:55 -0400 (EDT) Date: Tue, 9 May 2017 22:40:51 +0900 From: Oleg To: choeger@umpa-net.de Cc: caml-list@inria.fr, yminsky@janestreet.com, anil@recoil.org Message-ID: <20170509134051.GA3873@Magus.localnet> Mail-Followup-To: Oleg , choeger@umpa-net.de, caml-list@inria.fr, yminsky@janestreet.com, anil@recoil.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <5af3b33f-d4eb-05dc-d774-b42f63275776@umpa-net.de> User-Agent: Mutt/1.6.1 (2016-04-27) Subject: Re: [Caml-list] Transforming side-effects to a monad 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