Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

maplist - the quiz!

6 views
Skip to first unread message

Ulrich Neumerkel

unread,
Dec 2, 2009, 10:10:06 AM12/2/09
to
While I feel a little bit of regret to fill this newsgroup
with prosy considerations to normative codification - if
you are not completely into it, you must get the impression
that this language is some big can of worms and ambiguities
(Don't believe other languages are that much better off:
During our quest we found more than one problem in other
language *standards* - just en passant.), we have to go
through that nolens volens.

But there are shinier sides of the Prolog language!

This is a quiz with abundant prizes that will more than
recompensate your efforts:

Insights, insights, insights!

Take maplist/2,... as defined in SWI, YAP (only some arities
present), SICStus 4 and others.

What can we formulate with them? What are these many definitions
good for? But let's not get into prose. Take a functional
language, say Haskell's standard prelude. Which of those
functions can we emulate with ONE goal of maplist/2,... directly?

More precisely: The first argument of maplist/2,... which is

:- meta_predicate(maplist(1,?)).
:- meta_predicate(maplist(2,?,?)).
:- meta_predicate(maplist(3,?,?,?)).
...

shall be either a continuation referring to a predicate
defined with a single, nonrecursive rule - or
a lambda expression as defined by library(lambda).

http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl

A.L.

unread,
Dec 2, 2009, 2:06:47 PM12/2/09
to
On Wed, 02 Dec 2009 15:10:06 GMT, ulr...@mips.complang.tuwien.ac.at
(Ulrich Neumerkel) wrote:
[...]

>
>shall be either a continuation referring to a predicate
>defined with a single, nonrecursive rule - or
>a lambda expression as defined by library(lambda).
>
>http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl

Fine. But this doen't work on SICStus.

A.L.

Ulrich Neumerkel

unread,
Dec 2, 2009, 2:12:35 PM12/2/09
to

It should do as follows:

http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord#implementation

At least:

copy_term_nat(T, C) :-
copy_term(T, C, _).

A.L.

unread,
Dec 2, 2009, 2:41:56 PM12/2/09
to
On Wed, 02 Dec 2009 19:12:35 GMT, ulr...@mips.complang.tuwien.ac.at
(Ulrich Neumerkel) wrote:

>A.L. <alew...@aol.com> writes:
>>On Wed, 02 Dec 2009 15:10:06 GMT, ulr...@mips.complang.tuwien.ac.at
>>(Ulrich Neumerkel) wrote:
>>[...]
>>>
>>>shall be either a continuation referring to a predicate
>>>defined with a single, nonrecursive rule - or
>>>a lambda expression as defined by library(lambda).
>>>
>>>http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl
>>
>>Fine. But this doen't work on SICStus.
>
>It should do as follows:
>
>http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord#implementation
>
>At least:
>
>copy_term_nat(T, C) :-
> copy_term(T, C, _).


I don't understand tyour answer, especially the link above.

There is no "apply" library in Prolog. There is nothing like this

strip_module/3

A.L.

Ulrich Neumerkel

unread,
Dec 2, 2009, 2:46:36 PM12/2/09
to
A.L. <alew...@aol.com> writes:
>On Wed, 02 Dec 2009 19:12:35 GMT, ulr...@mips.complang.tuwien.ac.at
>(Ulrich Neumerkel) wrote:
>
>>A.L. <alew...@aol.com> writes:
>>>On Wed, 02 Dec 2009 15:10:06 GMT, ulr...@mips.complang.tuwien.ac.at
>>>(Ulrich Neumerkel) wrote:
>>>[...]
>>>>
>>>>shall be either a continuation referring to a predicate
>>>>defined with a single, nonrecursive rule - or
>>>>a lambda expression as defined by library(lambda).
>>>>
>>>>http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl
>>>
>>>Fine. But this doen't work on SICStus.
>>
>>It should do as follows:
>>
>>http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord#implementation
>>
>>At least:
>>
>>copy_term_nat(T, C) :-
>> copy_term(T, C, _).
>
>
>I don't understand tyour answer, especially the link above.
>
>There is no "apply" library in Prolog. There is nothing like this

It is in SICStus 4 but not present in SICStus 3.

