Prologs Lists, Negation and Imperative Control Flow - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Prologs Lists, Negation and Imperative Control Flow

Description:

1. Prolog's Lists, Negation and Imperative Control Flow. COMP 144 ... Go through all the examples. http://ktiml.mff.cuni.cz/~bartak/prolog/learning.html ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 16
Provided by: felixherna
Category:

less

Transcript and Presenter's Notes

Title: Prologs Lists, Negation and Imperative Control Flow


1
Prologs Lists, Negation and Imperative Control
Flow
The University of North Carolina at Chapel Hill
  • COMP 144 Programming Language Concepts
  • Spring 2003

Stotts, Hernandez-Campos
2
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
Tic-Tac-Toe Example
?
?
?
?
?
7
Tic-Tac-Toe Example
?
?
?
?
?
?
8
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, XT).
  • member(X, HT) - member(X, T).
  • member(X, XT) - !.
  • member(X, HT) - member(X, T).

member may succeed n times
member may succeed at most one time
9
Imperative Control Flow
  • Cut causes the unification stack to be frozen

10
Cut and the stack
Cut !
  • New stack base for backtracking

(c)
K 147
Z blue
(b)
Y tandoori
rochester
P
(a)
11
Imperative Control Flow
  • Alternative
  • member(X, XT).
  • member(X, HT) - not(XH), member(X, T).
  • 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 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))

13
Backward Chaining in Prolog
  • Backward chaining follows a classic depth-first
    backtracking algorithm
  • Example
  • Goal
  • Snowy(C)

14
Infinite Regression
Goal
15
Reading Assignment
  • Read
  • Rest of Scott Sect. 11.3.1
  • Guide to Prolog Example, Roman Barták
  • Go through all the examples
  • http//ktiml.mff.cuni.cz/bartak/prolog/learning.h
    tml
Write a Comment
User Comments (0)
About PowerShow.com