Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Hemant Kamat
Hemant.Kamat@shalaka.com
Agenda
Overview
Declarative Programming
Predicate Calculus
PROLOG Program Structure
Visual PROLOG Program Structure
Overview
PROgramming in LOGic
4G Programming language
Logic Programming language
Uses variation of predicate logic
Declarative Programming
Facts What is known
Rules What you can infer [from given facts]
Facts and Rules Stored in Knowledgebase
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
Variables
begin with Uppercase letter
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
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
10
short
ushort
long
ulong
integer
unsigned
byte
word
dword
11
char
real
string
symbol
12
13
14
Backtracking
Process of choosing an alternate path in
logical reasoning if one path fails
15
not predicate
Succeeds when the subgoal cannot be proven
true
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
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
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
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
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*
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
22
PROLOG Comments
/* This is a Comment */
23
24
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
25
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
26
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
27
28
readint(X)
readreal(X)
readchar(X)
readln(Line)
readterm(DomainName,Term)
December 19, 2016
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
readblock(Length,Bterm)
writeblock(Length,Bterm)
file_bin(FileName,BinTerm)
openread(SymbolicFileName, OSFileName)
openwrite(SymbolicFileName, OSFileName)
openappend(SymbolicFileName, OSFileName)
openmodify(SymbolicFileName, OSFileName)
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
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
32
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)
33
subchar(String,Position,Char)
substring(Str_in,Pos,Len,Str_out)
searchchar(String,Char,Position)
searchstring(SourceStr,SearchStr,Pos)
34
char_int(Char, Integer)
str_char(String, Char)
str_int(String, Integer)
str_real(String, Real)
upper_lower(Upper, Lower)
35
36
PROLOG
End of Session II
37