CS 541 Lecture -*- Outline -*- * Pure Prolog ** Syntax (as in C-Prolog, Edinburgh) don't forget those periods. ------------- ::= * % or of clauses ::= :- { , }* . % rule | . % assertion | ?- { , }* . % goal, question ::= | | ( ) ::= _[A-Za-z0-9_]* | [A-Z][A-Za-z0-9_]* ::= | ::= ::= [a-z][A-Za-z0-9_]* | ::= 'chars' comment convention is % rest of line or /* comment */ ------------- *** Case sensitive: variables begin with captial letter *** facts and rules **** facts ------------- good(prolog). ------------- **** rules ------------- good(X) :- logical(X). ------------- **** compound terms: functor + arguments functor like a noun, these are the data structures of Prolog ---------- tree(Subtree1,Root,Subtree2) lists: [a,b], which is sugar for .(a, .(b, [])) sentence(np(n(ron)), vp(v(gave),np(art(a), n(paper)), compls(prep(to), np(n(sue))))) ---------- Note: prolog doesn't really *understand* any of this! **** scope of variables limited to a clause --------------- understand(L, X) :- know(terms,L,X), know(syntax,L,X), know(semantics,L,X). ---------------- scope of L and X is just this clause! think of it as forall L,X ... ** Examples *** assertions ------------- % a semantic network object(event1, paper). recipeint(event1, sue). actor(event1, ron). action(event1, gave). ------------- *** queries: ------------------- ?- object(event1,paper). %(was the object of event1 a paper?) yes ?- object(event1,What). %(what was given in event1?) What = paper ?- actor(Event,Who), action(Event, gave). %(who gave in an what event?) Event = event1 Who = ron; no % no more ways to satisfy goal ?- actor(X,Y), action(X,Z). X = event1 Y = ron Z = gave ------------------- ** Interpretations of clauses e.g., h(X) :- b1(Y), b2(Z), b3(W). *** declarative to solve problem of form h, need to solve {b1,b2,b3}. logic (an implication): b1(Y) and b2(Z) and b3(W) imply h(X) i.e: h(X) if b1(Y) & b2(Z) & b3(W). Knowledge based systems rule (if-then rule), with antecedents bi() and consequent h(X). *** Procedural reading (programming language) procedure with head h(X), body b1(Y), ... head h identifies form of call it can answer, body {b1,b2,b3} is an ordered list of procedure calls. ** List Examples [] is empty list [3|L] is a list with head 3, tail L like (cons 3 L) in LISP [1,2,3] [1,2|L] is list with head 1, tail [2|L] [1|[2|L]] is same as above ---------------- list([]). list([X|Xs]) :- list(Xs). addtoend([], X, [X]). addtoend([Y|L], X, [Y|M]) :- addtoend(L,X,M). ---------------- try addtoend([3],4,L). addtoend(L,4,[2,4]). addtoend([3],4,[3,4]). can trace by adding a print statement to the rhs's. paper tracing: ?- addtoend([3],4,L). prime all the variables in the program, then unify by { Y' |-> 3, L' |-> [], X' |-> 4, M' |-> U1, L |-> [3|U1] } so get new goal ?- addtoend([],4,U1). unify with first clause in addtoend... by {X'' |-> 4, U1 |-> [4]} which means we're done so answer is L = [3|U1], where U1 = [4] so L = [3|[4]] = [3,4] variation on addtoend ---------------- equals(addtoend([],X), [X]). equals(addtoend([Y|L],X), [Y|M]) :- equals(addtoend(L,X), M). ---------------- try equals(addtoend([3],4), L). develop a version of reverse/2, append/3 run them forwards and backwards