130 likes | 150 Views
Syntax and Semantics of Prolog. Syntax + Terminologies Data Types Search Tree (Proof Tree) Unification Backtracking. Prolog Program consists of. Facts - unconditional statement boy_names(john, 1700, 1). likes(rong, prolog). Rules - conditional statement
E N D
Syntax and Semantics of Prolog Syntax + Terminologies Data Types Search Tree (Proof Tree) Unification Backtracking
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,Z), p(Z,Y). • Clause • Goal
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 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, …)
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
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).
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). 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). How Does a Prolog Program Work - by example
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 to change it to 3+x, i.e. remove redundant 0s. What are the rewriting rules? • simplify E+0 to E • simplify 0+E to E • keep E1+E2 unchanged if both E1 and E2 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, Y, X+Y):- X \== 0, Y \== 0. simp(X,X):- atomic(X). simp(X+Y, NewXY):- simp(X, NewX), simp(Y, NewY), rules_for_plus(NewX, NewY, NewXY).