Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Alessandro Baretta <alex@b...>
Subject: Re: [Caml-list] Has laziness changed type? (with a plea to Xavier...)


John Prevost wrote:
>>>>>>"ab" == Alessandro Baretta <alex@baretta.com> writes:

> 
>     ab> This is not terribly useful when you have to match against
>     ab> only one lazy value, but the situation is different when you
>     ab> have a tuple of lazy values, and need to perform different
>     ab> actions depending on which subset of them computes a
>     ab> meaningful value.
> 
> What's wrong with:
> 
> try (* do something with *) (Lazy.force foo) with _ ->
> try (* do something with *) (Lazy.force bar) with _ ->
> try (* do something with *) (Lazy.force doh) with _ ->
> (* fallback code *)

This control structure happens to match perfectly the 
example I gave, but it is not as general. What If you want 
to to match conditions where two-out-of-three compute a 
value? What if the action you take also depends on the 
actual exception raised by the third? There are a host of 
examples where a patterm matching would be marvellously 
clear and concise, that you cannot easily convert to a 
number of nested try-with expressions.

> or
> 
> let lf x = try Some (Lazy.force x) with _ -> None
> 
> match (lf a, lf b, lf c, lf d) with
>   ...

Yes. This is basically my solution. It adds a little 
"background noise" in the tuple expression being matched. No 
big deal really.

> or even
> 
> type 'a result = Value of 'a | Exception of 'a
> 
> let lf x = try Value (Lazy.force x) with e -> Exception e

Ok. This is perfect. It just takes a couple more lines of 
code and one extra function application per tuple position. 
This is what I meant when I stated I had to rework my code a 
little.

> 
> The change to the lazy datatype means you have to do a little extra
> effort if you want to maintain this kind of information.  But it's not
> really a huge deal.
> 
> John.

Right, no big deal really. And if it's done for the sake of 
efficiency, then welcome Xavier's "purple magic". Might I 
just make a plea for the following library function in the 
Lazy module?

type 'a forced = Value of 'a | Exception of exn
let eval susp =
  try Value(force susp) with ex -> Exception(ex)

Alex

-------------------
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