Skip to content

Instantly share code, notes, and snippets.

@jki127
Last active November 14, 2019 21:28
Show Gist options
  • Save jki127/a02e2d2ff8004d109a5e7248494483b2 to your computer and use it in GitHub Desktop.
Save jki127/a02e2d2ff8004d109a5e7248494483b2 to your computer and use it in GitHub Desktop.

Prolog I - Programming Languages - Nov 12th, 2019

Knowledge Base

The knowledge base is a set of facts.

legs(dog, 4).
legs(cow, 4).
legs(chicken, 2).
legs(goat, 4).
legs(person, 4).
edible(chicken).
edible(cow).
edible(goat).
edible(fish).

Atoms

  • dog, cow, chicken and goat are all examples of atoms
  • Atoms always begin with lowercase letters

Queries

Query:

?- legs(dog, 4)

Output:

true.

Query:

? - edible(X)

Output:

X = chicken ;
X = cow ;
X = goat ;
X = fish.

We have to enter a semicolon after each result in the console to mode to get the next result.

Which animals have 2 legs?

?- legs(X, 2)

Output:

X = chicken ;
X = person.

We get false if an entry does not exist in our Knowledge Base.


Query:

?- leg(giraffe, X)

Output:

false

Query:

?- leg(X, 4), edible(X)

Output:

X = cow ;
X = goat ;
false.

Unlike the previous queries the above query doesn't know which element is the last element. Instead the query keeps going and returns false when there are no more results.


Query:

?- edible(X), leg(X, 4), X=dog.

Output:

X = cow;
false.

Predicate

Let's add one more thing to our knowledge base.

willeat(jeff, X) :- edible(X), legs(X, 4).

This says Jeff will eat anything that is edible and has four legs.

willeat, edible, and legs are Predicates.

Query:

willeat(jeff, cow)

Output:

true ;
false.

fail

fail is a special predicate that let's you define facts as false.

legs(goat, 4) :- fail

writeln

legs(dog, 4) :- writeln("In legs(dog, 4)").

Query:

?- legs(X, 4), edible(X).

Output:

In legs(dog, 4)
In legs(cow, 4)
In edible(cow)
X = cow ;
In legs(goat, 4)
In edible(goat)
x = goat.

What happens when we change the order of our predicates?

We will logically get the same answer, but the search path will be different.

Query:

?- edible(X), legs(x,4).

Output:

In edible(chicken)
In edible(cow)
In legs(cow, 4)
X = cow ;
In edible(goat)
In legs(goat, 4)
X = goat ;
In edible(fish)
false.
?- edible(X), legs(x,4), X=cow.
?- X=cow, edible(X), legs(X, 4).

The second query above will run faster because the X is set to cow first and this tells the program to first search for cows.

in(professor, tandon)
in(tandon, brooklyn)
in(brooklyn, newyork)

Write a contains predicate such that:
contains(newyork, professor) => True

contains(A,B) :- in(B, A)
contains(A,B) :- in(B, X), contains(A, X).

If we used contains(A,B) :- contains(A, X), in(B, X) we would get a Stack Exceeded Error.

Unification

  • The process of determining if two values match

Query:

?- foo(bar) = foo(bar)

Output:

true

These predicates unify even thoughfoo is not defined. Prolog is just matching them like strings.

Query:

?- foo(bar) = foo(X)

Output:

X = bar ;
true.

X unifies with anything unless it is defined beforehand. Query:

?- father(X) = X.

Output:

X = father(X).

The output of the above query is dependent on the version of prolog you're using. In this implementation, it succeeds.

#school/f19/proglang-f19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment