Until today I had the assumption that fromInteger
in the Num
class was a ring homomorphism. I had assumed this because integer is is coterminal so every ring must have a unique homomorphism from integer, so it made sense that Num
, which is basically the ring class of the standard library, would include that homomorphism.
However today I was reading the laws for Num
and saw that fromInteger
is not required to be a homomorphism, but only required to preserve identities. So for example we can implement the Klein 4 group, and have fromInteger
map 1
to multiplicative identity and everything else to the addative identity, and the result is a legal Num
instance but not a homomorphism.
type Klein4
= ( Bool
, Bool
)
instance
(
)
=> Num Klein4
where
( a, b ) + ( c, d )
= ( a /= c
, b /= d
)
( a, b ) * ( c, d )
= ( a && b
, b && d
)
negate
= id
fromInteger x
= ( x == 1
, x == 1
)
This is a little surprising to me. So my question is why is this the case? Is the intention that fromInteger
is a ring homomorphism and I'm just being pedantic, or is there a good use case where you might want to define fromInteger
in a way that it is not a homomorphism (and still follows the Num
laws)?
question from:
https://stackoverflow.com/questions/65901824/is-frominteger-supposed-to-be-a-ring-homomorphism 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…