[mercury-users] function types + impure impurity

David Overton dmo at cs.mu.OZ.AU
Wed Mar 7 08:08:43 AEDT 2001


On Tue, Mar 06, 2001 at 03:19:43PM +0100, Dominique de Waleffe wrote:
> I wanted to define f0 as a function of  no arguments returning
> something: 
> 
> :- func f0=(data::out).
> f0=DS:-
> 	DS=mctime__current_date_string.
> 
> and wanted to store said function (unevaluated) in a data structure,
> 
> :-type data ->
>        data(int)
>        ; closure(func=data).
> 
> This did not work (strange messages about unknown type '='	 
> 
> I then tried to define a type:
> :-type funarg == (func=data).
> :-type data ->
>        data(int)
>        ; closure(funarg).
> 
> It did not work either same strange message.
> 
> When I made it as f1 below (added an argument, ignored), I could
> :-type funarg == (func(data)=data).
> 
> So what's the syntax to use for a function of zero argument?

You need to put parentheses around the word 'func'.  I.e.

	:-type data ->
	       data(int)
	       ; closure((func)=data).
	
should work.

> 
> 
> Then, of course I wanted to make f1 impure as it really should...
> 
> As in 
> :-impure func f1(T::in)=(data::out).
> f1(_)=DS:-
> 	DS=mctime__current_date_string.
> 
> At some spot, I build a data structure which needs to store the
> function pointer in a data structure, for evaluation at a later time.
> 
> add_other_dynamic_keys(args):-
> 	<stuff>
> 	add_dico_entry(Key,def(dynamic(f1)),Dico0,Dico),
> 	<more stuff>
> 
> To me, there is nothing impure here since the function is not
> evaluated,  however the compiler reports:
> 
> dispatcher.w:2934: In call to impure function `ndictionnaire:f1/1':
> dispatcher.w:2934:   purity error: call must be in an explicit unification
> dispatcher.w:2934:   which is preceded by `impure' indicator.

AFAIK, the current implementation doesn't allow impure closures.  What
you could do is make f1 a pure predicate with an io__state pair.  Then
it can be placed in a data structure.  When you extract it from the
data structure and want to call it, if you don't have an io__state
around, use the impure 'unsafe_perform_io'.

> 
> Then of course it thinks the caller is impure, ....
> 
> dispatcher.w:2821: In predicate `ndictionnaire:add_other_dynamic_keys/2':
> dispatcher.w:2821:   purity error: predicate is impure.
> dispatcher.w:2821:   It must be declared `impure' or promised pure.
> 
> 
> Of course, the mctime__current_date_string should also have been
> marked as impure, because it really is.... but since it is not,
> compiler thinks (and warns that f0, f1) are pure even though declared
> impure... 
>

Yes, you are right.  If mctime__current_date_string is impure then it
should be marked as impure.


David
-- 
David Overton      Department of Computer Science & Software Engineering
PhD Student        The University of Melbourne, Victoria 3010, Australia
+61 3 8344 9159    http://www.cs.mu.oz.au/~dmo
--------------------------------------------------------------------------
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