# 1 Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2003 The University of North Carolina at Chapel Hill.

## Presentation on theme: "1 Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2003 The University of North Carolina at Chapel Hill."— Presentation transcript:

1 Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2003 The University of North Carolina at Chapel Hill Stotts, Hernandez-Campos Modified by Charles Ling for CS2209b, UWO Use with Permission

2 Lists ConstructorsConstructors –[] Empty list constant –. Constructor functor ExampleExample –.(a,.(b,.(c, []))) –[a, b, c] (syntactic sugar) Tail notation:Tail notation: –[a | [b, c]] –[a, b | [c]] Head::a Tail::[a]

3 Lists Examples No notion of input or output parameters

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

5 Tic-Tac-Toe Example Winning conditionWinning condition

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

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

8 Imperative Control Flow The cut Prolog has a number of explicit control flow featuresProlog has a number of explicit control flow features ! Known as the cut! 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 ExampleExample 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 If this rule succeeded, do not try to use the following ones

9 Imperative Control Flow Cut causes the unification stack to be frozen…Cut causes the unification stack to be frozen…

10 Cut and the stack… Cut and the stack… (a) P: rochester (c) Y: tandoori (b) Z: blue K: 147 New stack base for backtracking Cut !

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

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

13 Backward Chaining in Prolog Backward chaining follows a classic depth-first backtracking algorithmBackward chaining follows a classic depth-first backtracking algorithm ExampleExample –Goal: Snowy(C) Snowy(C)

14 Infinite Regression Goal

15 Reading Assignment ReadRead –Rest of Scott Sect. 11.3.1 Guide to Prolog Example, Roman BartákGuide to Prolog Example, Roman Barták –Go through all the examples –http://ktiml.mff.cuni.cz/~bartak/prolog/learning.html http://ktiml.mff.cuni.cz/~bartak/prolog/learning.html

Download ppt "1 Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2003 The University of North Carolina at Chapel Hill."

Similar presentations