prolog syntax unification n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Prolog syntax + Unification PowerPoint Presentation
Download Presentation
Prolog syntax + Unification

Loading in 2 Seconds...

play fullscreen
1 / 16

Prolog syntax + Unification - PowerPoint PPT Presentation


  • 106 Views
  • Uploaded on

Prolog syntax + Unification. t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/. Prolog Syntax. Terms (Data objects) Atoms (symbolic) constants E.g., anna, x_25, ‘Tom_’, ::=, <--->, etc Numbers E.g., -25, 100.25e+5, etc Variables E.g., X, Result, _23, _, AnonymousVariable, etc

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Prolog syntax + Unification' - carney


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
prolog syntax unification

Prolog syntax + Unification

t.k.prasad@wright.edu

http://www.knoesis.org/tkprasad/

L2PrologUnify

prolog syntax
Prolog Syntax
  • Terms (Data objects)
    • Atoms
      • (symbolic) constants
        • E.g., anna, x_25, ‘Tom_’, ::=, <--->, etc
      • Numbers
        • E.g., -25, 100.25e+5, etc
      • Variables
        • E.g., X, Result, _23, _, AnonymousVariable, etc
    • Structures
        • E.g., f(X,23,g(‘Tom’,2)), etc
  • Formulae (Predicate-logic clauses)
    • Facts
    • Rules

L2PrologUnify

using variables
Using variables

Lexical scope of a variable is one clause (fact/rule).

Multiple named variable occurrences can be used to capture “equality” constraints.

Bindings to distinct variables are independent (but can be same too).

Multiple anonymous variable occurrences are considered distinct.

eq(X,X).

?-eq(a,b).

No

?-eq(b,b).

Yes

?-eq(A,B).

A = B.

p(X,Y).

?-p(a,b).

Yes

q(_,_).

?-q(a,b).

L2PrologUnify

anonymous variable
Anonymous Variable
  • p(X,Y).is equivalent to p(_,_).
    • Informally, it is equivalent to:

For all x, for all y: p(x, y) holds.

  • ?- p(a,_).
    • Informally, it is equivalent to:

There exists x, such that p(a, x) holds.

L2PrologUnify

cont d
(cont’d)
  • eq(X,X).
  • ?- eq(1,Ans).

Ans = 1

    • answer extraction
  • q(_,_).
  • ?- q(1,Ans).

Ans unbound

    • interpreted as: for all x, q(1,x) holds

L2PrologUnify

structures records
Structures (~ records)

arguments

functor

[name/arity]

Structures encode labelled trees (or DAGs if they contain variables).

add(5, mul(a,b))

L2PrologUnify

matching
Matching
  • Basic operation (cf. assignment)
  • t1 matches t2if
    • t1 is (syntactically) identical to t2, or
    • if there exists bindings to variables such that the two terms become identical after substituition.

L2PrologUnify

inductive definition of matching terms
Inductive Definition of Matching Terms
  • If S and T are constants, then S and T match only if they are the same constants.
  • If S is a variable and T is anything, then they match, and S is instantiated to T.
    • (Similarly, if T is a variable.)
  • … (cont’d)…

L2PrologUnify

cont d1
(cont’d)
  • If S and T are structures, then S and T match only if:
    • S and T have the same principal functor, and
    • all their corresponding components match, recursively.
  • The resulting instantiation is determined by matching of the components.

L2PrologUnify

examples
Examples

L2PrologUnify

extended example
Extended Example

f(X, g(a))

f(Y, g(Y))

{X<-Y} {X<-a, Y<-a}

Common instance: f(a,g(a))

L2PrologUnify

cont d2
(cont’d)

f(X, h(b), g(X))

f(a, h(Y), g(Y))

{X<-a}{Y<-b} Not unifiable

Common instance: ??

L2PrologUnify

occur s check problem
Occur’s Check Problem

f(X, g(X))

f(Y, Y)

  • These terms cannot be unified because there is no finite term substituition{X<- t} such that X = g(X).
  • In practice, this can cause non-terminating computation, requiring special handling.

L2PrologUnify

cont d3
(Cont’d)
  • ?-member(a,[f(a),a]).
  • Yes
  • ?-member(X,[f(X),X]).
  • Yes

Infinite loop:

X = f(f(f(…)))

L2PrologUnify

executable specification in prolog
Executable Specification in Prolog

type(i,int).

type(x,real).

type(+(E,F),T) :-

type(E,T), type(F,T).

type(+(E,F),real) :- type(E,T1),type(F,T2), T1 \= T2.

  • Type Checking?- type(+(i,x),real).
  • Type Inference?- type(+(x,x),T).

L167AG

alternative with conditional
Alternative : with conditional

type(i,int).

type(x,real).

type(+(E,F),T) :- type(E,TT),

( (TT == real) ->

T = real ;

type(F,T)

).

  • Type Checking?- type(+(i,x),real).
  • Type Inference?- type(+(x,x),T).

L167AG