Lecture 20 - Tue Nov 9 Notes by Dale Miller ---------------------- Logic programming is one of four broad programming language paradigms: imperative, functional, logic, object-oriented. Logic programming is used in areas such as Artificial Intelligence, parsing, data bases, programming language environments, and expert systems, to name a few. In the operational semantics of programming languages, you have seen how to build proofs of predicates. Generally that predicate is "typeof" or "evaluate", etc. What we really have here is a kind of logic programming. Consider giving an operational semantics for "L @ K = M". We see that this judgment or predicate can be described using two inference rules L @ K = M ___________ ___________________ nil @ L = L (X::L) @ K = (X::M) If we replace the three place relation "L @ K = M" which is written using two symbols (@ and =) with just one symbol, the relation append, we would have the following two "clauses" append nil L L. append (X::L) K (X::M) :- append L K M. Notice that we no longer have a distinction between inputs and outputs. The query ?- append (1::2::nil) (3::4::nil) L. will return the binding L = (1::2::3::4::nil) while the query ?- append L K (1::2::nil). will return the bindings L = nil K = (1::2::nil) L = (1::nil) K = (2::nil) L = (1::2::nil) K = nil Notice that we can define the tail predicate tail T L :- append H T L. which succeeds if T is a tail of L. Also the membership predicate can be defined as memb X L :- append H (X::T) L. This predicate can also be defined directly: memb X (X::L). memb X (Y::L) :- memb X L. Of course, a logic programming language will have built in arithmetic but let's define our own natural numbers for a moment. Let the terms, z, (s z), (s (s z)), (s (s (s z))), etc denote the "Peano" numbers 0, 1, 2, 3, etc. Then plus, half, and Fibonacci relations can be defined as plus z N N. plus (s X) Y (s Z) :- plus X Y Z. half N M :- plus N N M. fib z z. fib (s z) (s z). fib (s (s N)) F :- fib N G, fib (s N) H, plus G H F. Consider also some additional predicates: mult z N z. mult (s N) M P :- mult N M Q, plus N Q P. nat z. nat (s N) :- nat N. We can now ask the logic programming interpreter to look for all natural numbers N such that the N'th Fibonacci number is N*N. ?- nat N, fib N F, mult N N F. There are three answers to this query. Can you find them? Consider a simple database programming example for Ancestory. father bill sue. father bill john. mother jane sue. mother jane bob. ... etc ... parent X Y :- father X Y. parent X Y :- mother X Y. ancestor X Y :- parent X Y. ancestor X Z :- parent X Z, ancestor Z Y. sibling X Y :- parent Z X, parent Z Y, not (X = Y). __________________________________________________________________