Otherwise operator with erroneous predicate

Discussions related to Visual Prolog
Martin Meyer
VIP Member
Posts: 289
Joined: 14 Nov 2002 0:01

Otherwise operator with erroneous predicate

Unread post by Martin Meyer » 3 Aug 2019 12:19

Hello Thomas,

please check this code (in build 902). The compiler refuses it (unintentionally?). But it compiles when the indicated line is outcommented:

Code: Select all

    open core, exception   constants     myException : exception = exception(class_name(), constant_name(), "Error").   class predicates     raise_myException : (integer Value) erroneous [programPoint].     raise_myException : (string Value) erroneous [programPoint]. %try outcommenting this line   clauses     raise_myException_explicit(ProgramPoint, Value) :-         raiseDetailed_explicit(ProgramPoint, myException, [namedValue("Value", string(string::present(Value)))]).   class predicates     tryGetValue : (integer Input) -> integer Output determ. clauses     tryGetValue(1) = 1.   class predicates     getValue : (integer Input) -> integer Output. clauses     getValue(Input) = tryGetValue(Input)         otherwise raise_myException(Input). %throws error c502: The term does not produce a value   clauses     run() :-         stdIO::write(getValue(1)).
Regards Martin

User avatar
Thomas Linder Puls
VIP Member
Posts: 1622
Joined: 28 Feb 2000 0:01

Re: Otherwise operator with erroneous predicate

Unread post by Thomas Linder Puls » 5 Aug 2019 11:01

It seems that something has only been implemented as a special case. But I do not recall the history around about this.

Anyways, in principle the error is the correct behavior in all cases, because the predicates you call do actually not return values, and can therefore not be the second arguments of 'otherwise'.

I.e. to be fully type correct you should change your code like this:

Code: Select all

class predicates     raise_myException : (integer Value) -> AnyType erroneous [programPoint].     raise_myException : (string Value) -> AnyType erroneous [programPoint].   clauses     raise_myException_explicit(ProgramPoint, Value) = _ :-         raiseDetailed_explicit(ProgramPoint, myException, [namedValue("Value", string(string::present(Value)))]).
Now the predicates are declared to return a value, and can therefore be used as second argument to 'otherwise'.

It is however annoying to have to declare and implement exception raising predicates both as non-value returning and as value returning.

So as a special case otherwise accept non-value returning erroneous predicate calls as second argument, but for some unknown reason only if the predicate has exactly one declaration (i.e. does not have any overloading).

In future we will change the (type) semantics of erroneous predicates so that they can both be used as non-value returning and as value returning.

I.e. in future a declaration like this:

Code: Select all

class predicates     raise_myException : (integer Value) erroneous [programPoint].
imply this implicit overloading:

Code: Select all

class predicates     raise_myException : (integer Value)  erroneous [programPoint]. % explicit     raise_myException : (integer Value) -> _ erroneous [programPoint].  % implicit
Only the non-value returning (i.e. explicit) predicate should be implemented.
Regards Thomas Linder Puls
PDC

Martin Meyer
VIP Member
Posts: 289
Joined: 14 Nov 2002 0:01

Re: Otherwise operator with erroneous predicate

Unread post by Martin Meyer » 5 Aug 2019 19:21

Yes. Thank you for the detailed explanation.
Regards Martin

Post Reply