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

Use structs for inference and overloading rules #53

Open
GeorgeR227 opened this issue Jun 25, 2024 · 3 comments
Open

Use structs for inference and overloading rules #53

GeorgeR227 opened this issue Jun 25, 2024 · 3 comments

Comments

@GeorgeR227
Copy link
Contributor

Currently we use named tuples to hold our rules for type inference and overloading but that means we have massive type signatures in each of our functions. We should instead just use structs to hold this same information.

Instead of something like op2_rules::Vector{NamedTuple{(:proj1_type, :proj2_type, :res_type, :resolved_name, :op), NTuple{5, Symbol}}} we could just have op2_rules::Vector{Op2Rule}, holding the same information.

We might even be able to tie groups of rules together, like type rules, with an abstract type that allows us to dispatch and choose the correct type inference application function.

@jpfairbanks
Copy link
Member

Yes that makes sense to me. If you have multiple functions that take namedtuples with the same signature, then make it a struct. It should be exactly the same performance-wise.

@jpfairbanks
Copy link
Member

I wrote this on my fork of SymbolicUtils.

abstract type AbstractRule end
abstract type InferenceRule end

struct InferenceRule1 <: InferenceRule
  src_type
  tgt_type
  op_names
end
InferenceRule1(t) = InferenceRule1(t...)

struct InferenceRule2 <: InferenceRule
  proj1_type
  proj2_type
  res_type
  op_names
end
InferenceRule2(t) = InferenceRule2(t...)

struct ResolutionRule <: AbstractRule
  src_type
  tgt_type
  resolved_name
  op
end
ResolutionRule(t) = ResolutionRule(t...)

which lets you do:

"""
These are the default rules used to do type inference in the 1D exterior calculus.
"""
op1_inf_rules_1D = InferenceRule1.([
  # Rules for ∂ₜ 
  (:Form0, :Form0, [:∂ₜ,:dt]),
  (:Form1, :Form1, [:∂ₜ,:dt]),

  # Rules for d
  (:Form0, :Form1, [:d, :d₀]),
  (:DualForm0, :DualForm1, [:d, :dual_d₀, :d̃₀]),

  # Rules for ⋆
  (:Form0, :DualForm1, [:, :₀, :star]),
  (:Form1, :DualForm0, [:, :₁, :star]),
  (:DualForm1, :Form0, [:, :₀⁻¹, :star_inv]),
  (:DualForm0, :Form1, [:, :₁⁻¹, :star_inv]),

  # Rules for Δ
  (:Form0, :Form0, [, :Δ₀, :lapl]),
  (:Form1, :Form1, [, :Δ₁, :lapl]),

  # Rules for δ
  (:Form1, :Form0, [, :δ₁, :codif]),

  # Rules for negation
  (:Form0, :Form0, [:neg, :(-)]),
  (:Form1, :Form1, [:neg, :(-)]),
  
  # Rules for the averaging operator
  (:Form0, :Form1, [:avg₀₁, :avg_01]),
  
  # Rules for magnitude/ norm
  (:Form0, :Form0, [:mag, :norm]),
  (:Form1, :Form1, [:mag, :norm])])

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

2 participants