Structure of the program in Prolog. Execution management

Содержание

Слайд 2

Peculiarities of Visual Prolog Visual Prolog is a compiled language Other

Peculiarities of Visual Prolog

Visual Prolog is a compiled language
Other versions have

elements of interpretation of a code during the execution
Strict data typing
Rules are not data: you can’t add or remove them during the execution
You can’t define new operations
Слайд 3

Program sections compiler directives CONSTANTS – section of constants description DOMAINS

Program sections

compiler directives
CONSTANTS – section of constants description
DOMAINS – section

of domains description
DATABASE – section of description of internal database predicates
PREDICATES – section of predicates description
CLAUSES – section of clauses description
GOAL – section of internal goal description
Слайд 4

Peculiarities of Visual Prolog All sections can be in any order

Peculiarities of Visual Prolog

All sections can be in any order
Predicates and

domain must be defined before their usage
Predicates declared in a section DATABASE can be added and removed from internal database while the execution
Слайд 5

Program containing only the goal GOAL write("hello"), readchar(_).

Program containing only the goal

GOAL
write("hello"), readchar(_).

Слайд 6

Compiler directives trace – to trace the program nowarnings – to

Compiler directives

trace – to trace the program
nowarnings – to suppress the

message that a variable occurs only once
include – insertion of some file content
check_determ – compulsory check of predicates determination
Слайд 7

Peculiarities of Visual Prolog You can start tracing only for the

Peculiarities of Visual Prolog

You can start tracing only for the definite

predicate
If there is a tracing the optimization of recursion is not working
To establish non-determination of predicates by default: Options – Project – Compiler options – Warnings – Default predicate type – Nondeterm
Слайд 8

Constants CONSTANTS pi=3.14 path="c:\\prolog\\bgi"

Constants

CONSTANTS
pi=3.14
path="c:\\prolog\\bgi"

Слайд 9

Domains integer – an integer number (-32768...32767) real – a float

Domains

integer – an integer number (-32768...32767)
real – a float number (±e-307...±e308)
char

– a symbol in apostrophes
string – a sequence of symbols in double quotation marks
symbol – a symbolic constant (atom)
file – a file
Слайд 10

Description of your own domain = or file = ; ...;

Description of your own domain

=
or
file = ;

...;
or
= *
Examples:
DOMAINS
i=integer
list=i*
Слайд 11

