Version française
Home     About     Download     Resources     Contact us    
Browse thread
Lazy.force
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Andrej Bauer <Andrej.Bauer@f...>
Subject: Re: [Caml-list] Lazy.force
Jon Harrop wrote:
> Could the type checker automatically insert Lazy.force whenever a lazy 
> expression is used as an eager expression?

Consider:

let rec repeat n x =
   if n = 0 then [] else x :: (repeat (n-1) x)

let rec sum = function [] -> 0 | x::xs -> x + sum xs

let cow = sum (repeat 1000 (lazy 3))

The typechecker will determine that

repeat : int -> 'a -> 'a list

sum : int list -> int

In the definition of cow it will see that sum, which expects int list, 
is applied to int lazy_t list. Where and how should the compiler insert 
Lazy.force in this example? Note that it cannot insert anything in the 
definition of sum, because later on it might see

let chicken = sum [1;2;3;4]

Best regards,

Andrej