[m-users.] A predicate taking a lists of predicates as input?
Anders Lundstedt
mercury-users at anderslundstedt.se
Thu Sep 25 23:55:40 AEST 2025
Thanks for the answers!
I think what I am trying to do is not possible in Mercury. For parsing
purposes, I want to be able to do something like the following. Given a list of
DCG rules r₁, ..., rₙ, each either det or semidet, I want to be able to
write the rule
r --> ?([r₁, ..., rₙ]).
(Even better would to be to be able to skip the brackets.)
This rule r should have the expected parsing behavior: it should try to apply
each of r₁, ..., rₙ, in that order, and consume tokens if and only if this
succeeds, and succeed as a rule no matter what.
To be able to write exactly as in the above definition of r, each of r₁, …, rₙ
must be of the same type. Thus some of the r₋ cannot be det while some of the
r₋ are semidet. I guess one solution would be to construct a discriminated
union type such that instead of ‘r₋’ one writes, for example, either
‘c_det(r₋)’ or ‘c_semidet(r₋)’. That would be quite verbose, though.
Tomas By <tomas at basun.net> wrote:
> So `?' applies a list of rules, and then you are nesting them?
Yes.
> If you really want to use DCG, ie experiment with the grammar and have
> something that is easy to work with, then just declare everything
> nondet and be done with it.
I think it will suffice to declare each rule as semidet.
> Then it might be better if you try to explain what it is all about.
I have written a parser in Mercury, DCG-style, for a context-sensitive
language. (I do not know if the grammar formalism used to define the language
has a name. It is a parsing expression grammar with added features from affix
grammars.) I am currently trying to simplify the parser by using the well-known
?, * and + parser operators. The language or its Mercury-written parser is not
yet public, but my implementation of the operators are:
https://github.com/anderslundstedt/mercury-experiments/tree/master/parsing-operators
(Thanks to everyone on this mailing list who has helped me! Further input of
course appreciated, but for my present purposes probably not needed.)
Best,
Anders
More information about the users
mailing list