09-Difference-Lists and DCGs
09-Difference-Lists and DCGs
Geraint A. Wiggins
Unify: X = [a|D1], Y = D1
Unify: Y = [b|D2], Z = D2
I Writing
[a|X]-X = [a|Y]-Y
has unpredictable results, because X and Y may have
different values
I So even though they represent the same d-list, they may
do so in different ways; thus they are not unifiable
I Unification for d-lists is not the same as identity
Definite Clause Grammars
I An important application of d-lists is in Definite Clause
Grammars (DCGs)
I DCGs are a facility in Prolog which makes it easy to
define languages according to grammar rules
I For example, we might want to define a simple input
command language for a game:
start 1 player
start 2 players
start 3 players
etc.
stop
save game
save player 1
save player 2
save player 3
etc.
Definite Clause Grammars (2)
I There is a standard way of writing this down, borrowed
from linguistics:
StartCommand → ‘start’ ‘1’ ‘player’
StartCommand → ‘start’ Number∗ ‘players’
StopCommand → ‘stop’
SaveableThing → ‘game’
SaveableThing → ‘player’ Number
?- command([save,player,3],[]).
true
I Note the [], and that the definition of command using
--> has led to the creation of a predicate command/2