Page 1 of 1

Saving/Consulting a List of objects

Posted: 5 Sep 2019 19:46
by choibakk
Its been 20+ years since I used Prolog so please forgive if this is a bad question or has already been answered. I've been going through the "VisualPrologBeginners.pdf" and ran into a problem trying to save my class facts. On page 131 (Section 8.6) there is the following snippet which is exactly where I ended up in my project (except I don't use "createdCount", instead I use list::length).

Code: Select all

class facts     createdCount : unsigned := 0     createdObjects : person* := [].
When I tried to save these my output file just showed address pointers for the "createdObjects" rather than the actual data in the (person*) list. I finally just rewrote my program in old-school prolog. But I am curious how one might save the "contents" of an object/s in a list using file::save/2 file::consult/2?

Thanks in advance,

Re: Saving/Consulting a List of objects

Posted: 6 Sep 2019 11:36
by Harrison Pratt

Re: Saving/Consulting a List of objects

Posted: 6 Sep 2019 11:50
by Thomas Linder Puls
Objects (and predicates) cannot be saved and consulted.

Well as you experienced they can be saved, but they cannot be consulted.

There is a technical problem in dealing with cyclic and duplicate references between objects, but that could most likely be handled.

But there is also a conceptual problem. Numbers, string, etc, and list of such things, and lists of lists of such things are all pure values. But objects are "live" data with a current state and an identity. Pure values are the same if they have the same value, but objects are not the same just because they contain the same values.

So while save could save the data that an object contains, consult would not be able to recreate the object, it could at most create a similar copy. If you saved the object in a program and consulted it back again into the same program you would not get the same object, but to individual objects with each their state/identity/live.

For some purposes it could make sense to have more "pure" data object, for which save/consult could make sense. (But it is not one of the first things to come to the language).

Re: Saving/Consulting a List of objects

Posted: 6 Sep 2019 21:21
by choibakk
Thanks for the replies. The responses make perfect sense.