FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative...

Post on 19-Jan-2016

217 views 0 download

Tags:

Transcript of FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative...

FORMAL METHODSProlog

April 2 & 6, 2015

Formal Methods

Programming Languages

Imperative Declarative

Procedural Data Abstraction

C … ObjectsModules

Ada … Java …

Relational Functional

Constraints Logic ML …

CLP … Prolog …

… …ML

Jayaraman

Declarative vs Imperative Programming

• Focus on ‘what’ rather than ‘how’• Declarative Programs resemble

Specifications• Specifications are Executable• Program reasoning is easier• Greater Code Reuse• More amenable to Parallel Execution

Formal Methods Jayaraman

Simple Illustration• Directions for a party for 25 people at

41 Prestonwood Lane, East Amherst

• Imperative Solution:for i = 1 to 25 do {

tell person[i] how to go from house[i] to party; tell person[i] how to go from party to house[i];

}

Formal Methods Jayaraman

Declarative Solution

Formal Methods Jayaraman

• A map is a declarative specification of a set of relationships, i.e., which roads are connected to another.

• Every person needs to know the logic underlying maps:

path(X,Y) road(X,Y).path(X,Y) road(X,Z),

path(Z,Y).• A map has no explicit control and

facilitates greater “code re-use”.Formal Methods Jayaraman

Algorithm = Logic + Control

• Famous paper by Robert Kowalski, Communications of the ACM, 1979

• Imperative Programs explicit Control, implicit Logic

• Declarative Programs explicit Logic, implicit Control

Formal Methods Jayaraman

Prolog• Programming in Logic• Programs are logical assertions

(for the most part).• The logic is a restriction of first-order

predicate calculus called Horn clauses.• Horn clauses have a rule-like format,

and have a natural execution model.• Prolog was invented in 1972, and has

efficient implementations today.• Download SWI-Prolog (free, good Prolog)

Formal Methods Jayaraman

Applications of PrologTraditional:

- symbolic computing- database querying- language processing

Novel uses: - configuring mobile phones for

Nokia- engineering/marketing db in Boeing- network mangement in Lucent- network configuration in

Windows/NTFormal Methods Jayaraman

Prolog Personalities

Robert A. Kowalski, introducedthe concept of ‘logic programming’

Alain Colmerauer, with colleaguePhillipe Roussel, invented Prologat the University of Marseilles

Formal Methods Jayaraman

Horn Clauses

• Program Clauses

p(terms).p(terms) :- p1(terms), …, pn(terms).

• Goal Clause

?- pi(terms).

Unit clause

Conditional clause

Formal Methods Jayaraman

if

Syntactic Conventions• Variables begin with uppercase letter:

X, Y, From, To, City, State, …

• Constants and predicates begin with lowercase letter

• Special syntax for lists:• [ ], • [1,2,3], • [[10, apple], [20, banana]]• …

• Prolog is dynamically typed• variables are not typed, run-time type checking

Formal Methods Jayaraman

Horn Clauses as Procedures

p(terms).p(terms) :- p1(terms), …, pn(terms).

path(X,Y) :- edge(X,Y).path(X,Y) :- edge(X,Z), path(Z,Y).

Example:

procedurehead

procedurebody

nondeterministicprocedure

Formal Methods Jayaraman

Datalog is a subset of Prolog

• The family database example• Some relations of interest:

father, mother, parent,grandparent, ancestor

• Illustrates Horn clauses and the basic execution model for Prolog

Formal Methods Jayaraman

Family Database Program

mother(bob, mary).mother(ann, mary).mother(mark, jane).mother(mary, sue).

father(bob,mark).father(ann,mark).father(mark, joe).father(mary,steve).

bob ann

mark mary

joe jane steve sue

Formal Methods Jayaraman

Use quotes for constants starting with upper-case

mother(‘Bob’, ‘Mary’).mother(‘Ann’, ‘mary’).…

father(‘Bob’, ‘Mark’).father(‘Ann’, ‘Mark’).…

Bob Ann

Mark Mary

Joe Jane Steve Sue

Formal Methods Jayaraman

Representing a Graph

c

a

b d

e f

edge(a,b).edge(a,c).edge(c,e).edge(b,d).edge(d,e).edge(e,f).

Prolog systemsperform indexingon such facts for efficientretrieval

Formal Methods Jayaraman

Conditional Clauses

ancestor(X,Y) :- parent(X, Y).ancenstor(X,Y) :-

