Skip to content
Merged
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
27 changes: 26 additions & 1 deletion src/Data/Int.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Data.Int
, base36
, fromStringAs
, toStringAs
, Parity(..)
, parity
, even
, odd
, pow
Expand Down Expand Up @@ -77,6 +79,29 @@ foreign import toNumber :: Int -> Number
fromString :: String -> Maybe Int
fromString = fromStringAs (Radix 10)

-- | A type for describing whether an integer is even or odd.
data Parity = Even | Odd

derive instance eqParity :: Eq Parity
derive instance ordParity :: Ord Parity
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe Show too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good thinking. I added Bounded also.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also CommutativeRing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's a field, isn't it, if we're going to give it numeric instances? If we treat Parity as Z/2Z with Even = [0] and Odd = [1]?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm thinking of this as being an approximation to integers, not Z/2Z. Dividing an even integer by another even integer could be even or odd.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Although I think sometimes "ring" is defined such that multiplication need only be a semigroup, not necessarily a monoid; in that case you might be able to find two such rings with two elements.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah your logic seems sound to me. I was going from this:

https://en.wikipedia.org/wiki/Finite_ring

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does that suggest that we should give Boolean a Field instance?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we should. Just because it has two elements doesn't mean we should think of it as Z/2 in my opinion. I would rather have a separate type.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Makes sense, although we can make it into a field without thinking of it as Z/2Z - just by thinking of addition as xor and multiplication as &&. But yeah in the absence of any concrete use case I'm inclined to leave it how it is too.


instance showParity :: Show Parity where
show Even = "Even"
show Odd = "Odd"

instance boundedParity :: Bounded Parity where
bottom = Even
top = Odd

-- | Returns whether an `Int` is `Even` or `Odd`.
-- |
-- | ``` purescript
-- | parity 0 == Even
-- | parity 1 == Odd
-- | ```
parity :: Int -> Parity
parity n = if even n then Even else Odd

-- | Returns whether an `Int` is an even number.
-- |
-- | ``` purescript
Expand All @@ -90,7 +115,7 @@ even x = x .&. 1 == 0
-- |
-- | ``` purescript
-- | odd 0 == false
-- | odd 1 == false
-- | odd 1 == true
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

😱

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Haha yeah! 😆

-- | ```
odd :: Int -> Boolean
odd x = x .&. 1 /= 0
Expand Down