Presentation is loading. Please wait.

Presentation is loading. Please wait.

From Conventional Languages to Prolog –What we can do in conventional languages but not in Prolog –What we can do in Prolog but not in conventional languages.

Similar presentations


Presentation on theme: "From Conventional Languages to Prolog –What we can do in conventional languages but not in Prolog –What we can do in Prolog but not in conventional languages."— Presentation transcript:

1 From Conventional Languages to Prolog –What we can do in conventional languages but not in Prolog –What we can do in Prolog but not in conventional languages

2 What we can do in conventional languages but not in Prolog 1. Destructive assignment is not allowed in Prolog In conventional languages a variable represents a memory store. Thus we often use destructive assignment to variables. X = 100; X = X + 20;

3 What we can do in conventional languages but not in Prolog 1. Destructive assignment is not allowed in Prolog (cont) But in Prolog a variable represents an unknown object; assignment to the variable means giving a concrete value to the variable. Once the variable is instantiated, it can't be assigned to other values unless the instantiated value is removed through backtracking. X = X+20 (or X is X+20) fails!

4 What we can do in conventional languages but not in Prolog 2. Prolog procedures can't return values directly The value returned from executing a Prolog procedure is either ‘fail' or ‘succeed’. Any required results have to be obtained through the arguments of the procedure.

5 Prolog procedures can't return values directly (an example) Finding factorial in Java (or C) int fac(int n) { if(n==0) return 1; else { return n*fac(n-1); } Finding factorial in Prolog fac(0,1). fac(N,F):- M is N-1, fac(M,SF), F is N*SF.

6 Prolog procedures can't return values directly (another example) Finding the smallest number in a list % Use the 2nd argument to represent the ‘returned value’ min_of(List, Min):- List=[H|_], min_of(List, Min, H). % add the 3rd argument to represent smallest so far min_of([H|T], Min, CurrentMin):- H < CurrentMin, min_of(T, Min, H). min_of([H|T], Min, CurrentMin):- H >= CurrentMin, min_of(T, Min, CurrentMin). min_of([], Min, CurrentMin):- CurrentMin=Min.

7 What we can do in conventional languages but not in Prolog 3. Iteration needs to be changed to recursion Iterative algorithms can not be directly adapted to Prolog as destructive assignment is not allowed. The solution is to use recursion to achieve iteration.

8 Iteration needs to be changed to recursion (an example) Iterative program in Java (or C) for(i=0;i<n;i++) { dosomething(..args..); } Iterative program in Prolog for_loop(N,N,..args..). for_loop(I,N,..args..):- I < N, dosomething(..args..), J is I+1, for_loop(J,N,..args..).

9 What we can do in Prolog but not in conventional languages 1. Backtracking Backtracking is a unique facility which doesn't exist in conventional languages. It is very useful for writing a non- deterministic (i.e. search type) program.

10 What we can do in Prolog but not in conventional languages 2. Matching (i.e. Unification) Applications which need pattern matching are easy to write in Prolog.

11 What we can do in Prolog but not in conventional languages 3. The arguments of a Prolog program may be used as either input or output For example, the append program given in lecture 3 can be used either for appending two given lists to one list or decomposing a given list into two lists.

12 What we can do in Prolog but not in conventional languages 4. Program can be modified as it runs


Download ppt "From Conventional Languages to Prolog –What we can do in conventional languages but not in Prolog –What we can do in Prolog but not in conventional languages."

Similar presentations


Ads by Google