users-prolog
[Top][All Lists]

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

 From: emacstheviking Subject: Re: Some advice on a "simple" thing... Date: Thu, 10 Apr 2014 12:02:02 +0100

Just to close this off for me personally, am I correct iin my understanding that a term such as this:

something( 100, arg(int,100), string(c_style, "Hello")).

has a "primary" (principle?!) functor of something/3 and that the arguments are JUST terms i.e. they arg/2 and string/2 are not evaluated they are just lxed,parsed and stored as Prolog terms and that if I wanted to actually have them execute during the course being processed that I would have to use call/1 to make that happen.

That's how I understand it. It is a bit like quoting a list in Lisp, the list is not executed but stored as is.

If that is correct, I feel much better about how I understand Prolog. Somtimes I can't beleive I actually wrote that Redis collection but it just goes to show that you only need to know and understand "just enough" to actually amke something work but for me the real enjoyment is pushing your understanding down into eveyr nook and cranny of a language!

Thanks again,
Sean.

On 10 April 2014 08:26, Daniel Diaz wrote:
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.