Syntax and Semantics of Prolog v Syntax Terminologies

  • Slides: 12
Download presentation
Syntax and Semantics of Prolog v Syntax + Terminologies v Data Types v Search

Syntax and Semantics of Prolog v Syntax + Terminologies v Data Types v Search Tree (Proof Tree) v Unification v Backtracking

Prolog Program consists of • Facts - unconditional statement boy_names(john, 1700, 1). likes(rong, prolog).

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)

Some Funny Terminologies • • Head Body Neck Tail gp(X, Y) : - p(X,

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

Data Objects in Prolog • Variable - represents unknown object • Constant - represents

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)

Syntax - Data Objects • Variable - strings starting with a capital letter strings

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] Ø structure - atom(term, …)

Syntax – Prolog Program A Prolog program consists of either goal. or goal :

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

Can You Spot Any Syntax Errors? Student_id(adam, 04971111) done : - do[1], do[2] do[3].

Can You Spot Any Syntax Errors? Student_id(adam, 04971111) done : - do[1], do[2] do[3]. done(Job : - design(Job), imp(Job), test(Job), likes(tom marry).

How Does a Prolog Program Work - by example program: father(john, ben). father(john, steve).

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).

How Does a Prolog Program Work - by example program: father(john, ben). father(john, steve).

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).

Unification There is a matching operation between the goal and the head of clause.

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.

An Example: Simplify an Arithmetic Expression Problem: given an expression like 3+x+0, we want

An Example: Simplify an Arithmetic Expression Problem: given an expression like 3+x+0, we want to change it to 3+x, i. e. remove redundant 0 s. What are the rewriting rules? 1. simplify E+0 to E 2. simplify 0+E to E 3. keep E 1+E 2 unchanged if both E 1 and E 2 are not 0.

A Prolog Program to Simplify Arithmetic Expressions rules_for_plus(X, 0, X). rules_for_plus(0, X, X). rules_for_plus(X,

A Prolog Program to Simplify Arithmetic Expressions rules_for_plus(X, 0, X). rules_for_plus(0, X, X). rules_for_plus(X, Y, X+Y): - X == 0, Y == 0. simp(X, X): - atomic(X). simp(X+Y, New. XY): simp(X, New. X), simp(Y, New. Y), rules_for_plus(New. X, New. Y, New. XY).