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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:01:15+00:00 2026-06-08T19:01:15+00:00

Lets say we’ve got a function like f below, that returns a monad. However,

  • 0

Lets say we’ve got a function like f below, that returns a monad. However, where you see Int, pretend it’s a really complicated type.

f :: (Monad m) => m Int -- Pretend this isn't Int but something complicated
f = return 42

Now lets say we want to force this into the Maybe monad. We don’t need to write the full type of f to do this, we can just do the following:

g :: Maybe a -> Maybe a
g = id

main = print $ (g f)

The dummy function g forces f to become Maybe.

I think the above is rather messy. What I’d rather write is this:

main = print $ (f :: Maybe a)

But it fails with the following error:

Couldn't match expected type `a' against inferred type `Int'
  `a' is a rigid type variable bound by
      the polymorphic type `forall a. Maybe a' at prog.hs:7:16
  Expected type: Maybe a
  Inferred type: Maybe Int
In the second argument of `($)', namely `(f :: Maybe a)'
In the expression: print $ (f :: Maybe a)

Is there a way to do what g above does in a less messy way that doesn’t involve creating a new function? I don’t want to write f :: Maybe Int, as it becomes a maintenance problem if the return type changes. GHC extensions are okay in answers.

  • 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-06-08T19:01:18+00:00Added an answer on June 8, 2026 at 7:01 pm

    Use asTypeOf. It returns the first argument while unifying its type with that of the second. It’s just a type-restricted version of const, but useful for situations like this.

    main = print $ f `asTypeOf` (undefined :: Maybe a)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say that I have one object like: class First { int key; }
Lets say I have a class A that is fairly simple like this -
Lets say that I have this code $(document).ready(function() { $(#changeText).click(function() { $(#textBox).html(My<br><br><br>text<br><br><br>is<br><br><br>changed!); }); });
Lets say I have an enumerable source, that looks like this: IEnumerable<string> source =
Lets say I have a function which takes some input structure and returns some
Lets Say i have a table like this WEB_LIST_TABLE KEY Value ---------------------------------------- 134 google.com
Lets say I have multiple commands like 5000 which updates some column and row
Lets say that i have 2 pages: index.php and service.php index.php sends an http-post
Lets say that I have 4 view controllers (call them FirstView,SecondView,ThirdView,FourthView) which are created
Lets say i have a table: <table> <tr></tr> </table> $(function(){ $('#btn').click(function(){ $('table').append('<tr><input class=remove value=remove></tr>');

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.