prolog - emi.ac.ma 2 - prolog.pdf · karim bouzoubaa artificial intelligence 3 ! prolog is an ai...

13

Click here to load reader

Upload: lamdan

Post on 03-Oct-2018

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Prolog

Artificial Intelligence

Lecture 2 Karim Bouzoubaa

Page 2: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 2

Content

¢  Introduction ¢  Declarative and logic programming ¢  Example ¢  Computational model ¢  Prolog reasoning ¢  Structure of prolog programs ¢  Prolog concepts ¢  Installing Prolog+CG ¢  Executing a Prolog+CG program

Page 3: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 3

¢  Prolog is an AI tool

¢  PROLOG = PROgrammation LOGique

¢  In 1972 by Alain Colmerauer, Université de Marseille

¢  Basic, Modular & OO programming

¢  Prolog is a declarative programming language l  Subset of logic (1st order logic of predicates logic)

¢  Idea 1: symbolic language l  Declarative method (represent objects and their relations) l  Procedural method (automate the logical reasoning)

¢  Idea 2: the programmer declares what (s)he thinks about the solution using the language of logic

à

¢  Consequence: The programmer does not need to code explicitly all instructions

Introduction

Page 4: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 4

¢  A Prolog program = l {P ß A1 & A2 & A3 … & An} l P is true if all Ai are true

¢  Prolog Language = l Pure prolog (logic programming) + l  specific instructions (non logic)

•  Flow control •  Input/output instructions •  Arithmetic instructions •  etc.

Pure Prolog

Page 5: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 5

¢  Computational model of classical programming l  Data + Processing (describes step by step what the machine should do)

¢  Computational model of Prolog l  Knowledge + Question à Answer

¢  Example: l  K : saturday, it was raining

it is cold if it rains l  Q: was it cold saturday? l  A: it was cold saturday

¢  Prolog l  K: rains(saturday). cold(X) :- rains(X).

l  Q: cold(saturday)? l  A: cold(saturday)

Example

Page 6: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 6

¢  K: rains(saturday). cold(X) :- rains(X).

¢  Q: cold(saturday)?

¢  A: cold(saturday)

Prolog program ← facts

← rules (clauses)

Inference mechanism ¢  Try to match the question to

each rule: unification ¢  Substitute of variables if

necessary: instantiation

Computational model

Page 7: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 7

Computational model

¢  Prolog Language = l  Specific instructions (non logic) + l  Pure prolog (logic programming)

•  Knowledge + Question à Answer

¢  Set of Knowledge = Knowledge Base (KB) l  KB = Fact Base (FB) + Rules l  Fact: represents a true assertion l  Rule: conditions to verify a goal

(answer a question)

¢  Question/Answer = Inference Mechanism l  Consult the KB l  Generate/Delete Facts/Rules l  Verifies and satisfies goals

¢  K: rains(saturday). Fact1 cold(X) :- rains(X). Rule1

¢  Q: cold(saturday)?

¢  A: cold(saturday)

¢  IM l  Step 1: cold(saturday)? Goal l  Step 2: Try to match the question to each rule: unification l  Step 3: Q unifies with Rule1 à X = saturday l  Step 4: new goal rains(saturday)? l  Step 5: rains(saturday) is a fact à rains(saturday) is true à cold(saturday) is true

Page 8: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 8

¢  Every inference l  Step 1 (unification): unify the question with rules of the KB l  Step 2 (reduction): replaces the original question with other questions

derived from the chosen rule

¢  Exercise: l  add of rains(monday). l  Question: cold(Which_Day)? l  a question may have multiple different answers

¢  Prolog reasoning l  Search in the list of K (from top to down) l  Process of conditions from left to right l  If the goal matches with the head of a rule, it is then replaced with its body l  After a success or failure, Prolog goes back to the most recent list where a

rule can still be satisfied

Prolog Reasoning

Page 9: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 9

Prolog Reasoning

¢  Prolog Feature backtracking: if a track fails, Prolog backs to the last choice and tries another track

Page 10: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 10

¢  Prolog+CG Program = Rule* .

¢  Rule = Goal [":-" Goal {"," Goal}*] "." .

¢  Goal = Java_Message | SimpleGoal .

¢  SimpleGoal = Identifier | String | Variable | Term .

¢  Java_Message = (Identifier | String | Variable) ":" (Term | Variable)

Structure of a Prolog Program

Page 11: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 11

Concepts Comments Examples Regular types

integer, real, boolean, string

4 8.65 true "my string"

identifier (symbol)

An identifier begins with at least two letters followed with 0 or many characters

ali rabat

List Begins with ‘[‘ and ends with ‘]’ [ ali, 3, "my string" ]

Variable A variable begins with underscore followed with 0 or many characters ...

_X _XY _ r_x r2

Term (predicate) A term is a structure or relation between multiple objects (variable, identifier, regular type, list, term, etc.) parents(_child,_father,_mother)

father(_child,_father)

Fact A fact is a term ending with a dot .

// les faits (commentaire) parents(rachid, ali, aicha). parents(imane, ali, aicha). parents(jamal, ali, aicha). parents(hicham,khalid,fatima).

Rule

A rule of type P :-c1,c2,c3. Is interpreted as: predicate P is true if the conditions (predicates) c1, c2 et c3 are also true A rule end with dot and the conditions are separated with commas

// rules father(_child,_father) :- parents(_child,_father,_).

Predefined Predicate

write,writeln,read,readln,fail, !

Comments Java-like // this is a comment

Page 12: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 12

Installing and executing Prolog+CG

¢  Install jdk ¢  download Amine platform

l https://sourceforge.net/projects/amine-platform/ ¢ Copy the file runAmine.bat & Make a shortcut

on your desktop l or aminePlatform8.jar

¢ Double click ¢ Click Prolog+CG icon

Page 13: Prolog - emi.ac.ma 2 - Prolog.pdf · Karim Bouzoubaa Artificial Intelligence 3 ! Prolog is an AI tool ! PROLOG = PROgrammation LOGique ! In 1972 by Alain Colmerauer, Université de

Karim Bouzoubaa Artificial Intelligence 13

¢  To execute a Prolog+CG program l  Launch Prolog+CG l  Edit the program / open it l  Create a new console (if not already)

•  menu console/new

l  Load the program in memory and compile it •  menu console/consult

l  Type the question/goal in the Console menu •  Verify the validity of given arguments

•  parents(rachid, ali, aicha). •  Search possible instances for variables

•  father(_X, ali). •  To obtain all possible solutions, type ";" then "enter"

¢  To debug a program (question), activate debug mode l  menu console/Debug

¢  Example programs: l  cold1.pcg / cold2.pcg / familyTree.pcg / meal.pcg

Executing Prolog+CG