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 6567955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:23:53+00:00 2026-05-25T14:23:53+00:00

One could think of this case as follows: The application dynamically loads a module,

  • 0

One could think of this case as follows:
The application dynamically loads a module, or there is a list of functions from which the user chooses, etc. We have a mechanism for determining whether a certain type will successfully work with a function in that module. So now we want to call into that function. We need to force it to make the call. The function could take a concrete type, or a polymorphic one and it’s the case below with just a type class constraint that I’m running into problems with it.

The following code results in the errors below. I think it could be resolved by specifying concrete types but I do not want to do that. The code is intended to work with any type that is an instance of the class. Specifying a concrete type defeats the purpose.

This is simulating one part of a program that does not know about the other and does not know the types of what it’s dealing with. I have a separate mechanism that allows me to be sure that the types do match up properly, that the value sent in really is an instance of the type class. That’s why in this case, I don’t mind using unsafeCoerce. But basically I need a way to tell the compiler that I really do know it’s ok and do it anyway even though it doesn’t know enough to type check.

{-# LANGUAGE ExistentialQuantification, RankNTypes, TypeSynonymInstances #-}
module Main where

import Unsafe.Coerce

main = do
  --doTest1 $ Hider "blue"
  doTest2 $ Hider "blue"

doTest1 :: Hider -> IO ()
doTest1 hh@(Hider h) =
  test $ unsafeCoerce h

doTest2 :: Hider -> IO ()
doTest2 hh@(Hider h) =
  test2 hh

test :: HasString a => a -> IO ()
test x = print $ toString x

test2 :: Hider -> IO ()
test2 (Hider x) = print $ toString (unsafeCoerce x)

data Hider = forall a. Hider a

class HasString a where
  toString :: a -> String

instance HasString String where
  toString = id

Running doTest1

[1 of 1] Compiling Main             ( Test.hs, Test.o )

Test.hs:12:3:
    Ambiguous type variable `a1' in the constraint:
      (HasString a1) arising from a use of `test'
    Probable fix: add a type signature that fixes these type variable(s)
    In the expression: test
    In the expression: test $ unsafeCoerce h
    In an equation for `doTest1':
        doTest1 hh@(Hider h) = test $ unsafeCoerce h

Running doTest2

[1 of 1] Compiling Main             ( Test.hs, Test.o )

Test.hs:12:3:
    Ambiguous type variable `a1' in the constraint:
      (HasString a1) arising from a use of `test'
    Probable fix: add a type signature that fixes these type variable(s)
    In the expression: test
    In the expression: test $ unsafeCoerce h
    In an equation for `doTest1':
        doTest1 hh@(Hider h) = test $ unsafeCoerce h
  • 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-25T14:23:54+00:00Added an answer on May 25, 2026 at 2:23 pm

    I think it could be resolved by specifying concrete types but I do not want to do that.

    There’s no way around it though with unsafeCoerce. In this particular case, the compiler can’t infer the type of unsafeCoerce, because test is still to polymorphic. Even though there is just one instance of HasString, the type system won’t use that fact to infer the type.

    I don’t have enough information about your particular application of this pattern, but I’m relatively sure that you need to rethink the way you use the type system in your program. But if you really want to do this, you might want to look into Data.Typeable instead of unsafeCoerce.

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

Sidebar

Related Questions

This is the one thing I could never get to work. My problem is
There seem to be a number of weird things one could do if one
I would like to compare a screenshot of one application (could be a Web
Just could not get this one and googling did not help much either.. First
Sorry for the bad title, but I couldn't think of a better one. I'm
One solution that one could suggest is to periodically look for a special 'Screen-saver'
I'm wondering how one could use template databinding to accomplish what the following code
Classic ASP had 'server side includes' so that one could easily design a generic
Could any one give an explanation on how a DHT works? Nothing too heavy,
Could some one tell me how to capture SOAP messages passed between the client

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.