Presentation is loading. Please wait.

Presentation is loading. Please wait.

Declarative Programming Arithmetic in PROLOG Autumn 2014.

Similar presentations


Presentation on theme: "Declarative Programming Arithmetic in PROLOG Autumn 2014."— Presentation transcript:

1 Declarative Programming Arithmetic in PROLOG Autumn 2014

2 A way to do arithmetic in PROLOG How to define natural numbers? 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. Peano axioms (definition of equality)

3 A way to do arithmetic in PROLOG How to define natural numbers? Giuseppe Peano 1858-1932

4 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

5 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).

6 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*/

7 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).

8 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)).

9 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*/

10 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).

11 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.

12 Built-in arithmetic Some other functions may be available sin/1 cos/1 tan/1 log/1 exp/1

13 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

14 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)

15 Built-in arithmetic is cannot be used to solve equations e.g. X*X is 2*X + 17

16 Built-in arithmetic Besides is arithmetic expressions are evaluated by comparison predicates: –X =:= Ythe values of X and Y are equal. –X =\= Ythe values of X and Y are not equal. –X < YX is less than Y. –X > Y X is greater than Y. –X =< Y X is less than or equal to Y (sometimes <=...). –X >= Y X is greater than or equal to Y. In this case both X and Y are not allowed to contain uninstantiated variables

17 Arithmetic - example ax 2 + 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).


Download ppt "Declarative Programming Arithmetic in PROLOG Autumn 2014."

Similar presentations


Ads by Google