2.1 2. Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.

Slides:



Advertisements
Similar presentations
An Introduction to Programming By :- Vishal Hirani B.Tech II year (CSE)
Advertisements

GCSE Computing Lesson 5.
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
1. An Overview of Prolog.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
The Assembly Language Level
CS 111: Introduction to Programming Midterm Exam NAME _________________ UIN __________________ 10/30/08 1.Who is our hero? 2.Why is this person our hero?
Programming Types of Testing.
CHAPTER 1: AN OVERVIEW OF COMPUTERS AND LOGIC. Objectives 2  Understand computer components and operations  Describe the steps involved in the programming.
4. PROLOG Data Objects And PROLOG Arithmetic
Lecture 2 Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Language Paradigms Programming languages. Language paradigms. World view of imperative languages. World view of functional languages. World view.
Introduction to Analysis of Algorithms
Introduction to Computers and Programming. Some definitions Algorithm: –A procedure for solving a problem –A sequence of discrete steps that defines such.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2: Input, Processing, and Output
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
1 Programming & Programming Languages Overview l Machine operations and machine language. l Example of machine language. l Different types of processor.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Introduction to C Programming
Design and Analysis of Algorithms
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
Formal Models of Computation Part II The Logic Model
Simple Program Design Third Edition A Step-by-Step Approach
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Introduction to Python
Evolution of Programming Languages Generations of PLs.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Computer Science 101 Introduction to Programming.
S2008Final_part1.ppt CS11 Introduction to Programming Final Exam Part 1 S A computer is a mechanical or electrical device which stores, retrieves,
1 Problem Solving with C++ The Object of Programming Walter Savitch Chapter 1 Introduction to Computers and C++ Programming Slides by David B. Teague,
F28PL1 Programming Languages Lecture 16: Prolog 1.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
INTRODUCTION TO COMPUTING CHAPTER NO. 04. Programming Languages Program Algorithms and Pseudo Code Properties and Advantages of Algorithms Flowchart (Symbols.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Knowledge Based Information System
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Simple ALU How to perform this C language integer operation in the computer C=A+B; ? The arithmetic/logic unit (ALU) of a processor performs integer arithmetic.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Logic Programming Lecture 2: Unification and proof search.
Chapter 2: Input, Processing, and Output
Lecture 2 Introduction to Programming
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Algorithm Analysis CSE 2011 Winter September 2018.
PROLOG.
Unit# 9: Computer Program Development
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2: Input, Processing, and Output
Chapter 2: Prolog (Introduction and Basic Concepts)
Presentation transcript:

Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG query. A PROLOG search space.

2.2 World View Of Imperative Languages A Von Neumann Machine. Processor executes instructions stored in memory. –Instructions modify memory contents, read input and / or write output. Contents of memory + input not yet read + output written so far = state. Memory Processor inputoutput bytes

2.3 World View Of Imperative Languages II Imperative world view is based on state. –Imperative program = sequence of instructions which modify state (i.e. statements). –Result of computation is state after the computation has terminated. Most computers are Von Neumann machines. –Have the same world view as imperative languages. –Little translation overhead. –Imperative languages run efficiently on them. Imperative languages tend to use Von Neumann hardware as efficiently as possible. Imperative languages tend to use programmer time as inefficiently as possible.

2.4 World View Of Relational Languages Usually written : program(input, output) The program is a relation which holds between the input and the output. No notion of a state which can be modified so no statements. No memory in the model so no variables to assign to. Relational programs do have variables but they are logical variables. –Logical variables take on the values required to make the relations containing them hold. program inputoutput

2.5 World View Of Relational Languages II Relational programs tend to be a lot shorter because there is no consideration of memory, input and output. Relational program a lot less efficient (on a Von Neumann machine) because there is no consideration of memory, input and output. There is no real notion of memory, input or output. Can run a relational program backwards : give the program the output and it returns the input. Relational programs are bi-directional.

2.6 PROLOG PROLOG is the most commonly used relational language. –PROLOG stands for “PROgramming in LOGic”. –Often (incorrectly) written Prolog. PROLOG is sometimes referred to as a “Logic Programming Language”. –This is incorrect, PROLOG only implements first-order logic.  Horn Clause logic. First implemented by Alain Colmeraurer and his team at Marseille University in the early 1970s. Based on fundamental work by Alan Robinson and Robert Kowalski in the 1960s.

2.7 A PROLOG Program A PROLOG program is a set of facts and rules. A simple program with just facts : parent(alice, jim). parent(jim, tim). parent(jim, dave). parent(jim, sharon). parent(tim, james). parent(tim, thomas). c.f. a table in a relational database. Each line is a fact (a.k.a. a tuple or a row). Each line states that some person X is a parent of some (other) person Y. In GNU PROLOG the program is kept in an ASCII file.

2.8 Running a PROLOG Program To get information from the program we query it in much the same way as a relational database. First, run GNU PROLOG : $ gprolog GNU Prolog By Daniel Diaz Copyright (C) 1999,2000 Daniel Diaz | ?- The last line is the GNU PROLOG prompt. Next, load the file containing the program (consulting the program) : | ?- [lect1]. % File is called lect1.pl compiling... yes | ?- PROLOG always say yes if something works.

2.9 A PROLOG Query Now we can ask PROLOG questions : | ?- parent(alice, jim). yes | ?- parent(jim, herbert). no | ?- Not very exciting. But what about this : | ?- parent(alice, Who). Who = jim yes | ?- Who is called a logical variable. –PROLOG will set a logical variable to any value which makes the query succeed.

2.10 A PROLOG Query II Logical variables start with an upper case letter. Anything that starts with a lower case letter is a literal constant. Sometimes there is more than one correct answer to a query. PROLOG gives the answers one at a time. To get the next answer type ;. | ?- parent(jim, Who). Who = tim ? ; Who = dave ? ; Who = sharon ? ; yes | ?- After finding that jim was a parent of sharon GNU PROLOG detects that there are no more alternatives for parent and ends the search. NB : The ; do not actually appear on the screen.

2.11 A PROLOG Search Space The easiest way to understand how a PROLOG program works is to draw a search space. –Sometimes called a search tree. –A pictorial representation of Robinson’s Unification Algorithm in action. PROLOG is not very bright - it simply searches the program from top to bottom checking each fact in turn. For the query to succeed PROLOG must be able to make the query the same as the fact it is checking against. –The query and the fact must MATCH. C marks a choice point : a place where PROLOG has more than one way of obtaining a match. –Choice points are used in backtracking.

2.12 A PROLOG Search Space II Match 1 : alice \= jim, jim = Who. –Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to jim. –This is no help as alice and jim are different literal constants. There is no way they can be made equal. –The match FAILS. PROLOG automatically backtracks to the closest choice point ( C ) and checks the next fact for a match. Match 2 : jim = jim, tim = Who. – jim occurs in both the query and the fact. A constant always matches with itself. –Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to tim. –The match SUCCEEDS.

2.13 A PROLOG Search Space III At this point PROLOG prints Who = tim ? PROLOG is asking whether we want any more answers. If we press RETURN the search stops and we get the normal PROLOG prompt. If we press ; the search continues (we force backtracking to the closest choice point). Match 3 : jim = jim, dave = Who. – jim occurs in both the query and the fact. A constant always matches with itself. –Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to dave. –The match SUCCEEDS. PROLOG prints Who = dave ?

2.14 A PROLOG Search Space IV Match 4 : jim = jim, sharon = Who. – jim occurs in both the query and the fact. A constant always matches with itself. –Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to sharon. –The match SUCCEEDS. PROLOG prints Who = sharon ? Match 5 : tim \= jim, james = Who. – tim and jim are two different constants. They do not match. –Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to james. –The match FAILS.

2.15 A PROLOG Search Space V PROLOG automatically backtracks to C to find another alternative. Match 6 : tim \= jim, thomas = Who. – tim and jim are two different constants. They do not match. –Since Who is a logical variable PROLOG can set it to any value required to obtain a match, in this case Who is set to thomas. –The match FAILS. PROLOG automatically backtracks to C to find another alternative. There are no more alternatives at C. They have all been tried. The overall query FAILS. –Remember, it has already had three successes. –GNU PROLOG prints yes. Some implementations would print no in this case.

2.16 Summary Relational world view : program(input, output). –No memory, no I/O. –No state. Logical variables take on the values required to make the relations containing them hold. Relational programs are bi-directional. PROLOG is an implementation of first-order logic. –Horn clauses. A PROLOG program is a set of facts and rules.

2.17 Summary II PROLOG searches the program from top to bottom, left to right. PROLOG tries to match the query with the current fact (or the head of the current rule) by picking appropriate values for the logical variables in the query and / or the current fact (or the head of the current rule). –PROLOG tries to make things the same. A search space is a pictorial representation of how PROLOG solves a particular query. –Robinson’s Unification Algorithm. In the exam you will be asked to write at least one search space.