parent(X, Y) :- father(X, Y).parent(X, Y) :- mother(X, Y).grandparent(X, Y) :- parent(X, Z),

parent(Z, Y).

Formal Methods Jayaraman

Prolog predicates as Testers and Generators

father(bob,mark).father(ann,mark).father(mark, joe).father(mary,steve).

Example:

Sample Goals:?- father(mark, joe).

yes

?- father(mary, X).

X = steve

?

?- father(Who, mark). ?X = bob ;X = ann

Formal Methods Jayaraman

Prolog Computation Model

• Two key ideas:• Resolution-style Proof - unification of a goal with clause-

head• Backtracking Search - for answers in resolution search

tree

Formal Methods Jayaraman

Nondeterminism Backtracking

mother(bob, mary).mother(ann, mary).mother(mark, jane).mother(mary, sue).

father(bob,mark).father(ann,mark).father(mark, joe).father(mary,steve).

parent(X, Y) :- father(X, Y).parent(X, Y) :- mother(X, Y).Goal:

?- parent(mark, jane)

father(mark, jane)

fail

mother(mark, jane)

succeed

Formal Methods Jayaraman

Backtracking + Answers mother(bob, mary).mother(ann, mary).mother(mark, jane).mother(mary, sue).

father(bob,mark).father(ann,mark).father(mark, joe).father(mary,steve).

parent(X, Y) :- father(X, Y).parent(X, Y) :- mother(X, Y).Goal:

?- parent(mark, Who)

father(mark, Who)

Who = joe

mother(mark, Who)

Who = jane;Formal Methods Jayaraman

Backtracking + Answers

mother(bob, mary).mother(ann, mary).mother(mark, jane).mother(mary, sue).

father(bob,mark).father(ann,mark).father(mark, joe).father(mary,steve).

parent(X, Y) :- father(X, Y).parent(X, Y) :- mother(X, Y).gp(X,Y) :- parent(X,Z), parent (Z,Y)

?-gp(bob, sue)

p(bob, Z), p(Z, sue)

f(bob, Z), p(Z, sue)

p(mark, sue)Z = mark

m(bob, Z), p(Z, sue)

p(mary, sue)Z = mary

f(mary, sue)

fail succeed

m(mary, sue)

fail

f(mark, sue) m(mark, sue)

fail

Formal Methods

Jayaraman

Flexible Parameter Passing

• A predicate can be invoked in any “mode” of instantiation, e.g.,

… p(bob, sue) … p(bob, Y) … p(X, sue) … p(X, Y) …

valueresult

value-valuevalue-resultresult-valueresult-result

Formal Methods Jayaraman

Unification of T1 and T2

T2T1

const (c2)

var(V2)

f(terms2)

const (c1) var(V1)

g(terms1)

c1 = c2 V2 ← c1

V1 ← c2 V1 ← V2

fail

fail

Formal Methods Jayaraman

More General Terms

• Data Terms:term ::= constant | variable |

f(terms)terms ::= term1 , … , termn

• Examples constructor terms:leaf(10)node(node(leaf(1), leaf(2)), leaf(3)))name(arthur, conan, doyle)cons(1, cons(2, nil))

Formal Methods Jayaraman

List Processing in Prolog

Lists are made an infix constructor ‘.’:

.(1, .(2, .(3, [])))Special syntax:

[1, 2, 3]

Nested lists are fine: [[10, 20], [30], [50]]

Lists can be heterogeneous: [[10, banana], [20, orange], [30, grape]]

Formal Methods Jayaraman

List Patterns

The list pattern [H|T] unifies with a list such that H is the head of the list and T is the tail.

Examples: ?- [H|T] = [1,2,3]. H = 1, T = [2,3]

?- [[N,F] | T] = [[1,fig], [5, pear],[25,grape]]

N = 1, F = fig, T = [[5, pear], [25,grape]]

Formal Methods Jayaraman

Formal Methods

Member: Code Re-use• Consider the member predicate:

member(X, [X|_]).member(X, [_|T]) :- member(X,T).

• Can be used as a tester:

|?- member(2, [1,2,3]). yes

• Can also be used as a generator:

|?- member(E, [1,2,3]). E = 1 ; E = 2 ; E = 3 ;

no

_ is a “don’t care” variable, as in ML

Jayaraman

Formal Methods

How ‘member’ works

?- member(E,[1,2,3]).

member(E, [2,3]).

member(E, [3])

member(X, [X | _]).member(X, [_ | T]) :- member(X,T).

