logic programming james cheney cs 411. functional programming programs as functions write down...

22
Logic Programming James Cheney CS 411

Post on 22-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Logic Programming

James Cheney

CS 411

Functional Programming

• Programs as functions

• Write down function you want to calculate

• Computer evaluates to a value

• Based on lambda calculus, higher-order logic

• Examples: LISP/Scheme, ML

Declarative Programming

• Programs as relations

• Write down a logical description of problem

• Computer searches for answer

• Based on first-order logic

• Best-known example: Prolog

Example 1: Append

• In ML:

fun append [] l = l

| append (x::xs) l =

x::(append xs l)

- append([1,2],[3,4]);

val it : int list = [1,2,3,4]

Example 1: Append

• In Prolog:

append([],L,L).

append(X::L,M,X::N) :-

append(L,M,N).

?- append([1,2],[3,4], X).

X [1,2,3,4]

Reversibility

• Unlike ML programs, Prolog relations can be “evaluated in reverse”

?- append(X,[3,4],[1,2,3,4]).

X [1,2]

?- append([1,X],[3,4],[1,2,3,4]).

X 2

Non-determinsm

• Unlike ML, Prolog programs can be non-deterministic

• User can ask for more solutions by typing “;”?- append(X,Y,[1,2,3,4]).X -> [], Y -> [1,2,3,4];X -> [1], Y -> [2,3,4];...?- append(X,X,[1,2,3,4]).no

Types

• Prolog is untyped:

?- append([],5,X).

X 5

?- append(5,[],X).

no

Terminology

• Terms: – constants c, – function applications f(t1,t2,...),– variables X (uppercase)– lists [], [t1,t2,...]

• “Everything is a term”

Terminology

• Facts: atomic predicates on terms

append(t1,t2,t3)

• Clauses: facts or rules of the form

R(t1,...) :- R1(t11,...),

...,

Rn(tn1,...).

Terminology

• Goal/query: instance of relations

append([1,2],[3,4],X)

• Solution: substitution “answering” goal

X -> [1,2,3,4]

Execution Model

• A Prolog program is a set of clauses followed by a goal.

• The program is run by trying to find a solution to the goal using the clause

• There may be zero, one, or many solutions

Depth-first search

• Idea: To solve goal G,– If G is a clause in the program, then done– Else, if G :- G1,...,Gn is a clause in the

program, solve G1 ... Gn.– Else, give up on this goal.

Append example

append([],L,L).

append(X::L,M,X::N) :- append(L,M,N).

append([1,2],[3,4],X)?

Append example

append([],L,L).

append(X::L,M,X::N) :- append(L,M,N).

append([1,2],[3,4],X)? X=1,L=[2],M=[3,4],A=X::N

Append example

append([],L,L).

append(X::L,M,X::N) :- append(L,M,N).

append([1,2],[3,4],X)? X=1,L=[2],M=[3,4],A=X::N

append([2],[3,4],N)?

Append example

append([],L,L).

append(X::L,M,X::N’) :- append(L,M,N’).

append([1,2],[3,4],X)?

X=2,L=[],M=[3,4],N=2::N’append([2],[3,4],N)?

X=1,L=[2],M=[3,4],A=1::N

Append example

append([],L,L).

append(X::L,M,X::N’) :- append(L,M,N’).

append([1,2],[3,4],X)?

X=2,L=[],M=[3,4],N=2::N’append([2],[3,4],N)?

X=1,L=[2],M=[3,4],A=1::N

append([],[3,4],N’)?

Append example

append([],L,L).

append(X::L,M,X::N’) :- append(L,M,N’).

append([1,2],[3,4],X)?

X=2,L=[],M=[3,4],N=2::N’append([2],[3,4],N)?

X=1,L=[2],M=[3,4],A=1::N

append([],[3,4],N’)? L = [3,4], N’ = L

Append example

append([],L,L).

append(X::L,M,X::N’) :- append(L,M,N’).

append([1,2],[3,4],X)?

X=2,L=[],M=[3,4],N=2::N’append([2],[3,4],N)?

X=1,L=[2],M=[3,4],A=1::N

append([],[3,4],N’)? L = [3,4], N’ = L

Answer: A = 1::N = 1::2::N’ = 1::2::L

= 1::2::3::4

Matching goals and clauses

• Usually, when solving G with clause

G’ D, G and G’ not exactly the same

• But through clever instantiations of variables, can make G = G’

• Example: f(1,X) = f(X,1) if X = 1

Exercises

• Write a Prolog program that reverses a list

• Solve the following for X and Y:– f(z,g(X,X)) == f(X,Y)– g(X,Y,z) == g(Y,z,X)– f(f(X,X),Y) = f(f(f(Y),f(z)),w)