Description of a structured domain = ( , ..., ) [;

Description of a structured domain

= (, ..., ) [;(...)]*
Examples:
flatpoint

= p(integer, integer)
triangle = tr(point, point, point)
fullpoint = p(integer, integer); p(integer, integer, integer)
Слайд 12

Description of predicates ( , ..., ). Examples: PREDICATES mother(string,string). member(integer,integer*).

Description of predicates

(, ..., ).
Examples:
PREDICATES
mother(string,string).
member(integer,integer*).
member(real,real*).
member(char,char*).
member(string,string*).

One and the same predicate will

work with different data of different domains
Слайд 13

Standard predicates readln(_) readint(_) readreal(_) readchar(_) readterm(name_domain, term_domain) write([ ,…]) writef

Standard predicates

readln(_)
readint(_)
readreal(_)
readchar(_)
readterm(name_domain, term_domain)
write([,…])
writef – format output

nl
upper_lower(_,_)
str_int(_,_)
str_real(_,_)
str_char(_,_)
char_int(_,_)
true
fail
free(_)
bound(_)

Слайд 14

Standard predicates div() mod() trunc() round() random(_) random( ,_) All embedded predicates are determinated

Standard predicates

div()
mod()
trunc()
round()
random(_)
random(<число>,_)
All embedded predicates are determinated

Слайд 15

Program “Relatives” DOMAINS s=string PREDICATES nondeterm mother(s,s) nondeterm grandmother(s,s) CLAUSES mother("Наташа","Даша").

Program “Relatives”

DOMAINS
s=string
PREDICATES
nondeterm mother(s,s)
nondeterm grandmother(s,s)
CLAUSES
mother("Наташа","Даша").
mother("Даша","Маша").
grandmother(X,Y):-
mother(X,Z),
mother(Z,Y).

Well-formed program:
Input

an empty line between procedures
Start a body of a rule with an indent
Start each sub-goal from a new line with an indent
Print all grandmothers
Print all mothers
Print all mothers by pressing the button
Слайд 16

Execution management Method of a depth search (backtracking) Method of a

Execution management

Method of a depth search (backtracking)
Method of a rollback after

a failure
Cut and rollback
Method of a user-defined search
Слайд 17

Backtracking DOMAINS s=string PREDICATES mother(s,s) grandmother(s,s) CLAUSES mother(“Dasha",“Masha"). mother(“Natasha",”Dasha"). mother(“Natasha",”Glasha"). mother(“Dasha",”Sasha"). grandmother(X,Y):– mother(X,Z), mother(Z,Y). GOAL grandmother(B,V).

Backtracking

DOMAINS
s=string
PREDICATES
mother(s,s)
grandmother(s,s)
CLAUSES
mother(“Dasha",“Masha").
mother(“Natasha",”Dasha").
mother(“Natasha",”Glasha").
mother(“Dasha",”Sasha").
grandmother(X,Y):–
mother(X,Z),
mother(Z,Y).

GOAL
grandmother(B,V).

Слайд 18

Method of a rollback after a failure DOMAINS s=string PREDICATES mother(s,s)

Method of a rollback after a failure

DOMAINS
s=string
PREDICATES
mother(s,s)
grandmother(s,s)
CLAUSES
mother(“Dasha",“Masha").
mother(“Natasha",”Dasha").
mother(“Natasha",”Glasha").
mother(“Dasha",”Sasha").


grandmother(X,Y):–
mother(X,Z),
mother(Z,Y).

GOAL
grandmother(B,V)

,
write(“Grandmother’s name – “,B),
write(“Granddaughter's name – “,V).

Слайд 19

Method of a rollback after a failure DOMAINS s=string PREDICATES mother(s,s)

Method of a rollback after a failure

DOMAINS
s=string
PREDICATES
mother(s,s)
grandmother(s,s)
CLAUSES
mother(“Dasha",“Masha").
mother(“Natasha",”Dasha").
mother(“Natasha",”Glasha").
mother(“Dasha",”Sasha").


grandmother(X,Y):–
mother(X,Z),
mother(Z,Y).

GOAL
grandmother(B,V)

,
write(“Grandmother’s name – “,B), write(“Granddaughter's name – “,V), nl, fail.

Слайд 20

Method of a rollback after a failure DOMAINS s=string PREDICATES mother(s,s)

Method of a rollback after a failure

DOMAINS
s=string
PREDICATES
mother(s,s)
grandmother(s,s)
show_names
CLAUSES
mother(“Dasha",“Masha").
mother(“Natasha",”Dasha").
mother(“Natasha",”Glasha").
mother(“Dasha",”Sasha").


grandmother(X,Y):–
mother(X,Z),
mother(Z,Y).

GOAL
write(“Daughters’ names: ”),nl,
show_names.

show_names:–
mother(_,Name),
write(" ", Name), nl,
fail.

Слайд 21

Method of a rollback after a failure DOMAINS s=string PREDICATES mother(s,s)

Method of a rollback after a failure

DOMAINS
s=string
PREDICATES
mother(s,s)
grandmother(s,s)
show_names2(s)
CLAUSES
mother(“Dasha",“Masha").
mother(“Natasha",”Dasha").
mother(“Natasha",”Glasha").
mother(“Dasha",”Sasha").


grandmother(X,Y):–
mother(X,Z),
mother(Z,Y).

GOAL
write(“Dasha’s daughters names: ”),nl,
show_names2(“Dasha”).

show_names2(Mother):–
mother(M,Name),
M=Mother,
write(" ", Name), nl,
fail.

Слайд 22

Cut and rollback DOMAINS s=string PREDICATES mother(s,s) grandmother(s,s) show_names3(s) CLAUSES mother(“Dasha",“Masha").

Cut and rollback

DOMAINS
s=string
PREDICATES
mother(s,s)
grandmother(s,s)
show_names3(s)
CLAUSES
mother(“Dasha",“Masha").
mother(“Natasha",”Dasha").
mother(“Natasha",”Glasha").
mother(“Dasha",”Sasha").
grandmother(X,Y):–
mother(X,Z),
mother(Z,Y).


show_names3(Daughter):–
mother(_,Name),
write(" ", Name), nl,
Name=Daughter,
write(“We found her!"),!.
GOAL
write(“All daughters up to Glasha: ”),nl,
show_names3(“Glasha”).