Discussions related to Visual Prolog
Peter Muraya
VIP Member
Posts: 147
Joined: 5 Dec 2012 7:29

The parameter 'Max' is bound in the context

Unread post by Peter Muraya »

Hi
This error, The parameter 'Max' is bound in the context, surprised me in this code that I thought was valid. I solved it by re-writing it as commented, but I am still not sure why the first version raised the error.

Code: Select all

implement main     open core   /* Returns the string/frequency pairs that have the highest frequency*/ class predicates     get_max_pairs:()->tuple{string Term, integer Frequency}*. clauses     get_max_pairs()=MaxOccurences:-          Occurences = [tuple("str", 20), tuple("int", 10), tuple("bool", 5), tuple("real", 20)],          /*          Get the maximum frequency*/          Max = list::maximum([Frequency || tuple(_, Frequency) in Occurences]),          /*          Select all the strings with the highest frequency*/          MaxOccurences = list::filter(Occurences, {(tuple(_, Max))}).   /*Max raises an error here*/          %MaxOccurences = list::filter(Occurences, {(tuple(_, X)):-X=Max}).       run() :-         console::write(get_max_pairs()).   end implement main   goal     console::runUtf8(main::run),     console::readchar()=_.
Mutall Data Management Technical Support
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

It is due to a design choice we have made. The purpose is to "protect" the programmer from bugs caused by accidental reuse of a variable name.

Consider the anonymous predicate:

Code: Select all

..., { (X) :- ... }, ...
If X is used before the anonymous predicate it is actually a bound variable and the anonymous predicate would/should perform a test. If the test is intended (as it is in your case) there is obviously no problem. But if the intension was actually to have a (plain/fresh) input parameter to the anonymous predicate then the result will be a bug. So to avoid bugs coming from accidental reuse of a variable name we have decided that all variables used in the parameter list of an anonymous predicate must be free. If you really need the test it can easily be moved to the body of the anonymous predicate as you have also figured out.
Regards Thomas Linder Puls
PDC
Peter Muraya
VIP Member
Posts: 147
Joined: 5 Dec 2012 7:29

Unread post by Peter Muraya »

Thanks. That makes sense.
Mutall Data Management Technical Support
Post Reply