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

Postpone requiring Int dimensions in IfThenElse and Stencil #549

Merged
merged 2 commits into from
Jan 13, 2025

Conversation

tomsmeding
Copy link
Member

Description
As was done for the Shape type class in 9ece31c, this PR modifies the IfThenElse and Stencil type classes to only require the dimensionality of the array to be known before selecting the instance. The fact that the index types for each of the dimensions is in fact Int is only asserted with a type-equality in the instance context (i.e. only applied after the instance is selected).

Motivation and context
Previously, writing code like this:

foo :: Exp a -> Exp a
foo x = if x then x else x

would generate this error:

input.hs:33:9: error:
    • No instance for (IfThenElse (Exp a) (Exp a))
        arising from an if-then-else expression
    • In the expression: if x then x else x
      In an equation for ‘foo’: foo x = if x then x else x
   |
33 | foo x = if x then x else x
   |         ^^^^^^^^^^^^^^^^^^

which is not very helpful for the user. The updated instances will instead result in this error:

input.hs:33:9: error:
    • Couldn't match type ‘a’ with ‘Bool’
        arising from an if-then-else expression
      ‘a’ is a rigid type variable bound by
        the type signature for:
          foo :: forall a. Exp a -> Exp a
        at input.hs:32:1-21
    • In the expression: if x then x else x
      In an equation for ‘foo’: foo x = if x then x else x
    • Relevant bindings include
        x :: Exp a (bound at input.hs:33:5)
        foo :: Exp a -> Exp a (bound at input.hs:33:1)
   |
33 | foo x = if x then x else x
   |         ^^^^^^^^^^^^^^^^^^

which is somewhat more helpful.

A student ran into this. Similarly, another student ran into the Stencil case, which is also updated in this PR.

How has this been tested?
Tests pass.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist
Go over all the following points, and put an x in all the boxes that apply. If you're unsure about any of these, don't hesitate to ask. We're here to help!

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed

@tomsmeding tomsmeding merged commit 3967d71 into master Jan 13, 2025
68 of 86 checks passed
@tomsmeding tomsmeding deleted the infer-int-instance branch January 13, 2025 15:39
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

Successfully merging this pull request may close these issues.

1 participant