[m-dev.] proposal for promise_pure scope warning

Ian MacLarty maclarty at csse.unimelb.edu.au
Tue Apr 15 16:58:36 AEST 2008


On Tue, Apr 15, 2008 at 04:43:25PM +1000, Peter Schachte wrote:
> Ian MacLarty wrote:
>> On Tue, Apr 15, 2008 at 01:50:48PM +1000, Peter Schachte wrote:
>
>>> But the promise still might be correct.  The code might destructively 
>>> modify some data structure in an equivalence-preserving way, eg caching 
>>> the result of a computation.
>> But then why wouldn't the called predicate be declared pure in the first
>> place?
>
> It could be a general impure predicate to update a data structure in place, 
> but it might be used in this context to update a cache in an 
> equality-preserving way.
>

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

?

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.

Even if you use a general impure predicate that takes a closure to
update the cache instead of cache_result/2, you'd still need to put the
promise_pure around the whole body.

Or did you have something else in mind?  If so could you give an
example?

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