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

"Sean Charles (emacstheviking)" <[email protected]> Sun, 21 Sep 2025 21:10:41 +0100
Newsgroups gmane.comp.lang.mercury.general
Message-ID <[email protected]>
--===============8125789025688521967==
Content-Type: multipart/alternative;
	boundary="Apple-Mail=_5F3A05B7-9079-49EE-AFD7-055689221F8A"


--Apple-Mail=_5F3A05B7-9079-49EE-AFD7-055689221F8A
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

Hi Volker,

I just scanned Chapter 8, Higher Order... what is the meaning of the =
question marks here? I could find no reference.

>=20

> :- pred ?(list(ta_rule(TKNS)), TKNS, TKNS).

> :- mode ?(in(list(ta_rule)),   in,   out).


Thanks,
Sean


> On 21 Sep 2025, at 17:51, Volker Wysk <[email protected]> wrote:
>=20
> Hi, Anders
>=20
> You have two errors:
>=20
> 1. You don't need and can't have the TNKS argument to your ta_rule =
inst,
> since it isn't used on the right side.
>=20
> 2. You need to specify a higher order inst matching your higher order =
type,
> for both predicates.
>=20
> Do it like this:
>=20
> :- type ta_rule(TKNS) =3D=3D  pred(TKNS, TKNS). % 'ta' abbreviates =
'type alias'
> :- inst ta_rule       =3D=3D (pred(in,   out) is semidet).
>=20
> :- pred ?(list(ta_rule(TKNS)), TKNS, TKNS).
> :- mode ?(in(list(ta_rule)),   in,   out).
>=20
> :- pred apply_rules(list(ta_rule(TKNS)), TKNS, TKNS).
> :- mode apply_rules(in(list(ta_rule)),   in,   out) is semidet.
>=20
> Cheers,
> Volker
>=20
> Am Sonntag, dem 21.09.2025 um 18:28 +0200 schrieb Anders Lundstedt:
>> Dear all,
>>=20
>>=20
>> 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.
>>=20
>>=20
>> GitHub Gist of the following:
>>=20
>> =
https://gist.github.com/anderslundstedt/1f5b1d8ad6b4d2404dae4a602d3d920d
>>=20
>>=20
>> My attempt:
>>=20
>> :- type ta_rule(TKNS) =3D=3D  pred(TKNS, TKNS). % 'ta' abbreviates =
'type
>> alias'
>> :- inst ta_rule(TKNS) =3D=3D (pred(in,   out) is semidet).
>>=20
>> :- pred ?(list(ta_rule(TKNS)), TKNS, TKNS).
>> :- mode ?(in,                  in,   out).
>> ?(RULES) --> (
>>   apply_rules(RULES) -> {true};
>>                         {true}
>> ).
>>=20
>> :- 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).
>>=20
>>=20
>> Compilation error:
>>=20
>> 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.
>>=20
>>=20
>> Any advice would be greatly appreciated!
>>=20
>>=20
>>=20
>> Best,
>>=20
>> Anders Lundstedt
>> _______________________________________________
>> users mailing list
>> [email protected]
>> https://lists.mercurylang.org/listinfo/users
> _______________________________________________
> users mailing list
> [email protected]
> https://lists.mercurylang.org/listinfo/users


--Apple-Mail=_5F3A05B7-9079-49EE-AFD7-055689221F8A
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html;
	charset=us-ascii

