[mercury-users] Mercury Modes Problem

Mark Brown dougl at cs.mu.OZ.AU
Fri May 24 15:13:44 AEST 2002


On 24-May-2002, Bob McKay <bob_mckay at mac.com> wrote:
> :- module initiate.
> 
> %=========================================================================
> ======
> :- interface.
> 
> :- import_module random, list, int.
> 
> :- pred init_population(int,pred(P,supply,supply),list(P),
>         supply,supply).
> :- mode init_population(in,in,out,di,uo) is det.
> 
> %=========================================================================
> ======
> :- implementation.
> 
> :- import_module list, int.
> 
> init_population(N,Process,L) -->
>         ({ N =< 0 }
>         ->      { L = [] }
>         ;       (Process(I),
>                 init_population(N - 1,Process,Is),
>                 { L = [I|Is] } ) ).
> 
> initiate.m:029: In clause for `init_population(in, in, out, di, uo)':
> initiate.m:029:   in argument 1 (i.e. the predicate term) of 
> higher-order predicate call:
> initiate.m:029:   mode error: variable `Process' has instantiatedness 
> `ground',
> initiate.m:029:   expecting higher-order pred inst (of arity 3).
> For more information, try recompiling with `-E'.

The mode declaration for init_population/5 says that the variable
'Process' has mode 'in', which means the initial inst is 'ground',
hence the message that "variable `Process' has instantiatedness
`ground'".

However, Process is then used in the higher-order call Process(I),
where it is the "predicate term".  To do this, it needs to have an
inst other than ground -- it needs a higher-order inst.  Specifically,
it needs to have a "higher-order pred inst (of arity 3)".  The way to
achieve this is documented in the reference manual, in the section
"Higher-order modes".

The solution to your problem is to replace the mode 'in' in the mode
declaration with something that reflects the mode of the higher-order
term which will be passed to init_population.  If I understand your
code this should be something like 'in(pred(out, di, uo) is det)'.

> 
> (the errors with -E seem even more incomprehensible).
> 
> On a related note, both the reference manual and the library
> make the statement that
> 
> "These two modes are enough for most functions and predicates. 

Not higher-order predicates though, as your example shows.

> Nevertheless, Mercury's mode system is sufficiently expressive to handle 
> more complex data-flow patterns, including those involving partially 
> instantiated data structures. (The current implementation does not 
> handle partially instantiated data structures yet.)"
> 
> Does this mean what it seems to - that I can't take in a partially
> instantiated data structure, instantiate it further, and pass it
> out?

Yes, that is correct in our current implementation.

Cheers,
Mark.

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