Re: DCG question: bind to inputs.

Paulo Moura <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <[email protected]>
On 29/07/2013, at 13:56, emacstheviking <[email protected]> wrote:

> Hi,
> 
> After reading the manual (section 7.17.1 again) I am still confused... If I have this DCG rule:
> 
> foo(X) --> bar(X)
> 
> I know it expands to:
> 
> foo(X,A,B) :- bar(X, A, B).
> 
> How do I add a format statement like this:
> 
> foo(X) --> bar(X), {format("bar(X) ok, X is ~w~n", [???])}.
> 
> How do I bind to those two input arguments that are the source list and the resulting output list so I can do things with them / to them ? Sure I can print out X because that is in scope as it were but how to get A and B ?? I know this is a simple question and even worse I know I think I already know the answer, I just don't know I know yet.

Not sure if GNU Prolog DCGs implementation supports a built-in call//1 non-terminal. In Logtalk, you can write something like:

------ args.lgt ----
:- object(args).

	:- public(foo//1).

	foo(X) --> bar(X), call(rest(Rest)), {format("bar(~w) ok, Rest is ~w~n", [X,Rest])}.
	
	bar(a) --> "a".
	
	rest(Rest, Rest, _).

:- end_object.
--------------------

$ gplgt
...
Logtalk 3.0.0
Copyright (c) 1998-2013 Paulo Moura
...
GNU Prolog 1.4.4 (64 bits)
Compiled Apr 23 2013, 17:24:33 with /opt/local/bin/gcc-apple-4.2
By Daniel Diaz
Copyright (C) 1999-2013 Daniel Diaz
| ?- {args}.
compiling /Users/pmoura/Desktop/.lgt_tmp/args.pl for byte code...
/Users/pmoura/Desktop/.lgt_tmp/args.pl compiled, 32 lines read - 8415 bytes written, 28 ms
% [ /Users/pmoura/Desktop/args.lgt loaded ]
% (0 warnings)

(3 ms) yes
| ?- args<<phrase(foo(X), "abc", Rest).
bar(a) ok, Rest is [98,99]

X = a

yes

As illustrated above, the non-terminal call//1 is compiled into a call to the standard call/3 built-in predicate where the last two arguments are the grammar rule implicit arguments.

Cheers,

Paulo


-----------------------------------------------------------------
Paulo Moura
Logtalk developer

Email: <mailto:[email protected]>
Web:   <http://logtalk.org/>
-----------------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.