milvala
milvala

Reputation: 311

Prolog: syntactically correct objects + relation vs. structure

I'm new to Prolog and gradually working my way through Ivan Bratko's 'Prolog Programming for Artificial Intelligence' (4th ed.).

When doing exercise 2.1. (page 39), I fail to see why 5(X,Y) is syntactically not correct, while +(north,west) is correct. At first I thought it had to do with the fact that the functor is a number (as I thought this could only be an atom), but according to the book, an atom can also be a digit. I then thought it was a problem with the arguments being variables, but I fail to see why this would pose a problem for the syntactical correctness. Could anyone point me in the right direction of an explanation?

Furthermore I was wondering what the exact difference is between a relation and a structure. While date(1, may, 2001) (on page 35) is seen as a structure, I was wondering if we can say the same about earlier examples from chapter 1. Is, for example, the relation parent(pam,bob) from the first chapter also a so-called structure, or am I confusing things?

Thanks in advance.

Upvotes: 2

Views: 251

Answers (1)

Guy Coder
Guy Coder

Reputation: 24976

I fail to see why 5(X,Y) is syntactically not correct, while +(north,west) is correct.

A single digit is not an atom, but a number.

On page 33 section 2.1.1 Atoms and numbers it reads:

Atoms can be constructed in three ways:
1. Strings of letters, digits and the underscore character,  
     starting with a lower-case letter.
2. Strings of special characters such as + - * / < > = : . & _ ~
3. Strings of characters enclosed in single quotes.

The character 5

  • fails rule 1 because the string does not start with an underscore character or a lower-case letter
  • fails rule 2 because it is not one of the special characters
  • fails rule 3 because it is not enclosed in single quotes

The character + succeeds because by rule 2 it is a special character.

One way to explore this further is by using SWI-Prolog functor/3

?- functor(+(north,west),Name,Arity).
Name =  (+),
Arity = 2.

?- functor(5(X,Y).
ERROR: Syntax error: Operator expected
ERROR: functor(
ERROR: ** here **
ERROR: 5(X,Y) .

what the exact difference is between a relation and a structure

On page 4 Section 1.1 Defining relations by facts

In general, a relation is defined as the set of all its instances. For example parent(tom,bob) is a particular instance of the parent relation. With other instances being:

parent(pam,bob)
parent(tom,liz)
parent(bob,ann)
parent(bob,pat)
parent(pat,jim)

On page 35 Section 2.1.3 Structures

Structured objects (or simply structures) are objects that have several components, e.g.

date(1, may, 2001)
point(1,1)
seg(P1,P2)
seg(point(1,1),point(2,3))
(a + b) * (c - 5)           % uses infix operator
*(+(a,b),-(c,5))            % uses prefix operator

Looking at your question makes me think you are looking for a property common to both relation and structure that can be used to distinguish one from the other. I don't know of one; when I think about using Prolog I don't think should I use a relation or a structure, I think of how is the data structured which can be as simple as a number or more complex as a structure or a structure of structures, and then what are the predicates (rules) that are needed to reach a goal given the data. In looking at the definitions given the best I can say is that a specific relation can be composed of many structures that have the same functor, but that is not an idea that will last in my mind past this period.

Upvotes: 3

Related Questions