Syntax and Semantics of Prolog - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Syntax and Semantics of Prolog

Description:

likes(rong, prolog). Rules - conditional statement. likes(rong,X):- likes(X,prolog). grandparent(X,Y):- parent(X,Z), parent(Z,Y) ... – PowerPoint PPT presentation

Number of Views:141
Avg rating:3.0/5.0
Slides: 13
Provided by: rong94
Category:

less

Transcript and Presenter's Notes

Title: Syntax and Semantics of Prolog


1
Syntax and Semantics of Prolog
  • Syntax Terminologies
  • Data Types
  • Search Tree (Proof Tree)
  • Unification
  • Backtracking

2
Prolog Program consists of
  • Facts - unconditional statement
  • boy_names(john, 1700, 1).
  • likes(rong, prolog).
  • Rules - conditional statement
  • likes(rong,X)- likes(X,prolog).
    grandparent(X,Y)- parent(X,Z), parent(Z,Y).
  • Queries - something need to be proved (or goal
    needs to be solved)

3
Some Funny Terminologies
  • Head
  • Body
  • Neck
  • Tail
  • gp(X,Y) - p(X,Z), p(Z,Y).
  • Clause
  • Goal

4
Data Objects in Prolog
  • Variable - represents unknown object
  • Constant - represents simple instance
  • atom
  • number
  • Nested Structure
  • list represents a sequence of objects
  • structure - represents compound objects
  • (all data objects are called terms)

5
Syntax - Data Objects
  • Variable - strings starting with a capital letter
  • strings starting with a _
  • Constant
  • atom - strings starting with a lower-case
    letter, and any strings enclosed in single quotes
  • number same as numbers in Java
  • Nested Structure
  • list - term, term, term
  • structure - atom(term, term, )

6
Syntax Prolog Program
  • A Prolog program consists of either
  • goal. or
  • goal - goal, goal, , goal.
  • A goal can be either
  • atom (i.e. a name without arguments) or
  • atom(term, term, )
  • A term can be either
  • variable, constant, or nested structure

7
Can You Spot Any Syntax Errors?
  • Student_id(adam, 04971111)
  • done - do1, do2 do3.
  • done(Job - design(Job), imp(Job), test(Job),
  • likes(tom marry).

8
How Does a Prolog Program Work - by example
  • program
  • father(john,ben).
  • father(john,steve).
  • father(steve,chris).
  • father(chris,adam).
  • father(steve,tom).
  • grandfather(X,Z) -
  • father(X,Y), father(Y,Z).
  • ?- father(steve,X).

9
How Does a Prolog Program Work - by example
  • program
  • father(john,ben).
  • father(john,steve).
  • father(steve,chris).
  • father(chris,adam).
  • father(steve,tom).
  • grandfather(X,Z) -
  • father(X,Y), father(Y,Z).
  • ?- grandfather(X,chris).
  • ?- f(X,Y),f(Y,chris).

10
Unification
  • There is a matching operation between the
  • goal and the head of clause. This matching
  • operation is called unification.
  • Rules of unification
  • a variable can unify with any term
  • two constants (i.e. number or atom) can be
    unified if they are same
  • two structures can be unified if they have same
    name and all arguments can be unified.

11
An ExampleSimplify an Arithmetic Expression
  • Problem given an expression like 3x0, we want
    to change it to 3x, i.e. remove redundant 0s.
  • What are the rewriting rules?
  • simplify E0 to E
  • simplify 0E to E
  • keep E1E2 unchanged
  • if both E1 and E2 are not 0.

12
A Prolog Program to Simplify Arithmetic
Expressions
  • rules_for_plus(X, 0, X).
  • rules_for_plus(0, X, X).
  • rules_for_plus(X, Y, XY)- X \ 0, Y \ 0.
  • simp(X,X)- atomic(X).
  • simp(XY, NewXY)-
  • simp(X, NewX), simp(Y, NewY),
  • rules_for_plus(NewX, NewY, NewXY).
Write a Comment
User Comments (0)
About PowerShow.com