Skip to content

Checking for existence of inverse #13

@oschulz

Description

@oschulz

I have a use case that needs to determine if a function has an inverse or not in a type-stable fashion (try/catch won't do). I think such a functionality would be useful in general.

I see two options:

a) Adding a has_inverse(f) function that returns a Singleton. This would be a breaking change, functions that support inverse would now be required to support has_inverse as well.

b) A default implementation inverse(f) = nothing (or missing?). This would be simpler (especially if we add left_inverse and right_inverse as well later on) and non-breaking. On the other hand, code that depends on functions having an inverse would fail in a less obvious way, the root cause would be harder to track (users would get a MethodError: objects of type Missing are not callable instead of a MethodError: no method matching inverse(::typeof(f))). On the other hand, code that uses inverses could check if the inverse is not nothing to ensure the exception points to the right place, e.g via something(inverse(f)).

An example for approach b) would be ChainRulesCore (rrule(f, args...) returns nothing by default).

@devmotion what do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions