COP 4020 Lecture -*- Outline -*- * Relation to logic programming (9.3) ** What is logic programming ------------------------------------------ SPECIFICATION OF SORTED Specification: OutList = {Sorted InList} if {Permutation InList OutList} and {Ordered OutList} corresponds to formula: exists OutList . OutList = {Sorted InList} How could you prove this? ------------------------------------------ Q: How could you prove this? - give the sorted list OutList (constructive) - prove that every list can be sorted (nonconstructive) ------------------------------------------ ABSTRACTING FROM THE EXAMPLE So programs are modeled as Specification: a problem to be solved is Answer to a problem: ------------------------------------------ ... relations between input(s) and output(s) ... a description of facts and relationships and an existential conjecture about the existence of a relationship. for example, % description of relationships proc {Sorted ?InList ?OutList} {Permutation InList OutList} % and {Ordered OutList} end % existential query {Solve proc {$ Out} {Sorted [3 1 2] Out} end} ... a constructive proof of the conjecture, which thus actually produces the output. e.g., the sorted list The problem (for the language designer/implementor) is: how to state such descriptions and conjectures how to instruct a system to do such constructive proofs how to do it fast ** Constructive logic (cf. 9.3.1) There are several kinds of logics but only constructive logic is a suitable basis for programming ------------------------------------------ CONSTRUCTIVE LOGIC def: constructive logic is a variant of logic which requires evidence Example: Theorem/Query: exists Out . {Sorted [3 1 2] Out} Constructive proof: Out = [1 2 3] Theorem/Query: exists I . {Prime I} % and (I>6) = true Constructive proof: I = 7 ------------------------------------------ Q: Does constructive logic allow "proof by contradiction"? No ** Operational and logical semantics (9.3.2) ------------------------------------------ CORRESPONDENCE (T) BETWEEN STATEMENTS IN OZ LOGICAL FORMULA (Operational Semantics) (Logical Semantics) ================================================== T(skip) = true T(fail) = false T(S1 S2) = T(choice S1 = [] ... [] Sn end) T(local X in S end) = exists X . T(S) T(X=Y) = X=Y T(X=f(l1:X1 ... ln:Xn)) = X=f(l1:X1, ... ln:Xn) T(if X then S1 else S2) = (X=true /\ T(S1)) \/ (X=false /\ T(S2)) T(case X of = (exists X1, ..., Xn . f(l1:X1 ... ln:Xn) X=f(l1:X1, ... ln:Xn) then S1 /\ T(S1)) else S2) \/ (not(exists X1, ..., Xn . f(l1:X1 ... ln:Xn) X=f(l1:X1, ... ln:Xn)) /\ T(S2)) T(P = proc {$ X1 ... Xn} = all X1, ..., Xn . S p(X1, ..., Xn) <== T(S) end) {P Y1 ... Yn} = p(Y1, ..., Yn) ------------------------------------------ ... T(S1 S2) = T(S1) /\ T(S2) ... T(choice S1 = T(S1) \/ ... \/ T(Sn) [] ... [] Sn end) Q: Given these translations, what's another way to write if Z then A=nil else A=3|nil end in Oz (to produce the same logic)? choice Z=true A=nil [] Z=false A=3|nil end Q: What's translation of (X > Y)=true ? desugars to local Z in Z=(X > Y) Z=true end which translates to exists Z . Z=(X > Y) /\ Z=true (using ">" as a procedure or function) which is logically equivalent to (X > Y) Q: So if we want to prove that a relation p(Y1, ..., Yn) holds, what do we do (operationally)? We use the body of the procedure P *** Practice with the translation ------------------------------------------ FOR YOU TO DO What's the logical semantics of: % (d) case Ls of nil then skip [] _|nil then skip [] X|Y|Zs then (X =< Y)=true {Ordered Y|Zs} end % (nd) choice Ls=nil [] Ls=_|nil [] local X Y Zs in X|Y|Zs=Ls (X =< Y)=true {Ordered Y|Zs} end end ------------------------------------------ ... (Ls=nil /\ true) \/ (exists _ . Ls=_|nil /\ true) \/ (not(exists _ . Ls=_|nil) /\ (exists X, Y, Zs . Ls = X|Y|Zs /\ (X =< Y) /\ ordered(Y|Zs))) ... (Ls=nil) \/ (exists _ . Ls=_|nil) \/ (exists X, Y, Zs . Ls = X|Y|Zs /\ (X =< Y) /\ ordered(Y|Zs)) Q: What's the operational difference between these? The first one is deterministic, no backtracking or search *** Using the translation to do programming Reference: adapted from Sterling and Shapiro's book: "The Art of Prolog", chapters 1, 4 and 5. a "ground" fact or query is one without variables **** axioms (database facts) ------------------------------------------ GROUND DATABASES AND GROUND QUERIES (WITH FACTS WITHOUT VARIABLES) declare % a database of capitals proc {Capitals ?State ?City} choice State=florida City=tallahassee [] State=iowa City=desMoines [] State=kansas City=topeka [] State=virginia City=richmond [] State=michigan City=lansing [] State=newYork City=albany [] State=california City=sacremento end end \insert 'SolveOne.oz' fun {QueryCapitals State City} Ans = {SolveOne proc {$ Ok} {Capitals State City} Ok=yes end} in if Ans \= nil then yes else no end end {Test {QueryCapitals florida tallahassee} '==' yes} {Test {QueryCapitals newYork albany} '==' yes} logical rule: axiom (or fact) _______ p(a,b) ------------------------------------------ See Capitals.oz CapitalsTest.oz Q: What does QueryCapitals do? Q: What does the identity rule mean? above line is hypothesis, below is conclusion This says that if the fact is in the database, it can be concluded Q: What's the operational semantics of such a query? calls the procedure and finds choice points that unify successfully, returns the binding of the query's variable **** closed world assumption (finite failures) ------------------------------------------ CLOSED WORLD ASSUMPTION {Test {QueryCapitals georgia atlanta} '==' no} ------------------------------------------ See CapitalsTest.oz Q: Is atlanta the capital of Georgia? yes Q: Why doesn't the program agree? because it doesn't know that it only knows what we tell it... Deducing that if query is not deducible from the database it is false is called the "closed world assumption". Logic programming uses this assumption. This is a fallacious if the theory or proof system isn't complete: like "I tried to prove Fermat's last theorem, but I couldn't, so it must be false". Only works if trying to prove Q fails in a finite amount of time, otherwise loops forever **** generalization (existential queries) ------------------------------------------ ANSWERING QUERIES WITH UNBOUND VARIABLES (ON GROUND DATABASES) % What is the capital of iowa? {Test {SolveFirst proc {$ What} {Capitals iowa What} end} '==' desMoines} Logical translation of query: logical rule: generalization _____________________________ exists X1...Xn . p(X1,...,Xn) ------------------------------------------ See CapitalsTest.oz ... exists What . capitals(iowa, What) ... s[[p(X1,...,Xn)]] where s a substitution and Xi in dom(s) In other words, have to unify the query with the database Operational semantics: calls the procedure and finds choice points that unify successfully, returns the binding of the query's variable **** conjunction ------------------------------------------ CONJUNCTIVE QUERIES declare proc {Agriculture ?State ?Product} choice State=florida Product=oranges [] State=iowa Product=pork [] State=kansas Product=sunflowers [] State=virginia Product=ham [] State=michigan Product=cherries [] State=newYork Product=dairy [] State=california Product=wine end end \insert 'Capitals.oz' % What is the capital and chief agricultural product of florida? {Test {SolveFirst proc {$ Ans} Town#Farmed = Ans in {Capitals florida Town} %% and {Agriculture florida Farmed} end} '==' tallahassee#oranges} % What is the capital and chief agricultural product of iowa? {Test {SolveFirst proc {$ Ans} Town#Farmed=Ans in {Capitals iowa Town} {Agriculture iowa Farmed} end} '==' desMoines#pork} Logical translation of (2nd) query: exists Town, Farmed . capitals(iowa, Town) /\ agriculture(iowa, Farmed) logical rule: conjunction exists X1,...,Xn . s[[p1(X1,...,Xn)]], exists X1,...,Xn . s[[p2(X1,...,Xn)]] _____________________________________ exists X1,...,Xn . p1(X1,...,Xn) /\ p2(X1,...,Xn) where s is a substitution, and Xi in dom(s), and ------------------------------------------ Q: What syntax is used to represent conjunction? sequencing (juxtaposition) Q: What's the operational semantics? Q: How would you ask what state has capital sacremento and produces wine? **** universal modus ponens (rules) ------------------------------------------ AN EXAMPLE TO BUILD RULES ON % file 'FamilyExample.oz' declare proc {Dad ?Father ?Kid} choice Father=pop Kid=sarah [] Father=pop Kid=john [] Father=pop Kid=robert [] Father=pop Kid=jill end end proc {Mom ?Mother ?Kid} {Dad _ Kid} Mother=mom end proc {Male ?Person} choice Person=pop [] Person=robert [] Person=john end end proc {Female ?Person} choice Person=mom [] Person=sarah [] Person=jill end end ------------------------------------------ logical rules are encoded as procedures ------------------------------------------ LOGICAL MEANING OF RULES \insert 'FamilyExample.oz' declare proc {Son ?X ?Y} {Parent Y X} {Male X} end proc {Daughter ?X ?Y} {Parent Y X} {Female X} end proc {Parent ?Y ?X} choice {Dad Y X} [] {Mom Y X} end end Logical translation of rules: all X, Y . son(X, Y) <== parent(Y, X) /\ male(X) all X, Y . daughter(X, Y) <== parent(Y, X) /\ female(X) all X, Y . parent(Y, X) <== dad(Y, X) \/ mom(Y, X) ------------------------------------------ i.e., logical meaning of rule is leftward implication parameter variables in rules are universally quantified (and their scope is just the rule) proc {P A} :- {G A} is read as "{P A} if {G A}", or "infer {P A} from {G A}", logically this is p(A) <== g(A) (i.e., g(A) implies p(A)) ------------------------------------------ ANSWERING QUERIES USING RULES \insert 'FamilyRules.oz' \insert 'SolveAll.oz' % What children are sons of what adults? {Test {SolveAll proc {$ Ans} Child#Adult=Ans in {Son Child Adult} end} '==' [john#pop robert#pop john#mom robert#mom]} logical rule: universal modus ponens: where (all Y1,...,Yn . p(Y1,...,Yn) <== S) is a rule, s is a substitution, exists X1,...,Xn . S' p(X1,...,Xn) = s[[p(Y1,...,Yn)]], ________________________________ S' = s[[S]] exists X1,...,Xn . p(X1,...,Xn) ------------------------------------------ Q: Why does that make sense? What does it mean computationally? could show the proof of exists Child,Adult . Son(Child, Adult) for s such that s(Child)=john s(Adult)=pop dad(pop, john) _____________________________________ exists Child Adult . dad(Adult,Child) male(john) _________________________________________ _________________________________ exists Child,Adult . parent(Adult, Child), exists Child,Adult . male(Child) __________________________________________ exists Child,Adult . parent(Adult, Child) /\ male(Child) ______________________________________ [univ. m.p.] exists Child,Adult . Son(Child, Adult) **** exercise ------------------------------------------ FOR YOU TO DO Write Oz rules that encode your class schedule. Write a query to ask if you are in class Monday at 11:45. Write a query to find all the classes you are taking. ------------------------------------------ ** Logic programming in other models Q: Can we do logic programming in the declarative model? yes, but not using nondeterminism Q: What happens if we add concurrency to the relational model? It's still relational, no results change. Q: Can we use logic programming ideas in the stateful models? Only in part of the program, not for the stateful part