Programmaing in logic Apurva Patil Gayatri Kharavlikar Meenakshi Borse Manjiri Bankar Nikita Alai Shruti Govilkar.

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

CS344: Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 12–Prolog examples: Himalayan club, member, rem_duplicate,
Software Development Languages and Environments. Programming languages High level languages are problem orientated contain many English words are easier.
Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 16- Prolog.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Prolog Programming slides written by Dr W.F. Clocksin.
Declarative Programming Autumn 2014 Basic syntax and sample programs.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
Overview of Programming Paradigms
CS344: Introduction to Artificial Intelligence (associated lab: CS386) Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 27, 28: Prolog 17 th and 21.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 15 Logic Programming Q: How many legs does.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
CS344 : Introduction to Artificial Intelligence Pushpak Bhattacharyya CSE Dept., IIT Bombay Lecture 22, 23- Prolog.
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Evolution of Programming Languages Generations of PLs.
CS344 : Introduction to Artificial Intelligence
programmation en logique
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
Getting Started with Visual Prolog
Artificial Intelligence LECTURE 2 ARTIFICIAL INTELLIGENCE LECTURES BY ENGR. QAZI ZIA 1.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
1 CS2136: Paradigms of Computation Class 04: Prolog: Goals Backtracking Syntax Copyright 2001, 2002, 2003 Michael J. Ciaraldi and David Finkel.
ARTIFICIAL INTELLIGENCE DR. ABRAHAM AI a field of computer science that is concerned with mechanizing things people do that require intelligent.
Prolog Programming in Logic. 2 SWI-Prolog SWI-Prolog is a good, standard Prolog for Windows and Linux Can be installed on Macintosh with a little more.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Logical and Functional Programming
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Knowledge Based Information System
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
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.
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
Section 16.5, 16.6 plus other references
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog Programming slides written by
CS621: Artificial Intelligence
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
CS621: Artificial Intelligence
Chapter 2: Prolog (Introduction and Basic Concepts)
PROLOG.
Presentation transcript:

programmaing in logic Apurva Patil Gayatri Kharavlikar Meenakshi Borse Manjiri Bankar Nikita Alai Shruti Govilkar

PROgramming in LOGic Emphasis on what rather than how Problem in Declarative Form Basic Machine Logic Machine Basic Machine Logic Machine

LOGIC PROGRAMMING science of reasoning algorithm = logic + control purely based on rules and facts artificial intelligence declarative

HISTORY Created in 1972 by Philippe Roussel and Alain Colmerauer Based on Robert Kowalski’s procedural interpretation of Horn clauses

TWO TYPES OF PROGRAMMING LANGUAGES Procedural (BASIC, Fortran, C++, Java) Declarative (LISP, Prolog) In procedural programming, we tell the computer how to solve a problem. In declarative programming, we tell the computer what problem we want solved.

INTRODUCTION TO PROLOG Prolog is the most widely used language to have been inspired by logic programming research. Some features: Prolog uses logical variables. These are not the same as variables in other languages. Programmers can use them as ‘holes’ in data structures that are gradually filled in as computation proceeds.

…MORE Clauses provide a convenient way to express case analysis and nondeterminism. Sometimes it is necessary to use control features that are not part of ‘logic’. A Prolog program can also be seen as a relational database containing rules as well as facts.

WHAT A PROGRAM LOOKS LIKE /* At the Zoo */ bigger(elephant, horse). bigger(horse, donkey). bigger(donkey, dog). bigger(donkey, monkey). -Query: ?- bigger(donkey, dog). Yes is_bigger(X, Y) :- bigger(X, Z), is_bigger(Z, Y).

PROLOG IS A DECLARATIVE LANGUAGE Clauses are statements about what is true about a problem, instead of instructions how to accomplish the solution. The Prolog system uses the clauses to work out how to accomplish the solution by searching through the space of possible solutions. Not all problems have pure declarative specifications. Sometimes extralogical statements are needed.

