Home > Archive > Prolog > March 2006 > arithmetic_function (SWI)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
arithmetic_function (SWI)
|
|
| Mauro Di Nuzzo 2006-03-09, 7:56 am |
| Hi All.
This is an issue regarding arithmetic_function/1 predicate under SWI Prolog.
Suppose I define a new arithmetic function, namely a constant c/0, in this
way
:- arithmetic_function(c/0).
c(10).
Consider no changes in the context module when I consult it from the
toplevel.
Then I try:
?- X is c*2.
X = 20
That's ok. But I should have written:
?- c(Y).
Y = 10
So I can really access the predicate c/1. Instead, for SWI builtin
arithmetic function, this is not the case.
In fact, this happens:
?- Z is e.
Z = 2.71828
?- e(E).
ERROR: undefined procedure: e/1
So it seems that predicate e/1 associated with arithmetic function e/0 it is
asserted elseWHERE.
Finally, my question, WHERE?
In other words, how can I handle arithmetic functions so that associated
predicates (with arity + 1) are, let's say, invisible?
Thank you very much.
M
| |
| Jan Wielemaker 2006-03-09, 7:56 am |
| On 2006-03-09, Mauro Di Nuzzo <picorna@inwind.it> wrote:
> Hi All.
> This is an issue regarding arithmetic_function/1 predicate under SWI Prolog.
>
> Suppose I define a new arithmetic function, namely a constant c/0, in this
> way
>
>:- arithmetic_function(c/0).
> c(10).
>
> Consider no changes in the context module when I consult it from the
> toplevel.
> Then I try:
>
> ?- X is c*2.
> X = 20
>
> That's ok. But I should have written:
>
> ?- c(Y).
> Y = 10
>
> So I can really access the predicate c/1. Instead, for SWI builtin
> arithmetic function, this is not the case.
> In fact, this happens:
>
> ?- Z is e.
> Z = 2.71828
>
> ?- e(E).
> ERROR: undefined procedure: e/1
>
> So it seems that predicate e/1 associated with arithmetic function e/0 it is
> asserted elseWHERE.
> Finally, my question, WHERE?
Nowhere. All built-in arithmetic functions are connected to C-functions
using some internal interface (see pl-arith.c). One of these exploits a
call-back to Prolog to make predicates available as functions.
> In other words, how can I handle arithmetic functions so that associated
> predicates (with arity + 1) are, let's say, invisible?
You can't. You can put them in a module though.
--- Jan
| |
| Mauro Di Nuzzo 2006-03-09, 7:56 am |
| Thank you very much Jan.
Having understood what you said me, I tried to put an arithmetic function in
a module by this way:
--------
% name.pl
:- module(some, []). % I can export here the function c/0 in the same way
I export, for example, an operator.
:- arithmetic_function(c/0).
c(10).
% eof
--------
?- consult('name.pl').
yes
?- X is c.
ERROR: is/2: Arithmetic: 'c/0' is not a function
So I am unable to use c/0 from the toplevel (or just from a module other
than 'some').
What I want is to have the arithmetic function c/0 imported into context
module, and not the predicate c/1.
Does a method to achieve this exist, since module export declaration does
not include arithmetic functions?
:)
M
"Jan Wielemaker" <jan@nospam.ct.xs4all.nl> ha scritto nel messaggio
news:slrne105s3.4b0.jan@ct.lan...
> On 2006-03-09, Mauro Di Nuzzo <picorna@inwind.it> wrote:
Prolog.[color=darkred]
this[color=darkred]
it is[color=darkred]
>
> Nowhere. All built-in arithmetic functions are connected to C-functions
> using some internal interface (see pl-arith.c). One of these exploits a
> call-back to Prolog to make predicates available as functions.
>
>
> You can't. You can put them in a module though.
>
> --- Jan
| |
| Jan Wielemaker 2006-03-09, 6:58 pm |
| On 2006-03-09, Mauro Di Nuzzo <picorna@inwind.it> wrote:
> Thank you very much Jan.
> Having understood what you said me, I tried to put an arithmetic function in
> a module by this way:
>
> --------
> % name.pl
> :- module(some, []). % I can export here the function c/0 in the same way
> I export, for example, an operator.
> :- arithmetic_function(c/0).
> c(10).
> % eof
> --------
>
> ?- consult('name.pl').
> yes
> ?- X is c.
> ERROR: is/2: Arithmetic: 'c/0' is not a function
>
> So I am unable to use c/0 from the toplevel (or just from a module other
> than 'some').
> What I want is to have the arithmetic function c/0 imported into context
> module, and not the predicate c/1.
> Does a method to achieve this exist, since module export declaration does
> not include arithmetic functions?
>:)
I see. You're right. Only functions defined in user are visible (as
default) from all modules. So, I guess the correct answer is no.
Declaring a function f/n simply means you also have the predicate
f/n+1. The proper way around might be to allow exporting functions
without exporting the predicate. Although it can be done, its not
a totally trivial exercise. Guess the trigger was a name conflict?
First thing to try is to see whether renaming can come up with an
acceptable solution.
--- Jan
| |
| Mauro Di Nuzzo 2006-03-09, 6:58 pm |
| Just two more issue regarding arithmetic function handling in SWI Prolog.
In particular, I got the following segmentation fault errors, that I think
one might avoid to see.
?- arithmetic_function(c/0).
yes
?- X is c. % *see later
?- arithmetic_function(c/0).
yes
?- assert(( c(_) :- throw(error(instantiation_error, _)) )). % or some other
exception
yes
?- X is c. % *see later
Both goal* return this (I marked them with an asterisk):
ERROR: is/2: Undefined procedure: c/1
ERROR: is/2: Caught signal 11 (segv)
I do not know, but they seem bugs. Arent they?
Thanks again.
M
"Mauro Di Nuzzo" <picorna@inwind.it> ha scritto nel messaggio
news:yzVPf.64$8g7.61@nntpserver.swip.net...
> Thank you very much Jan.
> Having understood what you said me, I tried to put an arithmetic function
in
> a module by this way:
>
> --------
> % name.pl
> :- module(some, []). % I can export here the function c/0 in the same
way
> I export, for example, an operator.
> :- arithmetic_function(c/0).
> c(10).
> % eof
> --------
>
> ?- consult('name.pl').
> yes
> ?- X is c.
> ERROR: is/2: Arithmetic: 'c/0' is not a function
>
> So I am unable to use c/0 from the toplevel (or just from a module other
> than 'some').
> What I want is to have the arithmetic function c/0 imported into context
> module, and not the predicate c/1.
> Does a method to achieve this exist, since module export declaration does
> not include arithmetic functions?
> :)
> M
>
> "Jan Wielemaker" <jan@nospam.ct.xs4all.nl> ha scritto nel messaggio
> news:slrne105s3.4b0.jan@ct.lan...
> Prolog.
> this
> it is
associated[color=darkred]
>
>
| |
| Jan Wielemaker 2006-03-09, 6:58 pm |
| On 2006-03-09, Mauro Di Nuzzo <picorna@inwind.it> wrote:
> Just two more issue regarding arithmetic function handling in SWI Prolog.
> In particular, I got the following segmentation fault errors, that I think
> one might avoid to see.
>
> ?- arithmetic_function(c/0).
> yes
> ?- X is c. % *see later
>
> ?- arithmetic_function(c/0).
> yes
> ?- assert(( c(_) :- throw(error(instantiation_error, _)) )). % or some other
> exception
> yes
> ?- X is c. % *see later
>
> Both goal* return this (I marked them with an asterisk):
> ERROR: is/2: Undefined procedure: c/1
> ERROR: is/2: Caught signal 11 (segv)
>
> I do not know, but they seem bugs. Arent they?
I see. Fixed and added to test suite.
Cheers --- Jan
|
|
|
|
|