F unction Symb ols in Prolog F rom Deductive Databases to - - PDF document

f unction symb ols in prolog f rom deductive databases to
SMART_READER_LITE
LIVE PREVIEW

F unction Symb ols in Prolog F rom Deductive Databases to - - PDF document

F unction Symb ols in Prolog F rom Deductive Databases to Logic Programs In logic there a re t w o kinds of objects p redicates and functions Predicates rep resent statements ab out the w o


slide-1
SLIDE 1 F unction Symb
  • ls
in Prolog F rom Deductive Databases to Logic Programs In logic there a re t w
  • kinds
  • f
  • bjects
p redicates and functions
  • Predicates
rep resent statements ab
  • ut
the w
  • rld
John hates Mary hatesjohnmary John is short shortjohn hates is a p redicate symb
  • l
shortjohn is an atomic fo rmula
  • F
unction terms rep resent
  • bjects
in the w
  • rld
the mother
  • f
Mary motherofmary a rectangle
  • f
length
  • and
width
  • rectangle
motherofmary is a function term rectangle is a function symb
  • l
slide-2
SLIDE 2 F unction terms do not have values In Pro log they act as data structures let pXY denote a p
  • int
in dim space let pXYZ denote a p
  • int
in dim space W rite a Prolog p rogram SQDISTPointPointD that returns the squa re
  • f
the distance b e t w een t w
  • p
  • ints
The p rogram should w
  • rk
fo r
  • and
dim p
  • ints
W ant SQDISTp p D returns D
  • and
SQDISTp p D returns D
  • and
SQDISTp p D is undefined
slide-3
SLIDE 3 Prolog Program
  • SQDISTpXY
pXY D
  • XD
is XX YD is YY D is XDXD
  • YDYD
  • SQDISTpXYZ
pXYZ D
  • XD
is XX YD is YY ZD is ZZ D is XDXD
  • YDYD
  • ZDZD
Query
  • SQDISTp
p D This query unies with the head
  • f
rule
  • with
fXn Yn Xn Yng so XD is XX
  • YD
is YY
  • D
is
  • So
D is returned Note the query do es not unify with the head
  • f
rule
  • so
  • nly
rule
  • is
used
slide-4
SLIDE 4 Prolog Program
  • SQDISTpXY
pXY D
  • XD
is XX YD is YY D is XDXD
  • YDYD
  • SQDISTpXYZ
pXYZ D
  • XD
is XX YD is YY ZD is ZZ D is XDXD
  • YDYD
  • ZDZD
Query
  • SQDISTppD
This query unies with the head
  • f
rule
  • with
fXn Yn Zn Xn Yn Zng so XD is
  • YD
is
  • ZD
is
  • D
is
  • So
D is returned Note the query do es not unify with the head
  • f
rule
  • so
  • nly
rule
  • is
used
slide-5
SLIDE 5 Prolog Program
  • SQDISTpXY
pXY D
  • XD
is XX YD is YY D is XDXD
  • YDYD
  • SQDISTpXYZ
pXYZ D
  • XD
is XX YD is YY ZD is ZZ D is XDXD
  • YDYD
  • ZDZD
Query
  • SQDISTp
p D Note this query do es not unify with any rule so Prolog simply returns no ie no answ ers fo r D
slide-6
SLIDE 6 Returning F unction T erms as Answ ers eg
  • given
a p
  • int
pXY return a new p
  • int
with double the co
  • rdinates
eg
  • Query
  • doublepP
Answ er
  • P
  • p
Prolog Program doublepXY pXY
  • X
is X Y is Y In Plain English if X
  • X
and Y
  • Y
then the double
  • f
pXY is pXY An equivalent p rogram using
  • doublepXY
P
  • X
is X Y is Y P
  • pXY
Here
  • is
b eing used to assign a value to va riable P T ry to avoid this It reects p ro cedural thinking
slide-7
SLIDE 7 Sample Execution Prolog Program doublepXY pXY
  • X
is X Y is Y Query
  • doublepP
The query unies with the head
  • f
the rule where the mgu is fXn Yn PnpXYg The b
  • dy
  • f
the rule then evaluates X is X ie
  • Y
is Y ie
  • The
mgu b ecomes fXn Yn Pnpg So the answ er is P
  • p
slide-8
SLIDE 8 Recursion with F unction Symb
  • ls
Example Electrical circuits
  • Tw
  • resisto
rs in series
  • with
resistances R
  • and
R
  • resp
ectively
  • T
  • tal
resistance
  • f
the circuit is
  • Can
rep resent the circuit as a function term series
  • Tw
  • resisto
rs in pa rallel
  • T
  • tal
resistance
  • f
the circuit is
  • Rep
resent the circuit as a function term par
slide-9
SLIDE 9 Mo re Complex Circuits
  • par
series
  • seriespar
par
slide-10
SLIDE 10 Problem W rite a Prolog p rogram that computes the total resistance
  • f
any circuit F
  • r
example Query
  • resistanceseries
R Answ er
  • R
  • Query
  • resistancepar
R Answ er
  • R
  • Query
  • resistanceseriespar
R Answ er
  • R
  • Query
  • resistance
R Answ er
  • R
slide-11
SLIDE 11 Solution
  • resistanceRR
  • numberR
  • resistanceseriesCC
R
  • resistanceC
R resistanceC R R is RR
  • resistanceparCC
R
  • resistanceCR
resistanceCR R is RRRR Sample Query resistanceseriespar TR ie compute the total resistance TR
  • f
the follo wing circuit
slide-12
SLIDE 12

rule (1) R1 = 3

UNWINDING PHASE RECURSIVE (WINDING) PHASE

rule (2) rule (1) rule (1) R21 = 6 R22 = 3 resistance(series(3,par(6,3)),R), R = 5 resistance(3,R1), resistance(par(6,3),R2), R is R1+R2 is 5 resistance(6,R21), resistance(3,R22), R2 is (R21*R22)/(R21+R22) is (6*3)/(6+3) is 2

a

slide-13
SLIDE 13