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

68
FORMAL METHODS Prolog April 2 & 6, 2015

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

Page 1: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

FORMAL METHODSProlog

April 2 & 6, 2015

Page 2: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

Formal Methods

Programming Languages

Imperative Declarative

Procedural Data Abstraction

C … ObjectsModules

Ada … Java …

Relational Functional

Constraints Logic ML …

CLP … Prolog …

… …ML

Jayaraman

Page 3: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 4: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 5: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

Declarative Solution

Formal Methods Jayaraman

Page 6: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

• 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

Page 7: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 8: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 9: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 10: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

Prolog Personalities

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

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

Formal Methods Jayaraman

Page 11: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

Horn Clauses

• Program Clauses

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

• Goal Clause

?- pi(terms).

Unit clause

Conditional clause

Formal Methods Jayaraman

if

Page 12: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 13: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 14: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 15: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 16: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 17: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 18: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 19: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 20: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 21: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 22: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 23: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 24: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 25: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 26: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 27: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 28: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 29: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 30: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 31: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 32: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 33: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

in the differentprogramming

paradigms

Formal Methods Jayaraman

Page 34: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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; } }

Page 35: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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; }…}

Page 36: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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.

Page 37: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 38: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 39: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 40: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 41: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 42: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 43: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 44: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 45: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 46: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 47: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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.

Page 48: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 49: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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!

Page 50: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 51: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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.

Page 52: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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).

Page 53: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 54: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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).

Page 55: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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!

Page 56: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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).

Page 57: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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!

Page 58: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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).

Page 59: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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!

Page 60: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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).

Page 61: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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).

Page 62: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

Formal Methods Jayaraman

Constraints help prune unproductive search

Prolog: Generate-then-Test is the preferred strategy

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

Page 63: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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.

Page 64: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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!

Page 65: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 66: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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

Page 67: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

Applications of Constraints

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

Constrained Objects

Formal Methods Jayaraman

Page 68: FORMAL METHODS Prolog April 2 & 6, 2015. Formal Methods Programming Languages Imperative Declarative ProceduralData Abstraction C … Objects Modules Ada.

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.