Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attributed variables: High-level considerations #3

Closed
triska opened this issue May 10, 2017 · 8 comments
Closed

Attributed variables: High-level considerations #3

triska opened this issue May 10, 2017 · 8 comments

Comments

@triska
Copy link
Contributor

triska commented May 10, 2017

I would like to contribute some of the experience I gained with attributed variables, usable at your discretion in later stages of this project. I am mentioning this now, at a rather high level, because adding these provisions later may turn out to be very complex. I will, at your request or initiative, gladly discuss any of these aspects in their own separate issues.

At the surface, adding attributes to variables does not seem hard. However, once you look a bit more into it, there are several tough issues involved. I start with what proved to be the toughest part in SWI-Prolog, and is still unresolved: There should be a predicate like verify_attributes/3. Please see SWI-Prolog/roadmap#14 for the justification. This deserves its own issue, and so I only bring it briefly to your attention here, for future consideration.

Next, please take a look at Precise Garbage Collection in Prolog by Neumerkel et al.. This paper is critical to understand how an efficient implementation of library(pio) is possible, reclaiming stack space as soon as possible and thus lazily parsing a file with low memory overhead.

Finally, a critical predicate when working with attributed variables is call_residue_vars/2. Briefly, it lets you reason about all variables whose attributes were modified during the execution of a goal. This facility is of critical importance when reasoning about constraint logic programs. I only briefly touch on this essential topic here by showing the following example:

inconsistent :-
        X #> Y,
        Y #< X.

With this sample program, we have:

?- inconsistent.
true.

This although declaratively, the goal cannot hold! To reason about these constraints, we can do:

?- call_residue_vars(inconsistent, Vs).
Vs = [_3162, _3168],
_3162#=<_3168+ -1,
_3168#=<_3162+ -1.

This exposes the variables that are still involved in constraints whose satisfiability is not yet determined.

Overall, once you implement setting/getting attributes and a hook like verify_attributes/3, the relevant constraints like dif/2 and also CLP(FD) can be implemented in Prolog itself.

@mthom
Copy link
Owner

mthom commented May 23, 2017

Is there good source material on the implementation of attributed variables? Holzbaur doesn't appear to be what I'm looking for. I found a paper on the XSB system by Warren that contains some details, but its focus is removed from the basics.

@triska
Copy link
Contributor Author

triska commented May 28, 2017

One standard reference is:

Please consider that as a useful starting point. Beware though! The following publication (Section 4.7) explains that this interface is insufficient in general:

A good implementation of attributed variables must first and foremost enable the required generality of its interface predicates. This may require a completely different approach!

@mthom
Copy link
Owner

mthom commented May 28, 2017

It's a start. Thanks Markus.

@mthom
Copy link
Owner

mthom commented Aug 19, 2017

OK, I've read the papers, SICStus documentation, and most of the SWI Prolog issue thread. Nevertheless, I've concluded that implementing verify_attributes/3 is indeed not all that hard, so there are probably some subtleties I am missing. Would you mind walking me through them?

@triska
Copy link
Contributor Author

triska commented Aug 19, 2017

It is also my impression that implementing verify_attributes/3 should not be very hard if you know what you are doing.

In SWI-Prolog, interest for constraint-related functionality is exceptionally low (for a Prolog system), so I think the reason this is not implemented is mostly that nobody has seriously looked into it. Also, there are only two or three people working on constraint-related features in SWI-Prolog that are even aware of the need for this feature.

In general, there should be no major stumbling blocks. Any existing binding must be undone, the hook must run, and then the scheduled goals must be invoked.

It may be useful to consider a concrete program that uses verify_attributes/3 in non-trivial ways. For example, here is a port of library(clpb) in such a way that it works with the SICStus interface:

https://www.metalevel.at/clpb/clpb.pl

Once you manage to run this program in your system, it will also give you a free CLP(B) system. Only a few libraries are needed to make this work.

Please let me know any time if you have any questions about this!

mthom added a commit that referenced this issue Sep 13, 2018
@mthom
Copy link
Owner

mthom commented Mar 12, 2019

What does the :- initialization(...). directive in SICSTUS do? My guess is that it runs the contained code every time the module is loaded.

@triska
Copy link
Contributor Author

triska commented Mar 12, 2019

Yes, exactly! initialization/1 is a standard directive:

7.4.2.6 initialization/1

A directive initialization(T) converts the term T to
a goal G and includes it in a set of goals which shall
be executed immediately after the Prolog text has been
prepared for execution. The order in which any such goals
will be executed shall be implementation defined.

Note that it does not matter where the directive is placed in the file! It is always loaded after the (entire) file "has been prepared for execution".

triska referenced this issue in tau-prolog/www.tau-prolog.org Nov 19, 2019
mthom pushed a commit that referenced this issue Apr 20, 2020
@triska
Copy link
Contributor Author

triska commented May 7, 2020

Attributed variables work very well so far!

Thank you a lot, this is a very nice achievement!

@triska triska closed this as completed May 7, 2020
jjtolton pushed a commit to jjtolton/scryer-prolog that referenced this issue Aug 11, 2024
# This is the 1st commit message:

revert changes

# This is the commit message mthom#2:

Implement run_query() in terms of run_query_iter()

# This is the commit message mthom#3:

Drop implementation for QueryState

Fix not backtracking on query with no variables

Ignore new tests to make Miri happy

Remove TODO comment about QueryState::drop()

working generator

finish query generator

make machine_new() return machine, change fns to use machine refs

eliminate comment

add cbindgen
jjtolton pushed a commit to jjtolton/scryer-prolog that referenced this issue Aug 11, 2024
# This is the 1st commit message:

revert changes

# This is the commit message mthom#2:

Implement run_query() in terms of run_query_iter()

# This is the commit message mthom#3:

Drop implementation for QueryState

Fix not backtracking on query with no variables

Ignore new tests to make Miri happy

Remove TODO comment about QueryState::drop()

working generator

finish query generator

make machine_new() return machine, change fns to use machine refs

eliminate comment

add cbindgen

make cbindgen header generation location relative to manifest_dir

make cbindgen header generation location relative to manifest_dir

Update Cargo.toml

Co-authored-by: Bennet Bleßmann <bennet.blessmann+github@googlemail.com>

cargo fmt

fix docs

mthom#2465 (comment)

rename functions

mthom#2465 (comment)
mthom#2465 (comment)
mthom#2465 (comment)

fix docs

mthom#2465 (comment)
mthom#2465 (comment)
https://github.com/mthom/scryer-prolog/pull/2465/files#r1712066209

cargo fmt, various docstring Changes

mthom#2465 (comment)
mthom#2465 (comment)
mthom#2465 (comment)
mthom#2465 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants