1 / 12

Syntax and Semantics of Prolog

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

alban
Download Presentation

Syntax and Semantics of Prolog

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  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? • simplify E+0 to E • simplify 0+E to E • 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).

More Related