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.

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

CSE Lecture 3 – Algorithms I
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Dynamic Programming Nithya Tarek. Dynamic Programming Dynamic programming solves problems by combining the solutions to sub problems. Paradigms: Divide.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Programming Recursion.
Recursion. Binary search example postponed to end of lecture.
Symbolic AI Lecture Every Monday 12:30-13:30 Lab Session (two groups) Tuesday 16:30 3P30 Tuesday 17:30 3P30.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
Symbolic AI weeks of even numbers Lecture Monday 13:00-14:00 (2Q50) Lab Sessions (five groups, all in 2Q52) Monday.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. Lecture 8: Recursion Data Structures.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
School of Computing and Mathematics, University of Huddersfield Computing Science: WEEK 17 Announcement: next few weeks… 9 nd Feb: Comparative Programming.
Formal Models of Computation Part II The Logic Model
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
ΜIcon Bullets on parade. Preliminary: Continuation passing style The code following a function call is wrapped up in a method - the continuation.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CSC 211 Data Structures Lecture 13
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Simple algorithms on an array - compute sum and min.
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Abdul Rahim Ahmad MITM 613 Intelligent System Chapter 10: Tools.
HW7: Due Dec 5th 23:59 1.Describe test cases to reach full path coverage of the triangle program by completing the path condition table below. Also, draw.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Recursion ITFN The Stack. A data structure maintained by each program at runtime. Push Pop.
Lecture 2. Algorithms and Algorithm Convention 1.
Runtime support for region-based memory management in Mercury
Recursion ● Recursion or Iteration ● A Common Recursive Design Pattern ● Computing factorials ● Searching a filesystem tree ● Faster exponentiation ● Slow.
Recursion Salim Arfaoui.
COSC 2P93 Prolog: Debugging
Pseudo-code 1 Running time of algorithm is O(n)
User-Written Functions
Functions.
Recursion DRILL: Please take out your notes on Recursion
Nondeterministic Evaluation
Main Points of Haskell Functional programming Laziness
Topics Introduction to File Input and Output
Microprocessor and Assembly Language
CIS16 Application Development and Programming using Visual Basic.net
ㅎㅎ Fifth step for Learning C++ Programming Pointers Homework solution
Lecture 12 Recursion part 1 CSE /26/2018.
Artificial Intelligence
CS148 Introduction to Programming II
Algorithms An algorithm is a set of instructions used to solve a specific problem In order to be useful, an algorithm must have the following properties:
The Selection Problem.
Topics Introduction to File Input and Output
Recursion.
The DerUCP Algorithm Replay(P,C,G,…) Modified version of UCP: loop:
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
Lecture 20 – Practice Exercises 4
Advanced Analysis of Algorithms
Lecture 6 - Recursion.
Presentation transcript:

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

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;

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!

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.

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.

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.

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.

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

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.

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.

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.

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