Home > Archive > Prolog > November 2007 > Arrays, functor, and assert/retract
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 |
Arrays, functor, and assert/retract
|
|
| pineapple.link@yahoo.com 2007-09-27, 10:08 pm |
| Apparently, doing something as simple as constructing and using an
array in prolog is a hassle and a pain. However, in an attempt to try
to stick with prolog, vs. throwing it in the garbage and moving on to
another language with less headaches, I have decided to investigate
array construction and manipulation.
Jan says:
<<If you want simple not-too-long arrays of logical variables, you
create
them using functor(Array, Name, Size) and you access using arg(Index,
Array, Element). Works in any Prolog.>>
GNU prolog produces:
<<
| ?- functor(Array, array, 10).
Array = array(_,_,_,_,_,_,_,_,_,_)
yes[color=darkred]
What the heck is going on? Is this an array, or a "fact/predicate/
term?" Is there a difference, or not? If there isn't, why? If there
is, why does it look like there isn't? Is this no different than my
simply placing the following fact at the top of my code?
array(_,_,_,_,_,_,_,_,_,_).
<< If you want to modify elements beside instantiating variables,
things get more hairy. Plain Prolog can only further instantiate data.
This means you can only write an update_array(Index, Element,
OldArray,
NewArray). Naively this means copying the array.>>
So I take it I cannot change the values inside the so-called "array?"
Another question: I assume I could change values in the so-called
"array" (cough) by asserting/retracting a global array. Now, I
understand that many prologgers would frown upon this. However, I
don't care about that, and I don't care whether or not I'd be
violating some logic programming style. However, I do care (at least,
on a certain level) about efficiency. How expensive is it to assert/
retract?
Thanks.
| |
| Cesar Rabak 2007-09-27, 10:08 pm |
| pineapple.link@yahoo.com escreveu:
> Apparently, doing something as simple as constructing and using an
> array in prolog is a hassle and a pain. However, in an attempt to try
> to stick with prolog, vs. throwing it in the garbage and moving on to
> another language with less headaches, I have decided to investigate
> array construction and manipulation.
>
[snipped]
Does your "attempt to try to stick with Prolog" includes getting a good
book and learn the language effectively?
| |
| pineapple.link@yahoo.com 2007-09-27, 10:08 pm |
| > Does your "attempt to try to stick with Prolog" includes getting a good
> book and learn the language effectively?
Will my getting a good book magically add decent array capability to
the language? If it will, then sure - I'll order one tomorrow.
Does anyone else out there want to answer my questions?
Thanks.
| |
| Cesar Rabak 2007-09-28, 4:21 am |
| pineapple.link@yahoo.com escreveu:
>
> Will my getting a good book magically add decent array capability to
> the language? If it will, then sure - I'll order one tomorrow.
Will do better: will help you to think in the Prolog way, otherwise
you'll keep with these wording "decent array capability" etc,. etc.
You'll also should refer to the FAQ, especially question (and the
answer) number five.
>
> Does anyone else out there want to answer my questions?
If you insist in being so humble and polite as you're going 'til now,
we've to confess our enthusiasm will vanish...
>
> Thanks.
>
You're welcome.
| |
| pineapple.link@yahoo.com 2007-09-28, 4:21 am |
| > Will do better: will help you to think in the Prolog way, otherwise
> you'll keep with these wording "decent array capability" etc,. etc.
You are implying that I worded something incorrectly in regards to
prolog's array capability? Then I stand ready to be corrected. In
fact, the questions I asked, if answered, will do a good bit to
ascertain whether I've worded something incorrectly or not. If prolog
indeed does have decent array capability, then that's what I want to
hear, and that's why I made the post. Do you care to answer?
> You'll also should refer to the FAQ, especially question (and the
> answer) number five.
I've read the FAQ many times. From the FAQ:
<<5. I think language X is better than Prolog. What do you think? >>
This does not apply to me, as it has absolutely nothing to do with any
questions I've asked.
| |
| Joachim Schimpf 2007-09-28, 4:21 am |
| pineapple.link@yahoo.com wrote:
>
> Will my getting a good book magically add decent array capability to
> the language?
Maybe it would help you understand the difference between a hammer
and a screwdriver, and stop you from asking why the screwdriver
doesn't have decent nail driving capability.
If you wish to implement an algorithm that is specified in terms
of a sequence of reads and writes to indexed memory locations,
then you should use a language whose abstractions are such things
as arrays of memory locations. Prolog is a tool that provides
different abstractions, which are useful for different purposes.
Continuing to try using a screwdriver as a hammer won't help
you with understanding the purpose of a screwdriver.
-- Joachim
| |
| pineapple.link@yahoo.com 2007-09-28, 4:21 am |
| > If you wish to implement an algorithm that is specified in terms
> of a sequence of reads and writes to indexed memory locations,
> then you should use a language whose abstractions are such things
> as arrays of memory locations. Prolog is a tool that provides
> different abstractions, which are useful for different purposes.
> Continuing to try using a screwdriver as a hammer won't help
> you with understanding the purpose of a screwdriver.
This is sort of an esoteric response, but potentially useful if
clarified. Prolog's abstractions do, or do not, allow for things such
as standard arrays? It apparently allows for a list (or what one
person on this newsgroup calls a "term"). So you are saying a list is
not an abstraction of a sequence of memory locations? You are saying
that a list is metaphysically different than an array? Sure, in
practical aspects, they are different. One can be randomly-accessed
in constant time, and one cannot. But theoretically, they are both
sequences of "boxes" that hold things. Are you saying there is some
material difference between the two - that one is more "prolog-like"
than the other, and provides "prolog abstractions" and the other does
not and cannot?
<<Maybe it would help you understand the difference between a hammer
and a screwdriver, and stop you from asking why the screwdriver
doesn't have decent nail driving capability. >>
Just to clarify, I wasn't asking *WHY* the screwdriver doesn't have
decent nail driving capability. I am (essentially) asking *IF* the
screwdriver or whatever this tool that I am holding in my hand
(prolog) has decent nail driving capability. Jan, who's last name
starts with a W, made a post regarding how I can get and use arrays in
prolog. Since I am trying to evaluate prolog on whether it has any
capabilities I care for, and whether or not its handicaps and "pain in
the ass factor" make it worth the effort or not, I tried out what he
said to do in his post.
Essentially, I am trying to evaluate the "screwdriver," and in doing
so I'm being told to use another tool if that's what I require. Well
I won't know whether to go ahead and throw this tool in the trash or
not until it has been evaluated. If the only data structure I can
work with is something based on the list, and nothing else, I think I
have my answer. If someone says "no, really, there are arrays, and
you can update them, and here's how" I'm listening. I simply don't
care to implement every solution to every problem using a list. If
"logic programming" is totally beholden to the list, and if there is
absolutely no way to do "logic programming" without dropping to my
knees and worshipping the list, I find that hard to believe, but I'll
believe it if the experts here tell me to, and I'll move on.
To help answer one of my questions above, I did an extremely crude and
rough estimate of the performance of assert/retract:
%% 5 operations each, 1 with assert/retract, the other with simple
subtraction
xxx(X) :- X\=0, assert(blah(1)), retract(blah(1)), Y is X-1, xxx(Y).
yyy(X) :- X\=0, A is X-1, B is X-1, Y is X-1, yyy(Y).
19 ?- time(yyy(999999)).
% 4,999,997 inferences, 4.70 CPU in 7.18 seconds (65% CPU, 1064564
Lips)
No
20 ?- time(xxx(999999)).
% 4,999,997 inferences, 5.70 CPU in 6.11 seconds (93% CPU, 877471
Lips)
No
21 ?- time(yyy(999999)).
% 4,999,997 inferences, 5.56 CPU in 7.74 seconds (72% CPU, 899605
Lips)
No
22 ?- time(xxx(999999)).
% 4,999,997 inferences, 5.44 CPU in 7.12 seconds (76% CPU, 919486
Lips)
No
23 ?-
It appears from this rough and crude estimate that assert/retract is
not materially more expensive than simple operations like addition and
subtraction (at least with SWI-Prolog). If one of the compiler gurus
here can confirm this for me, I believe that I could essentially
implement an "array" (cough) by simply asserting/retracting the array
to simulate updating its contents, and not have to worry about some
extreme penalty for doing so.
Thanks.
| |
| Chui Tey 2007-09-28, 4:21 am |
| I'm new to prolog too. I find it useful to probe whether a particular
idiom is "prologish" rather than trying to shoehorn my expectations of
other programming languages into prolog. That said, I might frame a
problem-solution that I'd express in another language and try to learn
how Prolog handles this. I'm sure you can use assert and retract to
write a very non-declarative program, but if that's the case, you've
gained nothing by using prolog.
On the other hand, there may be mitigating factors, like: your place
of work *requires* you to develop in Prolog. There was a tale on the
net that there was this guy who, in order to keep his job, had to
program in Java, something that he wasn't prepared to do. So he wrote
a preprocessor that converted his new language into Java.
Why not just share with us all the size and magnitude of your problem
(e.g. large data set, has to be solved quickly) and let the experts
suggest how they'd go about doing this?
| |
| Jan Wielemaker 2007-09-28, 4:21 am |
| On 2007-09-28, pineapple.link@yahoo.com <pineapple.link@yahoo.com> wrote:
> %% 5 operations each, 1 with assert/retract, the other with simple
> subtraction
> xxx(X) :- X\=0, assert(blah(1)), retract(blah(1)), Y is X-1, xxx(Y).
> yyy(X) :- X\=0, A is X-1, B is X-1, Y is X-1, yyy(Y).
>
> 19 ?- time(yyy(999999)).
> % 4,999,997 inferences, 4.70 CPU in 7.18 seconds (65% CPU, 1064564
> Lips)
>
> No
> 20 ?- time(xxx(999999)).
> % 4,999,997 inferences, 5.70 CPU in 6.11 seconds (93% CPU, 877471
> Lips)
SWI-Prolog is very slow in arithmetic and very fast in assert/retract.
Using the -O option it will compile arithmetic a bit better and the
difference goes up to almost a factor 2. Performance however isn't the
biggest reason not to program this way. Using assert/restract you have
no backtracking and therefore no sensible search anymore. As it uses
global names you polute the global namespace. The code is not safe
against timeouts, interrupts, etc., neither multiple threads. The
code is hard to debug as the classical way of debugging where you
skip over calls until you do not like the result, do `retry' to get
back to the start and repeat the same procedure at the next level.
With side-effects (assert/retract), the `retry' can no longer undo
the changes.
--- Jan
| |
|
| On Thu, 27 Sep 2007 19:18:45 -0700, pineapple.link@yahoo.com wrote:
>
>Will my getting a good book magically add decent array capability to
>the language? If it will, then sure - I'll order one tomorrow.
>
>Does anyone else out there want to answer my questions?
>
Yes, agree. Whatever doestn't fit to iPod is not worth attention
A.L.
| |
|
| On Thu, 27 Sep 2007 17:47:16 -0700, pineapple.link@yahoo.com wrote:
>Apparently, doing something as simple as constructing and using an
>array in prolog is a hassle and a pain.
Yes. Reinventing light bulb is painfull.
Go to google and search for Prolog array.
A.L.
| |
| pineapple.link@yahoo.com 2007-09-28, 7:10 pm |
| > Yes, agree. Whatever doestn't fit to iPod is not worth attention
I'm not ipod generation. I'm roughly the same age as you, and
graduated college roughly the same time. You've got me pegged wrong,
for some reason.
| |
| pineapple.link@yahoo.com 2007-09-28, 7:10 pm |
| All extremely helpful, Jan. It doesn't seem that I will be able to
use Prolog the way I want to use it (without lists, for one thing).
Thanks again.
> SWI-Prolog is very slow in arithmetic and very fast in assert/retract.
> Using the -O option it will compile arithmetic a bit better and the
> difference goes up to almost a factor 2. Performance however isn't the
> biggest reason not to program this way. Using assert/restract you have
> no backtracking and therefore no sensible search anymore. As it uses
> global names you polute the global namespace. The code is not safe
> against timeouts, interrupts, etc., neither multiple threads. The
> code is hard to debug as the classical way of debugging where you
> skip over calls until you do not like the result, do `retry' to get
> back to the start and repeat the same procedure at the next level.
> With side-effects (assert/retract), the `retry' can no longer undo
> the changes.
>
> --- Jan
| |
| student 2007-09-28, 7:10 pm |
| pineapple.link@yahoo.com wrote:
>
> Will my getting a good book magically add decent array capability to
> the language? If it will, then sure - I'll order one tomorrow.
>
> Does anyone else out there want to answer my questions?
>
> Thanks.
>
What do you mean by "decent array capability"?
If you express the answer to /that/ question in Prolog, you will have
your answer, but whereas the difficulty here seems to me to be that you
are unsure as to how to proceed to begin to formulate a provisional
answer to that question in Prolog, please allow me to offer the
following general comments.
Description, use, and mention
-----------------------------
We /use/ a representation when we /mention/ the entity that the
representation represents.
Suppose I am a veterinarian and that in my office I have a row of
cages, each of which is either empty or contains exactly one cat or dog.
If I want to use Prolog to "program" my office, I will have to settle
on some unambiguous way of /representing/ those cages (in Prolog).
However, inventing unambiguous representations in no easier to do in
Prolog than it is to do in English (say), so you must be prepared to
patiently work your way through many laborious revisions ("debugging")
when you are doing it in Prolog.
In Prolog, we usually represent the state of an entity or system as
<identifier> "(" <term>, <term>, ... <term> ")"
so that, for example, a predicate that described a change of state of a
graphics screen in terms of its representations might look something like
transition( increase_width(Index,Amount),
window(Index,Height,OldWidth,Script),
window(Index,Height,NewWidth,Script)) :-
NewWidth is OldWidth + Amount.
Accordingly, a predicate that implemented Fortran statements of the form
A(I) = B(J)
might look something like
array_handler( replace(AI,BJ),
Curr_A_Descriptor,
Curr_B_Descriptor,
New_A_Descriptor ) :-
array_handler( fetch(BJ),
Curr_B_Descriptor, B_Value),
array_handler( store(AI),
Curr_A_Descriptor, B_Value,
New_A_Descriptor).
etc.
Therefore, if by an "array" you have in mind the sort of structure
that in Fortran would be specified by means of a DIMENSION statement,
you will either have to agree with yourself as to how you want to
represent those structures in Prolog, and then define the predicates
that you require to operate on those representations, or you will have
to make do with whatever you can find on the web or in a book.
BillH
--
| |
| Cesar Rabak 2007-09-28, 7:10 pm |
| pineapple.link@yahoo.com escreveu:
>
> You are implying that I worded something incorrectly in regards to
> prolog's array capability? Then I stand ready to be corrected. In
> fact, the questions I asked, if answered, will do a good bit to
> ascertain whether I've worded something incorrectly or not. If prolog
> indeed does have decent array capability, then that's what I want to
> hear, and that's why I made the post. Do you care to answer?
>
You've been already informed about Prolog structures, and 'arrays' have
been explained in the context of Prolog.
Also, we already pointed out you should rethink the analysis of the problem.
Now...
>
> I've read the FAQ many times. From the FAQ:
>
> <<5. I think language X is better than Prolog. What do you think? >>
>
> This does not apply to me, as it has absolutely nothing to do with any
> questions I've asked.
>
....it applies to you, because you insist in using a construct from other
languages, which in essence is the same as FAQ question # 5.
Do you want to solve a (programming) problem, or do you want to make a
point on array handling in Prolog?
| |
| Cesar Rabak 2007-09-28, 7:10 pm |
| pineapple.link@yahoo.com escreveu:
[snipped]
>
> To help answer one of my questions above, I did an extremely crude and
> rough estimate of the performance of assert/retract:
>
[snipped]
Still, you fail to understand our point. First of all, instead of
looking for synthetic tests to 'prove' or 'disprove' if arrays in Prolog
are good enough, why don't you tell us the real problem you need to solve?
It may be not necessary all this contortions. . .
| |
|
| On Fri, 28 Sep 2007 09:08:40 -0700, pineapple.link@yahoo.com wrote:
>
>I'm not ipod generation. I'm roughly the same age as you, and
>graduated college roughly the same time. You've got me pegged wrong,
>for some reason.
This is not the issue of age, thsiis the issue of attitude. That
follows from your response about reading books.
A.L.
| |
| pineapple.link@yahoo.com 2007-09-28, 10:09 pm |
| > This is not the issue of age, thsiis the issue of attitude. That
> follows from your response about reading books.
You don't know a damn thing about my attitude, nor do you know
anything about my philosophy on or history of reading books. I was
reading on the 12th grade level when I was in 2nd grade. I've read
more books than anyone I know. You should be more careful about
spouting off about something you know nothing about.
| |
| pineapple.link@yahoo.com 2007-09-28, 10:09 pm |
| > ...it applies to you, because you insist in using a construct from other
> languages
I was told by Jan W., and also another person on another thread, that
arrays exist in prolog. In investigating this array capability, and
asking questions about it, you accuse me of "using a construct from
another language," and thus accuse me of violating the FAQ. Since you
have little useful to say to me, and since I have little useful to say
to you, why continue to post on this thread? I give you permission to
move on.
<< which in essence is the same as FAQ question # 5.>>
Asking questions about a capability that a prolog expert told me
existed isn't anything close to saying "language X is better than
prolog." Work on your reading/comprehension skills. Who made you the
FAQ police anyway?
> Do you want to solve a (programming) problem, or do you want to make a
> point on array handling in Prolog?
Can you read? Not one to sit around lazily waiting for someone to
give me everything on a silver platter, I was trying to answer one of
my questions about assert/retract. Jan W. ended up giving me useful
info on the question. You have given no useful info on anything.
Have a nice day.
| |
| Cesar Rabak 2007-09-28, 10:09 pm |
| pineapple.link@yahoo.com escreveu:
>
> I was told by Jan W., and also another person on another thread, that
> arrays exist in prolog.
For a poster that wrote "I've read more books than anyone I know.", you
show two characteristics that counter your claim: you seem to not
understand what has been responded to you, or you see things were not
writen.
> In investigating this array capability, and
> asking questions about it, you accuse me of "using a construct from
> another language," and thus accuse me of violating the FAQ.
The FAQ cannot be violated in any English sense. . . besides, I'm not
"accusing" anyone. I'm just demonstrating a behavior.
> Since you
> have little useful to say to me, and since I have little useful to say
> to you, why continue to post on this thread? I give you permission to
> move on.
>
When you post in a public newsgroup as c.l.prolog, you're subject to all
comments all persons post in it.
_I_ post to the group, because it is considered rude to comment a post
in the third person, I address your posts in the second person.
If you find this unacceptable, hire private consulting services or
better, find a Prolog course offering.
I don't need your permission nor to stay nor to move on.
> << which in essence is the same as FAQ question # 5.>>
>
> Asking questions about a capability that a prolog expert told me
> existed isn't anything close to saying "language X is better than
> prolog." Work on your reading/comprehension skills. Who made you the
> FAQ police anyway?
>
>
> Can you read?
Sure! I also I'm able to interpret what I read, and give back my
impressions and opinions.
> Not one to sit around lazily waiting for someone to
> give me everything on a silver platter, I was trying to answer one of
> my questions about assert/retract.
I found funny your comments about your 'test' in GNU Prolog just after
Jan made a suggestion.
> Jan W. ended up giving me useful
> info on the question. You have given no useful info on anything.
The useful information I leave on the thread is: there are means of
solving problems in Prolog which include some data structures, you may
find less need to use assert/retract (or even the array as a data
structure) after you master logic programming.
As others pointed in this thread, it may be some boundary cases where a
less pure Prolog solution may be needed, but since until now you decided
not sharing the real problem you're after, and rather stick to the array
structure we cannot be of further help...
>
> Have a nice day.
>
You too!
| |
|
| On Fri, 28 Sep 2007 17:23:53 -0700, pineapple.link@yahoo.com wrote:
>
>You don't know a damn thing about my attitude, nor do you know
>anything about my philosophy on or history of reading books. I was
>reading on the 12th grade level when I was in 2nd grade. I've read
>more books than anyone I know. You should be more careful about
>spouting off about something you know nothing about.
I judge you by your posts.
A.L.
| |
| pineapple.link@yahoo.com 2007-09-29, 7:07 pm |
| > I judge you by your posts.
>
> A.L.
And I judge you by yours.
| |
|
|
| Nick Wedd 2007-11-19, 10:10 pm |
| In message <1190940436.628463.172960@n39g2000hsh.googlegroups.com>,
pineapple.link@yahoo.com writes
>Apparently, doing something as simple as constructing and using an
>array in prolog is a hassle and a pain.
It's certainly not what Prolog is good at. If a project involved much
work with arrays, I would not use Prolog for it.
> However, in an attempt to try
>to stick with prolog, vs. throwing it in the garbage and moving on to
>another language with less headaches, I have decided to investigate
>array construction and manipulation.
>
>Jan says:
>
><<If you want simple not-too-long arrays of logical variables, you
>create
>them using functor(Array, Name, Size) and you access using arg(Index,
>Array, Element). Works in any Prolog.>>
>
>GNU prolog produces:
>
><<
>| ?- functor(Array, array, 10).
>
>Array = array(_,_,_,_,_,_,_,_,_,_)
>
>yes
>
>What the heck is going on? Is this an array, or a "fact/predicate/
>term?" Is there a difference, or not? If there isn't, why? If there
>is, why does it look like there isn't?
This is a term. If you assert it (which wouldn't be a sensible thing to
do), it also becomes a fact/predicate. If you don't assert it, but
create it and pass it from predicate to predicate in the argument list,
you can use it to hold an array.
> Is this no different than my
>simply placing the following fact at the top of my code?
>
>array(_,_,_,_,_,_,_,_,_,_).
Doing that would cause it to be asserted. That does not look like a
sensible thing to do.
><< If you want to modify elements beside instantiating variables,
>things get more hairy. Plain Prolog can only further instantiate data.
>This means you can only write an update_array(Index, Element,
>OldArray,
>NewArray). Naively this means copying the array.>>
>
>So I take it I cannot change the values inside the so-called "array?"
If you assert it, then indeed you can't change the values in it. But if
you create it and pass it around, it can start life being
array(_,_,_,_,_,_,_,_,_,_), then its third argument might get
instantiated to 19 so that it becomes array(_,_,19,_,_,_,_,_,_,_). Now
you can't change the third argument at all, but you can still
instantiate the other arguments.
>Another question: I assume I could change values in the so-called
>"array" (cough) by asserting/retracting a global array.
Assuming you had asserted it: you could, yes.
> Now, I
>understand that many prologgers would frown upon this. However, I
>don't care about that, and I don't care whether or not I'd be
>violating some logic programming style. However, I do care (at least,
>on a certain level) about efficiency. How expensive is it to assert/
>retract?
Generally less efficient than many of the other things Prolog does. How
much less efficient would depend on the implementation.
If you really want to keep your array "on the heap", that is, as a
fact/predicate, you might instead consider asserting
a(1,_). a(2,_). a(3,_). a(4,_). a(5,_). a(6,_). a(7,_).
a(8,_). a(9,_). a(10,_).
Then you could set the third element to 19 by
retract(a(3,_)), assert(a(3,19)).
and later change it to 0 by
retract(a(3,_)), assert(a(3,0)).
Nick
--
Nick Wedd nick@maproom.co.uk
|
|
|
|
|