Re: DCG
Ulrich Neumerkel <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
Josef Frydl:
> Is there a way in DCG, to specify, that I want only the shortest
> solution, satisfying the Grammar rule?
What you are asking for is something inherently non-monotonic. To see
this, consider the grammar a --> [a]. Here, [a] is the
shortest solution. Now add a rule a --> []. and [a] is no longer the
shortest solution. So what you are asking for extends somewhat the
pure core of DCGs.
It is not clear to me what you mean by "specify". Specify where? At
the start symbol's level, or within a grammar rule?
once( ( length(L, N), \+ \+ phrase(Startsymbol,L) ) ),
phrase(Startsymbol,L)
determines the (set of) shortest sentences - provided they exist. Note
that there can be several answers!
Doing this within a grammar rule is a bit cumbersome. In particular
since it depends on the concrete instantiation.
seq([]) --> [].
seq([E|Es]) --> [E], seq(Es).
..., ( seq(Es), { \+ \+ phrase(Qq, Es) } -> [] ; {false} )
Remark, that it is often useful to replace a nonterminal
..., NT, ...
by
... seq(Es), {phrase(NT, Es)}, ...
of course, this diminishes termination.
What is rather unusual is that you are asking for the shortest
sentence, often the longest is of interest which is even more
difficult to determine.