From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 64E44BBAF for ; Tue, 25 Aug 2009 08:29:46 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AqUBAMchk0pRZ90wkWdsb2JhbACbBwEBAQEJCwoHEwO6boQaBQ X-IronPort-AV: E=Sophos;i="4.44,271,1249250400"; d="scan'208";a="31608928" Received: from mtaout02-winn.ispmail.ntl.com ([81.103.221.48]) by mail2-smtp-roc.national.inria.fr with ESMTP; 25 Aug 2009 08:29:46 +0200 Received: from aamtaout01-winn.ispmail.ntl.com ([81.103.221.35]) by mtaout02-winn.ispmail.ntl.com (InterMail vM.7.08.04.00 201-2186-134-20080326) with ESMTP id <20090825062945.ZYBZ6611.mtaout02-winn.ispmail.ntl.com@aamtaout01-winn.ispmail.ntl.com>; Tue, 25 Aug 2009 07:29:45 +0100 Received: from romulus.metastack.com ([81.102.132.77]) by aamtaout01-winn.ispmail.ntl.com (InterMail vG.2.02.00.01 201-2161-120-102-20060912) with ESMTP id <20090825062945.QQOW13254.aamtaout01-winn.ispmail.ntl.com@romulus.metastack.com>; Tue, 25 Aug 2009 07:29:45 +0100 Received: from Tenor ([212.183.134.67]) (authenticated bits=0) by romulus.metastack.com (8.14.2/8.14.2) with ESMTP id n7P6TbVj024473 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Tue, 25 Aug 2009 07:29:42 +0100 From: "David Allsopp" To: , "'OCaml'" References: <94AD5806-B6F6-44F7-AA3C-1E63B6C1A722@metaweb.com> <4A930EF0.3050900@glondu.net> <4A9311E4.7060600@ens-lyon.org> <4A931E3B.4010501@ens-lyon.org> <20090825051958.GA2066@happyleptic.org> In-Reply-To: <20090825051958.GA2066@happyleptic.org> Subject: RE: [Caml-list] lazy vs fun Date: Tue, 25 Aug 2009 07:29:35 +0100 Message-ID: <000301ca254d$6d4aab40$47e001c0$@metastack.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: AcolQ7v2OnruG8sGQgqgKff2swXo2AACEIYQ Content-Language: en-gb Organization: MetaStack Solutions Ltd. X-Scanned-By: MIMEDefang 2.65 on 81.102.132.77 X-Cloudmark-Analysis: v=1.0 c=1 a=WtMADFcyAAAA:8 a=9QnmTfmidXgG8fpxdTMA:9 a=KrMLrC49gkkASHYQWEIe5iWH2pUA:4 a=Rc5NFjuuoNsA:10 X-Spam: no; 0.00; exn:01 exn:01 ocaml:01 compiler:01 garbage:01 closures:01 wrote:01 compilers:01 caml-list:01 oops:01 closure:01 lazy:02 lazy:02 match:02 let:03 rixed@happyleptic.org wrote: > > Oops. > > The following makes it possible for f to be garbage-collected: > > ...? > Because the fact that the fun calls f does not count as a reference ? The anonymous function in the second version of Martin's function doesn't "call" [f] (at least directly): imagine if the `None case had been written with [g]s instead: let lz f = let result = ref (`None f) in fun () -> match !result with `None g -> (try let y = g () in result := `Result y; y with e -> result := `Exn e; raise e ) | `Result y -> y | `Exn e -> raise e The [fun] only "calls" (uses) [result]. In the first version, [f] could only be collected after the [fun] had been collected (i.e. when the lazy value itself is collected - not good). In the second version, [f] can be garbage collected if the lazy value has already been forced (meaning that "[result = `Result y | `Exn e]" so there will be no more references to [f]) - a clear optimisation with no performance penalty in terms of space. The optimisation would be irrelevant if the ocaml compiler was lazy and just included all of the scope in the closure for [fun], but minimising (and eliminating, where possible) closures is a critical part of ML compilers (I'd be amazed if it's not covered in all of the books you've mentioned previously and I wish I could remember the name usually given to the problem... but I'm sure someone will chip in with it!) David