Logic Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Logic Programming

Description:

Logic Programming James Cheney CS 411 – PowerPoint PPT presentation

Number of Views:166
Avg rating:3.0/5.0
Slides: 23
Provided by: jche150
Category:

less

Transcript and Presenter's Notes

Title: Logic Programming


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 (xxs) 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(XL,M,XN) -
  • 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 -gt , Y -gt 1,2,3,4
  • X -gt 1, Y -gt 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 -gt 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(XL,M,XN) - append(L,M,N).

append(1,2,3,4,X)?
15
Append example
  • append(,L,L).
  • append(XL,M,XN) - append(L,M,N).

append(1,2,3,4,X)?
X1,L2,M3,4,AXN
16
Append example
  • append(,L,L).
  • append(XL,M,XN) - append(L,M,N).

append(2,3,4,N)?
append(1,2,3,4,X)?
X1,L2,M3,4,AXN
17
Append example
  • append(,L,L).
  • append(XL,M,XN) - append(L,M,N).

X2,L,M3,4,N2N
append(2,3,4,N)?
append(1,2,3,4,X)?
X1,L2,M3,4,A1N
18
Append example
  • append(,L,L).
  • append(XL,M,XN) - append(L,M,N).

append(,3,4,N)?
X2,L,M3,4,N2N
append(2,3,4,N)?
append(1,2,3,4,X)?
X1,L2,M3,4,A1N
19
Append example
  • append(,L,L).
  • append(XL,M,XN) - append(L,M,N).

append(,3,4,N)?
L 3,4, N L
X2,L,M3,4,N2N
append(2,3,4,N)?
append(1,2,3,4,X)?
X1,L2,M3,4,A1N
20
Append example
  • append(,L,L).
  • append(XL,M,XN) - append(L,M,N).

Answer A 1N 12N
12L 1234
append(,3,4,N)?
L 3,4, N L
X2,L,M3,4,N2N
append(2,3,4,N)?
append(1,2,3,4,X)?
X1,L2,M3,4,A1N
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)
Write a Comment
User Comments (0)
About PowerShow.com