caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jon@ffconsultancy.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments
Date: Wed, 27 Jun 2007 16:53:26 +0100	[thread overview]
Message-ID: <200706271653.27116.jon@ffconsultancy.com> (raw)
In-Reply-To: <F2BC247F-45B1-4BD3-A006-D214D635ABC6@lrde.epita.fr>

On Wednesday 27 June 2007 16:06:51 Quôc Peyrot wrote:
> I mean, for someone like me, with quite some experience in the asm/c/c
> ++ world (i.e. a garbage collector-less world) but not much in other
> languages, it's easy to naively think of a garbage collector as a
> fancy feature to prevent from having to call "free/delete". But I'm
> starting to realize there is a whole new set of powerful design
> patterns which come along. It has been said multiple times on this
> mailing list, but I think we really miss a book about these design
> patterns and optimization tricks often specific to a given (or a set
> of) feature (functional, lazy computations, garbage collector...).

This is an excellent idea. I'll write an OCaml Journal article on design 
patterns! :-)

> > Other advanced optimization techniques that can be used for benefit
> > in very specialized situations include (explicit) continuation coding:
> > rather than returning a value (e.g. a tuple), you take as an argument
> > a function to which you then pass on your return value(s). This is
> > quite
> > useful whenever "execution flow branches out into multiple paths that
> > have to be taken", and may sometimes (though rarely) also be used for
> > good as a poor man's VALUES/MULTIPLE-VALUE-BIND substitute.
>
> I didn't get that part at all. I think I would need an example to
> understand
> why it is interesting to pass the function instead of just returning
> the tuple
> and processing it.

I think Thomas is referring to continuation passing style (CPS). That isn't an 
optimization though (it slows things down) but it does let you abstract away 
mutation. However, it is not entirely safe in the absence of linear types.

For example, the immutable Map and mutable Hashtbl both map keys to values. If 
you wrap them with an API written in CPS then you can switch between Maps and 
Hashtbls transparently:

module type MAP = sig
  type t
  val create : unit -> t
  val add : string -> string -> t -> (t -> 'a) -> 'a
  val remove : string -> t -> (t -> 'a) -> 'a
  val copy : t -> (t * t -> 'a) -> 'a
end;;

module Pure : MAP = struct
  module Map = Map.Make(String)

  type t = string Map.t

  let create() = Map.empty

  let add k v m f = f(Map.add k v m)
  let remove k m f = f(Map.remove k m)
  let copy m f = f(m, m)
end;;

module Impure : MAP = struct
  type t = (string, string) Hashtbl.t

  let create() = Hashtbl.create 1

  let add k v m f =
    Hashtbl.replace m k v;
    let f_m = f m in
    Hashtbl.remove m k;
    f_m

  let remove k m f =
    let v = Hashtbl.find m k in
    Hashtbl.remove m k;
    let f_m = f m in
    Hashtbl.add m k v;
    f_m

  let copy m f = f(m, Hashtbl.copy m)
end;;

However, this is not completely safe because you might erroneously return a 
map or hash table from the continuation "f" passed to these functions. 
Enforcing this statically requires linear types.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
The OCaml Journal
http://www.ffconsultancy.com/products/ocaml_journal/?e


  reply	other threads:[~2007-06-27 15:59 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-27 12:14 Jon Harrop
2007-06-27 13:18 ` [Caml-list] " Quôc Peyrot
2007-06-27 13:53   ` Jon Harrop
2007-06-27 14:18     ` Thomas Fischbacher
2007-06-27 15:09     ` Quôc Peyrot
2007-06-27 15:28     ` Mattias Engdegård
2007-06-27 15:38       ` Robert Fischer
2007-06-27 15:48         ` Mattias Engdegård
2007-06-27 16:01           ` Robert Fischer
2007-06-27 16:01           ` Mattias Engdegård
2007-06-27 18:06           ` Jon Harrop
2007-06-27 18:31             ` Brian Hurt
2007-06-27 19:56               ` skaller
2007-06-27 20:17               ` Jonathan Bryant
2007-06-27 22:57               ` Jon Harrop
2007-06-27 16:53     ` Hash-consing (was Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments) Daniel Bünzli
2007-06-30  8:19     ` [Caml-list] The Implicit Accumulator: a design pattern using optional arguments Pierre Etchemaïté
2007-06-27 13:55   ` Thomas Fischbacher
2007-06-27 15:06     ` Quôc Peyrot
2007-06-27 15:53       ` Jon Harrop [this message]
2007-06-28 11:01         ` Thomas Fischbacher
2007-06-28 11:32           ` Jon Harrop
2007-06-28 11:42             ` Joel Reymont
2007-06-28 12:08               ` Jon Harrop
2007-06-28 13:10                 ` Quôc Peyrot
2007-06-28 13:35                   ` Thomas Fischbacher
2007-06-28 12:59             ` Thomas Fischbacher
2007-06-28 13:05               ` Jon Harrop
2007-06-28 13:33                 ` Thomas Fischbacher
2007-06-28 14:43                   ` Jon Harrop
2007-06-28 16:01                     ` Thomas Fischbacher
2007-06-28 17:53                       ` Jon Harrop
2007-06-27 16:39       ` Thomas Fischbacher
2007-06-27 19:26         ` Quôc Peyrot
2007-06-28 11:39           ` Thomas Fischbacher
2007-06-28 14:44             ` Jon Harrop
2007-06-28 16:03               ` Thomas Fischbacher
2007-06-28 17:20                 ` Dirk Thierbach
2007-06-28 22:12                   ` Thomas Fischbacher
2007-06-29  1:10                     ` Jon Harrop
2007-06-29 10:55                       ` Thomas Fischbacher
2007-06-29  6:12                     ` Dirk Thierbach
2007-06-27 17:16       ` Book about functional design patterns Gabriel Kerneis
2007-06-27 17:48         ` [Caml-list] " Jon Harrop
2007-06-27 19:33           ` Quôc Peyrot
2007-06-27 19:30         ` Quôc Peyrot
2007-06-27 19:48           ` Brian Hurt
2007-06-27 20:04             ` Quôc Peyrot
2007-06-27 20:35               ` Brian Hurt
2007-06-27 20:55                 ` Quôc Peyrot
2007-06-27 20:58                   ` Pal-Kristian Engstad
2007-06-27 21:18                     ` Quôc Peyrot
2007-06-27 21:18                       ` Pal-Kristian Engstad
2007-06-27 21:34                         ` Quôc Peyrot
2007-06-27 22:13                           ` Pal-Kristian Engstad
2007-06-27 15:18     ` [Caml-list] The Implicit Accumulator: a design pattern using optional arguments Jon Harrop
2007-06-27 16:44       ` Thomas Fischbacher
2007-06-27 18:17         ` Jon Harrop
2007-06-28 11:18           ` Thomas Fischbacher
2007-06-29 13:15     ` Bill Wood

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=200706271653.27116.jon@ffconsultancy.com \
    --to=jon@ffconsultancy.com \
    --cc=caml-list@yquem.inria.fr \
    /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).