Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Agenda
Theory
Introduction Logic Programming Resolution
Practical Hands on
Logic Programming in ProLog Tools and Applications
Related Paper
1/18/2012 2
INTRODUCTION
1/18/2012
1/18/2012
http://en.wikipedia.org/wiki/Programming_paradigm
1/18/2012
Introduction
Logic Programming Languages by Example
Prolog is a declarative programing language. Example. This means that the user only needs to define the description of the problem and does not need to solve it. The solution is found by the Prolog interpreter in form of an answer to a question with the help of logical reasoning. append([],L,L). append(X::L,M,X::N) :append(L,M,N). ?- append([1,2],[3,4], X). X [1,2,3,4]
1/18/2012
Introduction
Another Example Prolog
1/18/2012
Introduction
Conventional vs. logic Programming Languages
Thus, the fundamental differences between conventional programing language and logical programming language are as follows:
In conventional programing language
The programmer defines an algorithm in the form of step by step instructions telling the computer how to solve the problem. The computer executes the instructions in the specified order.
Introduction
Logic Programming Languages Categories
There are two sub categories of Logic Programming languages:
(1) Functional Programming Language (2) Declarative Programming Language (ProLog Family)
1/18/2012
http://en.wikipedia.org/wiki/Category:Logic_programming_languages
10
Examples: LISP/Scheme, ML
1/18/2012 12
1/18/2012
13
LOGIC PROGRAMING
1/18/2012
14
Theorem Proving
Logic programs evaluated by a theorem prover Derivation of answer from a set of initial axioms
Under certain restrictions reasoning over knowledge bases based on such rules is decideable (within First Order Logic)
A FOL Horn clause is a disjunction of literals with one positive literal, with all variables universally quantified:
( ) C1 Cn Cn H H
In LP systems usually the following (non First Order Logic) syntax is used: Such rules can be evaluated very efficiently, since the resolution of two Horn clauses is a again a Horn clause
Examples:
cow, b, Jonny, loves(John)
Examples:
p(x ) loves(Jonny, Mary), worksAt(Jonny, SomeCompany) worksAt(loves(Mary), SomeCompany)
Literals
A literal is a an atom or its negation
A positive literal is an atom A negative literal is a negated atom A ground literals is a literal without variables
The head of the rule consists of one positive literal H The body of the rule consists of a number of literals B1, ..., Bn B1, , Bn are also called subgoals
Examples:
parent(x) :- hasChild(x,y) father(x) :- parent(x), male(x) hasAunt(z,y) :- hasSister(x,y), hasChild(x,z)
Examples:
hasChild(Jonny, Sue) Male(Jonny)).
Examples:
? - hasSister(Jonny,y), hasChild(Jonny , z) gives all the sisters and children of Jonny ? - hasAunt(Mary,y) gives all the aunts of Mary ?- father(Jonny) ansers if Jonny is a father
Model-theoretic semantics
Defines the meaning of a model in terms of its minimal Herbrand model.
These two semantics are different in style, but agree on the minimal model LP semantics is only equivalent to standard FOL semantics
Concerning ground entailment As long as LP is not extended with negation
The minimal Herbrand model is a First-Order model Every Herbrand model is a First-Order model There exist First-Order models which are not Herbrand models
Example:
p(a), p(x) q(x) The Minimal Herbrand model: {p(a), q(a)} This is actually the only Herbrand model! First-Order models: {p(a), q(a)}, {p(a), q(a), p(b)}, etc.
The Herbrand Universe U is the set of all ground terms which can be formed from
Constancts in a program Function symbols in a program
Example: a, b, c, f(a) The Herbrand Base B is the set of all ground atoms which can be built from
Predicate symbols in a program Ground terms from U
A very intuitive and easy way to capture the semantics of LP As soon as negation is allowed a unique minimal model is not guaranteed anymore
NAF is not classical negation and pushes LP beyond classical First Order Logic This allows a form of negation in rules:
( ) C1 H :- B1, Ci not Cn Bi, not Bn H
This is a problem as soon as negation is allowed since the minimal model is not uniquely defined anymore It is useful to consider this using a dependency graph
A predicate is a node in the graph There is a directed edge between predicates q and p if they occur in a rule where q occurs in the head and p in the body. If the dependency graph contains a cycle then the program is recursive
A Solution: Stratification
Mark edges with negation in the dependency graph Separate predicates which are connected through a positive edge in a individual stratum Strata can be (partially) ordered If each predicate occurs only in one stratum, then the program is called stratifiable Each stratum can be evaluated as usual and independently from other strata This guarantees a unique interpretation of a Logic Program using negation
Restrictions:
1. Datalog disallows function symbols 2. Imposes stratification restrictions on the use of recursion + negation 3. Allows only range restricted variables (safe variables)
Safe Variables:
Only allows range restricted variables, i.e. each variable in the conclusion of a rule must also appear in a not negated clause in the premise of this rule. This limits evaluation of variables to finitely many possible bindings
Logic Programming
Concrete Logic Programming System: IRIS
Java based Datalog reasoner
Developed at STI Innsbruck Freely available open source project Homepage: http://www.iris-reasoner.org/
Extensions:
Stratified / Well-founded default negation XML Schema data types Various built-in predicates (Equality, inequality, assignment, unification, comparison, type checking, arithmetic, regular expressions,... )
Logic Programming
Concrete Logic Programming System: IRIS
An example of a concrete combination of components within IRIS: Program Optimization Rewriting techniques from deductive DB research (e.g. Magic sets rewriting) Safety Processing & Stratification Ensure specific syntactic restrictions Rule Re-ordering Minimize evaluation effort based on dependencies between expressions Rule Optimizations Join condition optimization Literal re-ordering Rule compilation Pre-indexing Creation of views on required parts of information
Logic Programming
Concrete Logic Programming System: IRIS
Example program (also see online demo at http://www.iris-reasoner.org/demo):
man('homer'). woman('marge'). hasSon('homer','bart'). isMale(?x) :- man(?x). isFemale(?x) :- woman(?x). isMale(?y) :- hasSon(?x,?y).
Query:
?-isMale(?x).
Output:
Init time: 14ms ---------------------------------Query: ?- isMale(?x). ==>> 2 rows in 1ms Variables: ?x ('bart') ('homer')
Logic Programming
Concrete Logic Programming System: IRIS
IRIS performing computations using semi-naiv evaluation in combination with stratification and complex datatypes
RESOLUTION
1/18/2012
37
Theorems:
Expressions that can be derived from the axioms and the rules of inference.
1/18/2012 38
Resolution Principle
Resolution is a refutation system.
To prove a statement we attempt to refute its negation
Basic idea: Given a consistent set of axioms KB and goal sentence Q, we want to show that KB Q
This means that every interpretation I that satisfies KB also satisfies Q Any interpretation I only satisfies either Q or Q, but not both Therefore, if in fact KB Q holds, an interpretation that satisfies KB, satisfies Q and does not satisfy Q Hence KB {Q} is un-satisfiable, i.e., it is false under all interpretations
1/18/2012
39
Resolution Principle
Resolution commonly requires sentences to be in clause normal form (also called conjunctive normal form or CNF)
A clause is a disjunction of literals (implicitly universally quantified)
E.g. : l1 E.g.: C1 ln Cn = (ln lm ) (li lj )
1/18/2012
40
The clause produced by the resolution rule is called the resolvent of the two input clauses.
A literal and its negation produce a resolvent only if they unify under some substitution W. W Is then applied to the resolvent
1/18/2012
41
Modus ponens can be seen as a special case of resolution of a one-literal clause and a two-literal clause Example: Given clauses P(x, f(a)) v P(x, f(y)) v Q(y) and P(z, f(a)) v Q(z) P(x, f(a)) and P(z, f(a)) unify with substitution = {x/z} Therefore, we derive the resolvent clause (to which is applied): P(z, f(y)) v Q(y) v Q(z)
1/18/2012
42
2.
3.
1/18/2012
Step 4: Move all quantifiers to the left to obtain a prenex normal form
A formula is inn prenex normal form if it is written as a string of quantifiers followed by a quantifier-free part
1/18/2012
46
(X)(Y)(W)(foo(X,Y,f(X,Y),w))
1/18/2012 47
step 8: Split each conjunction into a separate clauses, which are just a disjunction of negated and non-negated predicates, called literals step 9: Rename so that no variable symbol appears in more than one clause.
(X)(a(X) b(X)) = (X)a(X) ( Y)b(Y)
1/18/2012
48
die(fido)
1/18/2012 49
1/18/2012
50
Resolution Strategies
Resolution defines a search space
The decision which clauses will be resolved against which others define the operators in the space A search method is required
Strategies
Depth-First Strategy Breadth-First Strategy Set of Support Strategy Unit Preference Strategy Linear Input Form Strategy
1/18/2012
52
ProLog Background
PROgramming in LOGic It is the most widely used logic programming language Its development started in 1970 and it was result of a collaboration between researchers from Marseille, France, and Edinburgh, Scotland
1/18/2012 54
1/18/2012
1/18/2012
56
Axioms are a theory Goal statement is a theorem Computation is deduction to prove the theorem within the theory Interpreter tries to find a collection of axioms and inference steps that imply the goal
1/18/2012 57
ProLog Relational
examples
xor boolean algebra
(smith, bob, 43, male, richmond, plumber), (smith, bob, 27, male, richmond, lawyer), (jones, alice, 31, female, durham, doctor), (jones, lisa, 12, female, raleigh, student), (smith, chris, 53, female, durham, teacher)
59
1/18/2012
ProLog Relational
examples
Prolog programs define relations and allow you to express patterns to extract various tuples from the relations Infinite relations cannot be defined by rote need rules
(A,B) are related if B is A*A (B,H,A) are related if A is B*H or gen all tuples like this (B,H,B*H*0.5)
Prolog uses Horn clauses for explicit definition (facts) and for rules
1/18/2012 60
ProLog Directionality
Parameters are not directional (in, out)
Prolog programs can be run in reverse
Horn clauses can capture most first-order predicate calculus statements but not all This is not the same issue as can Prolog compute all computable functions
any C program can be expressed in Prolog, and any Prolog program can be expressed in C
1/18/2012 62
Variables (identifiers starting with an uppercase letter) Structures (predicates or data structures)
e.g. indian(Food), date(Year,Month,Day)
1/18/2012
63
ProLog Resolution
The derivation of new statements is called Resolution The logic programming system combines existing statements to find new statements for instance
C A, B D C D A, B
1/18/2012
A and B imply C If we know that A and B imply C, and that C implies D, then we can deduce that A and B imply D
64
ProLog Example
Variable
Free Variable X acquired value Rochester during the resolution This is known as Unification
1/18/2012
65
T = tree(3, tree(2,nil,nil), tree(5,nil,nil)) Data and predicates are all the same prolog is symbolic text matching most of the time
1/18/2012 66
For example,
C2: likes(sam,Food) :- indian(Food), mild(Food). C1: indian(dahl). C3: mild(dahl).
We can replace the first and the second terms in C1 by C2 and C3 using the principle of resolution (after instantiating variable Food to dahl) Therefore, likes(sam, dahl) can be proved
1/18/2012 67
ProLog Unification
Prolog associates (binds) variables and values using a process known as unification
Variable that receive a value are said to be instantiated
Unification rules
A constant unifies only with itself Two structures unify if and only if they have the same functor and the same number of arguments, and the corresponding arguments unify recursively A variable unifies with anything
1/18/2012 68
ProLog Equallity
Equality is defined as unifiability For instance,
An equality goal is using an infix predicate =
?- dahl = dahl. Yes ?- dahl = curry. No ?- likes(Person, dahl) = likes(sam, Food). Person = sam Food = dahl ; No ?- likes(Person, curry) = likes(sam, Food). Person = sam Food = curry ; No
1/18/2012
69
ProLog Equality
What is the results of
?- likes(Person, Food) = likes(sam, Food).
Internal Representation for an UnUn-instantiated variable Any instantiation proves the equality
70
1/18/2012
Backward chaining is usually more efficient, so it is the mechanism underlying the execution of Prolog programs
Forward chaining is more efficient when the number of facts is small and the number of rules is very large
1/18/2012 71
Recursion is again the basic computational technique, as it was in functional languages Inappropriate ordering of the terms may result in non-terminating resolutions (infinite regression) For example: Graph
edge(a,b). edge(b, c). edge(c, d). edge(d,e). edge(b, e). edge(d, f). path(X, X). path(X, Y) :- edge(Z, Y), path(X, Z).
Correct
73
1/18/2012
Recursion is again the basic computational technique, as it was in functional languages Inappropriate ordering of the terms may result in non-terminating resolutions (infinite regression) For example: Graph
edge(a,b). edge(b, c). edge(c, d). edge(d,e). edge(b, e). edge(d, f). path(X, Y) :- path(X, Z), edge(Z, Y). path(X, X).
Incorrect
74
1/18/2012
1/18/2012
75
1/18/2012
76
1/18/2012
77
1/18/2012
78
1/18/2012
79
1/18/2012
80
1/18/2012
81
1/18/2012
82
1/18/2012
83
1/18/2012
84
Thank You
1/18/2012
86