PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

[FIXED] What is the difference between the keyword is and = in prolog?

 July 09, 2022     keyword, operators, prolog     No comments   

Issue

I'd appreciate it if someone could elaborate on the difference between the is keyword and the = operator in prolog. I saw this discussion in == and =, but it excludes is. The documentation talks about an unclear to me "unbound left operand." Can anyone elaborate?

I have a following example of is:

age(Person,X) :-
birth_year(Person,Y1),
current_year(Y2),
X is Y2-Y1. 

Is the difference assignment vs comparison? Any help is appreciated!

Edit: What is the relationship between == and is? I am not asking the relationship of == and =, unless I have a misunderstanding of the aforementioned relationship.


Solution

As usual, a bit of poking around helps:

?- X = 2 + 1. % unify X with 2 + 1
X = 2+1.

?- X = 2 + 1, write_canonical(X). % how does Prolog see X?
+(2,1)
X = 2+1.

?- is(X, +(2,1)). % evaluate the term +(2,1) as an arithmetic expression
                  % and unify X with the result
X = 3.

The point about X being a free variable is that since the result of the arithmetic expression is unified with it, you might get surprises when the terms are not the same even though the arithmetic expression seem as if they should be:

?- 1+2 is 2+1. % Evaluate 2+1 and try to unify with +(1,2)
false.

?- 1 is (1.5*2)-2. % Evaluates to 1.0 (float), unify with 1 (integer)
false.

?- 1+2 =:= 2+1.
true.

?- 1 =:= (1.5*2)-2.
true.

And please keep in mind that both =/2 and is/2 are predicates. They can also be just atoms, so they can also be names of functors. Both happen to be declared as operators. I don't think either should be called a "keyword".



Answered By - user1812457
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing