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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:03:01+00:00 2026-06-13T05:03:01+00:00

class Listy a b where fromList :: [b] -> a toList :: a ->

  • 0
class Listy a b where
   fromList :: [b] -> a
   toList :: a -> [b]
   lifted :: ([b] -> [b]) -> (a -> a) 
   lifted f = fromList . f . toList

data MyString = MyString { getString :: String } deriving Show

instance Listy MyString Char where
  toList = getString
  fromList = MyString

Now I need to write e.g. lifted (reverse::(String -> String)) (MyString "Foobar"). Is there a trick to avoid the need for the type signatures?

  • 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-13T05:03:02+00:00Added an answer on June 13, 2026 at 5:03 am

    Essentially the problem is that setting the type of a does not tell the compiler what the type of b is. You might think that since there is only one instance of the class (where a is MyString and b is Char), but anybody can add new instances at any time. So the fact that there’s only one instance now doesn’t help the compiler decide what types you want.

    The solution to this is either to use Functional Dependencies, or Type Families. The latter is the newer solution and is intended to eventually “replace” the former, but right now both are still fully supported. Whether FDs ever go away remains to be seen. Anyway, with FDs:

    class Listy a b | a -> b where ...
    

    Essentially this says “there can only ever be one class instance for each a“. In other words, once you know a, you can always determine b. (But not the reverse.) The rest of the class looks like it did before.

    The alternative is TFs:

    class Listy a where
      type Element a :: *
      ...
    
    instance Listy MyString where
      type Element MyString = Char
      ...
    

    Now instead of the second type being called b, it’s called Element a. The word Element acts like a class method that takes a listy type and returns the corresponding element type. You can then do

    instance Listy ByteString where
      type Element ByteString = Word8
      ...
    
    instance Listy [x] where
      type Element [x] = x
      ...
    
    instance Ord x => Listy (Set x) where
      type Element (Set x) = x
      ...
    

    and so on. (Not that Listy necessarily makes sense for all the types above; these are just examples of how to define the class.)

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

Sidebar

Related Questions

I have my class X : public class ClassX { public virtual IList<ClassY> ListY
I have a list of ProjectItem class instances: List<ProjectItem> allProjects . I need to
I am trying to implement a new class inherited from List<string> , to load
I'm using a class that is inherited from list as a data structure: class
So I have created the following script: class spImageUpload() { private static System.Collections.Generic.List<string> keywords;
I want to convert from public class Party { public string Type { get;
We're using a class library that performs calculations on 3D measurement data, it exposes
i have Two list ,List<String> and List<Item>. public class Item { private String name;
I want to derive a class from list , add a few instance attributes
I'm creating a class that derives from List... public class MyList : List<MyListItem> {}

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.