Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AI and Deep Learning by (50.2k points)

I am doing an exercise from 'Prolog Programming for Artificial Intelligence' by Ivan Bratko. The exercise says:

Define the operators 'if', 'then', 'else' and ':=', so that the following becomes a legal term:

if X > Y then Z := X else Z := Y

Choose the precedences so that 'if' will be the principal functor.

I am having trouble determining out of the operators 'then' and 'else', which one should have the lower precedence (and bind stronger). My answer to this question was:

:- op(900, fx, if).

:- op(800, xfx, else).

:- op(700, xfx, then).

:- op(600, xfx, :=).

(It is also stated in the book that the '>' operator has precedence of 700).

I thought that 'then' would bind stronger than 'else', however the answer for this exercise states otherwise:

:- op(900, fx, if).

:- op(800, xfx, then).   

:- op(700, xfx, else).  

:- op(600, xfx, :=).

I am not sure of the rationale behind making 'else' have lower precedence than 'then'. Any insights are greatly appreciated.

1 Answer

0 votes
by (107k points)

There is no particular correct answer for this question since the answer will vary based on how the definition is parsed. As to correctly parse "if...then...else...", you need to define ternary operators, which are not available in Prolog. So, applying infix and prefix operators, you at best get some ad hoc approximation, which will typically get some cases wrong.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...