users-prolog
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: DCG question: bind to inputs.


From: Paulo Moura
Subject: Re: DCG question: bind to inputs.
Date: Tue, 30 Jul 2013 14:55:34 +0100

On 29/07/2013, at 13:56, emacstheviking <address@hidden> 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:address@hidden>
Web:   <http://logtalk.org/>
-----------------------------------------------------------------







reply via email to

[Prev in Thread] Current Thread [Next in Thread]