Skip to content

Added fromInt #261

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- `fromInt`

Bugfixes:

Expand Down
12 changes: 12 additions & 0 deletions src/Data/Ring/Extra.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Data.Ring.Extra where
Copy link
Contributor

Choose a reason for hiding this comment

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

From the outside, it makes little sense to put this function into Data.Ring.Extra rather than just putting it into Data.Ring. What if we move abs and signum from Data.Ord into Data.Ring? I think that would mean that Data.Ord no longer needs to import Data.Ring, which would allow us to use >= here.

I think we should basically inline the definitions of power and Additive too, since importing either of those in Data.Ring will also cause a cycle.

Copy link
Author

Choose a reason for hiding this comment

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

That'd be a breaking change though, if Data.Ord doesn't export abs/signum anymore.

Copy link
Contributor

@hdgarrood hdgarrood Feb 26, 2021

Choose a reason for hiding this comment

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

Correct, so this would have to wait until the next round of breaking changes. Even though it is breaking, I prefer it over adding a new Data.Ring.Extra module because:

  • the API surface makes more sense,
  • we'd end up wanting to merge Data.Ring.Extra back into Data.Ring at some point in the future, which would also be breaking, and
  • I think it makes more sense to put abs and signum into Data.Ring anyway

Copy link
Contributor

Choose a reason for hiding this comment

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

Ord also depends on negate in its usage of -1:

instance ordArray :: Ord a => Ord (Array a) where
  compare = \xs ys -> compare 0 (ordArrayImpl toDelta xs ys)
    where
    toDelta x y =
      case compare x y of
        EQ -> 0
        LT -> 1
        GT -> -1 -- here it is


import Data.Ord ((>=))
import Data.Ring (class Ring, one, negate)
import Data.Monoid (power)
import Data.Monoid.Additive (Additive(..))

fromInt :: forall a. Ring a => Int -> a
fromInt x =
if x >= 0
then (\(Additive i) -> i) (power (Additive one) x)
else negate (fromInt (negate x))
8 changes: 8 additions & 0 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Data.HeytingAlgebra (ff, tt, implies)
import Data.Ord (abs)
import Test.Data.Generic.Rep (testGenericRep)
import Test.Utils (AlmostEff, assert)
import Data.Ring.Extra (fromInt)

main :: AlmostEff
main = do
Expand All @@ -15,6 +16,7 @@ main = do
testIntDegree
testRecordInstances
testGenericRep
testFromInt

foreign import testNumberShow :: (Number -> String) -> AlmostEff

Expand Down Expand Up @@ -151,3 +153,9 @@ testRecordInstances = do
assert "Record top" $
(top :: { a :: Boolean }).a
== top

testFromInt :: AlmostEff
testFromInt = do
assert "Zero" $ fromInt 0 == 0.0
assert "Negative" $ fromInt (-1) == (-1.0)
assert "Positive" $ fromInt 1 == 1.0
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add tests for 2 and 5 please? Just in case.