This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
2
Declarative Programming Motivation
Warm Fuzzies What is Logic? ... Logic Programming?
Other parts of Prolog “Impure'' Operators” --- NOT, ! Utilities
? Constraint Programming ? Bayesian Belief Nets
3
What is Logic?
Logic is formal system for reasoning Reasoning is inferring new facts from old
Given { All men are mortal. Socrates is a man. }
infer (conclude, reason
that, ...) Socrates is mortal.
What is role of Logic within CS? Foundation of discrete mathematics Automatic theorem proving Hardware design/debugging Artificial intelligence (Cmput366)
Components: Syntax (What does it look like?) Reasoning/ProofTheory (New facts from old) Semantics (What should it do?)
4
Logic Programming Program Logic Formula
Execution of program Proving a theorem
User: 1. Specifies WHAT is true 2. Asks if something else followsProlog: Answers question.
By comparison, in Procedural Programming (C, JAVA, Fortran, ...)
user must … decide on data-structure explicitly write procedures: search, match, substitute write diff programs forfather(X, tom) vs father(tom, Y)
5
What is Logic? Syntax: Set of Expressions
? Well-formed or not? SEQUENCE of Symbols
Accept: The boys are at home. at(X, home) :- boy(X).
Reject: boys.home the angrily democracyX(at), xY=, Boy(1X,( ) :-
AcceptReject
Given { All men are mortal. Socrates is a man. }
infer (conclude, reason
that, ...) Socrates is mortal.
Proof Process: Given Believed statements, Determine other Believed statements.
{ s1, ..., sn } ⊢P s Semantics: Which expressions are Believed ?
John's mother is Mary. T John's mother is Fred. F Colorless green ideas sleep furiously. F
6
Concept of PROLOG PROgramming in LOGic
Sound Reasoning Engine1. User asserts true statements.
User asserts { All men are mortal.Socrates is a man. }
2. User poses query. “Is Socrates mortal?” “Who/what is mortal?”
Similarly, f(Y) = f(a) = f(Y) [ ○ ]( is not a mgu, as ¬’s.t. ○ ’= )
17
MGU --- Example#2
A mgu for f( W, Z, g(Z) ) & f( X, h(X), Y )
{W/X }
W/X
h(X)/Zh(W)/Z
g(Z)/Y
h(W)/Z g(h(W))/Y
g(h(W))/Y
18
MGU (con't) Notes:
If t1 and t2 are unifiable, then mgu. Can be more than 1 mgu
but they differ only in variable names. Not every unifier is a mgu. A mgu uses constants only as necessary.
Implementation: fast algorithm that computes a mgu of t1 and t2, if one
exists;or reports failure.
Slow part is verifying legal substitution: none of vi appear in any tj.Avoid by resetting Prolog's occurscheck parameter.
19
MGU ProcedureRecursive Procedure MGU( x, y ) If x=y then Return() If Variable(x) then Return( MguVar(x,y) ) If Variable(y) then Return( MguVar(y,x) ) If Constant(x) or Constant(y) then Return( False ) If Not(Length(x) = Length(y)) then Return( False ) g [] For i = 0 .. Length(x) s MGU( Part(x,i), Part(y,i) ) g Compose( g, s ) x Substitute( x, g ) y Substitute( y, g ) Return( g )End
Procedure MguVar (v,e) If Includes(v,e) then Return( False ) Return( [v/e] )End
How to read as predicate calculus?father(john, sue) X. father(odin, X).X,Y. father(X,Y) parent(X,Y)X,Y,Z.parent(X,Y)} & parent(Y,Z) gparent(X,Z)
21
Relation to Predicate Calculus In general:
t. x1, …, xm. t [called “atomic formula”]
t :- t1, …, tn. x1, …, xm. t1, …, tn t. [called “(production) rule”]
Can write any Predicate Calculus Expression in Conjunctive Normal Form
Horn clauses are CNF, with ONE Positive Literal
Horn CNF Predicate Calculus expressions that canNOT be written as Horn Clauses.
Axioms
A v B
22
What User Really Types > sicstusSICStus 3.11.2 (x86-linux-glibc2.3): Wed Jun 2 11:44:50 CEST 2004Licensed to cs.ualberta.ca| ?- [user]. % For user to enter ``Assert-fact'' mode.% consulting user...| on(aa,bb).| on(bb,cc).| above(X,Y) :- on(X,Y).| % Typing ^D exits ``assert'' mode.% consulted user in module user, 0 msec 608 bytesyes % Prolog's answer to most operations| ?-
on(X,Y).X = aa,Y = bb ? ; % User types “;” to ask for ANOTHER answerX = bb,Y = cc ? ; no % Prolog's no means there are no other answers| ?-
on(aa,bb). % User asks a question.yes % Prolog's answer| ?- on(aa,Y). % User's second questionY = bb ? % Prolog's answer: a binding listyes % Prolog's statement that there was answer| ?-
23
What User Really Types -- II
> sicstusSICStus 3.11.2 (x86-linux-glibc2.3): Wed Jun 2 11:44:50
CEST 2004Licensed to cs.ualberta.ca| ?- [file1]. % file ”file1” contains facts,
on(aa,b10).no % Prolog's answer: not derivable| ?- on(X,b10). no % Again: NO entailments| ?- above(bb,cc).yes % More than simple look-up!| ?- above(bb,W). W = cc ; % Prolog finds answer; user wants anotherno % but this is only one…| ?-
24
Prolog's Proof Process User provides
KB: Knowledge Base (List of Horn Clauses --- axioms)
: Query (aka Goal, Theorem) -- Literal 1. Who is mortal? mortal(X). 2. Is Socrates mortal? mortal(socrates).