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=1.0 required=5.0 tests=AWL,SPF_FAIL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 23375BC69 for ; Tue, 20 Mar 2007 22:53:05 +0100 (CET) Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l2KLr4px020342 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Tue, 20 Mar 2007 22:53:04 +0100 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1HTmG5-00009K-N7 for caml-list@inria.fr; Tue, 20 Mar 2007 22:52:45 +0100 Received: from ivr94-8-88-162-26-239.fbx.proxad.net ([88.162.26.239]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 20 Mar 2007 22:52:45 +0100 Received: from li by ivr94-8-88-162-26-239.fbx.proxad.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 20 Mar 2007 22:52:45 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Zheng Li Subject: Re: possible way to improve "lazy" Date: Tue, 20 Mar 2007 22:53:43 +0100 Message-ID: <87zm67vaxk.fsf@pps.jussieu.fr> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: ivr94-8-88-162-26-239.fbx.proxad.net User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.95 (gnu/linux) Cancel-Lock: sha1:UHy8xksyYx/OZsCh6Wce6flkOiw= Sender: news X-Miltered: at discorde with ID 460057C0.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; semantics:01 semantics:01 stack:01 ocaml:01 cbv:01 model:01 model:01 ocaml:01 -rectypes:01 camlp:01 wheels:98 rec:01 rec:01 clearer:01 writes:01 Hi, "Jeffrey Loren Shaw" writes: > let cons a b = Cons (lazy a, lazy b) Note that there is nothing wrong with this expression, it has valid semantics, just not the semantics you want. > After a while I realized that there is a stack overflow because calling cons > evaluates its arguments *before* making them. This causes the infinite loop. Yes, OCaml use eager (strict) evaluation, which is CBV by default. If you want lazy, you must make it explicit, and more important, explicit it before any thing happens -- the first "anything" is the evaluation of arguments not the function call. > Now suppose we have a function like the above cons. Because the b argument is > never used in an eager way in the function, couldn't the interpreter say "oh, b > should not be evaluated"? This would allow the first example of fib work, which > looks like it should work unless you know what's going on behind the scenes. I'm afraid that's not easy, at least when talking about a universal solution. It's the base of evaluation model. Your proposal means the model must be intelligent enough to know what should be evaluated eagerly what should be evaluated lazily. > The fix for now is to define cons as > let cons a b = Cons (a,b) > let fib = > let rec aux a b = > cons (lazy a) (lazy (aux b (a+b))) > in > aux 0 1 Maybe this way is clearer: $ ocaml -rectypes # type 'a t = 'a * 'a t Lazy.t;; # let fib = let rec aux a b = a, (lazy (aux b (a+b))) in aux 1 2;; and you don't reinvent wheels, there's a nice Stream module in OCaml $ ocaml camlp4o.cma # let fib = let rec aux x y = [< 'x; aux y (x + y) >] in aux 1 2;; Regardz -- Zheng Li http://www.pps.jussieu.fr/~li