Subject: RE: [SWIPL] sumlist to arithmetic function

From: "Douglas R. Miles" <[email-masked]>
Date: Wed, 26 Sep 2001 13:04:20 -0700

> Possibly it would be nice to allow for functions to be passed
> their arguments literally.

That would be very nice.  I have an application that could use that kind of
support. 

Although and easy solution is to use a version of is/2 from prolog such as..

:-op(700,xfx,my_is).

my_is(R,E):-catch( R is E,_,fail),!.
my_is(R,(A+B)):-!,
	my_is(RA,A),
	my_is(RB,B),
	my_is(R,RA+RB).
my_is(R,(A-B)):-!,
	my_is(RA,A),
	my_is(RB,B),
	my_is(R,RA-RB).
my_is(R,(A*B)):-!,
	my_is(RA,A),
	my_is(RB,B),
	my_is(R,RA*RB).
my_is(R,(A/B)):-!,
	my_is(RA,A),
	my_is(RB,B),
	my_is(R,RA/RB).
...etc..
my_is(R,E):-
	E =..ECall, 
	append(ECall,[R],Call),!,
	Call.
..

But heh, what if is/2 '<'/2 '>'/2 (etc)  just 'assumed' that any
unknown/unregistered functors was grounds for 'arithmetic_function' and
naively constructed an extra argument on the end and sent it through the
call/1 .. The only issue I see, is it may inspire harder to track down
problems for people or less readable code if when it's not been declared as
arithmetic_function.  

-Douglas

> -----Original Message-----
> From: Jan Wielemaker [mailto:[email-masked]]
> Sent: Wednesday, September 26, 2001 6:40 AM
> To: Paul Singleton; SWI Prolog
> Subject: Re: [SWIPL] sumlist to arithmetic function
> 
> 
> On Fri, 24 Aug 2001, Paul Singleton wrote:
> >Hans-Ulrich Zimmermann <[email-masked]> writes:
> >
> >> converting the old sumlist predicate to an arithmetic function
> >> will not work:
> >
> >> :-arithmetic_function(sumlist/1).
> >
> >> sumlist([],0).
> >> sumlist([Kopf|Rumpf], Summe) :- sumlist(Rumpf,Teilsumme), 
> >                                Summe is Kopf + Teilsumme.
> >
> >> Is there any solution?
> >
> >Empirically, SWI-Prolog accepts this directive:
> >
> >  :- arithmetic_function(sumlist/1).
> >
> >but if you call e.g. this goal
> >
> >  ?- A is sumlist([1,2,3,4,5]).
> >
> >it raises this error at runtime:
> >
> >   '.'/2: Type error: `[]' expected, found `[2, 3, 4, 5]'
> 
> Point remains that an arithetic function will only be passed
> numbers as the arithmetic layer evaluates the arguments first.
> Now, "a" is accepted as an expression returning the character code
> for the letter 'a'.  As you might know, "a" is .(a, []) and the
> above therefore is realised by defining ./2 as a (special) function
> that demands the first argument to be a one-character atom and the
> second to be an empty list.  Hence the error message.
> 
> Possibly it would be nice to allow for functions to be passed
> their arguments literally.
> 
> 	--- Jan
> 
> 
> ----------------
> * To UNSUBSCRIBE, please use the HTML form at
> 
>     
> http://www.swi.psy.uva.nl/projects/SWI-Prolog/index.html#mailinglist
> 
> or send mail to [email-masked] using the 
> Subject: "unsubscribe"
> (without the quotes) and *no* message body.
> 
> ** An ARCHIVE of this list is maintained at
> 
>     http://www.swi.psy.uva.nl/projects/SWI-Prolog/mailinglist/archive/
> 
> 
>