# CSI 3120, Prolog recap, page 1 Prolog recap Prolog was invented in 1970, first implemented in 1972, first implemented efficiently in 1975. Excellent commercial-

## Presentation on theme: "CSI 3120, Prolog recap, page 1 Prolog recap Prolog was invented in 1970, first implemented in 1972, first implemented efficiently in 1975. Excellent commercial-"— Presentation transcript:

CSI 3120, Prolog recap, page 1 Prolog recap Prolog was invented in 1970, first implemented in 1972, first implemented efficiently in 1975. Excellent commercial- strength implementations have existed since early 1980s. In over 35 years, Prolog has been unmatched as an unusually powerful language for a fairly wide range of applications (including circuit design, language processing, software prototyping and runnable specifications) but it is amazingly underappreciated. People do not even know that Prolog is faster than Java when applied in the right way, and that software production can be an order of magnitude faster in this very high-level programming language.

CSI 3120, Prolog recap, page 2 An informative example (a)A prime number N is a number whose set of divisors contains only 1 and N. (b)The set of divisors of a number N is the set of numbers in the range 1.. N which divide N exactly. % (a) prime( N ) :- divisorSet( N, [1, N] ). % (b) divisorSet( N, DivSet ) :- setof( K, ( inRange( K, 1, N ), N mod K =:= 0 ), DivSet ). % (b') two cases are possible inRange( K, K, High ) :- K =< High. inRange( K, Low, High ) :- Low < High, Low1 is Low + 1, inRange( K, Low1, High ). Prolog has no conditional statements and no iterations. The only operations allowed in the body of a procedure are other procedure calls—and this is quite sufficient.

CSI 3120, Prolog recap, page 3 Two interpretations ancestor( X, Y ) :- father( X, Y ). ancestor( X, Y ) :- father( X, Z ), ancestor( Z, Y ). Your father is your ancestor, and the father of your ancestor is your ancestor, too. This is an example of the declarative (static, logical) interpretation of Prolog definitions. Queries, however, are used to find answers, not only to confirm truths recorded in the Prolog database. Finding is necessarily dynamic, so we need another interpretation.

CSI 3120, Prolog recap, page 4 Two interpretations The procedural (imperative, control) interpretation of Prolog facts and rules focuses on the process of finding answers: To find an ancestor of Y, find his father; or else, take his father (call him Z) and find Z's ancestor. This is the description of a procedure with two variants, one of which is chosen for execution. The procedure's name is ancestor, and it has two parameters. The body of the second variant consists of two procedure calls: first, father with 2 parameters; next, ancestor with 2 parameters.

CSI 3120, Prolog recap, page 5 Compound objects Compound objects are ordered collections of simpler objects that are in some relationship. Examples: two sides of a rectangle, such as 19 by 24, the time in hours and minutes, such as 19:24. The pair (19, 24) can represent a rectangle or a time, so we name the relationship: rectangle( 19, 24 ) timeOfDay( 19, 24 ) We can also represent the time with seconds: timeOfDay( 19, 24, 37 ) This is a different object, but Prolog manages OK. The name is the same, but the arity is not: timeOfDay/2 (two components), timeOfDay/3 (three components).

CSI 3120, Prolog recap, page 6 Compound objects Example: customer( name( jim, white ), address( street( 17, main ), city( bytown, ontario ) ) ) A compound object is incompletely specified if it contains variables – unknown components. An example: customer( X, address( street( 17, main ), city( bytown, ontario ) ) ) "Any customer who lives at 17 Main, Bytown, Ontario.” customer( name( X, white ), address( street( Y, main ), city( Z, ontario ) ) ) "Any customer by the name of White who lives at Main, any town, Ontario.” A variable may appear more than once. For example, rectangle( X, X ) represents a square.

CSI 3120, Prolog recap, page 7 Lists Lists are processed recursively. length( [], 0 ). length( [ _H | T ], Len ) :- length( T, Len1 ), Len is Len1 + 1. nth( 1, [Hd | _Tl], Hd ). nth( N, [_ | Tl], NthElem ) :- N > 1, N1 is N - 1, nth( N1, Tl, NthElem ). member( Hd, [Hd | _Tl] ). member( Elem, [ _Hd | Tl ] ) :- member( Elem, Tl ).

CSI 3120, Prolog recap, page 8 Lists List processing may be non-deterministic, and procedures may have many uses. append( [], L2, L2 ). append( [E | L1], L2, [E | L3] ) :- append( L1, L2, L3 ). The query tells us which of several operations it is. ?- append( [a], [b, c], LL ). ?- append( [a], X, [a, b, c] ). ?- append( Y, [c], [a, b, c] ). ?- append( F, S, [a, b, c] ). ?- append( F, S, FS ). ?- append([X, 55], [Y], [77, Z, 20]).

CSI 3120, Prolog recap, page 9 Prolog at its most elegant intersect( List1, List2 ) :- member( X, List1 ), member( X, List2 ). ?- intersect( [a, c, e, g], [b, c, d] ). true ?- intersect( [a, c, e, g], [b, d, f] ). fail Two lists intersect if they have a common element.

CSI 3120, Prolog recap, page 10 Last words Much more: Advanced control, including the cut. Customizable term syntax. Logic grammars. Dynamic (on-the-fly) modification of Prolog code. Programming with trees and graphs. Debugging tools. Programming in the large (modules). Graphics and user interface programming. Object-oriented extensions of Prolog. Where is Prolog is a language of choice? Rapid software prototyping. Deductive databases. Language design and development. Constraint programming. Artificial Intelligence: Games, Planning, Machine Learning, Natural Language Processing.

Download ppt "CSI 3120, Prolog recap, page 1 Prolog recap Prolog was invented in 1970, first implemented in 1972, first implemented efficiently in 1975. Excellent commercial-"

Similar presentations