Re: Arrays, functor, and assert/retract



In message <1190940436.628463.172960@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>, pineapple.link@xxxxxxxxx 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@xxxxxxxxxxxxx
.



Relevant Pages

  • Re: Revenge is a dish best served cold (arrays and 8 queens)
    ... In a Turing complete programming language like Prolog, saying a thing exists or not is non sense. ... If we put this constraint in the Array definition then you're right Turing completeness is not enough and you now have a 'structural' requirement. ... If we use the Array as a data abstraction, an array of data accessible via an index then the Prolog alternatives can emulate this data structure. ... It remains to be seen which problem cannot be solved in Prolog without this specific data structure out the cases of interfacing to other languages or accessing certain API, ...
    (comp.lang.prolog)
  • Arrays, functor, and assert/retract
    ... doing something as simple as constructing and using an ... array in prolog is a hassle and a pain. ... don't care about that, and I don't care whether or not I'd be ...
    (comp.lang.prolog)
  • Re: Arrays, functor, and assert/retract
    ... another language," and thus accuse me of violating the FAQ. ... If you find this unacceptable, hire private consulting services or better, find a Prolog course offering. ... Asking questions about a capability that a prolog expert told me ... 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. ...
    (comp.lang.prolog)
  • Re: Arrays, functor, and assert/retract
    ... another language," and thus accuse me of violating the FAQ. ... Asking questions about a capability that a prolog expert told me ... point on array handling in Prolog? ...
    (comp.lang.prolog)
  • Re: nonhomogenous structs (was: lisp performance questions and observations)
    ... ints ... naturally represented as an array of structs, ... So actually, I don't care about the mixture between bytes, longs, ... machine words for each struct before I even get to the "data" for the ...
    (comp.lang.lisp)