Download presentation
Presentation is loading. Please wait.
1
Logic Programming James Cheney CS 411
2
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
3
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
4
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]
5
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]
6
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
7
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
8
Types Prolog is untyped: ?- append([],5,X). X 5 ?- append(5,[],X). no
9
Terminology Terms: –constants c, –function applications f(t1,t2,...), –variables X (uppercase) –lists [], [t1,t2,...] “Everything is a term”
10
Terminology Facts: atomic predicates on terms append(t1,t2,t3) Clauses: facts or rules of the form R(t1,...) :- R1(t11,...),..., Rn(tn1,...).
11
Terminology Goal/query: instance of relations append([1,2],[3,4],X) Solution: substitution “answering” goal X -> [1,2,3,4]
12
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
13
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.
14
Append example append([],L,L). append(X::L,M,X::N) :- append(L,M,N). append([1,2],[3,4],X)?
15
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
16
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)?
17
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
18
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’)?
19
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
20
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
21
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
22
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)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.