* [Caml-list] Has laziness changed type?
@ 2002-09-07 22:24 Alessandro Baretta
2002-09-09 14:23 ` Xavier Leroy
0 siblings, 1 reply; 5+ messages in thread
From: Alessandro Baretta @ 2002-09-07 22:24 UTC (permalink / raw)
To: Ocaml
I think I recall Lazy.t being defined in 3.04 as = Value of
... | Exception of ... | Suspension of ...
or something of the sort. Now Lazy.t is defined simply as
lazy_t. But what *is* lazy_t exactly? Can I apply
pattern-matching on it?
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Has laziness changed type?
2002-09-07 22:24 [Caml-list] Has laziness changed type? Alessandro Baretta
@ 2002-09-09 14:23 ` Xavier Leroy
2002-09-09 14:59 ` Alessandro Baretta
0 siblings, 1 reply; 5+ messages in thread
From: Xavier Leroy @ 2002-09-09 14:23 UTC (permalink / raw)
To: Alessandro Baretta; +Cc: Ocaml
> I think I recall Lazy.t being defined in 3.04 as = Value of
> ... | Exception of ... | Suspension of ...
> or something of the sort. Now Lazy.t is defined simply as
> lazy_t. But what *is* lazy_t exactly?
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
-------------------
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Has laziness changed type?
2002-09-09 14:23 ` Xavier Leroy
@ 2002-09-09 14:59 ` Alessandro Baretta
2002-09-09 15:00 ` John Prevost
0 siblings, 1 reply; 5+ messages in thread
From: Alessandro Baretta @ 2002-09-09 14:59 UTC (permalink / raw)
To: Xavier Leroy; +Cc: Ocaml
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.
Cool!
>>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) -> ...
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Has laziness changed type?
2002-09-09 14:59 ` Alessandro Baretta
@ 2002-09-09 15:00 ` John Prevost
2002-09-09 15:25 ` [Caml-list] Has laziness changed type? (with a plea to Xavier...) Alessandro Baretta
0 siblings, 1 reply; 5+ messages in thread
From: John Prevost @ 2002-09-09 15:00 UTC (permalink / raw)
To: Alessandro Baretta; +Cc: Xavier Leroy, Ocaml
>>>>> "ab" == Alessandro Baretta <alex@baretta.com> writes:
ab> This is a pity, in a way, but not really a big problem. I
ab> often need to check whether a given lazy value corresponds
ab> computes a meaningful value or raises an exception. To do this
ab> I had code like the following
...
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 *)
or
let lf x = try Some (Lazy.force x) with _ -> None
match (lf a, lf b, lf c, lf d) with
...
or even
type 'a result = Value of 'a | Exception of 'a
let lf x = try Value (Lazy.force x) with e -> Exception e
...
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.
-------------------
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] Has laziness changed type? (with a plea to Xavier...)
2002-09-09 15:00 ` John Prevost
@ 2002-09-09 15:25 ` Alessandro Baretta
0 siblings, 0 replies; 5+ messages in thread
From: Alessandro Baretta @ 2002-09-09 15:25 UTC (permalink / raw)
To: Ocaml
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
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2002-09-09 15:17 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-07 22:24 [Caml-list] Has laziness changed type? Alessandro Baretta
2002-09-09 14:23 ` Xavier Leroy
2002-09-09 14:59 ` Alessandro Baretta
2002-09-09 15:00 ` John Prevost
2002-09-09 15:25 ` [Caml-list] Has laziness changed type? (with a plea to Xavier...) Alessandro Baretta
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox