1 / 18

Prolog’s Lists, Negation and Imperative Control Flow (Section 11.3)

Prolog’s Lists, Negation and Imperative Control Flow (Section 11.3). CSCI 431 Programming Languages Fall 2003. A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill. Head. Tail. Lists. Constructors [] Empty list constant . Constructor functor Example

steven-goff
Download Presentation

Prolog’s Lists, Negation and Imperative Control Flow (Section 11.3)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prolog’s Lists, Negation and Imperative Control Flow(Section 11.3) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill

  2. Head Tail Lists • Constructors • [] Empty list constant • . Constructor functor • Example • .(a, .(b, .(c, []))) • [a, b, c] (syntactic sugar) • Tail notation: • [a | [b, c]] • [a, b | [c]]

  3. ListsExamples No notion of input or output parameters

  4. Tic-Tac-Toe Example • 3x3 grid • Two Players: • X (computer) • O (human) • Fact x(n)indicates a movement by X • E.g.x(5), x(9) • Fact o(n) indicates a movement by O • E.g.o(1), o(6)

  5. Tic-Tac-Toe Example • Winning condition

  6. Strategy: good moves Ordered List of Choices Tic-Tac-Toe Example     

  7. Winning Split X Tic-Tac-Toe Example      

  8. If this rule succeeded, do not try to use the following ones Imperative Control FlowThe cut • Prolog has a number of explicit control flow features • ! Known as the cut • This is a zero-argument predicate that always succeeds • It commits the interpreter to the unification made between the parent goal and the left-hand side of the current rules • Example member(X, [X|T]). member(X, [H|T]) :- member(X, T). member(X, [X|T]) :- !. member(X, [H|T]) :- member(X, T). member may succeed n times member may succeed at most one time

  9. Imperative Control Flow • Alternative member(X, [X|T]). member(X, [H|T]) :- not(X=H), member(X, T). • How does not work? not(P) :- call(P), !, fail. not(P). • call attempts to satisfy the goal P. • fail always fails.

  10. Prolog Database Manipulation • Two built-in predicates can be used to modify the database of known facts • assert(P) adds a new fact. • E.g.assert(parent(kevin, john)) • retract(P) removes a known fact. • E.g.retract(parent(kevin, john))

  11. Logic Puzzles An Example (courtesy of Axel Schreiner): 1. There are 3 boys and 3 girls. 2. One girl is dressed in red, one in green, one in blue. 3. One boy is dressed in red, one in green, one in blue. 4. The boy in red danced with the girl in green. 5. No boy danced with a girl who was dressed in the same color. 6. Which boy danced with the girl dressed in red?

  12. A Manual Solution(courtesy of Axel Schreiner) A constraint applies to property values and is entered into the appropriate matrix. An assertion such as (4) is entered as a dot relating two values for two properties:

  13. Manual Solution Exclusions such as (5) are entered as crosses into the matrix. A cell may only contain a dot or a cross, not both: Each row and column can only contain a single dot because the relationship between values is one-to-one. This can lead to further exclusions:

  14. Manual Solution The process becomes more complicated once there are more properties and therefore more matrices; each property must be related to every other in a separate matrix. Eventually the solution can be read from the matrices: The boy in blue danced with the girl in red. It can also lead to further dots if only a single cell remains unfilled in a row or column:

  15. Prolog Solution(courtesy of Axel Schreiner) /* properties and values */ boy(red). boy(green). boy(blue). girl(red). girl(green). girl(blue). /* constraints */ a(red, green). c(red, X) :- X \= red. d(green, X) :- X \= green. e(blue, X) :- X \= blue.

  16. Prolog Solution solve(Boy1,Girl1, Boy2,Girl2, Boy3,Girl3) :- /* freeze first property */ Boy1 = red, Boy2 = green, Boy3 = blue, /* make values of other properties distinct */ girl(Girl1), girl(Girl2), girl(Girl3), Girl1 \= Girl2, Girl1 \= Girl3, Girl2 \= Girl3, /* assert constraints */ ( a(Boy1, Girl1); a(Boy2, Girl2); a(Boy3, Girl3) ), ( c(Boy1, Girl1); c(Boy2, Girl2); c(Boy3, Girl3) ), ( d(Boy1, Girl1); d(Boy2, Girl2); d(Boy3, Girl3) ), ( e(Boy1, Girl1); e(Boy2, Girl2); e(Boy3, Girl3) ).

  17. Prolog Solution main :- solve(Boy1,Girl1, Boy2,Girl2, Boy3,Girl3), /* print a solution */ write(Boy1), write('\t'), write(Girl1), write('\n'), write(Boy2), write('\t'), write(Girl2), write('\n'), write(Boy3), write('\t'), write(Girl3), write('\n'), /* backtrack for all solutions */ fail.

  18. Prolog Interpreter ?- ['c:/classes/431/boy.pl']. % c:/classes/431/boy.pl compiled 0.00 sec, 3,032 bytes Yes ?- main. red green green blue blue red No ?-

More Related