Page 1 of 1

The parameter 'Max' is bound in the context

Posted: 28 Jul 2015 7:45
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()=_.

Posted: 28 Jul 2015 9:22
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.

Posted: 29 Jul 2015 7:04
by Peter Muraya
Thanks. That makes sense.