[m-dev.] for discussion: stream library v2
Fergus Henderson
fjh at cs.mu.OZ.AU
Thu Sep 28 15:58:54 AEDT 2000
On 25-Sep-2000, Michael Day <mcda at students.cs.mu.oz.au> wrote:
>
> > * you have a non-unique handle on a stream which means that you can
> > still process that stream after an exception.
>
> Is it really not possible to achieve this by some extension of try_io:
>
> :- pred try_io(pred(T, S, S), exception_result(T), S, S) <= stream(S).
Oh, now I remember what the problem with that was.
The problem is that when the exception is thrown, the
value currently in the store may be completely invalid --
there is no guarantee in general that it even has the right type.
Consider the following code:
:- type foo ---> foo(bool, bool).
:- type bar ---> bar(int, int).
:- instance stream(foo) where [...].
:- pred main(state::di, state::uo) is det.
main -->
{ Foo0 = foo(no, no) },
{ try_io(update_foo_stream, R, Foo0, Foo) },
print("R = "), print(R), nl,
print("Foo = "), print(Foo), nl.
:- pred update_foo_stream(foo::di, foo::uo) is det.
update_foo_stream(Foo0, Foo) :-
Foo0 = foo(Bool, _),
Bar0 = bar(42, if Bool = yes then 10 else 20),
Foo = f(Bar0).
:- func f(bar::di) = (foo::uo) is det.
f(_) = _ :- throw("f/2 not yet implemented").
Here the compiler's structure reuse optimization may decide to
reuse the foo/1 cell to hold the bar/1 value in update_foo_stream/2.
So when the exception is thrown, the value of the fields pointed to by
`Foo0' will not even have the correct values for `bool'. Thus the
call `print(Foo)' may crash.
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3 | -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list