A Typical Prolog program Compute_length ([],0). Compute_length ([Head|Tail], Length):- Compute_length (Tail,Tail_length), Length is Tail_length+1. High level explanation: The length of a list is 1 plus the length of the tail of the list, obtained by removing the first element of the list. This is a declarative description of the computation.

Fundamentals (absolute basics for writing Prolog Programs)

Facts John likes Mary ◦ like(john,mary) Names of relationship and objects must begin with a lower-case letter. Relationship is written first (typically the predicate of the sentence). Objects are written separated by commas and are enclosed by a pair of round brackets. The full stop character ‘.’ must come at the end of a fact.

More facts PredicateInterpretation valuable(gold)Gold is valuable. owns(john,gold)John owns gold. father(john,mary)John is the father of Mary gives (john,book,mary)John gives the book to Mary

Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain). ◦ Generalizations  All men are mortal  mortal(X) :- man(X). ◦ Definitions  An animal is a bird if it has feathers  bird(X) :- animal(X), has_feather(X).

Syntax :- Read ‘:-’ as ‘if’. E.G. ◦ likes(john,X) :- likes(X,cricket). ◦ “John likes X if X likes cricket”. ◦ i.e., “John likes anyone who likes cricket”. Rules always end with ‘.’.

Question Answering in presence of rules Facts ◦ male (ram). ◦ male (shyam). ◦ female (sita). ◦ female (gita). ◦ parents (shyam, gita, ram). ◦ parents (sita, gita, ram).

Question Answering: Y/N type: is sita the sister of shyam? female(sita) parents(sita,M,F)parents(shyam,M,F) parents(sita,gita,ram) parents(shyam,gita,ram) success ?- sister_of (sita, shyam)

Complete Syntax of Terms Term Constant Variable Compound Term AtomNumber alpha17 gross_pay john_smith dyspepsia + =/= ’12Q&A’ e likes(john, mary) book(dickens, Z, cricket) f(x) [1, 3, g(a), 7, 9] -(+(15, 17), t) t X Gross_pay Diagnosis _257 _ Names an individual Stands for an individual unable to be named when program is written Names an individual that has parts

Compound Terms parents(spot, fido, rover) The parents of Spot are Fido and Rover. Functor (an atom) components (any terms) It is possible to depict the term as a tree: parents roverfidospot

Variables Variables always start with an upper case alphabetic character or an underscore ◦ ?- likes (john,X). ◦ ?- likes (john, Something). But not ◦ ?- likes (john,something)

Conjunctions Use ‘,’ and pronounce it as and. Example ◦ Facts:  likes(mary,food).  likes(mary,tea).  likes(john,tea).  likes(john,mary). ?-  likes(mary,X),likes(john,X).

Both facts and rules are predicate definitions. ‘Predicate’ is the name given to the word occurring before the bracket in a fact or rule: parent(jane,alan). By defining a predicate you are specifying relationship between objects. Predicate Definitions Predicate name

Clauses Predicate definitions consist of clauses. e.g. mother(jane,alan). = Fact parent(P1,P2):- mother(P1,P2). = Rule A clause consists of a head and sometimes a body. Facts don’t have a body because they are always true. head body

Arguments A predicate head consists of a predicate name and sometimes some arguments contained within brackets and separated by commas. mother(jane,alan). Arguments also consist of terms, which can be : Constants e.g. jane, Variables e.g. Person1, or Compound terms Predicate name Arguments

Arity The number of arguments a predicate has is called its arity. greetings is a predicate with no arguments. The arity of greetings is zero = greetings/0 By including more arguments predicates can be made more specific. greetings(hamish) = greetings/1 The predicate can then behave differently depending on the arguments passed to it.

Backtracking (an inherent property of prolog programming) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. First goal succeeds. X=food 2. Satisfy likes(john,food)

