Saturday, November 3, 2007

Introduction to Prolog (Part 2)

Examples of Facts

  • female(alison).
      • “alison is a female”
  • has_feathers(sparrow).
      • “sparrow has feathers”
  • father(“John”,”Sam”).
      • ”John is the father to Sam”
  • mother(”Jeanette”,”Mary”).
      • “Jeanette is the mother to Mary”
  • bird(type(sparrow), name(toto))).
      • “toto is a bird that is of sparrow class”

Examples of Rules

  • You should read the prolog operator “:-” as “if ”, “;” as an “or”, while “,” as meaning “and”.
  • Rules consist of a head and a body. For example the rule “a(X) :- b(X), c(X).” has head “a(X)” and body “b(X), c(X)”.
  • All arguments beginning with a capital letter (such as X and Y) are variables. Variables don't have to have values). Any constant should NOT begin with a capital letter else it will be treated as a variable.
  • sister(X,Y) :- father(Z,X),father(Z,Y).
  • “X and Y are sisters if Z is father to X and father to Y”

Example of a Prolog Program

PREDICATES

son(STRING, STRING)

CLAUSES

son("John", "Dan").

son("Allan","Dan").

Goal son("John", "Dan").

Example 2

PREDICATES

son(STRING,STRING)

sister(STRING,STRING)

brother(STRING,STRING)

married(STRING,STRING)

sister_in_law(STRING,STRING)

CLAUSES

son("John", "Dan").

sister("Mary","Suzan").

brother("Harold", "Larry").

married("John", "Mary").

married("Larry", "Sue").

married("Harold", "Emma").

CLAUSES

sister_in_law(A, B):-married(A, C), sister(C, B).

sister_in_law(A, B):-brother(A, C), married(C, B).

GOAL

sister_in_law("John", Z).

Example 3

domains

brand, color = symbol

age, price = integer

milage = real

predicates

car(brand, milage, age, color, price)

clauses

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

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

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

goal

car(renault, 13, 3.5, red, 12000).

/* car(ford, 9000, gray, 4, 25000).

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

car(Make, Odometer, Years_on_road, Body, Cost) and Cost < style=""> */

No comments: