Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between functor, a fact, a predicate and a rule in Prolog?

I would like to know the difference between those terms :

  1. facts
  2. functor
  3. predicate.
  4. rule

in Prolog.

if I write: brother(john, jack).
is that a fact? or a predicate?

like image 260
John Sall Avatar asked Nov 12 '18 16:11

John Sall


People also ask

What is fact and rule in Prolog?

We can define rule as an implicit relationship between objects. So facts are conditionally true. So when one associated condition is true, then the predicate is also true. Suppose we have some rules as given below −

What is functor in Prolog?

functor, functor In Prolog, the word functor is used to refer to the atom at the start of a structure, along with its arity, that is, the number of arguments it takes. For example, in likes(mary, pizza) , likes/2 is the functor.

What is a predicate in Prolog?

A Prolog program consists of predicate definitions. A predicate denotes a property or relationship between objects. Definitions consist of clauses. A clause has a head and a body (Rule) or just a head (Fact). A head consists of a predicate name and arguments.

Is fact a predicate Prolog?

A fact is a predicate expression that makes a declarative statement about the problem domain. Whenever a variable occurs in a Prolog expression, it is assumed to be universally quantified. Note that all Prolog sentences must end with a period.


2 Answers

To address your given example:

brother(john, jack).
   ^      ^     ^
functor   |     |
      argument  |
            argument
\________  ________/
         \/
   fact/predicate

brother/2 is a predicate AND a fact as well (see 3.72 fact in @GuyCoder's quote from the standard) since you can write it as a clause with a body that consists of the single goal true:

brother(john, jack) :-  % <- head of the clause
   true.                % <- body of the clause

The predicate brother in your example has two arguments, therefore the arity 2 and is referred to as brother/2 (see 3.129 predicate in @GuyCoder's post). The name or identifier of the predicate brother/2 is also called the functor of the predicate (see 3.77 functor; note that 3.77 and 3.129 use the same definition). You can think of a fact as a special kind of predicate that you can define without rules.

If you had some facts parent_of/2 and male/2 and defined a predicate brother_of/2 based on those, e.g...

brother_of(X,Y) :-           % X is brother of Y if
   dif(X,Y),                 % X and Y are different AND
   male(X),                  % X is male AND
   parent_of(P,X),           % X has a parent P AND
   parent_of(P,Y).           % Y has the same parent P

... then the above definition constitutes a rule because the body of the clause is not the goal true (see 3.154 rule). The rule above consists of the following parts:

brother_of(X,Y) :-     % head of the rule
   dif(X,Y),           % goal  \
   male(X),            % goal   \  body of
   parent_of(P,X),     % goal   /  the rule
   parent_of(P,Y).     % goal  /

The head of the rule is followed by :- which is an implication arrow pointing towards the head of the rule and the goals are separated by , which is a logical AND (conjunction). So the body of a rule consists of a single goal or a conjunction of goals and the body of a rule implies the head of the rule. Hence you can read the above definition of brother_of/2 as a logic formula:

brother_of(X,Y)dif(X,Y)male(X)parent_of(P,X)parent_of(P,Y)

If you come from mathematical logic you might find it helpful to recall that a predicate is defined as a boolean-valued function, that is, a function that maps its arguments to true or false. So a predicate is the characteristic function of a relation (see Predicate (mathematical logic). Since you can query facts and get true/false as an answer, they constitute predicates. You can observe this when you query your definition of brother/2:

?- brother(john,jack).
true.                      % <- maps to true

?- brother(john,jason).
false.                     % <- maps to false

If you issue queries with variables, you get substitutions for said variables that make the predicate true instead of the answer true, e.g.:

?- brother(X,Y).
X = john,                  % these substitutions for X and Y
Y = jack.                  % map to true

A final remark: When talking about Prolog the terms predicate and relation are often used interchangeably and it is quite helpful to think about writing predicates as describing relations (see the comments in the definition of brother_of/2 above). Hence, for the above queries it is also appropriate to say: The relation brother(john,jack) holds. The relation brother(john,jason) does not hold. The relation brother(X,Y) holds for the substitutions X=john and Y=jack.

like image 192
tas Avatar answered Oct 09 '22 19:10

tas


From

ISO/IEC 13211-1 First edition 1995-06-01
Information technology - Programming languages - Prolog -
Part 1:
General Core

3.9 arity: The number of arguments of a compound term. Syntactically, a non-negative integer associated with a functor or predicate.

3.19 body: A goal, distinguished by its context as part of a rule (see 3.154).

3.32 clause: A fact or a rule. It has two parts: a head, and a body.

3.37 compound term: A functor of arity N, N positive, together with a sequence of N arguments.

3.72 fact: A clause whose body is the goal true.
NOTE - A fact can be represented in Prolog text by a term whose principal functor is neither (:-)/1 nor (:-)/2.

3.77 functor: An identifier together with an arity.

3.81 goal: A predication which is to be executed (see body, query, and 7.7.3).

3.84 head (of a rule): A predication, distinguished by its context.

3.88 identifier: A basic unstructured object used to denote an atom, functor name or predicate name.

3.129 predicate: An identifier together with an arity.

3.133 predication: A predicate with arity N and a sequence of N arguments.

3.143 query: A goal given as interactive input to the top level.

3.154 rule: A clause whose body is not the goal true. During execution, if the body is true for some substitution, then the head is also true for that substitution. A rule is represented in Prolog text by a term whose principal functor is (:-)/2 where the first argument is converted to the head, and the second argument is converted to the body.

So brother(john, jack). is a fact.

like image 35
Guy Coder Avatar answered Oct 09 '22 21:10

Guy Coder