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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:16:22+00:00 2026-06-03T04:16:22+00:00

(please excuse the horribly contrived example) What I want to do is specify types

  • 0

(please excuse the horribly contrived example)

What I want to do is specify types in the where-clause:

somemap :: (a -> b) -> [a] -> [b]
somemap f xs = ys
  where
    some = take 5 xs :: [a]
    ys = map f some :: [b]

But this causes an error:

*Main> :load file.hs 
[1 of 1] Compiling Main             ( file.hs, interpreted )

fil.hs:15:18:
    Couldn't match expected type `a1' against inferred type `a'
      `a1' is a rigid type variable bound by
           an expression type signature at file.hs:15:25
      `a' is a rigid type variable bound by
          the type signature for `somemap' at file.hs:12:12
      Expected type: [a1]
      Inferred type: [a]
    In the second argument of `take', namely `xs'
    In the expression: take 5 xs :: [a]

file.hs:16:13:
    Couldn't match expected type `b1' against inferred type `b'
      `b1' is a rigid type variable bound by
           an expression type signature at file.hs:16:24
      `b' is a rigid type variable bound by
          the type signature for `somemap' at file.hs:12:17
    In the first argument of `map', namely `f'
    In the expression: map f some :: [b]
    In the definition of `ys': ys = map f some :: [b]
Failed, modules loaded: none.

Whereas if I just specify concrete types, substituting Int for a and Bool for b, no problem:

somemap :: (Int -> Bool) -> [Int] -> [Bool]
somemap f xs = ys
  where
    some = take 5 xs :: [Int]
    ys = map f some :: [Bool]

So my question is: How do I specify generic types and type constraints in a where-clause?

  • 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-03T04:16:24+00:00Added an answer on June 3, 2026 at 4:16 am

    Inside the where clause, the type variables a and b are new type variables; type variables aren’t scoped, so every type signature has a new supply of them, just as if they were defined at the top level.

    If you turn on the ScopedTypeVariables extension (put {-# LANGUAGE ScopedTypeVariables #-} at the top of your file), and change somemap‘s type declaration to:

    somemap :: forall a b. (a -> b) -> [a] -> [b]
    

    then the where clause definitions you specified will work correctly. I think the foralls are only required for backwards compatibility, so that code that reuses type variables in where clauses for polymorphic values doesn’t break.

    If you don’t want to use an extension, the alternative is to define ugly helper functions to unify the types, like asTypeOf.

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

Sidebar

Related Questions

Please excuse this numpty question, but when I do ChannelFuture future = channel.write(message); future.addListener(new
Please excuse me if there are already answers to this, but I can't quite
Please excuse my ignorance, but I have come across this term owner drawn listbox.
Now please excuse me if this is a stupid question - BUT... In my
Please excuse me if this is a very novice question, but I am little
Please excuse the simplicity and length of this, but I have a little test
Please excuse me if this question is really obvious but i've tried everything. I
Please excuse me if I'm simple here, I want to create a simple widget
Please excuse the noob-ish nature of this question. I have a project in which
Please excuse my lack of knowledge on the topic, but I have very little,

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.