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
Fine. But this doen't work on SICStus.
A.L.
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. <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.
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.
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
>
> 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
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.
> 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
... 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?
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
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
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