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

Add immutable funsors Dict, Set and use them for funsor data #525

Open
4 tasks
fritzo opened this issue Apr 12, 2021 · 2 comments
Open
4 tasks

Add immutable funsors Dict, Set and use them for funsor data #525

fritzo opened this issue Apr 12, 2021 · 2 comments

Comments

@fritzo
Copy link
Member

fritzo commented Apr 12, 2021

Alternative to #526

The problem

Funsors currently store data in immutable structures that are typically either nested tuples or frozensets, e.g. Subs converts its dict argument to a nested tuple-of-tuples representing key,value pairs. These internal data structures complicate funsor traversal algorithms (e.g. reinterpret, ast(), get_children()), since the intermediate non-Funsor data structures lack metadata such as .inputs, .output, .fresh, .bound. This complication manifests as extra checks for isinstance(-, Funsor) and extra handling to manually traverse funsor data asts.

Proposed solution

This issue proposes to replace all internal data structure of funsors by additional low-level Pythonic funsors Tuple, Dict, and Set. Like other more mathy funsors, these data structural funsors implement metadata like .inputs, .output, substitution via .__call__() and reinterpreted construction and hash consing via type(-).__call__().

For example Subs.subs : Dict, Delta.terms : Dict, etc.

Remaining questions

  1. Should we also implement Funsor subclasses for constants Str as used as keys? Ideally all terms would be immutable and hash-consed, however strs are used so ubiquitously as keys that it may not be worth the performance overhead. One option would be to instead rely on Python's built-in hash-consing of strings via sys.intern() and move metadata elsewhere, as in Support Python immutable datatypes without subclassing Funsor #526

Tasks

  • Implement an immutable Dict funsor
  • Implement an immutable Set funsor
  • Use Dict and Set internally in other Funsors
  • Simplify funsor.interpreter
@ordabayevy
Copy link
Member

@fritzo is this still relevant? I would like to try to implement Dict and Set funsors.

@fritzo
Copy link
Member Author

fritzo commented Oct 27, 2021

I'm unsure whether this is still relevant, and I'd be fine closing this. I haven't minded the singledispatch solutions to special casing. I'm also concerned that being more abstract will lead to higher overhead.

@eb8680 WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants