[m-dev.] proposal for promise_pure scope warning

Ian MacLarty maclarty at csse.unimelb.edu.au
Tue Apr 15 19:23:31 AEST 2008


On Tue, Apr 15, 2008 at 06:06:12PM +1000, Peter Schachte wrote:
> Ian MacLarty wrote:
>> Do you mean something like the following:
>> cached_computation(In, Out) :-
>>     ( semipure lookup_value_in_cache(In, Cached) ->
>>         Out = Cached
>>     ;
>>         preform_computation(In, Out),
>>         impure cache_result(In, Out)
>>     ).
>
> Yup.
>
>> Here the promise_pure must go around the whole body.  If you put the
>> promise_pure around the calls to lookup_value_in_cache and cache_result,
>> then the call to cache_result could get optimised away.
>
> Ah, yes, you're right.  As long as the impure pred is det (or cc_multi) and 
> has no outputs, which it usually would in the sort of example I was 
> thinking of.
>
> What about semipure goals?  They'll usually to have outputs and so couldn't 
> be optimised away.  If you remove the impure goal from your example above 
> (assuming some other code updated the cache at times), you'd be justified 
> wrapping a promise_pure around the lookup goal alone, since the code does 
> the right thing no matter in what order the lookup and update are executed.

If you had:

cached_computation(In, Out) :-
    ( promise_pure (semipure lookup_value_in_cache(In, Cached)) ->
        Out = Cached
    ;
        perform_computation(In, Out)
    ).

then yes, you'd probably get the behaviour you expect, however I still
think it's incorrect.  It's incorrect because the goal

    lookup_value_in_cache(In, Cached)

is not pure.  It is not pure because you may get different results for
the same input.  Only the body as a whole is pure, because only that
goal always returns the same results for the same inputs.

Ian.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at csse.unimelb.edu.au
Administrative Queries: owner-mercury-developers at csse.unimelb.edu.au
Subscriptions:          mercury-developers-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the developers mailing list