Skip to content

Exposing Symbols in the API #549

Open
@SamirDroubi

Description

@SamirDroubi

Currently, cursors that access variables implement a name(self) -> str function which return the name attribute of the Sym node. However, Syms don't just represent a string name, it really represents a pair of (name, id). This means that there is currently no way in user-code to (unambiguously) check if two symbols are the same. We need to expose the id information somehow to make sure we can ask questions about symbol equalities, build environments in a way that is unambiguous.

What we will want out of this API:

  1. Be able to do comparisons on two symbols.
  2. Be able to build environments out of symbols: it needs to be hashable.
  3. Be able to introspect it is components: the name and the id

There are a few design choices here:

  1. Should we just let the cursors return a free object (Symbol) or make it a navigation to another cursor SymbolCursor.
  2. How can we support the functionalities above with either of those APIs.

Symbol:

class Symbol:
     _impl: Sym
     
     # wrappers around all the functions Sym implements (except for copy()): https://github.com/exo-lang/exo/blob/59ed05f2aa6ec5f76db208889a7539543adedc6f/src/exo/prelude.py#L21

SymbolCursor:
It is a bit confusing to directly implement things like __hash__ or __eq__ on a cursor when we haven't implemented for any other cursor. The meaning of equality is also ambiguous between location (cursor path) and the node. One option is to have the cursor return the Symbol object above.

class SymbolCursor:
     _impl: Sym
     
     def get_symbol(self) -> Symbol

The alternative:

class SymbolCursor:
     _impl: Sym
     
     def name() -> str
     def id() -> int
     def get_pair() -> Tuple(str, int)
     # This tuple can be used for comparison and be hashable. 

I don't think there is really a reason to be able to point to a Symbol. So, I think the free object design is what makes more sense.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C: APIsThe API exposed by the languageS: Needs DiscussionThis needs discussion to decide if important to work

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions