ProLog (Artificial Intelligence) Introduction

29
PROLOG PROgramming in LOGic A Brief Introduction

Transcript of ProLog (Artificial Intelligence) Introduction

Page 1: ProLog (Artificial Intelligence) Introduction

PROLOG

PROgramming in LOGic

A Brief Introduction

Page 2: ProLog (Artificial Intelligence) Introduction

Compilers

• PDC Visual Prolog• SWI Prolog• Bin-Prolog• K-Prolog• Prolog ++

http://swish.swi-prolog.org

Page 3: ProLog (Artificial Intelligence) Introduction

PRO-LOG

• Programming in Logic• 1975, Phillippe Roussell• Predicate Calculus• Declarative Language• Predicates and Rules• Inference Mechanism

Page 4: ProLog (Artificial Intelligence) Introduction

PROLOG Paradigm• PROgramming in LOGic

– Draw inferences from facts and rules• PROLOG is

– declarative - specify facts and logical relationships• Non-procedural: "what", not "how". (we specify the problem but not

the solution)• Database language with automated search and the ability to follow

general rules.– symbolic - symbols are used to represent objects– high level - contains a built-in problem solving mechanism

• PROLOG Programming (When we program in ProLog we need to provide following three things)

– Declaring some facts about objects and their relationships– Defining some rules about objects and their relationships– Asking questions about objects and their relationships

Page 5: ProLog (Artificial Intelligence) Introduction

PROLOG Paradigm

• The PROLOG Programmer– Loads facts and rules into the database.– Makes queries to the database to see if a fact is:

• in the database or• can be implied from the facts and rules therein

Prolog Database

Facts + Rules

Query

Page 6: ProLog (Artificial Intelligence) Introduction

System InteractionProblem solving in PROLOG

– 1. insert facts and rules into the database– 2. ask questions (queries) based on the contents of the database

• Facts– Used to represent unchanging information about objects and their

relationships.– Only facts in the PROLOG database can be used for problem solving.– Insert facts into the database by,

• typing the facts into a file and loading (consulting) the file into a running PROLOG system

Page 7: ProLog (Artificial Intelligence) Introduction

System Interaction

• Queries– Retrieve information from the database by entering QUERIES– A query,

• is a pattern that PROLOG is asked to match against the database• has the syntax of a compound query• may contain variables

– A query will cause PROLOG to• look at the database• try to find a match for the query pattern• execute the body of the matching head• return an answer

Page 8: ProLog (Artificial Intelligence) Introduction

• Logic Programming• Logic programming is a form of declarative programming • A program is a collection of axioms

– Each axiom is a Horn clause of the form: H :- B1, B2, ..., Bn. where H is the head term and Bi are the body terms

– Meaning H is true if all Bi are true

Page 9: ProLog (Artificial Intelligence) Introduction

Basic Proof technique - modus ponens

A -> B A --------- B

Page 10: ProLog (Artificial Intelligence) Introduction

• Prolog Uses backward chaining – More efficient than forward chaining for larger

collections of axioms • Applications: expert systems, artificial

intelligence, natural language understanding, logical puzzles and games

Page 11: ProLog (Artificial Intelligence) Introduction

PROLOG ParadigmExamples (Facts)

English PROLOG

“A dog is a mammal” isa(dog, mammal).“A sparrow is a bird” isa(sparrow, bird).

Page 12: ProLog (Artificial Intelligence) Introduction

PROLOG ParadigmExamples (Rules)

English PROLOG

“Something is an animal animal(X) :- isa(X,mammal).if it is a mammal or a bird” animal(X) :- isa(X, bird).

Page 13: ProLog (Artificial Intelligence) Introduction

PROLOG ParadigmExamples (Queries)

English PROLOG

“is a sparrow an animal?” ?- animal(sparrow).answer: “yes” yes“is a table an animal?” ?- animal(table).answer: “no” no“what is a dog?” ?- isa(dog, X).answer: “a mammal” X = mammal

Page 14: ProLog (Artificial Intelligence) Introduction

PROLOG syntax• Constants

