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 RAA22009; Sat, 12 Apr 2003 17:51:51 +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 RAA22259 for ; Sat, 12 Apr 2003 17:51:50 +0200 (MET DST) X-SPAM-Warning: Sending machine is listed in blackholes.five-ten-sg.com Received: from h00045a4799d6.ne.client2.attbi.com (h00045a4799d6.ne.client2.attbi.com [65.96.179.155]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h3CFpnX20614 for ; Sat, 12 Apr 2003 17:51:49 +0200 (MET DST) Received: by h00045a4799d6.ne.client2.attbi.com (Postfix, from userid 500) id 4101C29A12; Sat, 12 Apr 2003 11:54:10 -0400 (EDT) From: Neel Krishnaswami MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <16024.13985.991950.862044@h00045a4799d6.ne.client2.attbi.com> Date: Sat, 12 Apr 2003 11:54:09 -0400 To: "Caml-List" Subject: Re: [Caml-list] [q] on implementing of "memoization" In-Reply-To: <000e01c30091$b66bc580$fe00a8c0@hama> References: <000e01c30091$b66bc580$fe00a8c0@hama> X-Mailer: VM 7.04 under 21.4 (patch 8) "Honest Recruiter" XEmacs Lucid X-Spam: no; 0.00; neel:01 krishnaswami:01 neelk:01 caml-list:01 memoization:01 memo:01 memoize:01 rec':01 'let:01 compiler:01 ocaml:01 rec:01 writes:01 referenced:02 right-hand:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk SooHyoung Oh writes: > > In testing "memoization" of fib function, it is OK on > let rec fib = ... and memo_fib n = memoize fib n;; > but it makes the ERROR on > let rec fib = ... and memo_fib = memoize fib;; > > The error message is > This kind of expression is not allowed as right-hand side of `let rec' > > Why does this error occur? I can't find good description on ocaml > manual. The sorts of values you are allowed to use as the right-hand side of a let rec expression is intentionally restricted, in order to prevent people from being able to ever use an unitialized variable. If random ML code were permitted, then you could write forms like this: let rec x = x There's no sensible value x can be bound to! So there is a restriction that you can only bind the rhs of a let rec to expressions that the compiler can be sure will not lead to users accessing unitialized variables. Currently, there are two classes of those, of which only one is really important.[*] In particular, you can bind them to recursive functions. Eg, let rec fact = fun n -> if n = 0 then 1 else n * fact (n - 1) Here, we can be sure that fact will be defined before use, because it is only referenced inside the body of the function, and the compiler must obviously build the function before it can be called. That's why the first version of your code was accepted, and the second didn't -- you defined a syntactic function expression in the first version, and not in the second. [*] The second legal kind of rhs is a constructor application -- eg, you can write 'let rec ones = 1 :: ones' to get an infinite list of ones. Kind of neat, but the manual is explicit that this is an implementation-specific extension, and might go away. -- Neel Krishnaswami neelk@alum.mit.edu ------------------- 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