From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA06471; Sat, 6 Jul 2002 17:26:37 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA06091 for ; Sat, 6 Jul 2002 17:26:36 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id g66FQYf25177; Sat, 6 Jul 2002 17:26:34 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA06532; Sat, 6 Jul 2002 17:26:33 +0200 (MET DST) From: Pierre Weis Message-Id: <200207061526.RAA06532@pauillac.inria.fr> Subject: Re: [Caml-list] yet another question on lazy lists In-Reply-To: <200207060148.g661mZp06460@orchestra.cs.caltech.edu> from Michael Vanier at "Jul 5, 102 06:48:35 pm" To: mvanier@cs.caltech.edu (Michael Vanier) Date: Sat, 6 Jul 2002 17:26:33 +0200 (MET DST) Cc: caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk [...] > # let rec fibs2 = > stream_cons 0 (stream_cons 1 (add_streams (stream_tl fibs2) fibs2)) > ------------------------------------------------------------------- > ;; > This kind of expression is not allowed as right-hand side of `let rec' > > What does this mean? My best guess is that ocaml sees that you're defining > a value in terms of itself (like e.g. "let rec a = 2 * a") and won't allow > it, but that doesn't explain why the lazy version works. It means right-hand side of `let rec' expressions are submitted to conditions in order to be able to compile the recursively defined value. Roughly speaking, you cannot use expressions that contain function calls, variables, or constants that are not under an explicit (i.e. syntactic) function abstration or arguments of a construtor application. Hence, neither let rec x = x nor let rec x = 1 is allowed. Hence, all recursive definitions of the form let rec f = function x -> .... are legal. On the other hand, constructor applications are allowed (since those are not function calls per se., and recursive values defined with constructors are easy to compile). For instance, # let rec x = 1 :: x;; val x : int list = [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; ...] This explains why replacing a constructor application by a function call may turn a legal recursive definition into an illegal one. This also explain why your previous ``lazy'' definition worked. > Does this mean > that my "stream_cons" function is useless? > > Thanks in advance, > > Mike No. It means you cannot always use it in place of a constructor application. Note that a similar phenomenon occurs for pattern matching: constructors are mandatory, you cannot subtitute them by equivalent function calls! Best regards, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners