[mercury-users] Impurity and foreign language calls

doug.auclair at logicaltypes.com doug.auclair at logicaltypes.com
Wed Mar 15 09:04:00 AEDT 2006


Dear all,

I'm interacting with a third-party C-API, which has me declaring
the Mercury proxies as impure, where I think the declaration is
meritted.

The problem is that impurity seems to taint the callers, too, and
I'm wondering how to limit this damage.

For example, let's say I have a digital camera with the C-API
to control the hardware (taking and storing the image, firing
the flash) and to read the battery power remaining.

:- impure func battery_power = float.

is the proxy to the C-API call.  Is this correctly declared
impure?  I get a different answer each time I call it, depending
on previous explicitly unrelated (but implicitly related)
calls (take_picture, discharge_flash).

If battery_power must be impure, is there a way to make
callers to it pure?  For example, I have:

:- type battery_state ---> battery_state(availibility, min_charge).
:- type availibility ---> ready; recharging.
:- type min_charge ---> minimum(float).

:- impure pred update_battery_state(battery_state, battery_state).
:- mode update_battery_state(in, out) is det.
update_battery_state(battery_state(_, minimum(X)), NewState) :-
  impure Power = battery_power,
  (Power > X -> Readiness = ready; Readiness = charging),
  NewState = battery_state(Readiness, minimum(X)).

Is update_battery_state pure, even though it calls battery_power?
If not, would writing a C wrapper to the external calls that
embed battery charge into the called and returned values (state
variables for C, as it were), and have that wrapper make the
battery power call internally, would that C wrapper (and other
like-kind C wrappers for take_picture and discharge_flash) then
be pure?

Sincerely,
Doug Auclair

--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list