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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:15:32+00:00 2026-05-23T21:15:32+00:00

Consider the following type-class: class Listable a where asList :: a t -> [t]

  • 0

Consider the following type-class:

class Listable a where
   asList :: a t -> [t]

It’s easy enough to create instances for types with one parameter:

instance Listable [] where
   asList = id

instance Listable Maybe where
   asList (Just x) = [x]
   asList Nothing = []

Now how would I make an instance for a pair with two identical type parameters? Of course I could do some wrapping:

data V2 a = V2 a a

v2 (p,q) = V2 p q

instance Listable V2 where
   asList (V2 p q) = [p,q]

Now I could write things like asList $ v2 (47, 11), but this kind of defeats the purpose.

Is there a way to limit the type of pair to cases where both type parameters are equal, and to write a Listable instance for that? If not, what is the usual workaround?

  • 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-23T21:15:33+00:00Added an answer on May 23, 2026 at 9:15 pm

    There are lots of ways to do this, conceptually. Unfortunately, most of them don’t actually work. Alas!

    First, as a functional programmer, I’d wager that this is what you really want to write:

    instance Listable (\a -> (a, a)) where
        asList (p, q) = [p,q]
    

    Unfortunately type-level lambdas don’t exist. We could write a named version of the above lambda using a type synonym:

    type Same2 f a = f a a
    
    instance Listable (Same2 (,)) where { ... }
    

    That’s not allowed either, because the type synonym isn’t fully-applied. We could instead imagine the type class taking an extra argument that would describe how to apply the type variables:

    class Listable app f where
        asList :: app f a -> [a]
    
    instance Listable __ Maybe where { ... }
    
    instance Listable __ (,) where { ... }
    

    Without even thinking about what app might be, this also fails because we don’t have a consistent kind for the f parameter.

    Moving on to things that actually work, I think the most common way is to wrap the type synonym approach inside a newtype, then just deal with the wrapping and unwrapping that involves.

    newtype Same2 f a = Same2 (f a a)
    
    instance Listable (Same2 (,)) where { ... }
    

    It’s workable, if a bit ugly. You can also define type constructor composition and other toys this way, then go nuts with type-level points-free expressions buried under a pile of hoop-jumping boilerplate.

    As a final approach, you can also encode the lambda-style approach above “in reverse”, going from the fully-applied version, to the single type parameter:

    class Listable t where
        type ListableElem t :: *
        asList :: t -> [ListableElem t]
    

    Being able to do this sort of thing is one of the main motivations for type families. The same thing can be expressed with MPTCs and fundeps, but it’s 1) equivalent and 2) much uglier, so I won’t bother writing it out.

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

Sidebar

Related Questions

Consider the following nested type: public class Book { private class Chapter { }
Consider the following class, with the inner struct Y being used as a type,
Let's consider I want to create the following hierarchy: Animal -> Class (mammal, amphibian,
Please consider the following code: type TFoo1 = class public procedure DoSomething1; end; TFoo2
So, please consider the following situation I have a super class of type Shapes
Consider my first attempt, a simple type in F# like the following: type Test()
Consider the following table: mysql> select * from phone_numbers; +-------------+------+-----------+ | number | type
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider following class class test { public: test(int x){ cout<< test \n; } };
How to get the underlying type of an Observable collection using reflection? Consider following

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.