– Atoms• Alphanumeric atoms - alphabetic character sequence starting with a

lower case letterExamples: apple a1 apple_cart• Quoted atoms “String” - sequence of characters surrounded by single

quotesExamples: ‘Apple’ ‘hello world’• Symbolic atoms - sequence of symbolic charactersExamples: & < > * - + >>• Special atomsExamples: ! ; [ ] {}

– Numbers• Integers and Floating Point numbersExamples: 0 1 9821 -10 1.3 -1.3E102

Page 15: ProLog (Artificial Intelligence) Introduction

PROLOG syntax

• Variable Namesa sequence of alphanumeric characters beginning with an

upper case letter or an underscoreExamples: Anything _var X _

• Compound Terms (structures)– an atom followed by an argument list containing terms.

The arguments are enclosed within brackets and separated by commas

Example: isa(dog, mammal)

Page 16: ProLog (Artificial Intelligence) Introduction

Prolog syntax• The names of all relationships and objects must

begin with a lower case letter. For example studies, ali, programming

• The relationship is written first and the objects are enclosed within parentheses and are written separated by commas. For example studies(ali, programming)

• The full stop character ‘.’ must come at the end of a fact.

• Order is arbitrary but it should be consistent

Page 17: ProLog (Artificial Intelligence) Introduction

Example Program with three facts and one

rule:

rainy(columbo). rainy(ayubia). cold(ayubia).

Rule: snowy(X) :- rainy(X), cold(X). Query and response:

?- snowy(ayubia). yes

Query and response: ?- snowy(columbo). no

Query and response: ?- snowy(lahore). No

• ?- snowy(C). C = ayubia

– because rainy(ayubia) and cold(ayubia) are sub-goals that are both true facts in the database

– snowy(X) with X=columbo is a goal that fails, because cold(X) fails, triggering backtracking

Page 18: ProLog (Artificial Intelligence) Introduction

snowy(C)

snowy(X)

AND

rainy(X) cold(X)

C = X

rainy(columbo) rainy(ayubia)

X = columbo X = ayubia

cold(ayubia)

C = X = ayubia

C = ayubia

rainy(columbo). rainy(ayubia). cold(ayubia). snowy(X) :- rainy(X), cold(X).

?- snowy(C). C = ayubia

OR

Page 19: ProLog (Artificial Intelligence) Introduction

Logic Representation : Propositional Calculus

• Propositional Logic– Boolean statements– Logic connectives

Example – a family

Atomic statementsp: Ahmed is father of Belalq: Ahmed is brother of Aslamr : Aslam is uncle of Belal

Complex statements

Statement Logic p Ahmed is not father of

Belal

p q Ahmed is father of Belal or Ahmed is brother of Aslam

p q r If Ahmed is father of Belal and brother of Aslam then Aslam is uncle of Belal

Page 20: ProLog (Artificial Intelligence) Introduction

Prolog ProgrammingPredicate Calculus

• Predicates (Facts)– man(Ahmed)– father(Ahmed, Belal)– brother(Belal, Chand)– brother(Chand, Delawar) – owns(Belal, car)– tall(Belal)– hates(Ahmed, Chand)– family()

• Formulae– X,Y,Z(man(X) man(Y) father(Z,Y) father(Z,X)

brother(X,Y))• Variables

– X, Y and Z

• Constants– Ahmed, Belal, Chand, Delawar and car

Prolog code

Predicatesman(symbol)father(symbol, symbol)brother(symbol, symbol)owns(symbol, symbol)tall(symbol)hates(symbol, symbol)family()

Clausesman(ahmed).father(ahmed, belal).brother(ahmed, chand).owns(belal, car).tall(belal).hates(ahmed, chand).family().

Rule:brother(X,Y):-

man(X),man(Y),father(Z,Y),father(Z,X).

Goalbrother(ahmed,belal).

Page 21: ProLog (Artificial Intelligence) Introduction

Sections of Prolog Program (1-3)

• Predicates– Declarations of Relations or Rules– Just like function prototype (declaration).– Zero or more arguments– Example

