Sei sulla pagina 1di 37

PROLOG

The Declarative Programming


Language

Hemant Kamat
Hemant.Kamat@shalaka.com

Agenda

Overview
Declarative Programming
Predicate Calculus
PROLOG Program Structure
Visual PROLOG Program Structure

December 19, 2016

Confidential & Proprietory

Overview
PROgramming in LOGic
4G Programming language
Logic Programming language
Uses variation of predicate logic

Declarative vs Procedural Language


"What to do" vs "How to do"

Facts and Rules base


Inference Engine
Process for reasoning logically
Pattern matcher
December 19, 2016

Confidential & Proprietory

Declarative Programming
Facts What is known
Rules What you can infer [from given facts]
Facts and Rules Stored in Knowledgebase

Queries use the rules and facts to infer


new "knowledge"
If a query succeeds in inferring new
knowledge, flow proceeds to next query
If a query fails in inferring new knowledge,
it backtracks to apply another rule
December 19, 2016

Confidential & Proprietory

Predicate Calculus
Predicates are symbolic names of Relations
Variables are General Clauses
Anonymous Variables represented by lone
underscore "_"
Used to ignore unwanted output values
Goals are Queries
Simple
Compound
Conjunctions
Both Subgoals have to be true. Subgoals separated by
","

Disjunctions
Either Subgoal has to be true. Subgoals separated by ";"
December 19, 2016

Confidential & Proprietory

PROLOG Program Structure


Constants
begin with lower case letters or enclosed in
single quotes

Variables
begin with Uppercase letter

December 19, 2016

Confidential & Proprietory

PROLOG Program Structure


Clauses
Two types of phrases
Facts and Rules
Facts are relations or properties that the
Domain Expert knows to be true
Rules are dependent relations; they allow Prolog
to infer one piece of information from another

Facts are of the form


property(object1, object2, object3, objectN)
relation(object1, object2, object3, objectN)
December 19, 2016

Confidential & Proprietory

PROLOG Program Structure


Clauses
Rules are of the form
Head:-Body
relation(object,object,...,object):relation(object,...,object),
.
.
relation(object,...,object).

Objects, Properties and Relations names


start with lowercase alphabet followed by any
combination of alphabets (lowercase or
uppercase), digits and underscore characters
December 19, 2016

Confidential & Proprietory

Visual PROLOG Program Structure


Sections
Clauses
Facts and Rules placed in this section

Predicates
User-defined predicates in the clauses section declared
in this section

Domains
User defined names to data types to make them
distinctive

Goal
Automatically executed when the PROLOG program runs
Same as the body of a rule
December 19, 2016

Confidential & Proprietory

Visual PROLOG Program Structure


Sections
Facts
Dynamic Facts placed in this section

Constants
User-defined symbolic constants declared in this section

Global
Declare global domains, global facts, global predicates

Compiler Directives
include "file"
Same as the body of a rule
December 19, 2016

Confidential & Proprietory

10

Visual PROLOG Program Structure


Domains
Integral Standard Domains

short
ushort
long
ulong
integer
unsigned
byte
word
dword

December 19, 2016

Confidential & Proprietory

11

Visual PROLOG Program Structure


Domains
Basic Standard Domains

char
real
string
symbol

December 19, 2016

Confidential & Proprietory

12

Visual PROLOG Program Structure


Data Objects
Simple Data Objects
Characters are char type
Numbers are of integer or real type
Atoms are of symbol or string type

Compound Data Objects


Functors
Holds its arguments together and does not
represent a computation to be performed
December 19, 2016

Confidential & Proprietory

13

Visual PROLOG Program Structure


Predicate Arity
Number of arguments a predicate takes
Multiple Arity
Two predicates with the same name can
have different arity
Predicates with same name but different
arity grouped together in the Predicates
and Clauses sections
December 19, 2016

Confidential & Proprietory

14

Unification and Backtracking


Unification
Process of matching a goal with a clause
If matching is successful then free variables
are bound to values so that the goal and the
clause are identical

Backtracking
Process of choosing an alternate path in
logical reasoning if one path fails

December 19, 2016

Confidential & Proprietory

15

Unification and Backtracking


Controlling the Search
fail predicate
Forces failure and hence backtracking

Cut (!) predicate


Prevents backtracking

not predicate
Succeeds when the subgoal cannot be proven
true

December 19, 2016

Confidential & Proprietory

16

Backtracking
Fail predicate
DOMAINS
name = symbol
PREDICATES
father(name, name)
everybody
CLAUSES
father(leonard,katherine).
father(carl,jason).
father(carl,marilyn).
everybody:father(X,Y),
write(X," is ",Y,"'s father\n"),
fail.
December 19, 2016

Confidential & Proprietory

17

Backtracking
Fail predicate
GOAL
father(X,Y).

Results
X=leonard, Y=katherine
1 Solutions

-------------------------------GOAL
everybody.

Results
leonard is katherine's father
carl is jason's father
carl is marilyn's father
December 19, 2016

Confidential & Proprietory

18

Backtracking
Cut predicate
PREDICATES
friend(symbol,symbol)
girl(symbol)
likes(symbol,symbol)
CLAUSES
friend(bill,jane):girl(jane),
likes(bill,jane),!.
friend(bill,jim):likes(jim,baseball),!.
friend(bill,sue):girl(sue).
girl(mary).
girl(jane).
girl(sue).
likes(jim,baseball).
likes(bill,sue).
December 19, 2016

