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 maybeResize (and friends) #2780

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jvnknvlgl
Copy link
Collaborator

@jvnknvlgl jvnknvlgl commented Aug 2, 2024

Add maybeFromIntegral, maybeResize and maybeTruncateB. Resolves #2779. No tests yet.

Still TODO:

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

@jvnknvlgl jvnknvlgl force-pushed the jvnknvlgl/clash-prelude/resize branch from efb3cc6 to dd3c71c Compare August 2, 2024 10:13
@jvnknvlgl jvnknvlgl force-pushed the jvnknvlgl/clash-prelude/resize branch from dd3c71c to c5b9bef Compare August 2, 2024 11:13
-- | Like 'resize', but returns 'Nothing' if /f a/ is out of bounds for /f b/.
-- Useful when you do not know /f a/ can be out of bounds, and would like to
-- have your assumptions checked.
maybeResize ::
Copy link
Member

@rowanG077 rowanG077 Aug 2, 2024

Choose a reason for hiding this comment

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

This will break for anything larger than native word size (64-bit usually) in hardware. At least mention this limitation in the doc.

edit: I think it would be possible to rewrite this using only Ord, Bounded, Resize and BitPack. resize to Max (BitSize a) (BitSize b) and compare to maxBound/minBound of b. That won't have this limitation. It will also sidestep the notorious clash Integer warning.

Copy link
Member

Choose a reason for hiding this comment

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

Oh in fact you can do a short circuit on the type level if BitSize a >= BitSize b which means this function will be free in that case in hardware.

Copy link
Member

@martijnbastiaan martijnbastiaan Aug 4, 2024

Choose a reason for hiding this comment

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

I agree this shouldn't go through dynamically sized constructs.

Something along the lines of:

case cmpNat @a @b Proxy Proxy of
  LTI | v > resize (maxBound @(f b)) -> Nothing
  EQI -> Just v -- optional, for performance reasons?
  _ -> Just (resize v)

Copy link
Member

@DigitalBrains1 DigitalBrains1 left a comment

Choose a reason for hiding this comment

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

I have some thoughts on the Haddock

-- | Like 'fromIntegral', but returns 'Nothing' if /a/ is out of bounds for /b/.
-- Useful when you do not know /a/ can be out of bounds, and would like to have
-- your assumptions checked.

Copy link
Member

Choose a reason for hiding this comment

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

The Haddock for maybeFromIntegral is not generated, and that's probably because of this blank line. This blank line means that what follows the blank line is a regular Haskell comment rather than a Haddock comment. I would have expected the Haddock to still be associated with the function, but apparently it's cut off and lost?

TL;DR: Remove the blank line :-). And always check the Haddock output :-).

To view the Haddock, you can click on the Details button next to the ci/gitlab/gitlab.com CI test result, and then click on haddock (in the column test), and then click the big Browse button in the rightmost column of that webpage.

Or just generate Haddock on your own system of course, before you run CI.

Comment on lines +151 to +152
-- Useful when you do not know /f a/ can be out of bounds, and would like to
-- have your assumptions checked.
Copy link
Member

Choose a reason for hiding this comment

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

I think all these maybe variants can be described much shorter:

Like resize, but returns Nothing if f a is out of bounds for f b.

The rest seems unnecessary, and also it mentions assumptions, but we don't go in with assumptions. This function is total; the assumption with the checked variant was that it would fit, but we want to error when that assumption is wrong. This is different.

Also, Martijn and I discussed that we'd best rephrase all these functions a bit, but that can be done in a followup PR. Or I can tack a commit onto this PR...

# 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.

Add maybeResize (and friends)
4 participants