Re: Extended DCG?

"Abdallah, Samer" <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 12 Feb 2014, at 20:38, Alan Baljeu <[email protected]>
 wrote:
Hi Alan,

In your particular case, yes, I think that would work, because in (L,R), both L and
R are lists, so the \< and \> operators I defined would work with any DCG phrase,
including those with comma, brace etc, e.g.

	\< (something,  {writeln(hello)}, etc) would work, because it results
in a call to phrase( (something, {writeln(hello)}, etc), L1, L2)

If the definition had been
	\< (P, (L1,R), (L2,R)) :- call(P,L1,L2).
bypassing phrase/3, then it would not work because (,)/4 and {}/3 are not
defined, but are interpreted by the phrase/3 machinery.

The problem with the restriction to list types would show up if the left
and right states L and R were themselves some alternative type. The restriction
breaks the composability of these little DCG sub-languages.

For your application, you could have something like this:

transduce(List1, List2) :- trans( (List1,List2), ([],[])).

trans --> [].  % base case for ending transduction
trans --> next, trans. % eat some input and recurse

next --> \<[b,a,n,g], \> ['!'], trans. % read "bang", write "!" 
next --> \<[X], chew(X), trans.

chew(a) --> \> [a].  % send a through unmolested
chew(b) --> [].        % ignore b
chew(c) --> \> [s,e,e]. % output several things instead of c
chew(d) --> \< [X], \> [ '(', X, ')' ].  % d means put the next thing in brackets
chew(e) --> \< [_].     % e means ignore the next thing.

and so on. Is that the sort of thing you were after?

Samer.

btw, I got my operator declaration the wrong way round: it should be


% fy to allow nesting, e.g.  \< (\< p, \> q)
:- op(800, fy, \<).  
:- op(800, fy, \>).

> I never bother with phrase/3, and just invoke my predicate xy(Rag, List, []).  So maybe I could do xy(Rag, (List1, List2), ([],[])) and design xy to work?  It's not clear to me how to pull this all together.
>  
> Alan Baljeu
> From: "Abdallah, Samer" <[email protected]>
> To: Alan Baljeu <[email protected]> 
> Cc: Prolog <[email protected]> 
> Sent: Wednesday, February 12, 2014 1:48:07 PM
> Subject: Re: [SWIPL] Extended DCG?
> 
> Hi all,
> I would just like to use this opportunity to request again that phrase/3 be 
> allowed to work with arbitrary types in the last two positions, not just
> lists, or an alternative, say gphrase/3  (for generalised phrase), or
> rundcg/3 which works exactly like phrase/3 but allows arbitrary types.
> Or even better, put phrase/3 back how it was and introduce a type
> checking list_phrase/3 or safe_phrase/3 for people who want that.
> 
> It used to be the case that an extended DCG for writing a transducer
> as  Alan would like, was easy to write using ordinary DCG notation.
> The type of the threaded argument would be a pair of lists (L,R)
> instead of just one list. In my DCG library I have some DCG rules for
> working with this conveniently. For example, operators (\<)//1 and (\>)//1 
> apply a DCG phrase to the left or right member of the pair:
> 
> :- op(800, \<, fy).
> :- op(800, \>, fy).
> 
> \<(P, (L1,R), (L2,R)) :- phrase(P,L1,L2). 
> \>(P, (L,R1), (L,R2)) :- phrase(P,R1,R2)
> 
> Then you can do things like \< integer(X) to read an integer from
> from the left sequence and \> [int(X)] to write a term to the right, and so on.
> Alan's scenario is easily and efficiently dealt with.
> 
> Forcing us to use lists for the arguments to phrase/3 forces an ugly
> encoding of the pair in a singleton list, e.g. as [(L,R)], and, if the list
> is [X] and we already have a bunch of predicates that want to work
> on X,  forces an unnecessary layer of meta-predicates to get through 
> the wrapping.
> 
> For the moment, I have deleted the two offending lines from boot/dcg.pl
> in my local installations, but of course, this is not good for the portability
> of my code!
> 
> cheers,
> Samer
> 
> 
> (
> 
> On 11 Feb 2014, at 16:51, Alan Baljeu <[email protected]>
> wrote:
> > I have a scenario where I'm mapping a list to a second list, but it's not 1 to 1.  For this, it would be nice to use an extended DCG, that supports two independent lists.  Does such a thing exist?
> >  
> > Alan Baljeu
> > -------------- next part --------------
> > HTML attachment scrubbed and removed
> > _______________________________________________
> > SWI-Prolog mailing list
> > [email protected]
> > https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
> > 
> 
> 

-------------- next part --------------
HTML attachment scrubbed and removed
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 495 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <https://lists.iai.uni-bonn.de/pipermail/swi-prolog/attachments/20140212/d6770aca/attachment.bin>
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.