Lab3

advertisement

Predicates and Variables in Prolog Lab #3

LAB #3

PREDICATES AND VARIABLES IN PROLOG

OBJECTIVE

Write programs declaring different predicates and variables.

THEORY

A predicate is the symbolic name (identifier) for a relation and a sequence of arguments. A Prolog program is a sequence of clauses and directives, and a procedure is a sequence of clauses defining a predicate. Clauses that belong to the same predicate must follow one another.

Variables enable you to write general facts and rules and ask general questions.

Variable names in Prolog must begin with a capital letter or an underscore character (_), after which you can use any number of letters (upper case or lower case), digits, or underscores.

Variables in Prolog get their values by being matched to constants in facts or rules. Until it gets a value, a variable is said to be free; when it gets a value, it becomes bound.

You can't store information globally by binding a value to a variable, because a variable is only bound within a clause.

If you only need certain information from a query, you can use anonymous variables to ignore the values you don't need. In Prolog, the anonymous variable is represented by a lone underscore (_).

The anonymous variable can be used in place of any other variable; it matches anything. The anonymous variable will never get set to a value.

Asking Prolog questions about the facts in your program is known as querying the

Prolog system ; the query is commonly called a goal . Prolog tries to satisfy a goal

(answer the query) by starting at the top of the facts, looking at each fact until it reaches the bottom.

There are several ways Prolog can match one thing to another:

Identical structures match each other.

A free variable matches a constant or a previously bound variable (and becomes bound to that value).

Two free variables can match (and be bound to) each other. As long as the binding lasts, they are treated as a single variable; if one gets a value the other will immediately have the same value.

CSC470: Artificial Intelligence

10

Predicates and Variables in Prolog

EXAMPLE PROGRAMS

Program 1

PREDICATES nondeterm phone_number(symbol,symbol)

CLAUSES

phone_number("Albert","EZY-3665").

phone_number("Betty","555-5233").

phone_number("Carol","909-1010").

phone_number("Dorothy","438-8400").

GOAL

phone_number("Carol", Number).

Lab #3

Program 2

PREDICATES

car(symbol,long,integer,symbol,long)

truck(symbol,long,integer,symbol,long)

nondeterm vehicle(symbol,long,integer,symbol,long)

CLAUSES

car(chrysler,130000,3,red,12000).

car(ford,90000,4,gray,25000).

car(datsun,8000,1,red,30000).

truck(ford,80000,6,blue,8000).

truck(datsun,50000,5,orange,20000).

truck(toyota,25000,2,black,25000).

vehicle(Make,Odometer,Age,Color,Price):- car(Make,Odometer,Age,Color,Price); truck(Make,Odometer,Age,Color,Price).

GOAL

car(Make, Odometer, Years_on_road, Body, 25000).

CSC470: Artificial Intelligence

11

Predicates and Variables in Prolog

Program 3

PREDICATES

male(symbol)

female(symbol)

nondeterm parent(symbol, symbol)

CLAUSES

male(bill).

male(joe).

female(sue).

female(tammy).

parent(bill,joe).

parent(sue,joe).

parent(joe,tammy).

GOAL

parent(Parent, _).

Lab #3

EXERCISES

1.

Type the above examples in Visual Prolog environment and execute them individually.

2.

Explain the working of the above examples.

3.

Identify all the variables used in the above examples. Also mention their types: normal or anonymous.

CSC470: Artificial Intelligence

12

Download