Predicatesman(symbol)family()a_new_predicate (integer, char)

Page 22: ProLog (Artificial Intelligence) Introduction

Sections of Prolog Program (2-3)• Clauses

– Definition(s) of Predicate = Sentence OR Phrase– Types

• Rules (function definitions)– 0 or more conditions (statements)

• Facts– 0 conditions– All parameters constant

– Examples• Fact

brother(ahmed, chand).• Rule

brother(X,Y):-man(X),man(Y),father(Z,Y),father(Z,X).

Page 23: ProLog (Artificial Intelligence) Introduction

Sections of Prolog Program (3-3)

• Goal– Goal of inference – Only and exactly one instance– The only tentative section of the program– The main() of prolog– Goal truth value = output of program– Syntactically and Semantically just another clause

• Empty, simple (one goal), or compound (sub-goals)– Examples

• Goalbrother(X,chand). <or> brother(ahmed,chand).

• Goalbrother(X,chand),father().

Page 24: ProLog (Artificial Intelligence) Introduction

Sample Program Updatedomains

person = symbolpredicates

nondeterm father(person,person)nondeterm brother(person,person)nondeterm cousin(person,person)nondeterm grandfather(person,person)

clausesfather(a,b).father(a,c).father(a,d).father(b,e).father(b,f).father(b,g).father(c,h).father(c,i).

father(d,j). father(d,k).

brother(X,Y):-

X<>Y, father(Z,X),

father(Z,Y).

cousin(X,Y):- father(A,X),

father(B,Y), brother(A,B).

grandfather(X,Y):- father(Z,Y), father(X,Z).goal cousin(X,Y).

Page 25: ProLog (Artificial Intelligence) Introduction

Prolog Variables• Constant placeholders (NOT variables)

– Bounded once• Loosely typed• Start with Capital letter or underscore• Examples

– brother(ahmed, Ahmed)– brother(ahmed, _x)– Brother(ahmed, X)

• Anonymous variable– The _– Some value that isn’t required– Example

brother(ahmed, _)

Page 26: ProLog (Artificial Intelligence) Introduction

Expert system• A simple medical expert system• relieves(Drug, Symptom).

– relieves(aspirin, headache).– relieves(aspirin, moderate_pain).– relieves(aspirin, moderate_arthritis).– relieves(aspirin_codine_combination, severe_pain).– relieves(cough_cure, cough).– relieves(pain_gone, severe_pain).– relieves(anti_diarrhea, diarrhea).– relieves(de_congest, cough).– relieves(de_congest, nasal_congestion).– relieves(penicilline, pneumonia).– relieves(bis_cure, diarrhea).– relieves(bis_cure, nausea).– relieves(new_med, headache).– relieves(new_med, moderate_pain).– relieves(cong_plus, nasal_congestion).

Page 27: ProLog (Artificial Intelligence) Introduction

• aggravates(Drug, Condition).– aggravates(aspirin, asthma).– aggravates(aspirin, peptic_ulcer).– aggravates(anti-diarrhea, fever).– aggravates(de_congest, high_blood_pressure).– aggravates(de_congest, heart_disease).– aggravates(de_congest, diabetes).– aggravates(de_congest, glaucoma).– aggravates(penicilline, asthma).– aggravates(de_congest, high_blood_pressure).– aggravates(bis_cure, diabetes).– aggravates(bis_cure, fever).

Page 28: ProLog (Artificial Intelligence) Introduction

should_take(Person, Drug) :-complains_of(Person, Symptom),relieves(Drug, Symptom),not(unsuitable_for(Person, Drug)). //conjunction of three classes.

unsuitable_for(Person, Drug) :-aggravates(Drug, Condition),suffers_from(Person, Condition).

complains_of(ali, headache).suffers_from(ali, peptic_ulcer).

?- should_take(ali, Drug).Drug = new_med;

Page 29: ProLog (Artificial Intelligence) Introduction

Books

• Prolog Programming for AI by Ivan Bratko• Visual Prolog; Language Tutorial by PDC