Re: metaprogramming

"Richard A. O'Keefe" <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 30/08/2013, at 5:50 AM, Josef Frydl wrote:

> Hi, does somebody already know how to take the list of predicates and enclose all the member in parentheses  and members are separated by comma.
> 
> example
> [ a(A),b(B),c(C) ]  -->  (a(A),b(B),c(C) )


If only more people bought The Craft of Prolog.

You *cannot* enclose all the members in parentheses;
parentheses are an artefact of a particular printing style
and context.  They are *never* part of a data structure per se.

[a,b,c] is the same as '.'(a, '.'(b, '.'(c, [])))
 a,b,c  is the same as ','(a, ','(b, c))

From this, it should be pretty obvious both how to do it and
why this is almost always a bad idea.

For example, did x,y arise from [x,y] or from [(x,y)]?

Amongst other things, the empty list is a perfectly good list,
so what do you want _that_ to turn into?   Here I am going to
assume that you are turning a list of goals into a Prolog
conjunction.

goal_list_to_conjunction([], true).
goal_list_to_conjunction([G], C) :- !,
    C = G.
goal_list_to_conjunction([G|Gs], (G,C)) :-
    goal_list_to_conjunction(Gs, C).

Now we start wondering what exactly should happen if some
of the terms in the list are already comma-terms.  Should
[(a,b),(c,d),(e,f)] turn into ((a,b),(c,d),e,f)
or into a,b,c,d,e,f?  What if some of the elements are
true or false?  Should [a,true,b,false,c] => [a,b,false]?

What I am getting at is that
 - as a *data structure* a so-called "round list" is about
   as horrible as it gets in Prolog
 - for at least some uses, simply "change square brackets
   to round parentheses" is not enough to get high-quality
   output.
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.