Logic Languages

advertisement
Logic Languages
Source: Scott, Michael Programming Language Pragmatics 3rd Ed.
Overview
• Predicate Calculus
– Some History and Background
– Operators & Quantifiers
– Predicates
• Horn Clause
– Resolution and Unification
• ProLog
– Basics
– Examples
• Limitations, Execution and Wrap up
History and Background
• 1970’s – Alain Clomeraurer & Philippe Roussel of the Univ. of Aix-Marseille
(France) with associates at the Univ. of Edinburgh (Scotland), due to their
work, the process of logical deduction began to be used as a generalpurpose model of computing.
• Logic Languages are based on first-order predicate calculus.
• Predicate Calculus is composed of predicates, operators and quantifiers
for statements (propositions).
• Logic languages however cannot capture the full power of predicate
calculus, therefore we have limitations as we will see later.
Operators & Quantifiers
• Operators:
–
–
–
–
–
And (∧)
Or (∨)
Not (¬)
Implication (→)
Equivalence (↔)
• Quantifiers:
– For all (∀) (Universal-indicates true for all values)
– There Exists (∃) (Existential-indicates true for at least one
value)
Predicates
• Definition from section 11.3 (On CD section):
– A Predicate is a function that maps constants
(atoms) or variables to the values true and false.
Examples from Text:
• ∀C [rainy(C) ∧ cold(C) → snowy(C)]
(For all cities C, if C is rainy and C is cold, then C is snowy.)
• ∀A, ∀B [( ∃C [takes(A, C) ∧ takes(B, C)]) → classmates(A, B)]
(For all students A and B, if there exists a class C such that A takes C
and B takes C, then A and B are classmates.)
• In Predicate calculus the same thing can be said in different ways.
( P  Q) can be written as (¬P ∨ Q) (P implies Q) — (Not P or Q)
Clausal Form:
• 5 Step procedure referred to in 11.3 on cd to translate
first order into clausal form.
– Step 1: Rewrite the expression to eliminate implication (→) and
equivalence (↔) operators.
– Step 2: Move any negations (¬) outside parenthesis as far inside
as you can.
– Step 3: Technique called Skolemization (after logician Thoralf
Skolem).
• Uses Math theorem: Axiom of choice to replace X with x in
the following to remove ∃:
∃X[takes(X, cs254) ∧ class year(X, 2)] then becomes:
takes(x, cs254), class year(x, 2)
Clausal Form Continued:
– Step 4: Move all universal quantifiers (∀) to the outside
then drop it.
– Step 5: Use Boolean algebra to form a conjunctive normal
form.
• - And (∧) & Or (∨) operators can’t be nested any more
than 2 levels deep with ∧ on the outside and ∨ on the
outside.
•
Example from text:
∀A [¬student(A) → (¬dorm resident(A)∧ ¬∃B[takes(A, B) ∧ class(B)])]
(For all students A, if A is not Student, then A is not Dorm Resident and there doesn’t exist Class B such that A
takes B and B is a class) –My interpretation of the above logical equation.
•
Step 1: ∀A[student(A) ∨ (¬dorm resident(A)∧ ¬∃B[takes(A, B) ∧ class(B)])]
(For all students A, A is student or (not a dorm resident and there doesn’t exist a class B such that
student A takes class B and B is a class.))
•
Step 2: ∀A[student(A) ∨ (¬dorm resident(A) ∧ ∀B[¬(takes(A, B) ∧ class(B))])]
∀A[student(A) ∨ (¬dorm resident(A) ∧ ∀B[¬takes(A, B)∨ ¬class(B)])]
(For all students A, A is a student or ( A is not a dorm resident and for all classes B student A doesn’t take
class B and B is a class./ 2nd one or B is not a class.))
•
Step 3: -NO CHANGE in this Example-
•
Step 4: ∀(student(A) ∨ (¬dorm resident(A) ∧ (¬takes(A, B)∨ ¬class(B))))
student(A) ∨ (¬dorm resident(A) ∧ (¬takes(A, B)∨ ¬class(B)))
(For all A is a student or (A is not a dorm resident and A doesn’t take class B or B isn’t a class)
•
Step 5: (student(A)∨ ¬dorm resident(A)) ∧ (student(A)∨ ¬takes(A, B)∨ ¬class(B))
(A is a student or A is not a dorm resident) and (A is a student or A doesn’t take class B or B isn’t a class)
•
Completed, it is now in clause form.
Horn Clause
• Almost all logic languages follow the standard
form called the Horn clause.
• Every clause consists of a head and a body
– H  B1, B2, …, Bn. (read: H if B1,…, and Bn)
• As we noted earlier, Horn clauses cannot
capture all logical statements, but most. We
will revisit in limitations later.
Resolution and Unification
• Resolution:
– The process in which we derive a new statement by
combining existing statements.
– Example:
• If A , B  C and C  D
• We can use resolution to obtain A, B  D
• Unification
– During Resolution, free variables may acquire values.
– Example:
• flowery(X)  rainy(X) and we have rainy(Rochester)
• We can use Resolution to get flowery(Rochester)
ProLog
• After translating the expression into the Clausal form,
we will need to translate into ProLog.
• The Clausal form of Predicate Calculus is very close to
ProLog language.
• A minor drawback we will see in limitations is as
ProLog cannot capture all of Predicate Calculus,
Predicate Calculus does not encompass all that ProLog
can do.
• For running ProLog, I found a java embedded version
called tuProlog ide. Google search: tuProLog and it is
located in the Google code online.
Translate to Prolog:
• The next step is to now translate what we have into
Prolog.
• Simply enough, we convert each logical clause to a
Prolog fact or rule.
1. (student(A) ∨ ¬dorm resident(A)) ∧ (student(A) ∨ ¬takes(A, B)∨ ¬class(B))
2. (student(A) ← ¬(¬dorm resident(A))) ∧ (student(A) ← ¬(¬takes(A, B)∨ ¬class(B)))
3. (student(A) ← dorm resident(A))
∧ (student(A) ← (takes(A, B) ∧ class(B)))
In step three, we now have 2 Horn Clauses, and the translation into ProLog rules becomes:
student(A) :- dorm_resident(A).
student(A) :- takes(A, B), class(B).
Syntax and Notations
•
•
•
•
•
ProLog runs off of a database of clauses.
A Clause is composed of terms.
A term can be composed of constants, variables, or structures.
Constants are either an atom or a number. A number which of course is a number.
An atom starts with a lowercase letter followed by any type of punctuation or can be a
quoted string. Example:
– foo
– my_Const+
– ‘Hi,Everyone’
•
A Variable starts with an uppercase letter :
– Foo
– My_var
– X
•
Structures consist of an atom called the functor and it’s arguments which can
be constants, variables or nested structures.
– bin_tree(foo, bin_tree(bar,glarch))
More about ProLog
• ProLog like Scheme is homoiconic, it can
represent itself and modify itself.
– Keywords assert and retract add and remove
clauses to the database respectively.
• ProLog can only return true or false. (Or Yes
or No)
Advanced ProLog
• Facts are like Horn Clauses without the Head
– We also use these as a Query to give the ProLog interpreter.
• A rule has a right-hand side.
• Variable only become instantiated when unified and given a
value.
• Unification rules of ProLog:
– Constants unifies only with itself.
– Two structures unify iff the functer name and arity are the
same, and the arguments unify recursively.
– A variable unifies with anything. If it’s a value, great its
instantiated. If it’s another non-instantiated variable, then if
either get a value later, they both will be instantiated.
Execution
• ProLog is defined as Backward Chaining
• Backward Chaining is defined as starting with
the goal and working backward, attempting to
“un-resolve” it into a set of preexisting
clauses.
Limitations
• So what is missing and what can go wrong?
• Consequence could result in a disjunction on
the left-hand-side or a headless clause.
• Example of disjunctive head:
Every living thing is an animal or a plant
animal(X) ∨ plant(X) ∨ ¬living(X)
animal(X) ∨ plant(X) ← living(X)
(two left hand side)
The Prolog then would be: (as close as can)
animal(X) :- living(X), \+(plant(X))
(\+ is a form of negation used to solve
plant(X) :- living(X), \+(animal(X))
some of the issues that arise with
negations as seen here)
Limitations continued
• Example of empty head:
∀N[big(N)→ ¬(∃A, ∃B, ∃C[works(A, B, C,N)])]
(For all N, if N is Big, then not(there exists A, B, C such that A, B, C and N works)
Clausal form is:
¬big(N)∨ ¬works(A, B, C,N)
(N isn’t big or A, B, C and N don’t work)
Prolog:
big(N), works(A, B, C, N).
But this will never terminate and is only a query, it
can’t be stated as a fact or a rule.
Wrap up
• Tic Tac Toe example
• Other Examples
Download