Skip to content

Add Clash.Sized.Vector.ToTuple #2682

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

Merged
merged 1 commit into from
Mar 1, 2024
Merged

Add Clash.Sized.Vector.ToTuple #2682

merged 1 commit into from
Mar 1, 2024

Conversation

martijnbastiaan
Copy link
Member

@martijnbastiaan martijnbastiaan commented Feb 27, 2024

Newer GHCs (mistakenly) emit warnings when pattern matching on Vecs, with neither :> nor Cons being able to prevent it. vecToTuple allows users to convert Vecs to tuples, sidestepping GHCs exhaustiveness checker while not compromising on safety.

Nested tuples were considered as an alternative implementation, simplifying implementation and removing the need for TemplateHaskell. This would, however, compromise on user comfort while offering no expected real-world benefits.


This is clearly a work around. Perhaps we should write a GHC plugin to circumvent these warnings (and depending on the solution upstream)?

Documentation

Screenshot 2024-02-28 at 09-17-26 Clash Sized Vector ToTuple

Still TODO:

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

@martijnbastiaan martijnbastiaan changed the title Added Clash.Sized.Vector.ToTuple Add Clash.Sized.Vector.ToTuple Feb 27, 2024
@martijnbastiaan martijnbastiaan force-pushed the vec-to-tuple branch 3 times, most recently from bc1599c to c33526e Compare February 27, 2024 19:59
@martijnbastiaan martijnbastiaan force-pushed the vec-to-tuple branch 2 times, most recently from 5f9ff3a to 7bb5668 Compare February 28, 2024 07:58
Newer GHCs (mistakenly) emit warnings when pattern matching on `Vec`s,
with neither `:>` nor `Cons` being able to prevent it. `vecToTuple`
allows users to convert `Vec`s to tuples, sidestepping GHCs
exhaustiveness checker while not compromising on safety.

Nested tuples were considered as an alternative implementation,
simplifying implementation and removing the need for `TemplateHaskell`.
This would, however, compromise on user comfort while offering no
expected real-world benefits.
Copy link
Member

@christiaanb christiaanb left a comment

Choose a reason for hiding this comment

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

Was there a reason you use :> instead of Cons for the pattern match?

@martijnbastiaan
Copy link
Member Author

Was there a reason you use :> instead of Cons for the pattern match?

Discussed in the office: there is a difference in behavior. E.g. in the following:

vecToTuplePat :: (Vec 2 a) -> (a, a)
vecToTuplePat (a1 :> a2 :> _) = (a1, a2)

-- True
pat :: Bool
pat = case vecToTuplePat (1 :> undefined) of
  ~(a, _) -> a `seq` True

vecToTupleCon :: (Vec 2 a) -> (a, a)
vecToTupleCon (a1 `Cons` a2 `Cons` _) = (a1, a2)

-- Bottom
con :: Bool
con = case vecToTupleCon (1 :> undefined) of
  ~(a, _) -> a `seq` True

then pat ~ True, while con ~ _|_. We're not sure whether this will be an issue in practice, but we've decided to err on the safe side and be lazy.

@martijnbastiaan
Copy link
Member Author

@leonschoorl I applied your suggestions, thanks!

@martijnbastiaan martijnbastiaan merged commit c3d197f into master Mar 1, 2024
@martijnbastiaan martijnbastiaan deleted the vec-to-tuple branch March 1, 2024 10:54
mergify bot pushed a commit that referenced this pull request Mar 1, 2024
Newer GHCs (mistakenly) emit warnings when pattern matching on `Vec`s,
with neither `:>` nor `Cons` being able to prevent it. `vecToTuple`
allows users to convert `Vec`s to tuples, sidestepping GHCs
exhaustiveness checker while not compromising on safety.

Nested tuples were considered as an alternative implementation,
simplifying implementation and removing the need for `TemplateHaskell`.
This would, however, compromise on user comfort while offering no
expected real-world benefits.

(cherry picked from commit c3d197f)
martijnbastiaan added a commit that referenced this pull request Mar 2, 2024
Newer GHCs (mistakenly) emit warnings when pattern matching on `Vec`s,
with neither `:>` nor `Cons` being able to prevent it. `vecToTuple`
allows users to convert `Vec`s to tuples, sidestepping GHCs
exhaustiveness checker while not compromising on safety.

Nested tuples were considered as an alternative implementation,
simplifying implementation and removing the need for `TemplateHaskell`.
This would, however, compromise on user comfort while offering no
expected real-world benefits.

(cherry picked from commit c3d197f)
martijnbastiaan added a commit that referenced this pull request Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants