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

Converting types to predicates for rule matching constraints #630

Open
jpfairbanks opened this issue Aug 5, 2024 · 1 comment
Open

Converting types to predicates for rule matching constraints #630

jpfairbanks opened this issue Aug 5, 2024 · 1 comment

Comments

@jpfairbanks
Copy link
Contributor

In trying to solve #629 with rewriting I came up with some rules:

abstract type Form <: Number end
struct Form0 <: Form end
struct Form1 <: Form end
struct Form2 <: Form end

d(::Form)::Form
d₀(::Form0)::Form1
# these don't work because Form0/Form1 are types and not predicates that check for types
@rule d(~x::Form0) => d₀(~x)
@rule d(~x::Form1) => d₁(~x)

# these functions fix it because we can match on predicates
form0p(x) = symtype(x) == Form0
form1p(x) = symtype(x) == Form1

@rule d(~x::form0p) => d₀(~x)
@rule d(~x::form1p) => d₁(~x)

Is there a way that we could hook into @rule so that if you tried to match on a type it automatically lifts that type to a predicate?

@jpfairbanks
Copy link
Contributor Author

A workaround that I found was to use the rule level predicates and an auxiliary function hastype. My macro for specifying these uses the following to insert a call to hastype which checks the symbolic type.

hastype(x, T) = symtype(x) == T
@rule $op(~x) => (hastype(~x,$src_type) ? $resolveto(~x) : nothing)

So the rules end up looking like

d(~x) => if hastype(~x, Form0)
        d₀(~x)
    else
        nothing
    end

but I'm not writing that verbosity because of an intermediate macro.

Just putting d(~x::Form0) in the LHS would be much more concise.

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

1 participant