[m-users.] A predicate taking a lists of predicates as input?

Anders Lundstedt mercury-users at anderslundstedt.se
Mon Sep 22 02:28:16 AEST 2025


Dear all,


For the purpose of a parser I am writing, I want to have a DCG rule ?
taking a list of DCG rules as input and that then tries to apply all
rules in the list.


GitHub Gist of the following:

https://gist.github.com/anderslundstedt/1f5b1d8ad6b4d2404dae4a602d3d920d


My attempt:

:- type ta_rule(TKNS) ==  pred(TKNS, TKNS). % 'ta' abbreviates 'type alias'
:- inst ta_rule(TKNS) == (pred(in,   out) is semidet).

:- pred ?(list(ta_rule(TKNS)), TKNS, TKNS).
:- mode ?(in,                  in,   out).
?(RULES) --> (
  apply_rules(RULES) -> {true};
                        {true}
).

:- pred apply_rules(list(ta_rule(TKNS)), TKNS, TKNS).
:- mode apply_rules(in,                  in,   out) is semidet.
apply_rules([])     --> {true}.
apply_rules([R|RS]) --> R, apply_rules(RS).


Compilation error:

In clause for `apply_rules(in, in, out)':
  in argument 1 (i.e. the predicate term) of higher-order predicate
  call:
  mode error: variable `R' has instantiatedness `ground',
  expecting higher-order pred inst of arity 2.


Any advice would be greatly appreciated!



Best,

Anders Lundstedt


More information about the users mailing list