Backtracking (continued) Returning to a marked place and trying to resatisfy is called Backtracking likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. Second goal fails 2. Return to marked place and try to resatisfy the first goal

Backtracking (continued) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. First goal succeeds again, X=tea 2. Attempt to satisfy the likes(john,tea)

Backtracking (continued) likes(mary,X),likes(john,X) likes(mary,food) likes(mary,tea) likes(john,tea) likes(john,mary) 1. Second goal also suceeds 2. Prolog notifies success and waits for a reply

Structure of Programs Programs consist of procedures. Procedures consist of clauses. Each clause is a fact or a rule. Programs are executed by posing queries. An example…

Example elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X). Procedure for elephant Predicate Clauses Rule Facts

Example ?- elephant(george). yes ?- elephant(jane). no Queries Replies

Body of a (rule) clause contains goals. likes(mary, X) :- human(X), honest(X). HeadBody Goals

Interpretation of Clauses Clauses can be given a declarative reading or a procedural reading. H :- G 1, G 2, …, G n. “That H is provable follows from goals G 1, G 2, …, G n being provable.” “To execute procedure H, the procedures called by goals G 1, G 2, …, G n are executed first.” Declarative reading: Procedural reading: Form of clause:

EXAMPLE berkshire wiltshire surrey hampshire sussex kent How to represent this relation? Note that borders are symmetric. (a) Representing a symmetric relation. (b) Implementing a strange ticket condition.

Contd... border(sussex, kent). border(sussex, surrey). border(surrey, kent). border(hampshire, sussex). border(hampshire, surrey). border(hampshire, berkshire). border(berkshire, surrey). border(wiltshire, hampshire). border(wiltshire, berkshire). This relation represents one ‘direction’ of border: What about the other? (a) Say border(kent, sussex). border(sussex, kent). (b) Say adjacent(X, Y) :- border(X, Y). adjacent(X, Y) :- border(Y, X).

Contd... valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y) Now a somewhat strange type of discount ticket. For the ticket to be valid, one must pass through an intermediate county. A valid ticket between a start and end county obeys the following rule:

Contd... border(sussex, kent). border(sussex, surrey). border(surrey, kent). border(hampshire, sussex). border(hampshire, surrey). border(hampshire, berkshire). border(berkshire, surrey). border(wiltshire, hampshire). border(wiltshire, berkshire). adjacent(X, Y) :- border(X, Y). adjacent(X, Y) :- border(Y, X). ?- valid(wiltshire, sussex). ?- valid(wiltshire, kent). ?- valid(hampshire, hampshire). ?- valid(X, kent). ?- valid(sussex, X). ?- valid(X, Y). valid(X, Y) :- adjacent(X, Z), adjacent(Z, Y)

How to save prolog file: Save as Filename.pl Conflict with Perl Alternative extension can be filename.pro or filename.pr No capitals C:/MyDocument//error C:/mydocument To exit prolog- ‘halt’

Different editors: SWI Prolog ◦ Extra libraries ◦ Built in predicates GNU Prolog ◦ Support constraints logic programming Visual Prolog ◦ A complete development environment for object oriented extension to prolog And many more…

SIGNIFICANT FEATURES Simplicity Regularity Intelligent systems Expert systems Natural language systems Relational database systems

ADVANTAGES:- Logic based languages are able to represent the real world more accurately. Prolog is able to derive new rules from the existing rules contained within the knowledge base. Strong ties to formal logic Many algorithms become trivially simple to implement

DISADVANTAGES:- -It can be very difficult to design a database that accurately represents relationships. Prolog is not best suited to solving complex arithmetical computations. Complicated syntax Difficult to understand programs at first sight Prolog programs are not best suited to the current PC architecture (sequential execution) and are best optimised on parallel architectures (fifth generation computers).

APPLICATIONS: Target User: ◦ Expert Systems (Knowledge Representation and Inferencing) ◦ Natural Language Processing ◦ Relational Databases ◦ Artificial intelligence

Thank u..!!