member(E, [ ])

fail fail

succeedE = 1

succeed

E = 2succeed

E = 3

Jayaraman

More Unification Examples

?- ([H|T], L2, [H|L3]) = ([1,2], [4,5], Ans)

H = 1T = [2]L2 = [4, 5]Ans = [1 | L3]

?- ([], L2, L2) = ([], [4,5], Ans)

L2 = [4, 5]Ans = [4, 5]

Formal Methods Jayaraman

The ‘append’ definition

app([], L2, L2).app([H|T], L2, [H|L3]) :- app(T,L2,L3).

?- app([1,2], [4,5], Ans).

failapp([2], [4,5], L3).

Ans = [1|L3]

app([], [4,5], L3’).L3 = [2|L3’]fail

succeed L3’ = [4,5] Ans = [1,2,4,5]

Formal Methods Jayaraman

Let’s compare the definition of the ‘append’ operation

in the differentprogramming

paradigms

Formal Methods Jayaraman

C definition of ‘append’

Formal Methods Jayaraman

typedef struct list { int data; struct list *next; } LIST;

LIST* append(LIST *l1, LIST *l2) { LIST *n; if (l1 == NULL) return l2 else { n = (LIST*) malloc(sizeof(LIST)); n->data = l1->data; n->next = append(l1->next, l2); return n; } }

Java definition of ‘append’

Formal Methods Jayaraman

class List { int data; List next;}List append(List l2) {

List n = new List();n.data = data;

if (next != null) n.next = next.append(l2); else n.next = l2; return n; }…}

Jayaraman

Lisp definition of ‘append’

(defun append (l1 l2)(if (null l1) l2 (cons (first l1)

(append (rest l1) l2))))

Formal Methods

Lisp definition is more abstract: 1. No explicit pointer variables. 2. No explicit storage allocation. 3. ‘Cons’ causes storage to be allocated.

ML and Prolog Definitions

fun append([ ], l2) = l2 | append (h::t, l2) =

h :: append(t, l2)

append([ ], L2, L2).append ([H|T], L2, [H|L3]) :-

append(T, L2, L3).

• ML supports definitions of functions, whereas Prolog supports definitions of relations.• ML and Prolog patterns are more declarative than Lisp’s explicit selection operations (first and rest).• ML uses pattern matching, whereas Prolog uses unification (a form of “two-way matching”).• ML is statically typed, but Prolog is dynamically typed.• ML execution is deterministic, while Prolog execution is nondeterministic, i.e., depth-first search + backtracking.

Formal Methods Jayaraman

From ML to Prolog

fun append([ ], l2) = l2 | append (h::t, l2) = h :: append(t, l2)

Formal Methods

fun append([ ], l2) = l2 | append (h::t, l2) = h :: l3 :-

append(t, l2) = l3

fun append([ ], L2) = L2 | append ([H|T], l2) = [H|L3] :-

append(T, L2) = L3

append([ ], L2, L2).append ([H|T], L2, [H|L3]) :- append(T, L2, L3).

flatten

change syntax

make relation

Jayaraman

Code Reuse in Prolog ‘append’

app([ ], L2, L2).app([H|T], L2, [H|L3]) :- app(T, L2, L3).

?- app([1,2], [4,5], [1,2,4,5]).yes

?- app([1,2], [4,5], Ans).Ans = [1,2,4,5]

?- app([1,2], Ans, [1,2,4,5]).Ans = [4,5]

Formal Methods Jayaraman

Code Reuse in ‘append’ (contd)

app([], L2, L2).app([H|T], L2, [H|L3]) :- app(T,L2,L3).

?- app(L, [4,5], [1,2,4,5]). L = [1,2]

?- app(X,Y, [1,2,4,5]).

X = [], Y = [1,2,4,5]X = [1], Y = [2,4,5]X = [1,2], Y = [4,5]X = [1,2,4], Y =[5]X = [1,2,4,5], Y = []

Formal Methods Jayaraman

Unification: Prolog parameter passing

Given a pair of terms, t1 and t2, a unifier of t1 and t2 is substitution θ for the variables occurring in t1 and t2 such that t1 θ = t2 θ.

Example: t1 = p(bob, Y) t2 = p(X, sue)

Unifier θ = {X = bob, Y = sue}

Formal Methods Jayaraman

Most General Unifier Given a pair of terms, t1 and t2,

the most general unifier (mgu) μ of t1 and t2 is related to an ordinary unifier θ by the relation θ = μ ρ.

