Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Has laziness changed type?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-09-09 (14:50)
From: Alessandro Baretta <alex@b...>
Subject: Re: [Caml-list] Has laziness changed type?

Xavier Leroy wrote:
> An abstract type.  You don't want to know :-)  More seriously: in
> 3.06, the compiler and runtime system represent lazy values more
> efficiently; in particular, the "Value of" indirections present in
> 3.04 are now shortened by the GC whenever possible.  As a consequence,
> the representation of lazy values no longer matches that of a Caml datatype.


>>Can I apply pattern-matching on it?
> No.  The general "contract" of a lazy value is that you should never
> have to distinguish whether it's been evaluated already or not.  Just
> perform Lazy.force on the lazy value and match on the result.
> - Xavier Leroy

This is a pity, in a way, but not really a big problem. I 
often need to check whether a given lazy value corresponds 
computes a meaningful value or raises an exception. To do 
this I had code like the following

let foo = lazy ( bar () )

let _ = try ignore (Lazy.force foo) with _ -> () in
match foo with Value(x) -> ...
Exception(x) -> ...

This is not terribly useful when you have to match against 
only one lazy value, but the situation is different when you 
have a tuple of lazy values, and need to perform different 
actions depending on which subset of them computes a 
meaningful value.

I solved my problem by reworking the code. It was not too 
much effort after all, but my code lost its former elegance:
match foo, bar, doh with
| Value(foo), _, _ -> ...
| _, Value(bar), _ -> ...
| _, _, Value(doh) -> ...


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: