/* Production rules in Picat. From Nilsson & Matuszynski: "Logic, Programming and Prolog" Page 186f (Chapter 10: Logic and Grammar) This defines the grammar (page 184) -> , -> the -> runs -> engine -> rabbit Also see logic_grammars1.pi for another (and better) approach to represent this (and other) grammar(s). This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import v3_utils. % import util. % import cp. main => go. /* [sentence] [noun_phrase,verb_phrase] [the,noun,verb_phrase] [the,engine,verb_phrase] [the,engine,runs] [the,rabbit,verb_phrase] [the,rabbit,runs] [the,noun,runs] [the,engine,runs] [the,rabbit,runs] [noun_phrase,runs] [the,noun,runs] [the,engine,runs] [the,rabbit,runs] */ go ?=> derives([sentence],X), println(X), fail, nl. go => true. prod_rule(sentence,[noun_phrase,verb_phrase]). prod_rule(noun_phrase,[the,noun]). prod_rule(verb_phrase,[runs]). prod_rule(noun,[engine]). prod_rule(noun,[rabbit]). derives_directly(X,Y) :- append(Left,[Lhs],Right,X), prod_rule(Lhs,Rhs), append(Left,Rhs,Right,Y). derives(X,X). derives(X,Z) :- derives_directly(X,Y), derives(Y,Z). /* append4(X,Y,Z,W) :- append(X,Y,Tmp), append(Tmp,Z,W). */ % "more efficient" % But Picat has append/4 so I use that.... append4([],[],X,X). append4([],[Head|Y],Z,[Head|W]) :- append4([],Y,Z,W). append4([Head|X],Y,Z,[Head|W]) :- append4(X,Y,Z,W).