Download presentation
Presentation is loading. Please wait.
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 :- do[1], do[2] do[3]. 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 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? 1.simplify E+0 to E 2.simplify 0+E to E 3.keep E1+E2 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, 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).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.