57
CLAUS BRABRAND OCT 21, 2009 PROGRAMMING PARADIGMS LOGIC PROGRAMMING with PROLOG Claus Brabrand [email protected] IT University of Copenhagen [ http://www.itu.dk/people/brabrand/ ] "Programming Paradigms", Dept. of Computer Science, Aalborg Uni. (Fall 2009)

L OGIC P ROGRAMMING with P ROLOG

  • Upload
    abeni

  • View
    55

  • Download
    0

Embed Size (px)

DESCRIPTION

"Programming Paradigms", Dept. of Computer Science, Aalborg Uni. (Fall 2009). L OGIC P ROGRAMMING with P ROLOG. Claus Brabrand [email protected] IT University of Copenhagen [ http://www.itu.dk/people/brabrand/ ]. Plan for Today. Lecture: "Lists and Arithmetic" ( 10:15 – 11:00 ) - PowerPoint PPT Presentation

Citation preview

Page 1: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

LOGIC PROGRAMMING with PROLOG

Claus [email protected]

IT University of Copenhagen

[ http://www.itu.dk/people/brabrand/ ]

"Programming Paradigms", Dept. of Computer Science, Aalborg Uni. (Fall 2009)

Page 2: L OGIC  P ROGRAMMING with  P ROLOG

[ 2 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Plan for Today

Lecture: "Lists and Arithmetic" (10:15 – 11:00) Exercise 1, 2, and 3 (11:15 – 12:00) Lunch break (12:00 – 12:30) Lecture: "Reversibility, Cut, Negation, and

Language Interpretation" (12:30 – 13:15)

Lecture: "Non-termination and Undecidability”

(13:30 – 14:00) Exercises 4, 5, and 6 (14:15 – 16:15)

Page 3: L OGIC  P ROGRAMMING with  P ROLOG

[ 3 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Outline

Part 1: Lists Arithmetic

Part 2: Reversibility Cut and Negation Language Interpretation

Part 3: Non-termination Undecidability

Page 4: L OGIC  P ROGRAMMING with  P ROLOG

[ 4 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Resolution:

1. Search knowledge base (from top to bottom) for(axiom or rule head) matching with (first) goal:

Axiom match: remove goal and process next goal [1] Rule match: (as in this case): [2] No match: backtrack (= undo; try next choice in 1.) [1]

2. "-convert" variables (to avoid name clashes, later): Goal: (record “Y = _G225”)

Match: [3]

3. Replace goal with rule body: Now resolve new goals (from left to right); [1]

f(a). f(b). g(a). g(b). h(b).k(X) :- f(X),g(X),h(X).

k(Y)

rule head rule body

axioms (5x)

rule (1x)

k(X) :- f(X),g(X),h(X).

k(_G225) :- f(_G225),g(_G225),h(_G225).

k(_G225)

f(_G225),g(_G225),h(_G225).

Possible outcomes:- success: no more goals to match (all matched w/ axioms and removed)- failure: unmatched goal (tried all possibilities: exhaustive backtracking)- non-termination: inherent risk (same- / bigger-and-bigger- / more-and-more -goals)

PROLOG's Search Order

Page 5: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

LISTS

Keywords:

(Encoded) lists, (built-in) lists, efficiency issues, ...

Page 6: L OGIC  P ROGRAMMING with  P ROLOG

[ 6 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Lists (home-made)

Lists are easily represented: // empty list // construct new list from element and list

Example: The list [1,2,3] may be represented as:

i.e., "representation of information"

Now, let's look at:

nil

cons(E,L)

cons(1, cons(2, cons(3, nil)))

"Transformation of representation of information" (= programming)

~Haskell

Page 7: L OGIC  P ROGRAMMING with  P ROLOG

[ 7 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Lists (example "functions")

Length:

Member:

Using underscore '_' (anonymous "ignore" variable) may improve readability

member(X, cons(X, L)).member(X, cons(E, L)) :- member(X, L).

len(0, nil).len(succ(N), cons(E, L)) :- len(N, L).

PROLOG also has built-in lists; let’s have a look at them…

member(X, cons(X, _)).member(X, cons(_, L)) :- member(X, L).

len(0, nil).len(succ(N), cons(_, L)) :- len(N, L).

T

Page 8: L OGIC  P ROGRAMMING with  P ROLOG

[ 8 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Lists (built-in)

Constant lists: // the empty list // constant list

Lists are (also) untyped;= finite sequence of (any) terms:

[] [ [] ] [ vincent, jules, marcellus ] [ [], mia, 42, 'The Gimp', dead(zed), Z ] [ [], [], [ [] ], [ [ x, X , [] ] ] ]

[]

[ X, Y, Z ]

Q: What is the length of the lists?

~Haskell

Page 9: L OGIC  P ROGRAMMING with  P ROLOG

[ 9 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

The Head-Tail Constructor

PROLOG (like many other languages) has: // "head-tail constructor"

H head (element) of list, T tail of list (rest) for construction and deconstruction:

(bigger) list element and (smaller) list

Example: “construction”:

Example: “deconstruction”:

[ H | T ] ~Haskellh:t

?- [ a | [b,c] ] = L.L = [a,b,c]

?- [a,b,c] = [ H | T ].H = aT = [b,c]

Page 10: L OGIC  P ROGRAMMING with  P ROLOG

[ 10 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Examples: “[ H | T ]”

Empty list example:

Mixed type example:

Tricky’ish example:

?- [] = [ H | T ].No

?- [ [], mia, 42, 'The Gimp', dead(zed), Z ] = [ X | Y ].X = []Y = [ mia, 42, 'The Gimp', dead(zed), _G225 ]Z = _G225

?- [ [], [] ] = [ X | Y ].X = []Y = [ [] ]

Page 11: L OGIC  P ROGRAMMING with  P ROLOG

[ 11 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Length and Member (revisited)

Length/2:

Member/2:

Usage:

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

len(0, []).len(succ(N), [ _ | L ]) :- len(N, L).

?- member(2, [1,2,3]).Yes?- member(X, [1,2,3]). // gimme elements from listX=1 ; // next...X=2 ;X=3 ;No

~ Haskell

Page 12: L OGIC  P ROGRAMMING with  P ROLOG

[ 12 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Append

Append/3:Search tree for:

append([], L, L).append([X|L1], L2, [X|L3]) :- append(L1,L2,L3).

append([a,b,c],[d,e,f],_G1)

append([b,c],[d,e,f],_G2)

_G1 = [a|_G2]

append([c],[d,e,f],_G3)

_G2 = [b|_G3]

append([],[d,e,f],_G4)

_G3 = [c|_G4]

axiom_G4 = [d,e,f]

?- append([a,b,c], [d,e,f], R) R = [a,b,c,d,e,f]

rule

rule

rule

append([a,b,c],[d,e,f],[a,b,c,d,e,f])

append([b,c],[d,e,f],[b,c,d,e,f])

append([c],[d,e,f],[c,d,e,f])

append([],[d,e,f],[d,e,f])

T

Page 13: L OGIC  P ROGRAMMING with  P ROLOG

[ 13 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Using Append

Prefix/2:

Suffix/2:

SubList/2:

...alternatively...:

prefix(P, L) :-

suffix(S, L) :- append(_, S, L).

subList(Lsub, L) :- suffix(S, L), prefix(Lsub, S).

subList(Lsub, L) :- prefix(P, L), suffix(Lsub, P).

append(P, _, L).

Page 14: L OGIC  P ROGRAMMING with  P ROLOG

[ 14 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Reverse (and efficiency issues)

Reverse/2: Idea; exploit property: “(x L)R = LR x”

Problem: [X|L] is asymmetrically left-to-right biased

we cannot put the list in front and write: “[ L | X ]”

Let's use append:

rev([], []).rev([X|L], R) :- rev(L, L_rev), [L_rev | X] = R.

Q: What about efficiency?1) …of append? ; 2) …of reverse?

rev([X|L], R) :- rev(L, L_rev), append(L_rev, [X], R).

Page 15: L OGIC  P ROGRAMMING with  P ROLOG

[ 15 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Efficiency(append):

Efficiency(reverse):

Efficiency

Q: Efficiency(reverse)...?

|arg1|+1

rev([], []).rev([H|T], R) :- rev(T,TR), app(TR,[H],R).

app([], L, L).app([X|L1], L2, [X|L3]) :- app(L1,L2,L3).

f O(g) def n,k>0: N>n => |f(N)| k·|g(N)|"f is dominated by g for large values (larger than n)"

Recall ("big-O definition"):

O(|arg1|)

Page 16: L OGIC  P ROGRAMMING with  P ROLOG

[ 16 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Search Tree: rev

rev([1, 2, 3], _G1)

rev rule

rev([2, 3], _G2), app(_G2, [1], _G1)

rev rule

rev([3], _G3), app(_G3, [2], _G2), app(_G2, [1], _G1)

rev rule

rev([], _G4), app(_G4, [3], _G3), app(_G3, [2], _G2), app(_G2, [1], _G1)

rev axiom

app([], [3], _G3), app(_G3, [2], _G2), app(_G2, [1], _G1)

(1 step of append)

_G4 = []

app([3], [2], _G2), app(_G2, [1], _G1)

app([3,2], [1], _G1)

_G3 = [3]

(2 steps of append)_G2 = [3,2]

(3' append)_G1 = [3,2,1]

1+2+3 steps (of append)

O(|L|2)

|L|+1 steps(of reverse)

O(|L|)

+

rev([], []).rev([H|T], R) :- rev(T,TR), app(TR,[H],R).

Page 17: L OGIC  P ROGRAMMING with  P ROLOG

[ 17 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Accumulator

Let's use an accumulator: accRev([H|T], A, R) :- accRev(T, [H|A], R).

accRev([], A, A).

rev(L, R) :- accRev(L, [], R).

rev([1,2,3], _G1)

rev rule

accRev([1,2,3], [], _G1)

accRev rule

accRev([2,3], [1], _G1)

accRev rule

accRev([3], [2,1], _G1)

accRev rule

accRev([], [3,2,1], _G1)

|L| steps(of accRev)

O(|L|)

+

1 step (of rev) oldRev vs. newRev:

O(n2)

O(n)

oldRev

newRev

steps

0 1 2 3 4 5 6 7 8 |L|

~ Haskell

T

Page 18: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

ARITHMETIC

Keywords:

Evaluation, ...

Page 19: L OGIC  P ROGRAMMING with  P ROLOG

[ 19 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Binary Infix Functors: {+,-,*,/}

Consider:

What is going on...?!?

The symbols {+,-,*,/} are just: (Binary) infix functors:

i.e., "2+2" is just a short-hand for "+(2,2)"; in fact:

?- 2+2 = 4No

?- 2*2 = 4No

?- 2-2 = 0No

?- 2/2 = 1No

?- 2+2 = +(2,2)Yes

~ Haskell

Page 20: L OGIC  P ROGRAMMING with  P ROLOG

[ 20 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Binary Infix Functors (cont'd)

The symbols {+,-,*,/} just (conveniently) represent structured information:

is understood as precedence: {*,/} stronger-than {+,-} associativity: {+,-,*,/} left-associative

...and is thus just a short-hand for: ...which is, structurally, no different than: However, their interpretation may be very different;

e.g., "represents" an expression that may be evaluated

1-2/3+4*5*6 (1-(2/3))+((4*5)*6)"standard"

precedence/associativity

rules

+(-(1,/(2,3)),*(*(4,5),6)

a(b(1,c(2,3)),d(d(4,5),6)

~ Haskell

Page 21: L OGIC  P ROGRAMMING with  P ROLOG

[ 21 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

The "is/2" Predicate

is/2 TERM TERM: Evaluates its right argument (as arithmetic expression)

...provided all variables are instantiated!

Example (in predicate definition):

?- X is 2+2X=4

?- 2+2 is X*** ERROR: is/2: uninstantiated argument

sq(X,Y) :- Y is X*X.

?- 4 is 2+2Yes

?- 2+2 is 4No

?- sq(5,Y).Y=25

?- sq(X,25).*** ERROR: is/2: uninstantiated argument

Page 22: L OGIC  P ROGRAMMING with  P ROLOG

[ 22 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Careful w/ Arithmetic Evaluation

Recall "len/2": Arithmetic version(s):

len1([], 0).len1([_|L], N_succ) :- len1(L, N), N is N_succ-1.

len([], 0).len([_|L], succ(N)) :- len(L, N).

len2([], 0).len2([_|L], N) :- len2(L, N_pred), N is N_pred+1.

?- len1([1,2,3], R).*** ERROR: is/2: uninstantiated argument

len3([], 0).len3([_|L], N) :- N is N_pred+1, len3(L, N_pred).

?- len2([1,2,3], R).R=3

-? len3([1,2,3], R).*** ERROR: is/2: uninstantiated argument

...with unary encoding of numerals:

T

Page 23: L OGIC  P ROGRAMMING with  P ROLOG

[ 23 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Accumulators (revisited)

"len/2": Version with

accumulator:

accLen([], A, A).accLen([_|T], A, N) :- Anew is A+1, accLen(T, Anew, N)

len(List, Length) :- accLen(List, 0, Length).

len([], 0).len([_|T], N) :- len(T, X), N is X+1.

len([1,2,3], _G1)

len rule

len([2,3], _G2), _G1 is _G2+1

len rule

len([3], _G3), _G2 is _G3+1, _G1 is _G2+1

len rule

len([], _G4), _G3 is _G4+1, _G2 is _G3+1, _G1 is _G2+1

accLen([1,2,3], 0, _G1)

accLen rule; then "is"

accLen([2,3], 1, _G1)

accLen([3], 2, _G1)

accLen([], 3, _G1)

len axiom

_G3 is 0+1, _G2 is _G3+1, _G1 is _G2+1is is is

accLen rule; then "is"

accLen rule; then "is"O(n) wide!

~ Haskell

accLen axiom

Tail recursive!"calculation during recursion"

However; NOT tail recursive:"calculation after recursion"

Same #steps (both 7x)…

Page 24: L OGIC  P ROGRAMMING with  P ROLOG

[ 24 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Comparison Operators

More integer comparison operators (with arithmetic evaluation side-effects):

"<" "less than" "<=" "less than or equal to" ">" "greater than" ">=" "greater than or equal to" "=:=" "equal to" "=\=" "not equal to"

Evaluate both arguments Again, all variables have to be instantiated

Otherwise no surprises...

Page 25: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

Exercise 1, 2, and 3:

11:15 – 12:00

Page 26: L OGIC  P ROGRAMMING with  P ROLOG

[ 26 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

1. ...

Purpose: Learn how to ...

Page 27: L OGIC  P ROGRAMMING with  P ROLOG

[ 27 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

2. ...

Purpose: Learn how to ...

Page 28: L OGIC  P ROGRAMMING with  P ROLOG

[ 28 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

3. ...

Purpose: Learn how to ...

Page 29: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

REVERSIBILITY

Page 30: L OGIC  P ROGRAMMING with  P ROLOG

[ 30 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Ex: Symbolic Differentiation

Symbolic differentiation: d dx (k) = 0

d dx (x) = 1

d dx (f+g) = d

dx (f) + d dx (g)

d dx (f-g) = d

dx (f) - d dx (g)

d dx (f*g) = f d

dx (g) + g d dx(f)

d dx (f/g) =

d dx (f) + f d

dx(g)g

g * g

d dx (xn) = n * xn-1

d dx (ex) = ex

d dx (ln(x)) =

1x

d dx (sin(x)) = cos(x)

d dx (cos(x)) = -sin(x)

d dx (gof) = (g) o

d dx f * ( ) d

dx(f)

Page 31: L OGIC  P ROGRAMMING with  P ROLOG

[ 31 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

...in PROLOG

In PROLOG: dX(K, 0) :- number(K). // constant

dX(x, 1). // variable

dX(F+G, Df+Dg) :- dX(F,Df), dX(G,Dg). // add

dX(F-G, Df-Dg) :- dX(F,Df), dX(G,Dg). // sub

dX(F*G, Df*G+F*Dg) :- dX(F, Df), dX(G, Dg). // mul

dX(F/G, (Df*G-F*Dg)/(G*G)) :- dX(F, Df), dX(G, Dg). // div

[...]

dX(cos(x), 0-sin(x)). // cos

dX(F;G, (F;Dg)*Df) :- dX(F,Df), dX(G,Dg). // compose

ex

Page 32: L OGIC  P ROGRAMMING with  P ROLOG

[ 32 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Differentiation

Interaction:

Reverse:

Does this mean we can do integration?

No, just certain functions in "anti - normal form" (ANF);

i.e., functions that are in the image of the differentiation

?- dX(x/exp(x), Df).Df = (1*exp(x)-x*exp(x)) / (exp(x)*exp(x))

?- dX(F,(1*exp(x)-x*exp(x)) / (exp(x)*exp(x))).F = x/exp(x)

ANF

f'

f

Page 33: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

CUT AND NEGATION

Keywords (chapter 10):

Side-effect, Backtracking, Cut, Fail, Cut-Fail, Negation, ...

Page 34: L OGIC  P ROGRAMMING with  P ROLOG

[ 34 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

The Cut Operator: '!'

Consider max/3:

Cut, "!", (locally) disables backtracking Cut version:

Note: this cut changes only efficiency properties

= "Green Cut"

max(X,Y,Y) :- X =< Y.max(X,Y,X) :- X > Y.

?- max(3,4,M).M = 4?- ; // backtracking now causes futile re-evaluation of max

Note: mutually exclusive conditions

max(X,Y,Y) :- X =< Y , ! . // commit (throw away max-backtracking)max(X,Y,X) :- X > Y.

Page 35: L OGIC  P ROGRAMMING with  P ROLOG

[ 35 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

"Green Cuts" vs. "Red Cuts"

"Green cut" version:

Alternative "Red cut" version (= relying on cut):

Seems okay...:

...but:

max(X,Y,Y) :- X =< Y , ! .max(X,Y,X) :- X > Y.

max(X,Y,Y) :- X =< Y , ! .max(X,Y,X). // only succeeds if above fails (...or?)

?- max(99,100,X).X = 100 // ok!?- max(100,99,X).X = 100 // ok!

?- max(1,100,1).Yes // Oops! (evaluation never made it to the cut)

Advice:"cut down on cut"

Page 36: L OGIC  P ROGRAMMING with  P ROLOG

[ 36 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Fail and exception predicating

Consider:

...but maybe Vincent likes all burgers, except "Big Kahuna burgers".

PROLOG features a built-in "fail/0"-predicate: Syntax: Semantics: "always fails (and forces backtracking)"

enjoys(vincent, X) :- burger(X).

fail

enjoys(vincent, X) :- big_kahuna_burger(X), !, failenjoys(vincent, X) :- burger(X).

big_mac(b0).

?- enjoys(vincent, b0).Yes

?- enjoys(vincent, b1).No

big_kahuna_burger(b1).

the rule relies(operationally)on the cut = red cut

Page 37: L OGIC  P ROGRAMMING with  P ROLOG

[ 37 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

The Cut-Fail Combination

The "cut-fail combination"...

...expresses negation ...and is so common that it is built-in:

not/1; equivalent to:

It's better to use "not" it's a higher level abstraction (than cut-fail); However...:

enjoys(vincent, X) :- big_kahuna_burger(X), !, failenjoys(vincent, X) :- burger(X).

not(Goal) :- Goal, !, fail.not(Goal).

Inf. Sys. vs. PROLOG

Cut has operationally (well-)defined semanticsIsn't always

"well-defined":|_ P(x)

|_ P(x)

whichrelation

?! p(x) :- not(p(x)).

Page 38: L OGIC  P ROGRAMMING with  P ROLOG

[ 38 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

If-then-else: "( A -> B ; C )"

PROLOG has an if-then-else construction: Syntax:

( A -> B ; C ) Semantics:

"if A; then B, else C"

Alternative version of max/3: ...using if-then-else:

max(X,Y,Z) :- ( X =< Y -> Z = Y ; Z = X ).

Page 39: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

LANGUAGE INTERPRETATION

Keywords:

Interpretation, Evaluation, Syntax, Semantics, ...

Page 40: L OGIC  P ROGRAMMING with  P ROLOG

[ 40 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Expressions (and syntax vs. semantics)

Expressions: Syntax:

Semantics (via evaluation relation: " |-eval Exp N "):

Exp : N [const] : +(Exp,Exp) [add] : *(Exp,Exp) [mul]

|_eval N N

|_eval E1 N1 |_

eval E2 N2

|_eval +(E1,E2) NN = N1 N2

|_eval E1 N1 |_

eval E2 N2

|_eval *(E1,E2) NN = N1 N2

[const]

[add]

[mul]

here in prefix notationjust to emphasize differencebetween syntax and semantics

syntactic "+" semantic

multiple levels of abstraction...!

Page 41: L OGIC  P ROGRAMMING with  P ROLOG

[ 41 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Alice in Wonderland

Different levels of abstraction: Things ‘A Sitting on a Gate’ Names of things ‘The Aged Aged Man.’ Things are called something ‘Ways and Means’ Names are called something ‘Haddocks’ Eyes.’

K> "[...] The name of the song is called ‘Haddocks’ Eyes.’”

A> “Oh, that’s the name of the song, is it?” Alice said, trying to feel interested.K> “No, you don’t understand,” the Knight said, looking a little vexed. K> “That’s what the name is called. The name really is ‘The Aged Aged Man.’”

A> “Then I ought to have said, ‘That’s what the song is called’?” Alice corrected herself.

K> “No, you oughtn’t: that’s another thing. The song is called ‘Ways and Means’: K> but that’s only what it’s called, you know!”

A> “Well, what is the song, then?” said Alice, who was by this time completely bewildered.

K> “I was coming to that,” the Knight said. “The song really is ‘A Sitting on a Gate’: K> and the tune’s my own invention.”

Alice and Knight talking

Page 42: L OGIC  P ROGRAMMING with  P ROLOG

[ 42 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

[...back to]: (syntax vs. semantics)

Expressions: Syntax:

Semantics (via evaluation relation: " |-eval Exp N "):

Exp : N [const] : +(Exp,Exp) [add] : *(Exp,Exp) [mul]

|_eval N N

|_eval E1 N1 |_

eval E2 N2

|_eval +(E1,E2) NN = N1 N2

|_eval E1 N1 |_

eval E2 N2

|_eval *(E1,E2) NN = N1 N2

[const]

[add]

[mul]

here in prefix notationjust to emphasize differencebetween syntax and semantics

syntactic "+" semantic

Page 43: L OGIC  P ROGRAMMING with  P ROLOG

[ 43 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Expressions (in PROLOG)

Syntax:

Semantics:

exp(con(N)) :- number(N).exp(add(E1,E2)) :- exp(E1), exp(E2).exp(mul(E1,E2)) :- exp(E1), exp(E2).

eval(con(N), N).

eval(add(E1,E2),N) :- eval(E1,N1), eval(E2,N2), N is N1 + N2.

eval(mul(E1,E2),N) :- eval(E1,N1), eval(E2,N2), N is N1 * N2.

?- eval(mul(add(con(2),con(4)),con(7)),X).X = 42

?- exp(mul(add(con(2),con(4)),con(7))).Yes

eval(N, N) :- number(N).

eval(E1+E2,N) :- eval(E1,N1), eval(E2,N2), N is N1 + N2.

eval(E1*E2,N) :- eval(E1,N1), eval(E2,N2), N is N1 * N2.

?- eval((2+4)*7,X).X = 42

binary infix syntax binary infix syntax

Page 44: L OGIC  P ROGRAMMING with  P ROLOG

[ 44 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

The "WHILE" Language

The "WHILE" Language: Syntax:

Semantics: Similar techniques (albeit somewhat more complicated)...

Exp : N // const : X // var : Exp Exp , {+,-,*,/} // binop

Com : skip // skip : X := Exp // assign : if ( Exp ) then Com else Com // if : while ( Exp ) do Com // while : Com ; Com // sequence

Page 45: L OGIC  P ROGRAMMING with  P ROLOG

[ 45 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

The Lambda Calculus

The Lambda Calculus Syntax:

Semantics (call-by-value): : VAR Z

Note: this is avariant of the semanticsyou saw earlier

You only have to understandhere that The Lambda Calculus can be encoded in PROLOG

Page 46: L OGIC  P ROGRAMMING with  P ROLOG

[ 46 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

The Lambda Calculus (in PROLOG)

Syntax: << Exercise 4 >>

Semantics: Similar techniques (as with the expression language)

?- eval(apply(lambda(x,variable(x)), lambda(y,variable(y)), Res).Res = lambda(y,variable(y))

Page 47: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

NON-TERMINATION

Keywords:

Turing-Completeness, Reduction, Self-referentiality, Undecidability, ...

Page 48: L OGIC  P ROGRAMMING with  P ROLOG

[ 48 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Prolog

A French programming language (from 1971): "Programmation en Logique" (="programming in logic")

A declarative, relational style of programming based on first-order logic:

Originally intended for natural-language processing, but has been used for many different purposes (esp. for programming artificial intelligence).

The programmer writes a "database" of "facts" and "rules";

e.g.:

The user then supplies a "goal" which the system attempts to prove (using resolution and backtracking); e.g., witch(girl).

%- FACTS ----------female(girl).floats(duck).sameweight(girl,duck).

%- RULES ----------witch(X) :- burns(X) , female(X).burns(X) :- wooden(X).wooden(X) :- floats(X).floats(X) :- sameweight(X,Y) , floats(Y).

Page 49: L OGIC  P ROGRAMMING with  P ROLOG

[ 49 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Undecidability

Consider "The Book-of-all-Books": This book contains the titles of all books

that do not have a self-reference (i.e. don't contain their title inside)

Finitely many books; i.e.: We can sit down & figure out whether to include or not...

Q: What about "The Book-of-all-Books"; Should it be included or not?

"Self-referential paradox" (many guises): e.g. "This sentence is false"

"The Bible"

"War and Peace"

"Programming Languages,

An Interp.-Based Approach"

...

The Book-of-all-Books

Page 50: L OGIC  P ROGRAMMING with  P ROLOG

[ 50 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Undecidability (of Termination)

Assume termination is decidable (in Java); i.e. some program, halts: PROG BOOL

Q: Does P0 loop or terminate...? :)

Hence: "Termination is undecidable" ...for WHILE, Java, C, Lambda Calculus, Prolog, ...

prog p0 = read_program("P0.prg");if (halts(p0) == "halts") loop();else halt();

-- P0.prg --

bool halts(prog p) { ... }

Page 51: L OGIC  P ROGRAMMING with  P ROLOG

[ 51 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Prolog

A French programming language (from 1971): "Programmation en Logique" (="programming in logic")

A declarative, relational style of programming based on first-order logic:

Originally intended for natural-language processing, but has been used for many different purposes (esp. for programming artificial intelligence).

The programmer writes a "database" of "facts" and "rules";

e.g.:

The user then supplies a "goal" which the system attempts to prove (using resolution and backtracking); e.g., witch(girl).

%- FACTS ----------female(girl).floats(duck).sameweight(girl,duck).

%- RULES ----------witch(X) :- burns(X) , female(X).burns(X) :- wooden(X).wooden(X) :- floats(X).floats(X) :- sameweight(X,Y) , floats(Y).

Page 52: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

Exercise 4, 5, and (6):

14:15 – 16:15

Page 53: L OGIC  P ROGRAMMING with  P ROLOG

[ 53 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

4. ...

Purpose: Learn how to ...

Page 54: L OGIC  P ROGRAMMING with  P ROLOG

[ 54 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

5. ...

Page 55: L OGIC  P ROGRAMMING with  P ROLOG

[ 55 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

(6). ...

Purpose: Learn how to ...

Page 56: L OGIC  P ROGRAMMING with  P ROLOG

CLAUS BRABRAND OCT 21, 2009PROGRAMMING PARADIGMS

The End

Questions?

Good luck at the exam (!)

Page 57: L OGIC  P ROGRAMMING with  P ROLOG

[ 57 ]CLAUS BRABRAND

?-

PROGRAMMING PARADIGMS OCT 21, 2009

Terminology

Predicates vs. Structured data:

Arities and Signatures: "Arity" #arguments of a function/relation:

(unary, binary, ternary, ..., N-ary, ...)

"Signature" type of a function/relation:(e.g.: "+fun": Z Z

Z ; "=rel" Z Z )

vertical(line(point(X,Y),point(X,Z)).

Predicate/relation(being defined)

structured data

odd(succ(0)).

predicate(being defined)

structured data