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 mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by sympa.inria.fr (Postfix) with ESMTPS id BC604800BE for ; Fri, 10 Feb 2017 16:16:52 +0100 (CET) Authentication-Results: mail2-smtp-roc.national.inria.fr; spf=None smtp.pra=markus.mottl@gmail.com; spf=Pass smtp.mailfrom=markus.mottl@gmail.com; spf=None smtp.helo=postmaster@mail-qt0-f181.google.com Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of markus.mottl@gmail.com) identity=pra; client-ip=209.85.216.181; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="markus.mottl@gmail.com"; x-sender="markus.mottl@gmail.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail2-smtp-roc.national.inria.fr: domain of markus.mottl@gmail.com designates 209.85.216.181 as permitted sender) identity=mailfrom; client-ip=209.85.216.181; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="markus.mottl@gmail.com"; x-sender="markus.mottl@gmail.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail2-smtp-roc.national.inria.fr: no sender authenticity information available from domain of postmaster@mail-qt0-f181.google.com) identity=helo; client-ip=209.85.216.181; receiver=mail2-smtp-roc.national.inria.fr; envelope-from="markus.mottl@gmail.com"; x-sender="postmaster@mail-qt0-f181.google.com"; x-conformance=sidf_compatible IronPort-PHdr: =?us-ascii?q?9a23=3AHuoeVxVt6YfmTej62ESlhq8E9/bV8LGtZVwlr6E/?= =?us-ascii?q?grcLSJyIuqrYZhaHt8tkgFKBZ4jH8fUM07OQ6PG8HzxfqsjQ+DBaKdoXCE9D0Z?= =?us-ascii?q?1X1yUbQ+e7SmTDZMbwaCI7GMkQHHRExFqcdXZvJcDlelfJqWez5zNBUj/2NA5y?= =?us-ascii?q?O/inUtWK15f//6mI9pbSewRFgiamKfM3dU3u7FaZis5DrJVtJ6AggiHOv3FJfe?= =?us-ascii?q?9W2W5pIxrHjRbx4Na5pcY7rAxfvvsg84hLVqCsLIoiSrkNKT08MH0urOjsqQXH?= =?us-ascii?q?BV+K72EfT35Qlx1UHwzt4xTzX5O3uSz/4LkukBKGNNH7GOhnEQ+p6L1mHVqx0H?= =?us-ascii?q?8K?= X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: =?us-ascii?q?A0CaAAAK2Z1YhrXYVdFeGwEBAQMBAQEJA?= =?us-ascii?q?QEBFQEBAQECAQEBAQgBAQEBhAgxRxEHg1KcFJAFhz4shXYCgnIHQhUBAQEBAQE?= =?us-ascii?q?BAQEBARIBAQEICwsKHS+CMwQBFgEEghYBAQEDASMEGQEUBx0BAwwGBQsDCgICE?= =?us-ascii?q?RUCAiEBAREBBQEcBhMJiVYBAw0IDqM1P4wCgWsYBQEcgwkFg10KGScNVYM5AQE?= =?us-ascii?q?BAQEBAQMBAQEBAQEBGQIGEnmFQYRvglGCDzOCDTqCXwWJDIc1inc6gX+EcIFCg?= =?us-ascii?q?V6DbAOEFpEFijWHGRQegRU1gSAfEx1RF4QeDxEMgX8iNQGIT4FOAQEB?= X-IPAS-Result: =?us-ascii?q?A0CaAAAK2Z1YhrXYVdFeGwEBAQMBAQEJAQEBFQEBAQECAQE?= =?us-ascii?q?BAQgBAQEBhAgxRxEHg1KcFJAFhz4shXYCgnIHQhUBAQEBAQEBAQEBARIBAQEIC?= =?us-ascii?q?wsKHS+CMwQBFgEEghYBAQEDASMEGQEUBx0BAwwGBQsDCgICERUCAiEBAREBBQE?= =?us-ascii?q?cBhMJiVYBAw0IDqM1P4wCgWsYBQEcgwkFg10KGScNVYM5AQEBAQEBAQMBAQEBA?= =?us-ascii?q?QEBGQIGEnmFQYRvglGCDzOCDTqCXwWJDIc1inc6gX+EcIFCgV6DbAOEFpEFijW?= =?us-ascii?q?HGRQegRU1gSAfEx1RF4QeDxEMgX8iNQGIT4FOAQEB?= X-IronPort-AV: E=Sophos;i="5.35,142,1484002800"; d="scan'208";a="259771618" Received: from mail-qt0-f181.google.com ([209.85.216.181]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES128-GCM-SHA256; 10 Feb 2017 16:16:32 +0100 Received: by mail-qt0-f181.google.com with SMTP id w20so37104779qtb.1 for ; Fri, 10 Feb 2017 07:16:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=vuMWrhmizLakoi19+c5+opYVvilbtReDiSxLaDY1d0Y=; b=a2PpDc5jgwT9OZLd2uh3MaHhPSyLzI8Tj+4KgkFrczo3zLUmYIzgGN0r9Tq2lUHOB8 YF2tMgSWR8VyYS+DHx7CE9TqGukzcbav6sxbe5F+qVrsqVYSIEpIGgQmzv0/USLWx1jL pDVXKgeLBABgw2H4MVXwAcgD8BbmFCvt8uFnClRJ3oSvAz8VuQR+wK03S5Lxm2hPBzKf fr7IK93+dnBboCmCE/8TYuGLkvHAkEbEknSRxGzmKcPtGwrv5mK7j5pgoeU1pMGU4D7u 4/DGSsd1ZFCCap/3e5/e8nn1pmXRFPUm3hsRDfFd7FUSWeCz7JtQj7FUta8uxSU2gxiz gRsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=vuMWrhmizLakoi19+c5+opYVvilbtReDiSxLaDY1d0Y=; b=t1cRhLPZeVD5ByKjWhrdpnM9V0uFjzQ2uzBD6FCDH1G+n/WB6B98zwj4qBUwaQphhn UMoMJ8/M6nSu7eRR24pQc6m+bneLzkjmf9KYu8A5FvPjCEgbq5l6ybecyIb12iljC5Ms 5aaN4TjnahJ//Xl7+4prgIUem00/vBXOyO8tOd+X9OJuEZuOl6ma76AouuX598UG1pAh dNasKdZl65CCPkL22+cpNDFPUPdwMUkKxc5fXuYDCypKMoPB+xBw0jDBUjSDddXagUeP bNwd+Whv4Kg/JJAWjUL93w852LLaT5fKuSvLSt797sAhTy08cKPNRFwuXfAYi/9YqGJO KHiQ== X-Gm-Message-State: AMke39nzdMCHISoxEI0AOxT59oVYf1pl+vVlPsIYxbp6HM2zMg+zVpQw3bgdADDIEvHj9NRMYDqVuOdC47unZQ== X-Received: by 10.237.44.103 with SMTP id f94mr9417773qtd.292.1486739791152; Fri, 10 Feb 2017 07:16:31 -0800 (PST) MIME-Version: 1.0 Received: by 10.12.176.36 with HTTP; Fri, 10 Feb 2017 07:16:30 -0800 (PST) In-Reply-To: References: <1486683451.12698.83.camel@gerd-stolpmann.de> <1486719507.2806603.876599584.3796D567@webmail.messagingengine.com> From: Markus Mottl Date: Fri, 10 Feb 2017 10:16:30 -0500 Message-ID: To: Evgeny Roubinchtein Cc: Leo White , OCaml List Content-Type: text/plain; charset=UTF-8 Subject: Re: [Caml-list] namespace inside object? Lets first see what closed recursion typically looks like: type t = Num of int | Add of t * t let rec eval = function Num n -> n | Add (x, y) -> eval x + eval y This type cannot be extended recursively with another operation, say "Sub" for subtraction, because "Add" only takes arguments of type "t", i.e. it only permits "Num" and "Add" under itself. If you want to permit extensibility, you have to "break" the recursion, leaving it open. This can be done by introducing a type variable that captures extensions to the datastructure, and an extra evaluation function argument to "eval" to deal with the evaluation of extensions. Polymorphic variants make implementations of open recursion much more elegant than regular variants. You can check out a simple example for the above problem here: https://github.com/mmottl/ocaml-prog-pats/blob/master/extensible_ast/ast.ml Regards, Markus On Fri, Feb 10, 2017 at 9:40 AM, Evgeny Roubinchtein wrote: > Apologies in advance for my ignorance, but is there some reference(s) you > could point me to where the notion of "open recursion" is defined? I am > pretty sure I understand "dynamic dispatch." > > -- > Best, > Zhenya > > On Fri, Feb 10, 2017 at 4:38 AM, Leo White wrote: >> >> I think it would be more correct to say that you want open recursion. >> Classes are the easiest way to get that in OCaml, and they indeed use >> dynamic dispatch and inheritance, but there are other mechanisms that >> provide open recursion. For example, most proposals for "mixin modules" >> support open recursion but I wouldn't say they use dynamic dispatch. >> >> Regards, >> >> Leo >> >> On Thu, 9 Feb 2017, at 10:16 PM, Steffen Smolka wrote: >> >> Right....but you have to explicitly make that choice when you implement >> `f`. With objects on the other hand, somebody may write a base >> implementation c: >> >> class c = object(this) >> method foo = ... this#bar ... >> method bar = ... this#foo ... (* original definition *) >> end >> >> Later on, somebody else may refine this implementation by overwriting >> c#bar: >> >> class c2 = object(this) >> inherit c >> method bar = ... (* new definition *) >> end >> >> Now c2#foo will invoke the new definition of bar, even though the author >> of `c` may have never anticipated this (and did not have to account for this >> possibility when he defined foo). >> >> So, I would say "dynamic dispatch" plus inheritance give you the ability >> to extend modules in ways that weren't necessarily anticipated by the >> original author of the module. And yet all the code written by the original >> author will work with your extended version of the module. >> >> -- Steffen >> >> >> >> On Thu, Feb 9, 2017 at 9:01 PM, Yaron Minsky >> wrote: >> >> I've always been a bit confused about what the term "dynamic dispatch" >> means, but don't first class modules provide what you want? After all, >> when you write: >> >> let f (module M : S) x = >> M.g x >> >> the specific function M.g is determined dynamically, depending on >> which first class module is passed into f. >> >> y >> >> On Thu, Feb 9, 2017 at 6:54 PM, Steffen Smolka >> wrote: >> > Yeah, I have seen that there is support for first class modules, that's >> > pretty cool stuff! >> > I do need dynamic dispatch, though. >> > >> > -- Steffen >> > >> > On Thu, Feb 9, 2017 at 6:37 PM, Gerd Stolpmann >> > wrote: >> >> >> >> Am Donnerstag, den 09.02.2017, 18:19 -0500 schrieb Steffen Smolka: >> >> >> >> Thanks for the detailed answer, Jeremy! >> >> >> >> If you're keen to stick with objects >> >> >> >> >> >> Yes, I rely on inheritance and dynamic dispatch for what I have in >> >> mind. >> >> (This is actually the first time I'm touching the dark object oriented >> >> side >> >> of OCaml :) ) >> >> >> >> To give some more context, I am refactoring some code that uses modules >> >> and no objects. The reason I want to move to objects is that I want to >> >> derive a slightly enhanced module from some base implementation. >> >> Inheritance >> >> + dynamic dispatch allow me to do so with very little trouble: I can >> >> simply >> >> overwrite a few methods from the base implementation. >> >> >> >> I suppose I could achieve the same by turning the base module into a >> >> functor, and abstracting over the functions that my enhanced >> >> implementation >> >> needs to replace. I think it won't be quite as natural, but I'll give >> >> that a >> >> try. >> >> >> >> >> >> First-class modules could also be an option: Let's assume both the >> >> base >> >> module and the modified one can use the same module type: >> >> >> >> module T = sig ... end >> >> >> >> Now, define the base module like >> >> >> >> module Base : T = >> >> ... >> >> end >> >> >> >> then, define the modified one: >> >> >> >> module Mod : T = >> >> include Base >> >> ... now override what you need to change but note that there's no >> >> dynamic dispatch ... >> >> end >> >> >> >> Of course, you could also use functors for making these modules. >> >> >> >> Now turn this into first-class modules and pass them around: >> >> >> >> let base = (module Base : T) >> >> let mod = (module Mod : T) >> >> >> >> The syntax for unpacking the module is quite cumbersome: >> >> >> >> let module M = (val base : T) in >> >> M.function ... >> >> >> >> Unfortunately, there's nothing simple like base.function. >> >> >> >> Compared with objects you get: >> >> >> >> You can also put types and (to some degree) modules into these "code >> >> containers" >> >> However, there's no dynamic dispatch except you arrange explicitly for >> >> that, e.g. with references to functions >> >> Generally, a heavier syntax, but it might be ok >> >> >> >> >> >> Gerd >> >> >> >> >> >> >> >> Or you could select the encoding using a variant type: >> >> >> >> >> >> Good idea, and I'm happy with the syntax for the caller. But I'm more >> >> concerned with the organization of the code; this would mix the Latin1 >> >> and >> >> Utf8 implementations. I would rather keep them separate. >> >> >> >> -- Steffen >> >> >> >> >> >> On Thu, Feb 9, 2017 at 5:55 PM, Jeremy Yallop wrote: >> >> >> >> Dear Steffen, >> >> >> >> On 9 February 2017 at 20:36, Steffen Smolka >> >> wrote: >> >> > Is it possible to create namespaces inside an object? Concretely, I >> >> > would >> >> > like to write >> >> > >> >> > class buffer = object(self) >> >> > ... >> >> > method get = ... >> >> > >> >> > module Latin1 = struct >> >> > method get = ... >> >> > end >> >> > >> >> > module Utf8 = struct >> >> > method get = ... >> >> > end >> >> > end >> >> > >> >> > so that given an object b : buffer, I can call methods >> >> > b#get >> >> > b#Latin1.get >> >> > b#Utf8.get >> >> >> >> It's possible to achieve something like this using methods that return >> >> objects. If your nested objects don't need to access the internal >> >> state of the parent then you might write it like this: >> >> >> >> class buffer = >> >> let latin1 = object >> >> method get = ... >> >> end >> >> and utf8 = object >> >> method get = ... >> >> end in >> >> object(self) >> >> ... >> >> method get = ... >> >> method latin1 = latin1 >> >> method utf8 = utf8 >> >> end >> >> >> >> With this approach you can write >> >> >> >> b#get >> >> b#latin1#get >> >> b#utf8#get >> >> >> >> which, apart from some minor orthographic differences, looks like what >> >> you were aiming for. >> >> >> >> Your intuition that this isn't really idiomatic OCaml is right, >> >> though. In OCaml, unlike some other languages with classes and >> >> objects, classes are not usually used as namespaces; method names are >> >> globally (or, rather, "ambiently") scoped, and there's no real support >> >> for the kind of nesting that you're interested in. Instead, people >> >> typically build nested namespaces using modules: >> >> >> >> module Buffer = >> >> struct >> >> let get = ... >> >> >> >> module Latin1 = struct >> >> let get = ... >> >> end >> >> >> >> module Utf8 = struct >> >> let get = ... >> >> end >> >> end >> >> >> >> With the module approach you write the 'receiver' after the 'method' >> >> rather than before, but that doesn't seem like a huge hardship. (10% >> >> of the world manages to get by with VSO languages.) >> >> >> >> Buffer.get b ... >> >> Buffer.Latin1.get b ... >> >> Buffer.Utf8.get b ... >> >> >> >> If you're keen to stick with objects there are slightly more idiomatic >> >> ways to make it work. You could, of course, replace the '.' with a >> >> '_' and define methods 'latin1_get', 'utf8_get' in place of >> >> 'Latin1.get', 'Utf8.get'. Or you could select the encoding using a >> >> variant type: >> >> >> >> type enc = Latin1 | Utf8 >> >> >> >> class buffer = >> >> object (self) >> >> method get = function >> >> | Latin1 -> ... >> >> | Utf8 -> ... >> >> end >> >> >> >> Of course, the order of the words in an invocation changes again, but >> >> there's no real increase in complexity for the caller: >> >> >> >> b#get Latin1 >> >> b#get Utf8 >> >> >> >> This last approach can be taken quite far -- for example, you could >> >> enrich the type 'enc' so that the return type of 'get' varies >> >> according to the encoding. >> >> >> >> Kind regards, >> >> >> >> Jeremy >> >> >> >> >> >> -- >> >> ------------------------------------------------------------ >> >> Gerd Stolpmann, Darmstadt, Germany gerd@gerd-stolpmann.de >> >> My OCaml site: http://www.camlcity.org >> >> Contact details: http://www.camlcity.org/contact.html >> >> Company homepage: http://www.gerd-stolpmann.de >> >> ------------------------------------------------------------ >> >> >> > >> >> > -- Markus Mottl http://www.ocaml.info markus.mottl@gmail.com