Download Prolog arithmetic

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Computability theory wikipedia , lookup

Infinity wikipedia , lookup

Mathematical logic wikipedia , lookup

Gödel's incompleteness theorems wikipedia , lookup

Surreal number wikipedia , lookup

Foundations of mathematics wikipedia , lookup

Ordinal arithmetic wikipedia , lookup

List of first-order theories wikipedia , lookup

Laws of Form wikipedia , lookup

Peano axioms wikipedia , lookup

Transcript
Declarative
Programming
Arithmetic in PROLOG
Autumn 2014
A way to do arithmetic in PROLOG
How to define natural numbers?
Peano axioms (definition of equality)
Giuseppe Peano
1858-1932
For every natural number x, x = x. That is, equality is reflexive.
For all natural numbers x and y, if x = y, then y = x. That is, equality is
symmetric.
For all natural numbers x, y and z, if x = y and y = z, then x = z. That is,
equality is transitive.
For all a and b, if a is a natural number and a = b, then b is also a natural
number. That is, the natural numbers are closed under equality.
A way to do arithmetic in PROLOG
How to define natural numbers?
Giuseppe Peano
1858-1932
A way to do arithmetic in PROLOG
How to define natural numbers?
0 is a natural number
for each number x there is a successor number x+1
A way to do arithmetic in PROLOG
How to define natural numbers?
0 is a natural number
for each number x there is a successor number x+1
numb(nil).
numb(s(X)) :- numb(X).
A way to do arithmetic in PROLOG
How to add 2 numbers?
x+0 = 0
x + (0+1+...+1 /*n time */) = x+1+...+1 /*n times*/
A way to do arithmetic in PROLOG
How to add 2 numbers?
x+0 = 0
x + (0+1+...+1 /*n time */) = x+1+...+1 /*n times*/
sum(X,nil,X).
sum(X,s(Y),s(Z)) :sum(X,Y,Z).
A way to do arithmetic in PROLOG
How to add 2 numbers?
x+0 = 0
x + (0+1+...+1 /*n time */) = x+1+...+1 /*n times*/
sum(X,nil,X).
sum(X,s(Y),s(Z)) :sum(X,Y,Z).
subtr(X,nil,X).
subtr(X,s(Y),Z) :subtr(X,Y,s(Z)).
A way to do arithmetic in PROLOG
How to multiply 2 numbers?
x*0 = 0
x * (0+1+...+1 /*n times */) = x+x+...+x /*n times*/
A way to do arithmetic in PROLOG
How to multiply 2 numbers?
x*0 = 0
x * (0+1+...+1 /*n time */) = x+x+...+x /*n times*/
mult(X,nil,nil).
mult(X,s(Y),W) :mult(X,Y,Z),sum(X,Z,W).
Built-in arithmetic


Position.

infix operators:

prefix operator:

postfix operator:
Precedence.




+, , *, /

!
Each operator has a precedence value associated with it.
Precedence values are used to decide which operator is carried out
first.
In Prolog, multiplication and division have higher precedence values
than addition and subtraction.
Associativity.



An operator is either left associative or right associative.
In Prolog, arithmetic operations are left associative.
Round brackets can be used to enforce precedence and
associativity.
Built-in arithmetic
Some other functions may be available
sin/1
cos/1
tan/1
log/1
exp/1
Built-in arithmetic
The predicates
+/2, /2, */2, //2, /1, !/1
sin/1, cos/1, tan/1, log/1, exp/1
etc.
are just structure names
Their “arithmetical” meaning is only that they are
interpreted by truly arithmetical predicate is
Built-in arithmetic
Predicate is performs arithmetical operations as a
side effect
The logical value of is is always true
A correct usage:
X is expression
where expression is syntactically correct
arithmetical expression, containing only numerical
constants (or variables instantiated to numerical
constants)
Built-in arithmetic
is cannot be used to solve equations e.g.
X*X is 2*X + 17
Built-in arithmetic
Besides is arithmetic expressions are evaluated by
comparison predicates:
–X =:= Y
–X =\= Y
–X < Y
–X > Y
–X =< Y
–X >= Y
the values of X and Y are equal.
the values of X and Y are not equal.
X is less than Y.
X is greater than Y.
X is less than or equal to Y (sometimes <= ...).
X is greater than or equal to Y.
In this case both X and Y are not allowed to
contain uninstantiated variables
Arithmetic - example
ax2 + bx + c = 0
solve(A,B,C,X1,X2) :- discr(A,B,C,D),X1 is (–B + D)/(2*A), X2 is (–B)/(2*A).
discr(A,B,C,D) :- D1 is B*B–4*A*C,D1 >= 0,D is sqrt(D1).