I. Introduction to the Declarative Paradigm and Logic Programming A. Motivation: programming is difficult, expensive B. Goal: extremely high level, declarative (what, not how) C. Orientation 1. Procedural a. Imperative or von Neumann (mutation, assignment) b. Applicative (no mutation, no assignment) 2. Declarative (nonprocedural) a. Logic-oriented (horn-clauses) b. Database systems (relations + queries) c. Constraint-based --------------- ; constraints between Farenheit and Celsius F = M*C + B ; linear relationship 212 = M*100 + B ; boiling points 32 = M*0 + B ; freezing points F = 80 => C = 26.667 --------------- d. Equational-logic based e. Rule-based (forward chaining) D. History E. Claims of Logic programming: (questions we'll look at) F. Logic programming techniques 1. nonprocedural, what is to be done, instead of how 2. proofs can be constructive 3. programs are propositions that assert existence of desired result 4. Think of programs as *relations* II. Pure Prolog A. Syntax (as in C-Prolog, Edinburgh) ------------- ::= * % 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 */ ------------- 1. Case sensitive: variables begin with captial letter 2. facts and rules a. facts ------------- good(prolog). ------------- b. rules ------------- good(X) :- logical(X). ------------- c. compound terms: functor + arguments ---------- 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))))) ---------- d. scope of variables limited to a clause --------------- understand(L, X) :- know(terms,L,X), know(syntax,L,X), know(semantics,L,X). ---------------- B. Examples 1. assertions ------------- % a semantic network object(event1, paper). recipeint(event1, sue). actor(event1, ron). action(event1, gave). ------------- 2. 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 ------------------- C. Interpretations of clauses 1. declarative 2. Procedural reading (programming language) D. Examples ---------------- addtoend([], X, .(X,[])). addtoend(.(Y,L), X, .(Y,M)) :- addtoend(L,X,M). add2end([], X, [X]). add2end([Y|L], X, [Y|M]) :- addtoend(L,X,M). ---------------- ---------------- equals(addtoend([],X), [X]). equals(addtoend([Y|L],X), [Y|M]) :- equals(addtoend(L,X), M). ----------------