From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.6.10/8.6.6) id TAA02425 for caml-redistribution; Mon, 16 Oct 1995 19:31:32 +0100 Received: (from weis@localhost) by pauillac.inria.fr (8.6.10/8.6.6) id TAA02405; Mon, 16 Oct 1995 19:31:07 +0100 From: Pierre Weis Message-Id: <199510161831.TAA02405@pauillac.inria.fr> Subject: Re: streams and lazy evaluation To: cheno@micronet.fr (Laurent CHENO) Date: Mon, 16 Oct 1995 19:31:05 +0100 (MET) Cc: caml-list@pauillac.inria.fr In-Reply-To: from "Laurent CHENO" at Oct 15, 95 10:41:14 am MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: weis > #let rec from n = [< 'n ; from (n+1) >] ;; > from : int -> int stream = > #let rec version2 flot = match flot with > [< 'n ; version2 reste >] -> [< '(2*n) ; reste >] ;; > version2 : int stream -> int stream = > #version2 (from 0) ;; > Uncaught exception: Out_of_memory This is due to the evaluation strategy. You force the evaluation of the lazy thunk by your pattern matching, since to prove that your stream can be matched by the pattern: [< 'n ; version2 reste >] the compiler must generate code to call version2 on the tail of the stream. This falls in an infinite loop. In contrast, in your first version, the recursive call to version is in the right hand-side of the clause, when you build the final result. This way it can be left unevaluated, until some piece of code has to do some pattern matching on this stream result. > #let rec version1 flot = match flot with > [< 'n >] -> [< '(2*n) ; version1 flot >] ;; > version1 : int stream -> int stream = To sumarize: stream construction leads to lazyness, while stream pattern matching leads to unfrozing. Pierre Weis ---------------------------------------------------------------------------- WWW Home Page: http://pauillac.inria.fr/~weis Projet Cristal INRIA, BP 105, F-78153 Le Chesnay Cedex (France) E-mail: Pierre.Weis@inria.fr Telephone: +33 1 39 63 55 98 Fax: +33 1 39 63 53 30 ----------------------------------------------------------------------------