Confidential & Proprietory

19

Backtracking
Cut predicate
goal
friend(bill, Who).
Without Cut in friends clause:
Who=Jim
Who=Sue
2 Solutions
With Cut in friends clause:
Who=Jim
1 Solutions

Cut predicate makes clauses deterministic


December 19, 2016

Confidential & Proprietory

20

PROLOG Lists
List is an Object that contains an
arbitrary number of other objects within it
e.g: [1,2,3,4,5]
Declaring a List
DOMAINS
integerlist=integer*

Head is the first element in the List


Tail is the remaining part of the List
excluding the Head
December 19, 2016

Confidential & Proprietory

21

PROLOG Lists
Using a List
DOMAINS
list = integer*
PREDICATES
write_a_list(list)
CLAUSES
write_a_list([]).
write_a_list([H|T]):write(H),nl,
write_a_list(T).
GOAL
write_a_list([1, 2, 3]).
December 19, 2016

Confidential & Proprietory

22

PROLOG Comments
/* This is a Comment */

/* This is also a comment but


spread over more than one line */

% Another example of a comment


December 19, 2016

Confidential & Proprietory

23

PROLOG Internal Database


Internal database is a collection of facts to which more
facts of the same type can be added to or removed from
Declaring Facts Database
Facts Section

Predicates declared in facts section are used like other


predicates in the clauses section and these predicates
can be inserted or removedfrom the facts database at
runtime
Predicates declared in facts section can be added as
facts and not as rules
Facts in fact database cannot have free variables
December 19, 2016

Confidential & Proprietory

24

PROLOG Internal Database


Updating facts Database
Adding facts at runtime

asserta
Assert a fact into the fact database before
all existing facts

assertz
Assert a fact into the fact database after all
existing facts

assert
Behaves same as assertz
December 19, 2016

Confidential & Proprietory

25

PROLOG Internal Database


Updating facts Database
Removing facts at runtime

retract
Removes the first fact in the facts database
that matches the fact argument

retractall
Removes the all facts in the facts database
that match the fact argument

December 19, 2016

Confidential & Proprietory

26

PROLOG Internal Database


Updating facts Database
Loading facts from a file at runtime

consult
Reads in a file and appends all facts in the
facts section to the end of the internal
database
Saving facts to a file at runtime

save
Saves facts to a file
December 19, 2016

Confidential & Proprietory

27

Arithmetic and Comparison


in PROLOG
Arithmetic operations
Arithmetic functions and predicates
Relational Operations
Comparison Characters, Strings and
Symbols

December 19, 2016

Confidential & Proprietory

28

Writing, Reading in PROLOG


write(Param1,Param2,Param3,ParamN)
nl inserts a newline
writef(FormatString,Param1,Param2,ParamN)
FormatString= %-m.pf

readint(X)
readreal(X)
readchar(X)
readln(Line)
readterm(DomainName,Term)
December 19, 2016

Confidential & Proprietory

29

Files in PROLOG
file_str(FileName, Text)
Reads and Writes from and to a file
Reads from file if the Text variable is a free variable

Binary block transfer

readblock(Length,Bterm)
writeblock(Length,Bterm)
file_bin(FileName,BinTerm)
openread(SymbolicFileName, OSFileName)
openwrite(SymbolicFileName, OSFileName)
openappend(SymbolicFileName, OSFileName)
openmodify(SymbolicFileName, OSFileName)

December 19, 2016

Confidential & Proprietory

30

Files in PROLOG
filemode(SymbolicFileName, FileMode)
If FileMode=0, then SymbolicFileName is set to binary
filemode else
set to text filemode

closefile(SymbolicFileName)
readdevice(SymbolicFileName)
writedevice(SymbolicFileName)
filepos(SymbolicFileName, FilePosition, Mode)
eof(SymbolicFileName)
flush(SymbolicFileName)
December 19, 2016

Confidential & Proprietory

31

Files in PROLOG

existfile(OSFileName)
searchfile(PathList,Name,FoundName)
deletefile(OSFileName)
renamefile(OldOSFileName, NewOSFileName)
disk(Path)
copyfile(SourceName,DestinationName)
filenamepath(QualName,Path,Name)
filenameext(Name,Name,Ext)
December 19, 2016

Confidential & Proprietory

32

String Processing in PROLOG

frontchar(String1,Char,String2)
fronttoken(String1, Token, Rest)
frontstr(NumberOfChars, String1, StartStr, EndStr)
frontstr(NumberOfChars, String1, StartStr, EndStr)
str_len(StringArg, Length)
isname(String)
format(OutputString,FormatString,Arg1,Arg2,Arg3,..
.,ArgN)

December 19, 2016

Confidential & Proprietory

33

String Processing in PROLOG

subchar(String,Position,Char)
substring(Str_in,Pos,Len,Str_out)
searchchar(String,Char,Position)
searchstring(SourceStr,SearchStr,Pos)

December 19, 2016

Confidential & Proprietory

34

Type Conversion in PROLOG

char_int(Char, Integer)
str_char(String, Char)
str_int(String, Integer)
str_real(String, Real)
upper_lower(Upper, Lower)

December 19, 2016

Confidential & Proprietory

35

Interfacing PROLOG with other


Languages
Mixed Language development
Make best of both worlds!!
Using DLL's

December 19, 2016

Confidential & Proprietory

36

PROLOG

End of Session II

December 19, 2016

Confidential & Proprietory

37

Potrebbero piacerti anche