Download presentation

Presentation is loading. Please wait.

Published byElwin Ward Modified over 2 years ago

1
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS L OGIC P ROGRAMMING with P ROLOG Claus Brabrand brabrand@itu.dk IT University of Copenhagen [ http://www.itu.dk/people/brabrand/ ] "Programming Paradigms", Dept. of Computer Science, Aalborg Uni. (Fall 2009)

2
[ 2 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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 )

3
[ 3 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Outline Part 1: Lists Arithmetic Part 2: Reversibility Cut and Negation Language Interpretation Part 3: Non-termination Undecidability

4
[ 4 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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) P ROLOG 's Search Order

5
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS L ISTS Keywords: (Encoded) lists, (built-in) lists, efficiency issues,...

6
[ 6 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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

7
[ 7 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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). P ROLOG 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

8
[ 8 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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

9
[ 9 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 The Head-Tail Constructor P ROLOG (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 ] ~Haskell h:t ?- [ a | [b,c] ] = L. L = [a,b,c] ?- [a,b,c] = [ H | T ]. H = a T = [b,c]

10
[ 10 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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 = [ [] ]

11
[ 11 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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 list X=1 ; // next... X=2 ; X=3 ; No ~ Haskell

12
[ 12 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Append Append/3 : Search tree for: append([], L, L). append([X|L 1 ], L 2, [X|L 3 ]) :- append(L 1,L 2,L 3 ). 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 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

13
[ 13 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Using Append Prefix/2 : Suffix/2 : SubList/2 :...alternatively...: prefix(P, L) :- suffix(S, L) :- append(_, S, L). subList(L sub, L) :- suffix(S, L), prefix(L sub, S). subList(L sub, L) :- prefix(P, L), suffix(L sub, P). append(P, _, L).

14
[ 14 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Reverse (and efficiency issues) Reverse/2 : Idea; exploit property: “ (x L) R = L R 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).

15
[ 15 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Efficiency( append ): Efficiency( reverse ): Efficiency Q: Efficiency(reverse)...? |arg 1 |+1 rev([], []). rev([H|T], R) :- rev(T,T R ), app(T R,[H],R). app([], L, L). app([X|L 1 ], L 2, [X|L 3 ]) :- app(L 1,L 2,L 3 ). 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(|arg 1 |)

16
[ 16 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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,T R ), app(T R,[H],R).

17
[ 17 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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(n 2 ) O(n) oldRev newRev steps 0 1 2 3 4 5 6 7 8 |L| ~ Haskell T

18
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS A RITHMETIC Keywords: Evaluation,...

19
[ 19 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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 = 4 No ?- 2*2 = 4 No ?- 2-2 = 0 No ?- 2/2 = 1 No ?- 2+2 = +(2,2) Yes ~ Haskell

20
[ 20 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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*61-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

21
[ 21 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 The " is/2 " Predicate is/2 T ERM T ERM: Evaluates its right argument (as arithmetic expression)...provided all variables are instantiated! Example (in predicate definition): ?- X is 2+2 X=4 ?- 2+2 is X *** ERROR: is/2: uninstantiated argument sq(X,Y) :- Y is X*X. ?- 4 is 2+2 Yes ?- 2+2 is 4 No ?- sq(5,Y). Y=25 ?- sq(X,25). *** ERROR: is/2: uninstantiated argument

22
[ 22 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Careful w/ Arithmetic Evaluation Recall " len/2 ": Arithmetic version(s): len 1 ([], 0). len 1 ([_|L], N_succ) :- len 1 (L, N), N is N_succ-1. len([], 0). len([_|L], succ(N)) :- len(L, N). len 2 ([], 0). len 2 ([_|L], N) :- len 2 (L, N_pred), N is N_pred+1. ?- len 1 ([1,2,3], R). *** ERROR: is/2: uninstantiated argument len 3 ([], 0). len 3 ([_|L], N) :- N is N_pred+1, len 3 (L, N_pred). ?- len 2 ([1,2,3], R). R=3 -? len 3 ([1,2,3], R). *** ERROR: is/2: uninstantiated argument...with unary encoding of numerals: T

23
[ 23 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Accumulators (revisited) " len/2 ": Version with accumulator: accLen([], A, A). accLen([_|T], A, N) :- A new is A+1, accLen(T, A new, 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+1 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)…

24
[ 24 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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...

25
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS Exercise 1, 2, and 3: 11:15 – 12:00

26
[ 26 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 1.... Purpose: Learn how to...

27
[ 27 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 2.... Purpose: Learn how to...

28
[ 28 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 3.... Purpose: Learn how to...

29
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS R EVERSIBILITY

30
[ 30 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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 (x n ) = n * x n-1 d dx (e x ) = e x d dx (ln(x)) = 1x1x d dx (sin(x)) = cos(x) d dx (cos(x)) = -sin(x) d dx (g o f) = (g) o d dx f * () d dx (f)

31
[ 31 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009...in P ROLOG In P ROLOG : 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 e x

32
[ 32 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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

33
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS C UT AND N EGATION Keywords (chapter 10): Side-effect, Backtracking, Cut, Fail, Cut-Fail, Negation,...

34
[ 34 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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.

35
[ 35 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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"

36
[ 36 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Fail and exception predicating Consider:...but maybe Vincent likes all burgers, except "Big Kahuna burgers". P ROLOG 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), !, fail enjoys(vincent, X) :- burger(X). big_mac(b 0 ). ?- enjoys(vincent, b 0 ). Yes ?- enjoys(vincent, b 1 ). No big_kahuna_burger(b 1 ). the rule relies (operationally) on the cut = red cut

37
[ 37 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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), !, fail enjoys(vincent, X) :- burger(X). not(Goal) :- Goal, !, fail. not(Goal). Inf. Sys. vs. P ROLOG Cut has operationally (well-)defined semantics Isn't always "well-defined": | _ P(x) which relation ?! p(x) :- not(p(x)).

38
[ 38 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 If-then-else: " ( A -> B ; C ) " P ROLOG 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 = Z = Y ; Z = X ).

39
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS L ANGUAGE I NTERPRETATION Keywords: Interpretation, Evaluation, Syntax, Semantics,...

40
[ 40 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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 E 1 N 1 | _ eval E 2 N 2 | _ eval +(E 1,E 2 ) N N = N 1 N 2 | _ eval E 1 N 1 | _ eval E 2 N 2 | _ eval *(E 1,E 2 ) N N = N 1 N 2 [const] [add] [mul] here in prefix notation just to emphasize difference between syntax and semantics syntactic "+" semantic multiple levels of abstraction...!

41
[ 41 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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

42
[ 42 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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 E 1 N 1 | _ eval E 2 N 2 | _ eval +(E 1,E 2 ) N N = N 1 N 2 | _ eval E 1 N 1 | _ eval E 2 N 2 | _ eval *(E 1,E 2 ) N N = N 1 N 2 [const] [add] [mul] here in prefix notation just to emphasize difference between syntax and semantics syntactic "+" semantic

43
[ 43 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Expressions (in P ROLOG ) Syntax: Semantics: exp(con(N)) :- number(N). exp(add(E 1,E 2 )) :- exp(E 1 ), exp(E 2 ). exp(mul(E 1,E 2 )) :- exp(E 1 ), exp(E 2 ). eval(con(N), N). eval(add(E 1,E 2 ),N) :- eval(E 1,N 1 ), eval(E 2,N 2 ), N is N 1 + N 2. eval(mul(E 1,E 2 ),N) :- eval(E 1,N 1 ), eval(E 2,N 2 ), N is N 1 * N 2. ?- 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(E 1 +E 2,N) :- eval(E 1,N 1 ), eval(E 2,N 2 ), N is N 1 + N 2. eval(E 1 *E 2,N) :- eval(E 1,N 1 ), eval(E 2,N 2 ), N is N 1 * N 2. ?- eval((2+4)*7,X). X = 42 binary infix syntax

44
[ 44 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 The "W HILE " Language The "W HILE " 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

45
[ 45 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 The Lambda Calculus Syntax: Semantics (call-by-value): : V AR Z Note: this is a variant of the semantics you saw earlier You only have to understand here that The Lambda Calculus can be encoded in P ROLOG

46
[ 46 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 The Lambda Calculus (in P ROLOG ) Syntax: > Semantics: Similar techniques (as with the expression language) ?- eval(apply(lambda(x,variable(x)), lambda(y,variable(y)), Res). Res = lambda(y,variable(y))

47
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS N ON -T ERMINATION Keywords: Turing-Completeness, Reduction, Self-referentiality, Undecidability,...

48
[ 48 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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).

49
[ 49 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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 P e ac e" "Programm i n g La nguages, An Interp.- B a s e d Approach"... The Book-of-all-Books

50
[ 50 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 Undecidability (of Termination) Assume termination is decidable (in Java); i.e. some program, halts: PROG BOOL Q: Does P 0 loop or terminate...?:) Hence: "Termination is undecidable"...for W HILE, Java, C, Lambda Calculus, Prolog,... prog p0 = read_program("P 0.prg"); if (halts(p0) == "halts") loop(); else halt(); -- P 0.prg -- bool halts(prog p) {... }

51
[ 51 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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).

52
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS Exercise 4, 5, and (6): 14:15 – 16:15

53
[ 53 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 4.... Purpose: Learn how to...

54
[ 54 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 5....

55
[ 55 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 21, 2009 (6).... Purpose: Learn how to...

56
C LAUS B RABRAND O CT 21, 2009 P ROGRAMMING P ARADIGMS The End Questions? Good luck at the exam (!)

57
[ 57 ] C LAUS B RABRAND ?- P ROGRAMMING P ARADIGMS O CT 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

Similar presentations

OK

C LAUS B RABRAND Inference Systems J AN 27, 2009 I NFERENCE S YSTEMS Claus Brabrand IT University of Copenhagen [

C LAUS B RABRAND Inference Systems J AN 27, 2009 I NFERENCE S YSTEMS Claus Brabrand IT University of Copenhagen [

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Glanz ppt on action research Ppt on cloud computing security from single to multi-clouds Ppt on personality test Ppt on pf and esi in india Ppt on hydroelectric plants in india Ppt on right to information act 2005 Slides for ppt on robotics Ppt on mindset change Ppt on sea level rise florida Ppt on happy teachers day