Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7442243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:08:45+00:00 2026-05-29T11:08:45+00:00

I am getting error for the code below – I suspect it has to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:08:46+00:00Added an answer on May 29, 2026 at 11:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting LNK2001 error. The code has been included below. Can someone please
I am getting the following error message on the code below (which is at
HI, I have code below but getting error object does not match target type
I'm getting this error message with the code below: class Money { public: Money(float
I’m getting system error when I try to compile the code below on Visual
I'm getting the following error on the line of code below: Syntax error (missing
I am getting JQUERY undefined error. Do you have any idea (code below). <%@
I am getting an compilation error not a statement for the below code. Not
All, I'm getting an error with the code below. Here is the error message
I am not able to understand the error with the code below which simply

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.