Question about a concept and syntax of a story generator

Hi,
while searching for ideas for a story generator I found this example with a syntax completely new to me and I would like to understand what is happening here. It works in SWI Prolog swipl.

First it says
% Generate a random story from productions of form Nonterminal=>Expression
so I guess the atoms like start generate text with the expression on the right side.

to do so it redefines the operator :-op(600, xfy, =>).
or what is its puspose ?

What happens especiall in these kind of clauses, eseciall the * and + operators on atoms and strings inside the brackets?

start=>'Earth'*(catestrophe +  science +  attack +  collision).
catestrophe=>(('burns up' +  'freezes' +  'falls into the sun')
	*'and'
	*possible_megadeath
).

Could not find anything about it but may be I am looking in the wrong places? Any help appreciated!
Many thanks, C.

It looks like DCG but using a different operator =>. It is basically stringing together words and phrases to form larger structures that eventually form the story.

See: Using Definite Clause Grammars in SWI-Prolog


Anne has a tutorial about DCGs that is more to the point of building structures up using DCGs but the URL (http://www.pathwayslms.com/swipltuts/ …) uses http and not https so my browsers are blocking my access and I don’t plan to lower the access and switching to https is not being accepted.

Thanks I did this and used even -**> which is fun (I called it the vegetable skewer) but I don´t understand how and why it works in this conetxt, or what it is actually doing. As @EricGT wrote it looks like DCG but would like to understand the idea or mechanism behind it.

Many thanks, I thought so, too, but I dont understand how this stringing together with + and * in the brackets work. I have never seen this syntax before.

The link unfortunately does not work.

Note: I have not loaded and run this code, these comments are just from eyeballing the code.

After looking at this more it becomes apparent that one of the higher level concepts to understand is as you note + and *. These are defined at the top of the code.

story(A * B):- story(A), story(B).

Which is just using * as the Prolog and (,).

story(A  +  B):-makelist(A  +  B, L),
	length(L, M),
	P is random( M)+1,!,
	nth1(P, L, X), 
	story(X).

Which is building a list of words or phrases then randomly picking one.


While it did look like DCGs at first, I won’t say that now.


While I have not tried this it might shine more light on how this works.

Use write_canonical/1 on the code or predicates. If it works the output should be like an AST.


A curious question.

Since + has only two values, story(A + B), for more than two such as ('some people'+ 'everybody' + 'almost everybody') does it first choose one of the first two values then use that result with the third value? If so are stories skewed toward using values from the end of list.

I see! I wasn´t aware that it is even possible. Thanks again!