Summary. likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book).

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Lecturer: Dr. Abeer Mahmoud Logic Programming in Prolog.
Prolog.
Introduction to PROLOG ME 409 Lab - 1. Introduction to 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).
1Lecture 12 Introduction to Prolog SWI-Prolog. 2Lecture 12 Introduction to Prolog Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) University.
Chapter 12 - Logic Programming
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
Chapter 8: The Logical Paradigm Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CPSC 322 Introduction to Artificial Intelligence September 15, 2004.
JavaScript, Third Edition
LING 364: Introduction to Formal Semantics Lecture 5 January 26th.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Logic Programming Languages
Formal Models of Computation Part II The Logic Model
1 Prolog I. 2 Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
First Order Logic Chapter 7. PL is a Weak Representational Language §Propositional Logic (PL) is not a very expressive language because: §Hard to identify.
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.
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.
Parsing. Language A set of strings from an alphabet which may be empty, finite or infinite. A language is defined by a grammar and we may also say that.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
1Lecture 12 Introduction to Prolog Logic Programming Prolog.
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Chapter 3 Describing Syntax and Semantics
By: Cory Canter CSC 415 Programming Languages. History  Created by Alain Colmerauer, Phillipe Roussel and Robert Kowalski in 1971  Started as a natural.
Dr. Muhammed Al-Mulhem ICS An Introduction to Logical Programming.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
For Monday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Programming Languages Third Edition Chapter 4 Logic Programming.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
Knowledge Based Information System
Artificial Intelligence CS370D
1-1 An Introduction to Logical Programming Sept
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
For Friday No reading Prolog Handout 2. Homework.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Prolog Overview Syntax Mechanism Summary. Overview PROLOG : Programming with Logic Uses predicate (first-order) calculus History: Roots: J.A. Robinson.
1 member(X,[Y| _ ] ) :- X = Y. member(X, [ _ | Y]) :- member(X, Y). It would be easier to write this as: member(X,[X| _ ]). member(X, [ _ | Y]) :- member(X,
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
Prolog a declarative language
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
Predicate Logic: Syntax.
Predicate Logic.
Chapter 2: Prolog (Introduction and Basic Concepts)
PROLOG.
Presentation transcript:

Summary

likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). Queries ?- likes(jerry,cheeze). yes ?-likes(X,john). X=mary; X=tom; no Prolog: Facts

Rules X is brother of Y if X is a male and X and Y have the same parents. In Prolog is_brother_of(X,Y):-male(X), parents(X, Father, Mother), parents(Y, Father, Mother).

Backtracking ?- concen(X,Y). X = us Y = ; X = china Y = ; X = nz Y = 16 ; X = india Y = ; No population(us,275). population(china,1262). population(nz,4). Population(india,1000). land(us,3000). land(china,4000). land(nz,250). land(india,3288). concen(X,Y):- population(X,P), land(X,L), Y is P*1000/L.

Cut: ! Eliminates choices Always succeeds but stops backtracking a:-b,c,!,d. a:-e,f. max(X,Y,Y) :- Y>X. max(X,Y,X). ?- max(1,2,X). X = 2 ; X = 1 ; No ?- max(X,Y,Y) :- Y>X, !. max(X,Y,X). ?- max(1,2,X). X = 2 ; No ?-

[ ] [a,b,c,d,e] [5,8,3,9,7] [the, boy, run] A list can be split into a head and a tail: [H|T]. grades(john, [70,87,90,58]). ?- grades(john, [H|T]). H = 70 T = [87,90,58] member(X,[X| _ ]). member(X, [ _ | Y]) :- member(X, Y). ?- member(1, [3,4,5,8,1,9]). Yes Recursion and Lists Lists

put(Ch). get(Ch). get0(Ch). tab(X). nl. read(X). write(X). I/O tell(Filename) telling(X) told see(Filename) seeing(X) seen File I/O

male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). ?- male(X). X= andrew; X= john ; X= george ; X= greg ; X= adam ; ?- female(X). X= mary; X= jennifer; X= eve; ?- parents(X, adam, eve). X= greg; X= jennifer; X= andrew; ?- findall(X, male(X), List). List= [andrew, john, george, greg, adam] ?- findall(X, female(X), List). List= [mary, jennifer, eve] ?- findall(X, parents(X,adam,eve), List). List= [greg, jennifer, andrew] findall(X,Term,List).

?- arg(2,likes(mary,john),X). X = john Yes ?- arg(2,likes(mary,X),john). X = john Yes ?- arg(3,parents(john,george,X),Val). X = _G346 Val = _G346 Yes ?- arg(3,parents(john,george,victoria),Val). Val = victoria Yes ?- functor(likes(mary,john),Fun,Arity). Fun = likes Arity = 2 Yes ?- X=likes(mary,john),functor(X,Func,Arity). X = likes(mary, john) Func = likes Arity = 2 Yes ?- functor(parents(adam,john,mary),F,N). F = parents N = 3 Yes ?- functor(X,likes,2). X = likes(_G303, _G304) Yes functor(Term, Functor, Arity) arg(N,Term,Value)

