[GHC] #13498: More storable instances?

GHC ghc-devs at haskell.org
Fri Mar 31 10:55:05 UTC 2017


#13498: More storable instances?
-------------------------------------+-------------------------------------
        Reporter:  wyager            |                Owner:  (none)
            Type:  feature request   |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  libraries/base    |              Version:  8.0.1
      Resolution:                    |             Keywords:  Storable,
                                     |  Foreign, Ptr
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):
       Wiki Page:                    |
-------------------------------------+-------------------------------------

Comment (by svenpanne):

 Replying to [comment:2 wyager]:
 > > the encoding would be quite arbitrary
 >
 > How is this an argument against adding the encodings? If they function
 correctly, why does it matter that they aren't "fixed" in some sense?

 The general rule of thumb is: The Prelude doesn't make arbitrary choices
 when there is no obviously "right" way of doing things. Well, there are
 exceptions, but that's the idea. In our case, there ''are'' various
 arbitrary choices.

 Remember that the prime motivation for `Storable` is the FFI, and there
 are numerous ways used in actual C libraries to encode the equivalent of
 `Maybe` or `Either`, and none of them is "more correct" than the other.

 > > Why are the tags 8 bit wide, and why are the tag values 0 and 1?
 >
 > Because it makes sense and works.

 `CChar` or `CInt` or `Word` make sense, too, and depending on the
 circumstances, they would be even better. That was just an example for the
 arbitrary decisions which would have to be made.

 > [...]
 > > If you need serialization, you can use `Data.Binary`.
 >
 > Besides `Data.Binary` being vastly slower than `Storable`, the primary
 use case here is `Data.Vector.Storable`  and `Data.Array.Storable`. You
 can't write an `Unbox` instance for `Maybe a`, but you can (as I've done
 here) write a `Storable` instance.

 If you directly want to use `Maybe` and `Either` with those containers,
 nothing is going to stop you from rolling your own. A better approach
 would probably be `newtype` wrappers for `Maybe` and `Either` with the
 corresponding `Storable` instances, making your arbitrary decisions
 explicit.

 Is using `Binary` really that much slower than your own code?

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13498#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list