Principles of Programming Languages Slides by Yaron Gonen
Principles of Programming Languages Slides by Yaron Gonen and Dana Fisman Based on Book by Mira Balaban and Lesson 21 – An Introduction to Logic Programming – Relational Logic Programming www. cs. bgu. ac. il/~ppl 172
3 main paradigms in PL Imperative Turing Machine [Turing 1936] Functional Lambda Calculus [Church 1936] Declarative Constructive Logic Proofs [Kowalski, 1970, Colmerauer, 1972]
How it Came to be Kowalski's Observation (early 70 s): An axiom H B 1 and B 2. . . and Bn represents a procedure H is the procedure’s head and the Bi’s are its body To solve (execute) H, we recursively solve B 1. . . Bn In a quest to automate the process of proving logical statements, Colmerauer and his group embodied this observation into Prolog.
Prolog and subsets of interest Relational Logic Programming Datalog Logic Programming Prolog
Prolog and subsets of interest Relational Logic Programming Prolog
Topics 1. Relational logic programming Programs specify relations among entities 2. Logic Programming Programs with data structures: lists, binary trees, symbolic expressions, natural numbers. 3. Meta tools Backtracking optimizations (cuts), unify, metacircular interpreters. 4. Prolog and more advanced programming Arithmetic, cuts, negation.
Prolog and its subsets Relational Logic Programming Prolog
Simple Example db. pl ? - parent(sarah, isaac). parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). male(abraham). male(isaac). male(joseph). female(sarah). true ? - parent(sarah, joseph). false ? - parent(miriam, joseph). false
Queries with vars Simple Example db. pl ? - parent(sarah, X). parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). male(abraham). male(isaac). male(joseph). X = isaac ? - parent(X, isaac). X = sarah X = abraham female(sarah). Note the mind switch: Working with relations as opposed to functions We don’t have to define multiple related functions
Simple Example Fact s parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). male(abraham). male(isaac). male(joseph). female(sarah). ? - grand(sarah, jacob). true ? - grand(sarah, joseph). false ? - grad(X, jacob). Rul e X = sarah grand(X, Z) : - parent(Y, Z), parent(X, Y). Adding Rules X = abraham
Logic Programming – as a programming language Every programming language has: In Logic Programming: Syntax What expressions are legal Formulas (Facts, Rules) Semantics What is the value of a given expression Answer to query: true / false + assignments to the variables An algorithm to compute the semantics Unification, Backtracking Operational Semantics
Logic Programming - Mind Switch: Formula (Fact/Rule) ⇔ procedure declaration ⇔ procedure call ◦ Query ⇔ computation ◦ Proving ◦
Relational Logic Programming A computational model based on Kowalski's interpretation The Prolog language ('70) - contains RLP + additional programming features
Relational LP – Atomic Formulas Syntax atomic formula: predicate_symbol(term 1, . . . , termn) Predicate symbols start with lowercase terms: ◦ symbols (representing individual constants) ◦ variables (start with uppercase or with an underscore ‘_’ for anonymous)
Relational LP – Atomic Formulas Examples parent(abraham, isaac) Predicates and constants are distinguished by their context ancestor(adam, Person) address(City, Street, _Number)
Relational LP – Facts Syntax A fact is an assertion of an atomic formula H. Makes it a fact where H is an atomic formula. Variables in facts are universally quantified.
Relational LP – Facts Examples Variables in facts are universally quantified parent(abraham, isaac). color(red). shape(square). ancestor(adam, Person). related(Person, Person).
Simple Example parent(abraham, isaac). parent(sarah, isaac). Capitalization! ? - female(abraham). false male(abraham). female(sarah). parent(abraham, isaac). parent(sarah, isaac). male(abraham). female(Sarah). ? - female(abraham). true
Relational LP – Rules Syntax Rules are formulas that state conditioned relationships between entities H : - B 1 , B 2 , . . . , Bn. Rule head Rule body Meaning: If B 1 and B 2 and. . . Bn hold then H also holds.
Relational LP – Rules Examples mother(X, Y) : - parent(X, Y), female(X). father(X, Y) : - parent(X, Y), male(X). X, Y, Z. parent(X, Y) & parent(Y, Z) grand(X, Z) : - parent(X, Y), parent(Y, Z). X, Z. ( Y parent(Y, Z) & parent(Y, Z)) grand(X, Z) grandmother(X) : - female(X), grand(X, _). grandfather(X) : - male(X), grand(X, _). o o Variables in rules are universally quantified Variables in rule heads are universally quantified Variables appearing only in rule bodies are existentially quantified Variables are bound within a rule
Simple Example parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). male(abraham). male(isaac). male(joseph). female(sarah). grand(X, Z) : - parent(X, Y), parent(Y, Z). mother(X) : - parent(X, _), female(X). anonymous vars ? - mother(sarah). true ? - mother(abraham). false ? - grand(sarah, X). X = jacob ? - grand(sarah, _X). true
Relational LP – Rules Special Case o Facts can be considered as rules with an empty body. father(isaac, jacob). This fact and this rule have equivalent meaning. father(isaac, jacob): - true is a zero- arity predicate
Relational LP – Procedures Syntax & Examples Procedures are ordered collections of rules and facts sharing the same name and the same arity % Signature: parent(Parent, Child)/2 % Purpose: Parent is a parent of Child parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). % Signature: male(Person)/1 % Purpose: Person is a male(abraham). male(isaac). male(joseph).
Relational LP – Procedures Examples % Signature: rise(CO, Time, City)/3 % Purpose: CO is a celestial object that in City at Time % is seen as if it rises. rise(son, six-am, beer-sheva). rise(moon, _, beer-sheva). These are two different procedures. % Signature: rise(S, B)/2 % Purpose: B is a bigger amount than S rise(milliliter, liter). rise(gram, kilogram). rise(centimeter, decimeter). rise(centimeter, meter).
Relational LP – Queries Syntax A query has the syntax ? - Q 1 , Q 2 , . . . , Qn. Query invocation Meaning: Assuming the program facts and rules, do Q 1 and Q 2 and. . . Qn hold? Variables in queries are existentially quantified
Relational LP – Queries Syntax ? - parent(abraham, isaac). Is abraham a parent of isaac? ? - parent(isaac, X). Who are isaac sons? ? - parent(sarah, _X). Is sarah a parent of someone? ? - parent(sarah, _X), female(sarah). Is sarah a parent of someone, and a female?
Simple Example parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). parent(jacob, dan). parent(jacob, benjamin). male(abraham). male(isaac). male(joseph). female(sarah). queries ? - parent(sarah, _X), female(sarah). true ? - parent(sarah, X), female(sarah). X = isaac ? - parent(abraham, isaac), parent(isaac, Y), parent(sarah, _X). Y = jacob
Simple Example parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). parent(jacob, dan). parent(jacob, benjamin). male(abraham). male(isaac). male(joseph). female(sarah). Queries w. mult. answers ? - parent(X, issac), parent(Y, isaac). X = abraham, X = sarah, Y false. ? - Y Y = = parent(X, parent(Y, X = Y. = abraham; = sarah; abraham; sarah; issac), isaac), X = abraham, Y = sarah; X = sarah, Y = abraham; false.
Simple Example parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). parent(jacob, dan). parent(jacob, benjamin). male(abraham). male(isaac). male(joseph). female(sarah). ? - Queries w. mult. answers parent(F, X), parent(F, Y), X /= Y. F=jacob, F=jacob, false. X=joseph, Y=dan; X=dan, Y=joseph; X=joseph, Y=benjamin; X=benjamin, Y=joseph; X=dan, Y=benjamin; X=benjamin, Y=dan;
Simple Example parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). parent(jacob, dan). parent(jacob, benjamin). male(abraham). male(isaac). male(joseph). female(sarah). ? - parent(F, X), parent(F, Y), X /= Y. F=jacob, X=joseph, Y=dan F=jacob, X=dan, Y=joseph F=jacob, X=joseph, Y=benjamin F=jacob, X=benjamin, Y=joseph F=jacob, X=dan, Y=benjamin F=jacob, X=benjamin, Y=dan
Relational LP Recursive rules Syntax The ancestor relationship - a recursive rule that computes the transitive closure of the parent relationship. A, D. ( P parent(A, P) & ancestor(P, D)) ancestor (A, D) % Signature: % Purpose: A ancestor(A, D) ancest. or(A, D)/2 is an ancestor of D. : - parent(A, D). : - parent(A, P), ancestor(P, D). Variables occurring in the rule body and not in the head are existentially quantified.
Recursive Rules Example parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). % Signature: ancestor(A, D)/2 % Purpose: A is an ancestor of D. ancestor(A, D) : - parent(A, D). ancestor(A, D) : - parent(A, P), ancestor(P, D). ? - ancestor(A, joseph). A = jacob; A = abraham; A = isaac; A = sarah; False. ? - ancestor(sarah, D). D = isaac; D = jacob; D = joseph; False.
Recursive Rules Example parent(abraham, isaac). parent(isaac, jacob). parent(sarah, isaac). parent(jacob, joseph). % Signature: ancestor(A, D)/2 % Purpose: A is an ancestor of D. ancestor(A, D) : - parent(A, D). ancestor(A, D) : - ancestor(P, D), parent(A, P). ? - ancestor(A, joseph). A = A = Out jacob; abraham; isaac; sarah; of local stack. ? - ancestor(sarah, D). D D D = Out = isaac; = jacob; joseph; of local stack. ? - ancestor(sarah, isaac). true; Out of local stack.
Recursive Rules Example A ancestor(A, D) : - parent(A, D). ancestor(A, D) : - parent(A, P), ancestor(P, D). First Then P A ancestor(A, D) : - parent(A, D). ancestor(A, D) : - ancestor(P, D), parent(A, P). D D First Then P Ancestor(P, D) Ancestor(P 2, D) Ancestor(P 3, D) Ancestor(P 4, D) …
Relational LP – Concrete Syntax <program> ==> <procedure>+ <procedure> ==> (<rule> | <fact>)+ with identical predicate and arity <rule> ==> <head> : - <body>. <fact> ==> <head> ==> <atomic-formula> <body> ==> (<atomic-formula> , )* <atomic-formula> ==> <constant> | <predicate> ( (<term>, )* <term> ) <predicate> ==> <constant> <term> ==> <constant> | <variable> <constant> ==> A string starting with a lower case letter. <variable> ==> A string starting with an upper case letter. <query> ==> ? - (<atomic-formula>, )* <atomic-formula>.
Relational LP – Abstract Syntax <program>: Components: <procedure>: Components: Rule: <rule> Fact: <atomic-formula> Overall amount of rules and facts: >=1. Ordered. <rule>: Components: Head: <atomic-formula> Body: <atomic-formula> Amount: >=1. Ordered. <atomic-formula>: Kinds: <predication>, <constant> <predication>: Components: Predicate: <constant> Term: <term> Amount: >=1. Ordered. <query>: Components: Goal: <atomic-formula> Amount: >=1. Ordered. <term>: Kinds: <constant>, <variable>
Relational LP – Interim Summary Concepts: o Predicate symbol o Term o Facts o Rules o Query Semantics: o o Quantification of variables (existential/universal) Answers are partial substitutions to query variables (or true/false indications)
Operational Semantics for LP Input: a program P and a query Q Output: partial substitutions to query variables (or true/false indications) Method: by trying to build a proof tree for the given query
Proof Tree - Example parent(abraham, isaac). %p 1 parent(isaac, jacob). %p 2 parent(sarah, isaac). %p 3 parent(jacob, joseph). %p 4 parent(jacob, dan). %p 5 parent(jacob, dinah). %p 6 male(abraham). male(isaac). male(jacob). male(joseph). male(dan). %m 1 %m 2 %m 3 %m 4 %m 5 son(X, Y) : - parent(Y, X), %s 1 male(X). ? - son(S, jacob) %s 1 {S=X 1, Y 1=jacob} parent(jacob, X 1), male(X 1) %p 4 {X 1=joseph} male(joseph) %m 4 {} %p 5 {X 1=dan} %p 6 {X 1=dinah} male(dan) male(dinah) %m 5 {} true S=joseph S=dan false
Proof Tree - Definition Tree T is a proof tree of query Q in program P if: o Every node is a query (list of goals) o The root node is the query Q o Every edge is labeled by a substitution and a rule fact name o If there is a transition on edge e = (Subs, Rname) from node N = (G 1, …, Gn) to node N’ then: There exists rule Rname = (H : - B 1, …, Bk) in P such that H can be unified with a goal Gi via substitution Subs and Node N’ is obtained from node N by substituting the goal Gi with B 1, …, Bk and applying the substitution Subs on the result That is, N’ = (G 1, …Gi-1, B 1, …, Bk , Gi+1, …Gn) Subs A proof tree is said to be a complete proof tree of query Q in program P if no more transitions can be added.
Proof Tree – Obtaining Answers o o o A leaf with goal true, is a success node son(S, jacob) %s 1 {S=X 1, Y 1=jacob} A leaf with goal false is a failure node If a leaf is marked true, in order to extract the result we can collect all substitutions along the path to the leaf, compose them, parent(jacob, X 1), male(X 1) %p 4 {X 1=joseph} male(joseph) and return only substitutions to the required vars %p 6 {X 1=dinah} %p 5 {X 1=dan} %m 4 {} true S=joseph {S=X 1, Y 1=jacob} {X 1=joseph} {} = {S=joseph, Y 1=jacob, X 1=joseph} male(dan) male(dinah) %m 5 {} true false S=dan {S=X 1, Y 1=jacob} {X 1=dan} {} = {S=dan, Y 1=jacob, X 1=dan}
Proof Tree – Terminology A proof tree is a finite tree if it has no infinite paths. Otherwise it is an infinite tree. A proof tree is a success tree if it has a success leaf. Otherwise it is a failure tree. Thus, o given a finite tree we can always determine whether it is a success tree or failure tree. o given an infinite tree, if it has a success leaf, we can determine it is a success tree, otherwise we don’t know…
Operational Semantics for LP Method: Build a node N for the given query Q From current node N: If it is a success leaf (N = true), o extract answer and return it o Backtrack to obtain more answers Output: partial subs to query variables (or true/false) If it is a failure leaf (N =false) Backtrack o Input: a program P and a query Q If it is not a leaf node and N = (G 1, …, Gn) o select a goal Gi and o select a rule H : - B 1, …, Bk o Algorithm uses procedures: • Gsel - for selecting a goal • Grule - for selecting a rule • Unify – for trying to unify rule head and goal. If H can be unified with Gi via substitution Subs then create a new node N’ = (G 1, …Gi-1, B 1, …, Bk , Gi+1, …Gn) Subs Otherwise, create a new node N’ = false. Make N’ the current node.
Unification The unification operation: Given two atomic formulas A 1, A 2 Return a substitution s such that A 1 s = A 2 s. Examples: p(3, X), p(Y, 4) ==> {X=4, Y=3} p(X, 3, X), p(Y, Z, 4) ==> {X=4, Z=3, Y=4}
Substitution A substitution is a finite mapping, s, from variables to terms, such that s(X)≠X. Examples: {X=4, {X=4, Y=4, Z=3, Z=3} U=X} U=V} Y=Y} X=Y}
Application of substiution atomic formula ◦ substitution ==> atomic formula‘ The application of substitution s to an atomic formula A, denoted A◦s, simultaneously replaces the terms for their variables. Examples: p(X, 3, X, W) ◦ {X = 4, Y = 4} = p(4, 3, 4, W) p(X, 3, X, W) ◦ {X = 4, W = 5} = p(4, 3, 4, 5) p(X, 3, X, W) ◦ {X = W, W = X} = p(W, 3, W, X)
Instantiation and Generalization An atomic formula A’ is an instance of an atomic formula A if there exists a substitution s such that A◦s = A’ A is more general than A’ if A’ is an instance of A Examples: p(X, 3, X, W) is more general than p(4, 3, 4, W), which is more general than p(4, 3, 4, 5). p(X, 3, X, W) is more general than p(W, 3, W, W), which is more general than p(5, 3, 5, 5). p(X, 3, X, W) is more general than p(W, 3, W, X), which is more general than p(X, 3, X, W).
Unifier A unifier of atomic formulas A and B is a substitution s, such that A◦s = B◦s. For example, the following substitutions are unifiers of p(X, 3, X, W) and p(Y, Z, 4, W): {X=4, Z=3, Y=4} {X=4, Z=3, Y=4, W=5} {X=4, Z=3, Y=4, W=0} The mgu of atomic formulas A and B is a unifier s of A and B such that A◦s = B◦s is more general than all other instances of A and B obtained by applying a unifier
Composition of Substitutions s ◦ s' 1. A variable X for which s(X) is defined, is removed from the domain of s‘ 2. s' is applied to the terms of s 3. The modified s' is added to s. 4. Identity bindings are removed. Example: {X=Y, Z=3, U=V} ◦ {Y=4, W=5, V=U, Z=X} = {X=4, Z=3, Y=4, W=5, V=U}
- Slides: 49