Games Robot control Natural language processing Expert systems Image processing Parsing of context-free languages Compiler writing VLSI Design Relational database applications Other AI applications Applications

expr ::= term | term addop expr term ::= factor | factor multop term factor ::= ‘x’ | ‘y’ | lbr expr rbr addop ::= ‘+’ | ‘-’ multop ::= ‘*’ | ‘/’ lbr ::= ‘(’ rbr ::= ‘)’ A simple grammar for expressions Applications

expr --> term. expr --> term, addop, expr. term --> factor. term --> factor, multop, term. factor --> [x]. factor --> [y]. factor --> lbr, expr, rbr. addop --> ['+']. addop --> ['-']. multop --> ['*']. multop --> ['/']. lbr --> ['(']. rbr --> [')']. ?- phrase(expr, [y, '*', '(', x, '+', x, ')']) Yes ?- phrase(factor, [y]) Yes ?- phrase(rbr, [')']) Yes ?- phrase(factor, [y, '*', x]) No ?- phrase(expr, [y, '*', x]). Yes ?- phrase(factor, ['(',y, '*', x,')']). Yes ?- Applications

A Grammar for a very small fragment of English sentence --> noun_phrase, verb_phrase. noun_phrase --> determiner, noun. noun_phrase --> proper_noun. determiner --> [the]. determiner --> [a]. proper_noun --> [pedro]. noun --> [man]. noun --> [apple]. verb_phrase --> verb, noun_phrase. verb_phrase --> verb. verb --> [eats]. verb --> [sings]. Applications

Inference Engine Knowledge Base Working Memory Explanation Facility User User Interface Domain Expert(S) Knowledge Engineer Knowledge Formalized Knowledeg Applications

Horn Clauses Definition: A Horn clause is a clause with at most one positive literal. A Horn clause therefore belongs to one of four categories: A rule: 1 positive literal, at least 1 negative literal. A rule has the form: ~P1 V ~P2 V... V ~Pk V Q This is logically equivalent to P1^P2^... ^Pk => Q thus, an if-then implication with any number of conditions but one conclusion. Examples: ~man(X) V mortal(X) (All men are mortal); ~parent(X,Y) V ~ancestor(Y,Z) V ancestor(X,Z) If X is parent of Y and Y is ancestor of Z then X is ancestor of Z.

A fact or unit: 1 positive literal, 0 negative literals. Examples: man(socrates) parent(elizabeth,charles)", A negated goal : 0 positive literals, at least 1 negative literal. In virtually all implementations of Horn clause logic, the negated goal is the negation of the statement to be proved The null clause : 0 positive and 0 negative literals. Appears only as the end of a proof.

Prolog is designed to represent Horn clauses, does backward chaining only, is a full Turing equivalent programming language, and can compute anything any other programming language can. Modern Prolog implementations have GUI facilities, fast compilers and (if your code is designed appropriately) very high run-time performance.

Program ::= Clause... Query | Query Clause ::= Predicate. | Predicate :- PredicateList. PredicateList ::= Predicate | PredicateList, Predicate Predicate ::= Atom | Atom( TermList ) TermList ::= Term | TermList, Term Term ::= Numeral | Atom | Variable | Structure Structure ::= Atom ( TermList ) Query ::= ?- PredicateList. Numeral ::= an integer or real number Atom ::= string of characters beginning with a lowercase letter or enclosed in apostrophes. Variable ::= string of characters beginning with an uppercase letter or underscore Terminals = {Numeral, Atom, Variable, :-, ?-, comma, period, left and right parentheses } Prolog Grammer

Comparing Prolog and Haskell Syntax In Prolog: functions are not evaluated - they are like data constructors the language is untyped variables begin with upper-case letters or an underscore predicate and function symbols begin with a lower-case letter the list constructor functor is [ | ], not :

HaskellProlog Kinds of objects functionsrelations Arity (number of parameters) fixed; if fewer arguments, then return a functionvariable; indicated with /n ending Variables start with lower casestart with upper case Values start with upper casestart with lower case Clause parameters separated by spacesseparated by commas Head-body separator = (after possible alternatives):- (when body is needed) Alternatives preceded by | (or can use if-then-else)separated by semi-colons Clause ending off-side rule (or semi-colon)full stop List constructor : (inside parens when potentially ambiguous)| (always inside brackets)

reverse [] = [] reverse (h:t) = (reverse t) ++ [h] reverse([], []). reverse([H|T], R) :- reverse(T, RT), append(RT, [H], R).