users-prolog
[Top][All Lists]

## Re: Some advice on a "simple" thing...

 From: Daniel Diaz Subject: Re: Some advice on a "simple" thing... Date: Thu, 10 Apr 2014 09:26:17 +0200 User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0

```Hi Sean and Lindsey,

some more explanations... In case of...

Le 09/04/2014 22:44, Sean Charles a écrit :
```
```        foo(a - b, c + d, e = f) is semantically equivalent to
foo(-(a, b), +(c, d), =(e,f))
'-(a,b)' does not have a particular interpretation in Prolog, it's meaning is
determined (in this case) by what 'foo' does with it and what other clauses
referencing 'foo' do with it.
```
```OK, so the “traditional” operators are defined but they are given no special
meaning unless used with “is”. Correct??
```
```
```
As Lindsey explained, operator notation is just syntactic sugar. Internally you just have compound terms (structures) composed of : 1 functor (must be an atom) and N arguments which are Prolog terms (N is called the arity). For instance foo(zoe,1) is a structure of arity 2 whose functor is the atom foo and the 2 arguments are zoe and 1. We use the notation foo/2 to speak about a structure independently of the arguments.
```
```
Operators can be defined (some are predefined) to ease the interaction with the user (and to make programs more readable). So operators are only used with I/O. When an operator is defined, by default the operator notation is used when the term is written. But you can use the display built-in to prevent this notation. Examples:
```
| ?- write(foo(zoe,1)).
foo(zoe,1)

yes
| ?- write(=(zoe,1)).
zoe=1

yes
| ?- display(=(zoe,1)).
=(zoe,1)

yes

```
You can also use the operator notation as input (as you can see the results are similar to above):
```
| ?- write(zoe=1).
zoe=1

yes
| ?- display(zoe=1).
=(zoe,1)

You can also define your own operator:

| ?- op(400,xfx,foo).

yes
| ?- write(foo(zoe,1)).
zoe foo 1

yes
| ?- display(foo(zoe,1)).
foo(zoe,1)

yes

```
```This is how arithmetic operations are defined using the 'is' builtin. The 'is'
builtin gets a nested function term that uses functors that it interprets as
arithmetic operations:
X is 3 + 4 *  2 * 7.
is the same as
X is +(3, *(4, *(2, 7))).
The 'is' builtin applies common arithmetic processing to this nested structure
to unify a numeric value 59 with X.
```
```Within the context of “is”, does the functor know what it has to do with its
arguments or is it the “is” processing code the decides to add, subtract etc. ?
I guess that’s an implementation detail and would vary between vendors.
```
NB: Prolog uses a unified syntax both for the programs (ie. the predicates) and the data (arguments of the predicates). This is nice for meta-programming (Prolog programs handling as data other Prolog programs). In the case of
```
X is 3 + 4 *  2 * 7.

which is the same as

X is +(3, *(4, *(2, 7))).

It is also the same as:

is(X, +(3, *(4, *(2, 7)))).

| ?- is(X, +(3, *(4, *(2, 7)))).

X = 59

```
You can now see that the predicate you invoke is called is/2. This is a mathematical predicate which evaluates its second argument (an expression given as a Prolog term) and unifies the result with its first argument.
```
Daniel

--
Ce message a ete verifie par MailScanner
pour des virus ou des polluriels et rien de
suspect n'a ete trouve.

```