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

Custom types (RFC) #4

Open
skrah opened this issue Mar 30, 2018 · 4 comments
Open

Custom types (RFC) #4

skrah opened this issue Mar 30, 2018 · 4 comments

Comments

@skrah
Copy link
Member

skrah commented Mar 30, 2018

There have been various questions about custom types. This is the current state and an opportunity for concrete feedback.

Custom types are created as follows, an example is the graph custom type:

  1. You need to know at least the abstract typedef that describes the underlying memory. Here we define a weighted graph as an adjacency list. We use a typedef to declare a new custom type:

https://github.com/plures/gumath/blob/daa7bb94730efe163e6129de5998a28d016e538d/libgumath/extending/graph.c#L346

  1. To validate graph invariants, the constraint function graph_constraint is automatically called upon initialization.

  2. Functions that operate on the graph obviously still need to be written in C.

  3. We define a Python class that inherits from xnd, with type checked initialization of values:

https://github.com/plures/gumath/blob/daa7bb94730efe163e6129de5998a28d016e538d/python/extending.py#L11

  1. We use this xnd subclass just like any other Python type:

https://github.com/plures/gumath/blob/daa7bb94730efe163e6129de5998a28d016e538d/python/test_gumath.py#L241

  1. If desired, type hints could look like this:
def f(g : Graph):
    pass
@skrah
Copy link
Member Author

skrah commented Mar 30, 2018

Naturally, the above is syntax sugar and dispatching on types can still look like this:

def f(x):
    if ndt("graph").match(x.type):
        do_this()
    elif ndt("2 * 2 * int64").match(x.type):
        do_that()
    ...

@sklam
Copy link

sklam commented Apr 3, 2018

For numba usecases, we will need to construct types dynamically. I suppose we can invoke ndt_typedef_from_string directly.

@skrah
Copy link
Member Author

skrah commented Apr 3, 2018

Yes, all that matters is that the typedef exists in the typedef table before ndt_nominal() is called.

@dhirschfeld
Copy link

A dual number type would be very interesting for forward mode AD!

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

3 participants