http://www.sics.se/sicstus/docs/latest4/html/sicstus.html/Predicate-Index.html#Predicate-Index
->
http://www.sics.se/sicstus/docs/latest4/html/sicstus.html/lib_002dlists.html#index-maplist_002f_005b2_002c3_002c4_005d-_0028lists_0029-2624

In case you are using SICStus 3

http://prolog.cs.vu.nl/git/pl-57x.git?a=blob;f=packages/plunit/swi.pl

But this defines only maplist/2,3 and not 4 which is very
necessary for this quiz btw...

Which contains also

>strip_module/3

as

http://prolog.cs.vu.nl/git/pl-57x.git?a=blob;f=packages/plunit/swi.pl#l111

mgoal_to_module_goal(MGoal, Module, Goal)

which is the same (except for fatal cases, as)

strip_module(MGoal, Module, Goal)

Sorry for this inconvenience, I thought it was already there.


afa

unread,
Dec 3, 2009, 11:57:13 AM12/3/09
to
On Dec 2, 10:10 am, ulr...@mips.complang.tuwien.ac.at (Ulrich

I don't think lambda expressions fit in Prolog as well as they do in
FP because Prolog does not even support first-order functions well. So
it'd be easier to solve your quiz than to have a new proposal.

I have been proposing declarative loop constructs and list
comprehensions for Prolog. Your can take a look at my current
proposal:

http://www.sci.brooklyn.cuny.edu/~zhou/papers/loops.pdf

Here is an example for the magic-square problem:

go(N):-
new_array(Board,[N,N]),
NN is N*N,
Vars @= [Board[I,J] : I in 1..N, J in 1..N],
Vars :: 1..NN,
Sum is NN*(NN+1)//(2*N),
foreach(I in 1..N,sum([Board[I,J] : J in 1..N]) #= Sum),
foreach(J in 1..N,sum([Board[I,J] : I in 1..N]) #= Sum),
sum([Board[I,I] : I in 1..N]) #= Sum,
sum([Board[I,N-I+1] : I in 1..N]) #= Sum,
all_different(Vars),
labeling([ffc],Vars),
foreach(I in 1..N,
(foreach(J in 1..N, [Bij],
(Bij @= Board[I,J], format("~4d ",[Bij]))),nl)).

I am wondering how well you can describe this problem using maplist.
You can try the examples in the proposal with B-Prolog 7.4a1 (alpha
version) available at probp.com/download/bp74a1_win.zip. Currently,
foreach and list comprehensions are interpreted. I am working on the
compiler, which is expected to be released soon.

Cheers,
Neng-Fa

afa

unread,
Dec 3, 2009, 11:59:53 AM12/3/09
to
On Dec 3, 11:57 am, afa <neng.z...@gmail.com> wrote:

>
> I don't think lambda expressions fit in Prolog as well as they do in
> FP because Prolog does not even support first-order functions well. So
> it'd be easier to solve your quiz than to have a new proposal.

I meant "it'd be easier to have a new proposal than to solve your
quiz".

Cheers,
Neng-Fa

Ulrich Neumerkel

unread,
Dec 3, 2009, 12:25:52 PM12/3/09
to
afa <neng...@gmail.com> writes:
>On Dec 2, 10:10=A0am, ulr...@mips.complang.tuwien.ac.at (Ulrich

>Neumerkel) wrote:
>> More precisely: The first argument of maplist/2,... which is
>>
>> :- meta_predicate(maplist(1,?)).
>> :- meta_predicate(maplist(2,?,?)).
>> :- meta_predicate(maplist(3,?,?,?)).
>> ...
>>
>> shall be either a continuation referring to a predicate
>> defined with a single, nonrecursive rule - or
>> a lambda expression as defined by library(lambda).
>>
>> http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl
>
>I don't think lambda expressions fit in Prolog as well as they do in
>FP because Prolog does not even support first-order functions well. ...

This quiz is rather about maplist/2,...

A predicate serving as a continuation would do as well -
no need for lambdas if you do not like them.

bart demoen

unread,
Dec 3, 2009, 3:57:12 PM12/3/09
to
On Wed, 02 Dec 2009 15:10:06 +0000, Ulrich Neumerkel wrote:


> Take maplist/2,... as defined in SWI, YAP (only some arities present),
> SICStus 4 and others.

I got confused by the above sentence as it gives the impression that only Yap
has just a few arities for maplist/*,

but it seems that SWI and SICStus 4 also have only some arities present, see:

pl-5.8.2/library/apply.pl
and
sicstus-4.0.2/library/lists.pl


Bart Demoen

Ulrich Neumerkel

unread,
Dec 3, 2009, 4:12:27 PM12/3/09
to
bart demoen <b...@cs.kuleuven.be> writes:
>On Wed, 02 Dec 2009 15:10:06 +0000, Ulrich Neumerkel wrote:
>> Take maplist/2,... as defined in SWI, YAP (only some arities present),
>> SICStus 4 and others.
>I got confused by the above sentence as it gives the impression that only Yap
>has just a few arities for maplist/*,

... in the meantime some details in YAP have improved. git pull! I would
say the three are now on a par for the purpose of this quiz.

I *think* it is sufficient to have maplist/2,3,4. I.e. I do not see
(currently) uses for higher arities to emulate some of Haskell's
standard prelude. But who knows!

>but it seems that SWI and SICStus 4 also have only some arities present, see:
>
>pl-5.8.2/library/apply.pl
>and
>sicstus-4.0.2/library/lists.pl

Yes, I believe maplist/5 is more than enough. Or do you
have some cases?

Jan Wielemaker

unread,
Dec 4, 2009, 3:40:28 AM12/4/09
to
On 2009-12-03, bart demoen <b...@cs.kuleuven.be> wrote:
> On Wed, 02 Dec 2009 15:10:06 +0000, Ulrich Neumerkel wrote:
>
>
>> Take maplist/2,... as defined in SWI, YAP (only some arities present),
>> SICStus 4 and others.
>
> I got confused by the above sentence as it gives the impression that only Yap
> has just a few arities for maplist/*,
>
> but it seems that SWI and SICStus 4 also have only some arities present, see:
>
> pl-5.8.2/library/apply.pl

If you load library(apply_macros) however, it will happily *compile* maplist
constructs with any arity (<1024) :-) This library rewrites a maplist call
into an auxilary classic Prolog loop predicate and calls that.

Cheers --- Jan

bart demoen

unread,
Dec 7, 2009, 1:31:51 PM12/7/09
to

I appreciate that someone (you probably) has put effort into extending
library(apply_macros) to do that, but every time something works "compiled"
(with some extra lib loaded makes it worse) and it doesn't "interpreted"
(or the other way around) I feel something is wrong with the language design.

Remember call(X is Y) behaved (behaves) differently from X is Y in some
systems ? .... BAD !

Cheers

Bart Demoen

Jan Wielemaker

unread,
Dec 7, 2009, 3:08:38 PM12/7/09
to
On 2009-12-07, bart demoen <b...@cs.kuleuven.be> wrote:
> On Fri, 04 Dec 2009 08:40:28 +0000, Jan Wielemaker wrote:
>
>> On 2009-12-03, bart demoen <b...@cs.kuleuven.be> wrote:
>>> but it seems that SWI and SICStus 4 also have only some arities
>>> present, see:
>>>
>>> pl-5.8.2/library/apply.pl
>>
>> If you load library(apply_macros) however, it will happily *compile*
>> maplist constructs with any arity (<1024) :-) This library rewrites a
>> maplist call into an auxilary classic Prolog loop predicate and calls
>> that.
>
> I appreciate that someone (you probably) has put effort into extending
> library(apply_macros) to do that, but every time something works "compiled"
> (with some extra lib loaded makes it worse) and it doesn't "interpreted"
> (or the other way around) I feel something is wrong with the language design.

So do I. What I'm looking for is partial evaluation to deal with these
things. Even more general, I'm looking for a good infrastructure to perform
this type of rewrites as a decompile-rewrite-compile cycle, as well as general
source-to-source rewrites and general program analysis, for example to comment
on dubious programming constructs. If you happen to know about a good
starting point, let me know.

> Remember call(X is Y) behaved (behaves) differently from X is Y in some
> systems ? .... BAD !

I'm not aware of any such problems in SWI :-)

Cheers --- Jan

0 new messages