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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:30:49+00:00 2026-05-27T15:30:49+00:00

Context: I’m trying to produce an error monad that also keeps track of a

  • 0

Context: I’m trying to produce an error monad that also keeps track of a list of warnings, something like this:

data Dangerous a = forall e w. (Error e, Show e, Show w) =>
    Dangerous (ErrorT e (State [w]) a)

i.e. Dangerous a is an operation resulting in (Either e a, [w]) where e is a showable error and w is showable.

The problem is, I can’t seem to actually run the thing, mostly because I don’t understand existential types all that well. Observe:

runDangerous :: forall a e w. (Error e, Show e, Show w) =>
    Dangerous a -> (Either e a, [w])
runDangerous (Dangerous f) = runState (runErrorT f) []

This doesn’t compile, because:

Could not deduce (w1 ~ w)
from the context (Error e, Show e, Show w)
...
`w1' is a rigidtype variable bound by
    a pattern with constructor
    Dangerous :: forall a e w.
                 (Error e, Show e, Show w) =>
                 ErrorT e (State [w]) a -> Dangerous a
...
`w' is a rigid type variable bound by
    the type signature for
    runDangerous :: (Error e, Show e, Show w) =>
                    Dangerous a -> (Either e a, [w])

I’m lost. What’s w1? Why can’t we deduce that it’s ~ w?

  • 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-27T15:30:49+00:00Added an answer on May 27, 2026 at 3:30 pm

    An existential is probably not what you want here; there is no way to “observe” the actual types bound to e or w in a Dangerous a value, so you’re completely limited to the operations given to you by Error and Show.

    In other words, the only thing you know about w is that you can turn it into a String, so it might as well just be a String (ignoring precedence to simplify things), and the only thing you know about e is that you can turn it into a String, you can turn Strings into it, and you have a distinguished value of it (noMsg). There is no way to assert or check that these types are the same as any other, so once you put them into a Dangerous, there’s no way to recover any special structure those types may have.

    What the error message is saying is that, essentially, your type for runDangerous claims that you can turn a Dangerous into an (Either e a, [w]) for any e and w that have the relevant instances. This clearly isn’t true: you can only turn a Dangerous into that type for one choice of e and w: the one it was created with. The w1 is just because your Dangerous type is defined with a type variable w, and so is runDangerous, so GHC renames one of them to avoid name clashes.

    The type you need to give runDangerous looks like this:

    runDangerous
      :: (forall e w. (Error e, Show e, Show w) => (Either e a, [w]) -> r)
      -> Dangerous a -> r
    

    which, given a function which will accept a value of type (Either e a, [w]) for any choices of e and w so long as they have the instances given, and a Dangerous a, produces that function’s result. This is quite hard to get your head around!

    The implementation is as simple as

    runDangerous f (Dangerous m) = f $ runState (runErrorT m) []
    

    which is a trivial change to your version. If this works for you, great; but I doubt that an existential is the right way to achieve whatever you’re trying to do.

    Note that you’ll need {-# LANGUAGE RankNTypes #-} to express the type of runDangerous. Alternatively, you can define another existential for your result type:

    data DangerousResult a = forall e w. (Error e, Show e, Show w) =>
       DangerousResult (Either e a, [w])
    
    runDangerous :: Dangerous a -> DangerousResult a
    runDangerous (Dangerous m) = DangerousResult $ runState (runErrorT m) []
    

    and extract the result with case, but you’ll have to be careful, or GHC will start complaining that you’ve let e or w escape — which is the equivalent of trying to pass an insufficiently polymorphic function to the other form of runDangerous; i.e. one that requires more constraints on what e and w are beyond what the type of runDangerous guarantees.

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

Sidebar

Related Questions

CONTEXT I have a list of items (or arbitrary length). I'd like to group
Context Using Ruby I am parsing strings looking like this: A type with an
Context Sometimes, I like reading code on paper rather than on screen (so I
Context I am trying to understand the internals of a big, hairy JavaScript library.
Context I'm currently reading about Clojure's implementation of monads: org.clojure/algo.monads Intuitively, reduce looks like
Context I have this piece of Java Code btn.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent
Context I want to make this call via reflection instanceOfEventPublisher.Publish<T>(T eventInst); When I call
Context: Trying to link a tab to a specific content div on a html
Context: From my javascript web UI, I launch a long-running (several minutes) operation that
Context: I notice that the major databases support only the Gregorian calendar. Thus, it

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.