[mercury-users] functions in DCGs

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Jan 7 07:17:40 AEDT 2002


On 05-Jan-2002, Dave Slutzkin <dave_slutzkin at yahoo.com> wrote:
> Is it impossible (or at least annoying) to use functions in DCGs?

You can call functions from DCG clauses.

You can't write DCG clauses for functions, though.

> For example, to access the result of a function you
> need to use 'L = some_function'.  But there is no such
> thing as '='/4, so you need to enclose this in braces:
>   { L = some_function }
> 
> But then some_function doesn't have access to the DCG
> arguments it needs and so you need to state them
> explicitly - destroying any DCG advantage.

You can use =//1 in DCG clauses to access the DCG state,
and you can then pass it to functions:

	foo -->
		=(DCGState),
		{ L = some_function(DCGState) },
		...

The same technique is also used when you have non-DCG predicates
that need access to the DCG argument, e.g.

	foo -->
		=(DCGState),
		{ some_predicate(DCGState, L, M, N) },
		...

> The easiest solution I can think of is to write '='/4
> and just copy the DCG argument.

Hmm, you mean like this:

	:- pred '='(T, func(S) = T, S, S).
	:- mode '='(in, in, in, out) is det.

	'='(X, F, S, S) :-
		X = F(S).

	foo -->
		L = some_function,
		...

?  I guess that would work.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list