Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add COMPLETE pragma for Vec: Nil, (:>) #2716

Merged
merged 3 commits into from
Jun 30, 2024

Conversation

axman6
Copy link
Contributor

@axman6 axman6 commented May 7, 2024

I was banging my head against some type errors for hours until I decided to use the Cons constructor instead of the :> pattern - hopefully this will save someone else the same pain.

Still TODO:

  • Write a changelog entry (see changelog/README.md)
  • Check copyright notices are up to date in edited files

@axman6
Copy link
Contributor Author

axman6 commented May 8, 2024

It looks like the tests are failing, but as far as I can tell the errors have nothing to do with my changes.

@martijnbastiaan
Copy link
Member

martijnbastiaan commented May 8, 2024

Can you rebase @axman6? The CI failures are caused by haskell-actions/setup#77, and has been worked around in master.

Copy link
Member

@martijnbastiaan martijnbastiaan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does LGTM. Surprising we didn't have this.

@axman6
Copy link
Contributor Author

axman6 commented May 8, 2024

Hmm, I had hoped that I could rebase in github, but apparently it just merges the target branch in - hopefully this is still fine.

@axman6 axman6 force-pushed the axman6-vec-complete-pragma branch from f4f97e3 to a6cf272 Compare May 10, 2024 00:50
@axman6
Copy link
Contributor Author

axman6 commented May 10, 2024

Ok, I've rebased properly now, we'll see how this goes (though merging in master also didn't pass which is odd).

@martijnbastiaan martijnbastiaan force-pushed the axman6-vec-complete-pragma branch from a6cf272 to d71bb3c Compare June 30, 2024 07:55
@martijnbastiaan
Copy link
Member

I've rebased again.. Hopefully the CI errors are now gone. @christiaanb could you give this a look to? To me this looks "obviously correct", but we had some subtleties creeping in with Vec's patterns before.

@christiaanb
Copy link
Member

elimExistentials :: HasCallStack => NormRewrite
will likely not fire on the pattern synonym, meaning Clash won’t be able to unroll recursive functions defined in terms of Nil and :>. From that perspective I’d rather the compiler suggests you use Cons.

@martijnbastiaan
Copy link
Member

Sure, that would be a good reason to remove the patterns entirely, but we've tried before and couldn't for various reasons. So while we're stuck with them, isn't the correct thing to do is add a complete pragma? Or are you saying that that makes things worse somehow?

@christiaanb
Copy link
Member

Or are you saying that that makes things worse somehow?

I guess we're fine, I cannot even get a recursive function to type-check:

module Test where

import Clash.Prelude

myMap :: forall n a b . (a -> b) -> Vec n a -> Vec n b
myMap _ Nil = Nil
myMap f (x :> (xs :: Vec m a)) = f x :> myMap f xs

topEntity :: Vec 3 Int -> Vec 3 Int
topEntity = myMap (+1)

gives:

Test.hs:8:10: error:
    • Couldn't match type ‘n’ with ‘m + 1’
      Expected: Vec n a
        Actual: Vec (m + 1) a
      ‘n’ is a rigid type variable bound by
        the type signature for:
          myMap :: forall (n :: Nat) a b. (a -> b) -> Vec n a -> Vec n b
        at Test.hs:6:1-54
    • When checking that the pattern signature: Vec (m + 1) a
        fits the type of its context: Vec n a
      In the pattern: (x :> (xs :: Vec m a)) :: Vec (m + 1) a
      In an equation for ‘myMap’:
          myMap f ((x :> (xs :: Vec m a)) :: Vec (m + 1) a)
            = f x :> myMap f xs
    • Relevant bindings include
        myMap :: (a -> b) -> Vec n a -> Vec n b (bound at Test.hs:7:1)
  |
8 | myMap f ((x :> (xs :: Vec m a)) :: Vec (m+1) a) = f x :> myMap f xs
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

So I guess I don't have to worry that Clash won't be able to unroll the recursion.

axman6 added 3 commits June 30, 2024 21:57
I was banging my head against some type errors for hours until I decided to use the `Cons` constructor instead of the `:>` pattern - hopefully this will save someone else the same pain.
@DigitalBrains1 DigitalBrains1 force-pushed the axman6-vec-complete-pragma branch from d71bb3c to 3f4f65a Compare June 30, 2024 19:57
@martijnbastiaan
Copy link
Member

Thanks for the comment @christiaanb, thanks for the CI fixes @DigitalBrains1. I think we can merge this one.

@martijnbastiaan martijnbastiaan enabled auto-merge (squash) June 30, 2024 20:45
@DigitalBrains1
Copy link
Member

@kloonbot run_ci 3f4f65a

@martijnbastiaan martijnbastiaan merged commit b0f166b into clash-lang:master Jun 30, 2024
13 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants