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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:16:45+00:00 2026-06-08T15:16:45+00:00

I have a utility function that enumerates all values of a type that is

  • 0

I have a utility function that enumerates all values of a type that is both enumerable and bounded:

enumerate :: (Enum a, Bounded a) => [a]
enumerate = [minBound .. maxBound]

and a data type that involves mapping enumerable types to integers:

data Attribute a = Attribute { test :: a -> Int
                             , vals :: [Int]
                             , name :: String }

Where vals is the list of integers representing all possible enumerable values. For example, if I had

data Foo = Zero | One | Two deriving (Enum,Bounded)

then vals would be [0,1,2].

I want to be able to create these attributes programatically, just given a function that maps an a to an enumerable type, and a name. Something like this:

attribute :: (Enum b, Bounded b) => (a -> b) -> String -> Attribute a
attribute f str = Attribute (fromEnum . f) vs str
  where
    vs = map fromEnum enumerate

This doesn’t typecheck, because there’s no way of connecting the call to enumerate with the b in the type signature. So I thought I could do this:

vs = map fromEnum $ enumerate :: [b]

but that doesn’t compile either – the compiler renames that b to b1. I tried to be smarter, using the GADTs extension:

attribute :: (Enum b, Bounded b, b ~ c) => {- ... -}
vs = map fromEnum $ enumerate :: (Enum c,Bounded c) => [c]

but again, the c is renamed to c1.

I don’t want to include the type of b as a parameter in the Attribute type (mainly because I want to store lists of attributes with potentially different values of b – that’s why test has type a -> Int and vals has type [Int]).

How can I write this code so that it does what I want it to do?

  • 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-08T15:16:47+00:00Added an answer on June 8, 2026 at 3:16 pm

    The problem with type variables is that they are only bound in the type signature. Any use of type variables inside definition will refer to new, fresh type variable (even though it has the exact same name as in the type signature).

    There are two ways to refer to type variables from signature: ScopedTypeVariables extension and asTypeOf.

    With ScopedTypeVariables a type variable explicitly bound with forall is also available in definition, thus:

    attribute :: forall a b. (Enum b, Bounded b) =>
                 (a -> b) -> String -> Attribute a
    attribute f str = Attribute (fromEnum . f) vs str
      where
        vs = map fromEnum (enumerate :: [b])
    

    The other way involves function asTypeOf defined as:

    asTypeOf :: a -> a -> a
    asTypeOf = const
    

    If we can get an expression of type [b] into the second parameter, unification will make sure that first parameter also has type [b]. Because we have f :: a -> b and f undefined :: b, we can write:

    attribute :: (Enum b, Bounded b) => (a -> b) -> String -> Attribute a
    attribute f str = Attribute (fromEnum . f) vs str
      where
        vs = map fromEnum (enumerate `asTypeOf` [f undefined])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an utility function which I include in almost all Javascript code
I have a multi-threading Python program, and a utility function, writeLog(message) , that writes
I have a utility function that will shuffle the elements of any Vector, but
I'd like to have a utility function that conditionally updates my request and response
I have tried to write an utility function that can convert my custom data
I've created a small utility function for string conversion so that I don't have
I have a utility function in my program for searching for entities. It takes
Suppose I have a utility function like this - public static boolean isABlankSpace(char c)
I have a utility class that creates & returns a db connection: Public Shared
I have a utility build script that gets called from a variety of project-specific

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.