Example: t1 = p(X, Y) t2 = p(Z, sue)

Unifiers: θ1 = {X = a, Z = a, Y = sue} θ2 = {X = b, Z = b, Y = sue}

….

MGU μ = {X = T, Z = T, Y = sue}Formal Methods Jayaraman

Unification of General Terms

?- node(T1, T2) = node(leaf(10), leaf(20))

T1 = leaf(10)T2 = leaf(20)

?- app([H|T], L2, [H|L3]) = app([1,2], [4,5], Ans)

H = 1T = [2]L2 = [4, 5]Ans = [1 | L3]

Formal Methods Jayaraman

Unification of T1 and T2

T2T1

const (c2)

var(V2)

f(terms2)

const (c1) var(V1)

g(terms1)

c1 = c2 V2 ← c1

V1 ← c2 V1 ← V2

fail

fail

V1 ← f(terms2)

if occurs-check

V2 ← g(terms1)

if occurs-check

Unify terms1 and terms2 if f = g

Formal Methods Jayaraman

Occurs Check

Consider the unification ofL = [1 | L]

This equation does not have a solution, unless the infinite list [1,1,1,…] is permitted. First-order logic does not support such infinite structures, hence unification fails. “Occurs check” => check whether a variable, such as L, occurs in a term, such as [1 | L].

Formal Methods Jayaraman

Most General Unifier is Unique

Given a pair of terms, t1 and t2, the most general unifier (mgu) μ of t1 and t2 is unique – the possible exception being that two mgu’s might differ by the names of unbound variables.

Formal Methods Jayaraman

Formal Methods Jayaraman

CLP(R):Constraint Logic

Programming

• Generalizes Prolog by extending unification to more general form of constraint solving.

• Constraints are of different kinds:- Finite Domain: boolean, finite sets, …- Infinite Domain: integers, reals, …

• Constraints bring up new issues in the computational strategy.

Formal Methods Jayaraman48

Prolog’s Behavior

|?- 1 + 2 = 3.

no

|?- X = 3 * 2.

X = 3*2

Prolog’s unification operation treats +, *, etc.syntactically as a infix binary constructors.

|?- 1 + 2 = 1 + 2.

yes

|?- 1 + 2 = 2 + 1.

no

Formal Methods Jayaraman49

SICStus Prolog Constraints

| ?- use_module(library(clpr)).

| ? - {X = 2 * 3}.

X = 6.0

clp = constraintlogic programming

r = reals

| ? – {X + Y = 6, X – Y = 2}.

X = 4.0, Y = 2.0

| ? – {X * X = 64}.

64 – X*X = 0

No support forNonlinear equations!

Formal Methods Jayaraman

Constraint OperationsWhat are constraint operations?

They are builtin functions (+, *, … ) and relations (>, <, …) whose properties are known

to the underlying system, e.g.,X+Y = Y+X,X+0 = X,

X*0 = 0, X>Y ۸ Y>X false

Formal Methods Jayaraman

Definite Clauses + Constraints

p(terms).p(terms) :- p1(terms), … c(terms), … pn(terms)

In addition to ordinary goals, there could be constraint goals, c(terms), in the body.

Unification replaced by Constraint Solving.

Backtracking is used as before.

Formal Methods Jayaraman

Compound Interest

Compute the amount A that a principal P would grow to at monthly interest I in time T, compounded monthly.

We can define money1(P, T, I, A) as follows.

money1(P, 0, I, P).money1(P, T, I, A) :-

T > 0,money1(P*(I+1), T-1, I, A).

Formal Methods Jayaraman

money1(100, 5, 0.1, A)• money1(100, 5, 0.1, A)

• money1(100*1.1, 4, 0.1, A)

• money1(110*1.1, 3, 0.1, A)

• money1(121*1.1, 2, 0.1, A)

• money1(133.1*1.1, 1, 0.1, A)

• money1(146.41, 0, 0.1, A)

Answer: A = 146.41

Formal Methods Jayaraman

Constraints are Bi-directional

Compute the principal P that would grow to amount A at monthly interest I in time T, compounded monthly.

We can define money1(P, T, I, A) as follows.

money1(P, 0, I, P).money1(P, T, I, A) :-

T > 0,money1(P*(I+1), T-1, I, A).

Formal Methods Jayaraman

money1(P, 5, 0.1, 200)• money1(P, 5, 0.1, 200)

• money1(P*1.1, 4, 0.1, 200)