<html aria-label=3D"message body"><head><meta http-equiv=3D"content-type" =
content=3D"text/html; charset=3Dus-ascii"></head><body =
style=3D"overflow-wrap: break-word; -webkit-nbsp-mode: space; =
line-break: after-white-space;">Hi Volker,<div><br></div><div>I just =
scanned Chapter 8, Higher Order... what is the meaning of the question =
marks here? I could find no reference.</div><blockquote style=3D"margin: =
0 0 0 40px; border: none; padding: 0px;"><div><br></div><div><blockquote =
type=3D"cite"><br></blockquote></div><div><blockquote type=3D"cite">:- =
pred ?(list(ta_rule(TKNS)), TKNS, =
TKNS).</blockquote></div><div><blockquote type=3D"cite">:- mode =
?(in(list(ta_rule)), &nbsp;&nbsp;in, =
&nbsp;&nbsp;out).</blockquote></div><div><br></div></blockquote><div>Thank=
s,</div><div>Sean</div><div><br =
id=3D"lineBreakAtBeginningOfMessage"><div><br><blockquote =
type=3D"cite"><div>On 21 Sep 2025, at 17:51, Volker Wysk =
&lt;[email protected]&gt; wrote:</div><br =
class=3D"Apple-interchange-newline"><div><div>Hi, Anders<br><br>You have =
two errors:<br><br>1. You don't need and can't have the TNKS argument to =
your ta_rule inst,<br>since it isn't used on the right side.<br><br>2. =
You need to specify a higher order inst matching your higher order =
type,<br>for both predicates.<br><br>Do it like this:<br><br>:- type =
ta_rule(TKNS) =3D=3D &nbsp;pred(TKNS, TKNS). % 'ta' abbreviates 'type =
alias'<br>:- inst ta_rule &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=3D=3D =
(pred(in, &nbsp;&nbsp;out) is semidet).<br><br>:- pred =
?(list(ta_rule(TKNS)), TKNS, TKNS).<br>:- mode ?(in(list(ta_rule)), =
&nbsp;&nbsp;in, &nbsp;&nbsp;out).<br><br>:- pred =
apply_rules(list(ta_rule(TKNS)), TKNS, TKNS).<br>:- mode =
apply_rules(in(list(ta_rule)), &nbsp;&nbsp;in, &nbsp;&nbsp;out) is =
semidet.<br><br>Cheers,<br>Volker<br><br>Am Sonntag, dem 21.09.2025 um =
18:28 +0200 schrieb Anders Lundstedt:<br><blockquote type=3D"cite">Dear =
all,<br><br><br>For the purpose of a parser I am writing, I want to have =
a DCG rule ?<br>taking a list of DCG rules as input and that then tries =
to apply all<br>rules in the list.<br><br><br>GitHub Gist of the =
following:<br><br>https://gist.github.com/anderslundstedt/1f5b1d8ad6b4d240=
4dae4a602d3d920d<br><br><br>My attempt:<br><br>:- type ta_rule(TKNS) =
=3D=3D&nbsp; pred(TKNS, TKNS). % 'ta' abbreviates 'type<br>alias'<br>:- =
inst ta_rule(TKNS) =3D=3D (pred(in,&nbsp;&nbsp; out) is =
semidet).<br><br>:- pred ?(list(ta_rule(TKNS)), TKNS, TKNS).<br>:- mode =
?(in,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in,&nbsp;&nbsp; out).<br>?(RULES) =
--&gt; (<br>&nbsp; apply_rules(RULES) -&gt; =
{true};<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
; {true}<br>).<br><br>:- pred apply_rules(list(ta_rule(TKNS)), TKNS, =
TKNS).<br>:- mode =
apply_rules(in,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in,&nbsp;&nbsp; out) is =
semidet.<br>apply_rules([])&nbsp;&nbsp;&nbsp;&nbsp; --&gt; =
{true}.<br>apply_rules([R|RS]) --&gt; R, =
apply_rules(RS).<br><br><br>Compilation error:<br><br>In clause for =
`apply_rules(in, in, out)':<br>&nbsp; in argument 1 (i.e. the predicate =
term) of higher-order predicate<br>&nbsp; call:<br>&nbsp; mode error: =
variable `R' has instantiatedness `ground',<br>&nbsp; expecting =
higher-order pred inst of arity 2.<br><br><br>Any advice would be =
greatly appreciated!<br><br><br><br>Best,<br><br>Anders =
Lundstedt<br>_______________________________________________<br>users =
mailing =
list<br>[email protected]<br>https://lists.mercurylang.org/listi=
nfo/users<br></blockquote>_______________________________________________<=
br>users mailing =
list<br>[email protected]<br>https://lists.mercurylang.org/listi=
nfo/users<br></div></div></blockquote></div><br></div></body></html>=

--Apple-Mail=_5F3A05B7-9079-49EE-AFD7-055689221F8A--

--===============8125789025688521967==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: base64
Content-Disposition: inline

X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KdXNlcnMgbWFp
bGluZyBsaXN0CnVzZXJzQGxpc3RzLm1lcmN1cnlsYW5nLm9yZwpodHRwczovL2xpc3RzLm1lcmN1
cnlsYW5nLm9yZy9saXN0aW5mby91c2Vycwo=

--===============8125789025688521967==--