



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Programming Languages; Subject: Engineering Computer Science; University: University of California - Davis; Term: Unknown 1989;
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Motivation
functions. Substitute a function with its body. Also given a set of input, get an output
where a relation is a set of tuple. n-ary relation: set of n-tuples
〈[], [], []〉 ∈ append 〈[6], [7, 8], [6, 7 , 8]〉 ∈ append 〈[6, 7], [8], [6, 7 , 8]〉 ∈ append 〈[7, 8], [6], [6, 7 , 8]〉 6 ∈ append 〈[6, 7 , 8], [6], [7, 8]〉 6 ∈ append
Terms
sequence of sub-terms. Atom is called functor and sub-terms are called arguments.
- Example: link(bcpl, c)
- Compound terms can be nested: tree(a, tree(b, nil, nil), tree(c, tree(d, nil, nil), tree(e, nil, nil) ) ) What this term may mean in real world: a
b c
d e
tree(a, tree(b, nil, nil), tree(c, tree(d, nil, nil), tree(e, nil, nil) ) )
Facts
terms.
languages:
Algol
Fortran
Simula
CPL
BCPL
C
C++
SmallTalk
term link: let link(x, y) mean that language y is derived from x.
link(fortran,algol60). link(algol60,cpl). link(cpl,bcpl). link(bcpl,c). link(c,cplusplus). link(algol60,simula67). link(simula67,cplusplus). link(simula67,smalltalk80).
Rules
that is guaranteed only under certain conditions.
Logically connects a collection of relations in a conditional form: How rules are written concl :- cond1, cond2, .. . condN
What they mean concl is true if cond1 is true and if cond2 is true and .. . if condN is true
x grandfather z :- x father y, y father z. x grandfather z :- x father y, y mother z.
P if (Q 1 and Q 2 and ... and Qk ), where k ≥ 0
true. Formally:
Queries
execution is driven by evaluation of expressions. A logic program, on the other hand, is defined as a set of rules ; its execution answers a certain query.
regarding the existence of certain relationships.
the database of facts?
deduce a goal. If it is successful, it will return yes , otherwise no.
deduce a fact. Rules cannot be used to deduce negative facts.
?- link(c, cplusplus). yes
?- link(algol60, simula67), link(bcpl, c). yes
?- link(c, bcpl), link(bcpl, c). no
Relations containing variables
Not a very succinct way of representing facts; will need to enumerate all such facts one by one. Quite tedious.
generic terms, that is terms that can stand for other terms.
gradfather(X, Z) :- father(X, Y), father (Y, Z). gradfather(X, Z) :- father(X, Y), mother (Y, Z).
grandfather(bill, mary) :- father(bill, Y), father(Y, mary). grandfather(bill, Z) :- father(bill, Y), mother(Y, Z). grandfather(john, jim) :- father(john, bob), father(bob, jim).
anything, but never takes any value. Example: check if bill has a father. We don’t care who the father is. ?- father(bill, _).
Unification
Prolog bind variables to values?
Unification is an attempt (by Prolog interpreter) to match two terms, by instantiating the variables contained in two terms.
match?
So unification will involve assigning these values to the variables.
Queries
Algol
Fortran
Simula
CPL
BCPL
C
C++
SmallTalk
?- link(cpl, X). X = bcpl; no means set of all X such that <cpl, X> ∈ link
?- link(X, cplusplus). X = C; X = simula67; means set of all X such that <X, cplusplus> ∈ link
?- link(X, smalltalk80), link(X, cpluscplus). X = simula67. means set of all X such that <X, smalltalk80> ∈ link ∧ <X, cpluscplus> ∈ link
?- link(algol60, X), link(X, Y). X = cpl Y = bcpl;
X = simula Y = cplusplus;
X = simula Y = smalltalk80; no
Arithmetic in Prolog
arithmetic. Use infix notation for specifying expressions.
rules apply on them as well: ?- X = 2 + 3. X = 2 + 3
?- X is 2 + 3. X = 5
?-X is 2+3, X = 2 + 3. no
sum(1, 1). sum(N, Res) :- N > 1, N1 is N - 1, sum(N1, Res1), Res is Res1 + N.
?- sum(5, X). X = 15
equal to within 0.0001. close_enough(X, X) :-. close_enough(X, Y) :- X < Y, Y-X < 0.0001. close_enough(X, Y) :- X > Y, close_enough(Y, X).
Logical operators
Prolog will try to match them: ?- X = 5. X = 5.
?- X = 2, not(X = 1). X = 2.
Unify X with 2 first, and use that X to check if X = 1. ?- not (X = 1), X = 2. no
fails causing the whole clause to fail.
X succeeds or Y succeeds. Fails when both fail. person(X) :- (X = adam; X=eve; mother(X, Y)).
Control in Prolog
Response to a query is affected both by goal order within the query and by rule order within database of facts and rules
Start with a query as the current goal; while the current goal is nonempty do let the current goal = G 1 , ..., Gk , where k ≥ 1 choose the leftmost goal G 1 ; if a rule applies to G 1 then select first such rule A :- B 1 , ..., Bj , where j ≥ 0 let σ be most general unifier of A and B 1 current goal = B 1 σ, B 2 σ, ...Bj σ, G 2 σ, ..., Gk σ else backtrack; end while success
Example
append([], Y, Y). append([H|X], Y, [H|Z]) :- append(X, Y, Z).
prefix(X, Z) :- append(X, Y, Z).
suffix(Y, Z) :- append(X, Y, Z).
sublist1(S, L):- prefix(X, L), suffix(S, X). sublist2(S, L):- suffix(S, X), prefix(X, L).
X S Z
How does search take place?
?- suffix([a], L), prefix(L, [a,b,c]). L = [a].
{_1−>[], L−>[a]} append([], [a], [a]).
{_2−>[b,c]} append([],[b,c],[b,c]).
suffix([a], L), prefix(L, [a, b, c])
append([a],_2, [a,b,c])
prefix([a], [a,b,c])
append([], _2, [b, c])
append(_1, [a], L), prefix(L, [a, b, c])
yes
prefix([a],[a,b,c]) if append([a],_2, [a,b,c]).
suffix([a], L) if append(_1, [a], L).
append([a],_2,[a,b,c]) if append([],_2,[b,c]).
How does search take place?
?- suffix([b], L), prefix(L, [a,b,c]). L = [a, b].
{_1−>[], L−>[b]}
{_1−>[_3|_4], L−>[_3|_5]}
{_3−>a}
{_6−>[c]}
...
suffix([b], L), prefix(L, [a, b, c])
append(_1, [b], L), prefix(L, [a, b, c])
prefix([b], [a,b,c])
append([b],_2, [a,b,c])
backtrack
append(_4, [b], _5), prefix([_3|_5], [a, b, c])
prefix([_3,b], [a,b,c])
append([_3,b],_6, [a,b,c])
append([b],_6, [a,b,c])
append([],_6, [c])
Yes
?- suffix([a], L), prefix(L, [a, b, c]). L = a ; [ infinite computation ]
?- suffix([a], L). L = a ; L = [_1, a] ; L = [_1, _2, a] ; ...
has an infinite number of solutions, only the first of which
The system first finds a solution without backtracking. A futile search through the rest of infinite tree ensues if we ask for a further solution.
?- prefix(L, [a, b, c]), suffix([a], L). L = a ; no
This query has a finite prolog search tree.
solutions are searched. app2([H|X], Y, [H|Z]) :- app2(X, Y, Z). app2([], Y, Y).
?- appen2(X, [c], Z).
{X−>[], Z−>[c]}
yes
yes
yes
X=[], Z=[c]
X=[_1], Z=[_1,c]
appen2(X, [c], Z)
{X−>[_1|_2], Z−>[_1|_3]} {X−>[], Z−>[c]}
...
appen2(_2, [c], _3)
append(X, [c], Z)
{X−>[_1|_2], Z−>[_1|_3]}
yes
{_2−>[], _3−>[c]}
X=[_1], Z=[_1,c] (^) ...
X=[], Z=[c] append(_2, [c], _3)
{_2−>[], _3−>[c]}