• money1(P*1.21, 3, 0.1, 200)

• money1(P*1.331, 2, 0.1, 200)

• money1(P*.14641, 1, 0.1, 200)

• money1(P*1.6105, 0, 0.1, 200)

Solve: P*1.6105 = 200

LINEAR!

Formal Methods Jayaraman

Constraint solving is limited

What is the interest rate I needed to grow principalP to amount A in time T?

Can we use the previous definition for this query?

money1(P, 0, _, P).money1(P, T, I, A) :- T > 0, money1(P*(1+I), T-1, I, A).

Formal Methods Jayaraman

money1(100, 5, I, 200)• money1(100, 5, I, 200)

• money1(100*(I+1), 4, I, 200)

• money1(100*(I+1)*(I+1), 3, I, 200)

• money1(100*(I+1)*(I+1)*(I+1), 2, I, 200)

• money1(100*(I+1)*(I+1)*(I+1)*(I+1), 1, I, 200)

• money1(100*(I+1)*(I+1)*(I+1)*(I+1)*(I+1), 0, I, 200)

Solve: 100*(I+1)*(I+1)*(I+1)*(I+1)*(I+1) = 200

NONLINEAR!

Formal Methods Jayaraman

Carrying along constraints

Compute the least time T that it takes to grow a principal P to amount A at monthly interest I, compounded monthly.

Will the previous definition work?

money1(P, 0, _, P).money1(P, T, I, A) :- T > 0, money1(P*(1+I), T-1, I, A).

Formal Methods Jayaraman

money1(100, 5, 0.1, 125)• money1(100, T, 0.1, 125)

• money1(100*1.1, T-1, 0.1, 125), T>0

• money1(110*1.1, T-2, 0.1, 125), T>0, T>1,

• money1(121*1.1, T-3, 0.1, 125), T>0, T>1, T>2

• money1(133.1*1.1, T-4, 0.1, 125), T>0, T>1, T>2,

T>3

• …NONTERMINATION!

Formal Methods Jayaraman

Definite Clauses + Constraints

Compute the least time T that it takes to grow a principal P to amount A at monthly interest I, compounded monthly.

money2(P, 0, _, A) :- P >= A.money2(P, T, I, A) :- T > 0, money2(P*(1+I), T-1, I, A).

Formal Methods Jayaraman

Combining money1 and money2

money(P, T ,I, A) :- (var(T)

-> money2(P, T, I, A). ; money1(P, T, I, A)

).

The money predicate can work in differentmodes of instantiation of its arguments,except that it cannot solve nonlinearequations, a limitation of CLP(R).

Formal Methods Jayaraman

Constraints help prune unproductive search

Prolog: Generate-then-Test is the preferred strategy

CLP®: Test-then-Generate is a better strategy

Formal Methods Jayaraman

Generate-then-Test (Prolog)

gen(0). gen(1). gen(2). ... gen(10).

Prolog: |?- generate(X, Y), test(X, Y)

Lots of unnecessary backtracking!

generate(X, Y) :- gen(X), gen(Y).

test(X, Y) :- X > 6, Y > 6.

Formal Methods Jayaraman

Test-then-Generate (CLP®)

generate(X, Y) :- gen(X), gen(Y).

test(X, Y) :- X > 6, Y > 6.

CLP(R) |?- test(X, Y), generate(X, Y)

Efficient generation of values!

Formal Methods Jayaraman

CLP(R) Computation

|?- test(X, Y), generate(X, Y)

|?- X > 6, Y > 6, generate(X, Y)

|?- gen(X), gen(Y) X > 6, Y > 6

Constraint Store

gen(1)gen(0) …. gen(7)F F F S

X=0 X=7

CLP(R) Computation

|?- test(X, Y), generate(X, Y)

|?- X > 6, Y > 6, generate(X, Y)

|?- gen(X), gen(Y) X > 6, Y > 6 X = 7

|?- gen(Y) Y > 6

gen(1)gen(0) …. gen(7)F F F S

Y=0 Y=7 { X 7, Y 7 } computed answer substitution

Formal Methods Jayaraman

Applications of Constraints

• Optimization (Scheduling)• Drawing Layouts• Modeling of Physical Systems

Constrained Objects

Formal Methods Jayaraman

Formal Methods Jayaraman

Multi Paradigm Languages

• Over the last two decades, there have been several efforts to combine language paradigms:– Functional + Logic– Functional/Logic + Concurrent– Objects + Concurrency– etc.