Defining restricted subset of Storable for type match

自古美人都是妖i 提交于 2020-01-24 11:04:04

问题


I am getting error for the code below - I suspect it has to do with the type signature of dispatch function which returns a Vector of type Storable a. What is the simple way to update dispatch function type signature to do only Int32 and CChar in type signature:

{-#  LANGUAGE BangPatterns #-}
import Data.Vector.Storable as SV
import Foreign.C.Types (CChar)
import GHC.Int (Int32)

data AList = I {-# UNPACK #-} !(SV.Vector Int32)
              | S {-# UNPACK #-} !(SV.Vector CChar)


dispatch :: (Storable a) => AList -> SV.Vector a
dispatch (I x) = x
dispatch (S x) = x

Error in ghci 7.4.1:

    test.hs:11:18:
    Couldn't match type `CChar' with `Int32'
    Expected type: Vector a
      Actual type: Vector Int32
    In the expression: x
    In an equation for `dispatch': dispatch (I x) = x
Failed, modules loaded: none.

I am asking this question assuming my error diagnosis is correct. If my diagnosis is incorrect, I will appreciate pointers on how to fix the error above.


回答1:


The type signature

dispatch :: (Storable a) => AList -> SV.Vector a

says "give me an AList, and I'll give you an SV.Vector a for any a you want, so long as it's an instance of Storable". That's not right! For any given value, there's only one a you can supply, and you choose it, not the calling code. The problem might be easier to see if you explicitly add the quantifier:

dispatch :: forall a. (Storable a) => AList -> SV.Vector a

What you really want to say is "give me an AList, and I'l give you an SV.Vector a for some a that I choose, but I promise it'll be an instance of Storable". For this, we need an existential type:

data SomeSVVector = forall a. (Storable a) => SomeSVVector (SV.Vector a)

dispatch :: AList -> SomeSVVector
dispatch (I x) = SomeSVVector x
dispatch (S x) = SomeSVVector x

(You'll need {-# LANGUAGE ExistentialQuantification #-} for this.)

This gives SomeVVector the type:

SomeVVector :: (Storable a) => SV.Vector a -> SomeVVector

You can then take the SV.Vector out of the result of dispatch with case dispatch x of SomeSVVector vec -> .... However, this probably isn't all that useful: since the existential can contain a vector with elements of any instance of Storable, the only operations you'll be able to perform on the data inside are those offered by the Storable class. If you want something that user code can analyse and "dispatch" on the type of, you'll need a tagged union — which is exactly what your AList type already is.

If you do want to go down the existential route, then I would suggest defining your own typeclass as a subclass of Storable that contains all the operations you might want to perform on the values inside. At the very least, you'll probably want to add Integral to SomeSVVector's constraint.


As you mentioned in the comments, if you don't mind an AList of Int32s and an AList of CChars having different types, you can use a GADT:

data AList a where
  I :: {-# UNPACK #-} !(SV.Vector Int32) -> AList Int32
  S :: {-# UNPACK #-} !(SV.Vector CChar) -> AList CChar

dispatch :: AList a -> SV.Vector a
dispatch (I x) = x
dispatch (S x) = x

Here, AList is essentially a version of SV.Vector that only works on certain element types. However, dispatch isn't really that useful — there's no real way to "get back in" to an AList after you take it out with dispatch, because the type unification GADT pattern-matching offers only works with explicit pattern-matching; you can't tell that the result of dispatch is either an SV.Vector Int32 or an SV.Vector CChar. Something like

dispatch :: (SV.Vector Int32 -> r) -> (SV.Vector CChar -> r) -> AList a -> r

would work, but that's equivalent to (and more awkward than) pattern-matching on your original tagged union version.

I don't think there's a reasonable way to define a useful dispatch; I would suggest using explicit pattern-matching on your original AList definition instead.



来源:https://stackoverflow.com/questions/9178308/defining-restricted-subset-of-storable-for-type-match

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!