|
From: | emacstheviking |
Subject: | Re: Some advice on a "simple" thing... |
Date: | Thu, 10 Apr 2014 12:02:02 +0100 |
Hi Sean and Lindsey,
some more explanations... In case of...
Le 09/04/2014 22:44, Sean Charles a écrit :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.
foo(a - b, c + d, e = f) is semantically equivalent toOK, so the “traditional” operators are defined but they are given no special meaning unless used with “is”. Correct??
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.
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)
yesNB: 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
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: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.
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.
which is the same as
X is 3 + 4 * 2 * 7.
It is also the same as:
X is +(3, *(4, *(2, 7))).
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 MailScannersuspect n'a ete trouve.
pour des virus ou des polluriels et rien de
[Prev in Thread] | Current Thread | [Next in Thread] |