180 likes | 353 Vues
Formal Models of Computation Part II The Logic Model. Lecture 2 – Prolog: History and Introduction. Prolog: Origin and History. Prolog = Pro gramming in Log ic (1974) R.A. Kowalski: ‘Predicate logic as a programming language’ :
E N D
Formal Models of ComputationPart IIThe Logic Model Lecture 2 – Prolog: History and Introduction
Prolog: Origin and History • Prolog = Programming in Logic • (1974) R.A. Kowalski: ‘Predicate logic as a programming language’ : • First-order predicate logic for the specification of data and relations among data • Computation = logical deduction • (1972) A. Colmerauer, P. Roussel: first Prolog-like interpreter • (1980’s) Adopted as language for Japanese 5th Generation project. formal models of computation
Logic Programming (LP) • R.A. Kowalski (Imperial College): Algorithm = Logic + Control • Imperative languages (Pascal, C++, Java): • data (what) • operations on data (how) • no separation between ‘what’ and ‘how’ formal models of computation
LP: Logic + Control • Logic: What Logical Formulae: x (p(x) q(x)) • Control: How Logical Deduction: if A B and A then B formal models of computation
What: Problem Description • Horn clause: A B1,…,Bn • A,B1,…,Bn are predicates, i.e., p(a1,…,am) • Meaning: • Aistrueif B1, B2,…., and Bnare true • Horn clauses allow us to specify • Facts • Rules • Queries about objects and relations between them. formal models of computation
What: Problem Description • Facts are represented as A has(john,jaguar) “john has a jaguar” • Rules are represented as A B1,…,Bn rich(X) has(X,jaguar) “if someone has a jag they are rich” • Queries are represented as B1,…,Bn rich(Y) “who is rich?” • Facts + rules: Knowledge Base(KB) formal models of computation
How: Computing with Logic • Computation as Resolution, a deduction mechanism. • A query starts up a computation which uses rules and facts (KB) to answer the query. • A query is answered as true or false, and its variables are (possibly) assigned values. • Queries may loop! (as in any programming language programs may loop…) formal models of computation
How: Computing with Logic resolution(KB,Query):boolean let Query be C1,…,Cn fori = 1 to ndo if there is a fact A in KB such that A = Ci then true else if there is a rule A B1,…,Bnin KB such that A = Ci then resolution(KB, B1,…,Bn) elsefalse if all Ci are true then return true else return false formal models of computation
How: Computing with Logic • Recursive formulation of resolution; • Exhaustive: • in order to say “no” resolution must try all possibilities!! • Simple & general definition of computation formal models of computation
How: Computing with Logic KB={has(john,jaguar) , rich(X) has(X,jaguar)} • resolution(KB, rich(Y)) • let Query be rich(Y) • fori = 1 to 1 do • if there is a fact A in KB such that A = rich(Y) • then true • else if there is a ruleA B1,…,Bnin KB • such thatA=Ci • then resolution(KB, B1,…,Bn) • elsefalse • if all Ciare true then return true • else return false formal models of computation
How: Computing with Logic KB={has(john,jaguar) , rich(X) has(X,jaguar)} • resolution(KB, rich(Y)) • let Query be rich(Y) • fori = 1 to 1 do • if there is a factA in KB such thatA = rich(Y) • then true • else if there is a ruleA B1,…,Bnin KB • such that A = rich(Y) • then resolution(KB, B1,…,Bn) • elsefalse • if all Ciare true then return true • else return false Yes, If X = Y then rich(X) has(X,jaguar) formal models of computation
How: Computing with Logic KB={has(john,jaguar) , rich(X) has(X,jaguar)} • resolution(KB, rich(Y)) • let Query be rich(Y) • fori = 1 to 1 do • if there is a factA in KB such thatA = rich(Y) • then true • else if there is a ruleA B1,…,Bnin KB • such that A = rich(Y) • then resolution(KB, B1,…,Bn) • elsefalse • if all Ciare true then return true • else return false formal models of computation
How: Computing with Logic KB={has(john,jaguar) , rich(X) has(X,jaguar)} • resolution(KB, rich(Y)) • let Query be rich(Y) • fori = 1 to 1 do • if there is a factA in KB such thatA = rich(Y) • then true • else if there is a ruleA B1,…,Bn in KB • such that A=rich(Y) • then resolution(KB, has(Y,jaguar)) • elsefalse • if all Ciare true then return true • else return false formal models of computation
How: Computing with Logic KB={has(john,jaguar) , rich(X) has(X,jaguar)} Yes, If Y = john • resolution(KB, has(Y,jaguar)) • let Query be has(Y,jaguar) • fori = 1 to 1 do • if there is a fact A in KB such that A = has(Y,jaguar) • then true • else if there is a ruleA B1,…,Bnin KB • such thatA=Ci • then resolution(KB, B1,…,Bn) • elsefalse • if all Ciare true then return true • else return false formal models of computation
How: Computing with Logic KB={has(john,jaguar) , rich(X) has(X,jaguar)} • resolution(KB, rich(Y)) • let Query be rich(Y) • fori = 1 to 1 do • if there is a factA in KB such thatA = rich(Y) • then true • else if there is a ruleA B1,…,Bn in KB • such that A=rich(Y) • then resolution(KB, has(john,jaguar)) • elsefalse • if all Ciare true then return true • else return false That is, Y = john formal models of computation
How: Computing with Logic KB={has(john,jaguar) , rich(X) has(X,jaguar)} • resolution(KB, rich(Y)) • let Query be rich(Y) • fori = 1 to 1 do • if there is a factA in KB such thatA = rich(Y) • then true • else if there is a ruleA B1,…,Bn in KB • such that A=rich(Y) • then resolution(KB, has(john,jaguar)) • elsefalse • if rich(Y)is true then return true • else return false Yes, and Y = john formal models of computation
How: Computing with Logic resolution(KB,Query):boolean let Query be C1,…,Cn fori = 1 to ndo if there is a fact A in KB such that A = Ci then true else if there is a rule A B1,…,Bnin KB such that A = Ci then resolution(KB, B1,…,Bn) elsefalse if all Ci are true then return true else return false non-determinism: If there is more than one, which one to choose? unification: Is it possible to find values of variables that would make A and Ci equal? formal models of computation