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

Inspect kanren states generated by walko #55

Open
brandonwillard opened this issue Jul 28, 2022 · 0 comments
Open

Inspect kanren states generated by walko #55

brandonwillard opened this issue Jul 28, 2022 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed important performance question Further information is requested term rewriting

Comments

@brandonwillard
Copy link
Member

brandonwillard commented Jul 28, 2022

Looking at the walko example in aesara-devs/aesara#1082, I'm starting to wonder if some of those entries in the state are necessary or not. In particular,

from kanren import eq, var
from kanren.core import lall
from kanren.graph import walko


# A simple tuple-based graph that's easier to investigate than an Aesara graph
graph = (1, (2, (4, 5)))

# A logic variable representing the "output" of the relation that follows
res_lv = var()
res_lv
# ~_139

# Apply the goal `eq` to each matching element in the graphs between `graph`
# and `res_lv`.
# This goal implements a relation like `apply(eq, graph) == res_lv`.
goal = walko(eq, graph, res_lv)

initial_state = {}
stream = lall(goal)(initial_state)

# First node
next_state = next(stream)
next_state
# {~_139: (1, (2, (4, 5)))}

next_state = next(stream)
next_state
# {~_146: 1,
#  ~_147: ((2, (4, 5)),),
#  ~_139: ConsPair(~_144, ~_145),
#  ~_144: 1,
#  ~_158: (2, (4, 5)),
#  ~_159: (),
#  ~_145: ConsPair(~_156, ~_157),
#  ~_156: (2, (4, 5)),
#  ~_157: ()}

# After a few more iterations, we get the following:
next_state = next(stream)
next_state
# {~_146: 1,
#  ~_147: ((2, (4, 5)),),
#  ~_139: ConsPair(~_144, ~_145),
#  ~_144: 1,
#  ~_174: (2, (4, 5)),
#  ~_175: (),
#  ~_145: ConsPair(~_172, ~_173),
#  ~_182: 2,
#  ~_183: ((4, 5),),
#  ~_172: ConsPair(~_180, ~_181),
#  ~_180: 2,
#  ~_206: (4, 5),
#  ~_207: (),
#  ~_181: ConsPair(~_204, ~_205),
#  ~_204: (4, 5),
#  ~_205: (),
#  ~_173: ()}

For instance, there are two logic variables mapped to 1, and only one of them (i.e. ~_144) is referenced elsewhere in the states that are generated. The gives the impression that ~_146 could be removed or avoided altogether.

We should take a closer look at what walko is doing and see if there are "temporary/intermediate" unifications that can be trimmed from the state (or removed altogether), because we don't want graph walking to generate unnecessarily large state dicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed important performance question Further information is requested term rewriting
Projects
None yet
Development

No branches or pull requests

1 participant