13
Introduction to Prolog Prolog Programs are: declarative language for symbolic programming (non-numeric) easy to create objects and relationships between objects consist of Facts and Statements (Clauses) they are “case” sensitive upper case used for variable names lower case used for facts Output can be VERY terse i.e. Yes or NO unless you explicitly provide output statements Goals are tests we perform to check the truth of certain statements There are NO For loops Repeat While loops IF..THEN statements Type declarations Gotos Arrays All of the above is handled by recursion

Introduction to Prolog

  • Upload
    ghita

  • View
    12

  • Download
    0

Embed Size (px)

DESCRIPTION

Introduction to Prolog. Prolog Programs are: declarative language for symbolic programming (non-numeric) easy to create objects and relationships between objects consist of Facts and Statements (Clauses) they are “case” sensitive upper case used for variable names lower case used for facts - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to Prolog

Introduction to Prolog

Prolog Programs are: declarative language for symbolic programming (non-numeric) easy to create objects and relationships between objects consist of Facts and Statements (Clauses) they are “case” sensitive

upper case used for variable names lower case used for facts

Output can be VERY terse i.e. Yes or NO unless you explicitly provide output statements

Goals are tests we perform to check the truth of certain statements There are NO

For loops Repeat While loops IF..THEN statements Type declarations Gotos Arrays

All of the above is handled by recursion

Page 2: Introduction to Prolog

Introduction to Prolog

SWI-Prolog :

Page 3: Introduction to Prolog

Introduction to PrologSWI-Prolog Tutorial:

The SWI-Prolog editor is divided into a section for code where several source files can be accessed and a command line interface for interrogating the knowledge base. Begin by creating a new program from the File menu and selecting New. Enter the code from listing 2.0, save the file to your H: drive. Go to Start from the menu and select Consult. If all goes well the following output will be sent to the window:

?- consult('C:/prolog_code/example1.pl'). % C:/prolog_code/example1.pl compiled 0.00 sec, 0 bytes Try the following goals in the command line window: ?- can_marry(jane,paul). ?- can_marry(paul,jane). Now create a new file for Listing 1.0 and run it.

Page 4: Introduction to Prolog

Introduction to PrologSWI-Prolog Tutorial:

male(fred).

male(spot).

male(paul).

female(tigger).

female(jane).

female(sue).

human(paul).

human(fred).

human(jane).

cat(tigger).

dog(spot).

married(sue).

can_marry(Person1,Person2):-

%write(" can ", Person1, " marry ", " ",Person2," ? "),

human(Person1),

human(Person2),

male(Person1),

female(Person2),

not(married(Person1)),

not(married(Person2)).

can_marry(fred,paul). % this is the goal, try entering different combinations of people

Page 5: Introduction to Prolog

Programming in Visual Prolog

Our first Prolog program FAMILY.PL

parent( ken, rebecca). % identifies ken as the parent of Rebecca - note full stop

parent( fred, bob).

parent( ken, bob). % this statement is what we are seeking to prove/disprove

% parent (ken,rebecca).

Page 6: Introduction to Prolog

Programming in Visual Prolog

Version 2 FAMILY.PL

parent( ken, rebecca). % identifies ken as the parent of rebecca - note full stop

parent(ken,ken_junior).

parent( fred, bob).

parent( steven, bob).

parent( ken, X). % use a variable to list all children

% parent (ken,rebecca).

% parent(Parent,Child). % give variables meaningful names where possible

Page 7: Introduction to Prolog

Programming in Visual Prolog

Version 3 FAMILY.PLparent( ken, rebecca). %

parent(ken,ken_junior).

parent( fred, bob).

parent( steven,bob).

parent( X, Y) , parent(Y, Z). % does anyone have grandchildren?

Page 8: Introduction to Prolog

Programming in Visual Prologstudent(male,harry,biol).

student(female,anna,biol).

student(male,fred,math).

student(male,bob,computing).

student(male,john,chem).

student(female,liz,biol).

student(female,susan,math).

student(female,caron,chem).

student( X , Y , biol). % using variables with a fact

% student(male, X, _ ). % using blank variables

Page 9: Introduction to Prolog

PEOPLE.PL Ver 1.0

displaylist(list)

people1(list)

people2(list)

people1([joe,fred,anne,susan]).

people2([tina,caron,alan,tom,john]).

member(X,[X|_]).

member(X,[_|Y]):-member(X,Y).

displaylist([]).

displaylist([H|T]):-

write("person is - ",H),

nl,

displaylist(T).

people1(X), displaylist(X).

Page 10: Introduction to Prolog

PEOPLE.PL Ver 2.0Domains

list = symbol*

Predicates

nondeterm member(symbol,list)

nondeterm getlist(list).

displaylist(list)

people1(list)

people2(list)

append(list,list,list)

count(list,integer)

clauses

people1([joe,fred,anne,susan]).

people2([tina,caron,alan,tom,john]).

member(X,[X|_]).

member(X,[_|Y]):-member(X,Y).

displaylist([]).

displaylist([H|T]):-

write("person is - ",H),

nl,

displaylist(T).

append([],L,L).

append([H|T],M,[H|T1]):- append(T,M,T1).

count([],0).

count([_|T],N):-count(T,M),N = 1+M.

displaylist([]).

displaylist([H|T]):-

write("person is - ",H),

nl,

displaylist(T).

getlist([]).

getlist([H|T]):-

write("Enter item: "),readln(X),nl,

not(X=end),H=X,getlist(T).

Goal

%people2(X), member(alan,X), count(X,M).

%people1(Y), people2(X), append(X,Y,Z), displaylist(Z).

getlist(M),nl,displaylist(M).

Page 11: Introduction to Prolog

More Prolog Features

Backtracking: Backtracking enables the solution to a goal to be pursued should a sub-goal fail. It tries

all possible solutions before admitting failure. The exhaustive search method employed by backtracking is a depth first search. Prolog keeps track of everything using internal markers I.e. to remember which clauses are true and which are false. These can cause stack overflow problems in programs using many recursive rules.

Control Backtracking because may not need all the possible solutions - some may not even be useful. may just need a single solution too much backtracking can lead to slow program execution

Fail The Fail command always causes backtracking to occur Mainly used to list out data from a number of facts

The Cut The cut “!” command is used to make programs more efficient backtracking cannot retrace its steps past a cut cut is also used to form repetitive loops and to make rules mutually exclusive

Page 12: Introduction to Prolog

Programming in Visual Prolog/* backtrack1.pro uses the fail command to force backtracking*/

predicates

listplanes()

nondeterm aeroplane(symbol)

clauses

aeroplane(hurricane).

aeroplane(spitfire).

aeroplane(comet).

aeroplane(harrier).

aeroplane(jumbo).

aeroplane(dakota).

listplanes:- aeroplane(Plane),write(Plane),nl,fail.

goal

listplanes().

Page 13: Introduction to Prolog

/* backtrack2.pl uses the cut command to stop backtracking*/

predicates

nondeterm go()

nondeterm menu()

nondeterm repeat()

nondeterm choice(symbol)

clauses

repeat.

repeat:-repeat.

go:- write("CUT MENU"),nl,nl,

repeat,

menu.

menu:- write("Enter a, b, or e to end"),

readln(X), choice(X), nl, !, X=e.

choice(a):-write("Choice a").

choice(b):-write("Choice b").

choice(e):-write("Goodbye").

choice(_):-write("Try again.").