basvandijk / scientific

Arbitrary-precision floating-point numbers represented using scientific notation
BSD 3-Clause "New" or "Revised" License
73 stars 40 forks source link

instance Generic Scientific ? #83

Closed ocramz closed 2 years ago

ocramz commented 2 years ago

I realize this question might have been asked a number of times already. I don't see a Generic instance, which would be handy when doing serialisation etc.

Thoughts?

phadej commented 2 years ago

It would break Eq substantivity, for example. Similar reason why Ratio from base doesn't have Generic instance.

ocramz commented 2 years ago

Afraid I don't follow. How is the Eq instance related?

phadej commented 2 years ago

It's easy to make instance which would serialize equal Scientific values to different serialized forms.

jchia commented 2 years ago

It's easy to make instance which would serialize equal Scientific values to different serialized forms.

I don't understand why this is a concern. Does the built-in Binary instance always serialize equal Scientific values to equal serialized forms? It makes no attempt at normalization, so it seems that it has the same problem that you described. And if Binary does not have this nice property, why do we care about other types of serialization that are Generically derived? Are there other issues besides normalization that allow two equal Scientific values to serialize have different encoding? Or, are there plans to fix the Binary instance?

instance Binary Scientific where
    put (Scientific c e) = put c *> put (toInteger e)
    get = Scientific <$> get <*> (fromInteger <$> get)
phadej commented 2 years ago

The main concern is that Scientific is an abstract type. Generic doesn't fit well. Writing instances by hand is not hard, and then everyone can decide whether normalization is needed or not.