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

  • Home
  • SEARCH
  • 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 7631715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:25:32+00:00 2026-05-31T06:25:32+00:00

Suppose I have a data type like data D a = D a a

  • 0

Suppose I have a data type like

data D a = D a a a

and a typeclass

class C c ...
instance (C c1, C c2) => C (c1, c2)

Then, I want to be able to write

data D a = D a a a deriving C

and have that generate an instance,

instance C ((a, a), a) => C (D a)

by using the modulo-lazy-evaluation isomorphism,

D a ~ ((a, a), a)

Note. Using a newtype and GeneralizedNewtypeDeriving will not work if, for example, one has data D m = D (m Integer) (m Integer).

Note 2. This question has relevance for Haskell expressiveness in general — languages like Python have something called named tuples, which can be used anywhere tuples are used; this question shows where/how I don’t know how to emulate the same thing in Haskell.

  • 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-31T06:25:33+00:00Added an answer on May 31, 2026 at 6:25 am

    You can do this relatively cleanly and efficiently using GHC 7.4’s generic programming support. The documentation for GHC.Generics may be helpful. Here’s an example.

    Consider the following example class and some sample instances:

    class C a where
      -- | Double all numbers
      double :: a -> a
    
    instance C Int where
      double i = 2 * i
    
    instance (C a, C b) => C (a, b) where
      double (a, b) = (double a, double b)
    

    We need some language pragmas and imports:

    {-# LANGUAGE TypeOperators, DefaultSignatures, DeriveGeneric, FlexibleContexts, FlexibleInstances #-}
    module Example where
    
    import GHC.Generics hiding(C, D)
    

    We now give some “generic instances”. The generic types all have a phantom parameter x, which makes the instance heads a little more complicated:

    -- "Insert" a normal value into a generic value
    instance C c => C (K1 i c x) where
      double (K1 c) = K1 (double c)
    
    -- Ignore meta-information (constructor names, type names, field names)
    instance C (f x) => C (M1 i c f x) where
      double (M1 f) = M1 (double f)
    
    -- Tuple-like instance
    instance (C (f x), C (g x)) => C ((f :*: g) x) where
      double (f :*: g) = double f :*: double g
    

    We now redefine our class C to take advantage of GC

    class C a where
      -- | Double all numbers
      double :: a -> a
    
      -- specify the default implementation for double
      default double :: (Generic a, C (Rep a ())) => a -> a
      double = to0 . double . from0
    
    -- from, with a more specialised type, to avoid ambiguity
    from0 :: Generic a => a -> Rep a ()
    from0 = from
    
    -- to, with a more specialised type, to avoid ambiguity
    to0 :: Generic a => Rep a () -> a
    to0 = to
    

    Now we can define some instances very easily:

    data D a = D a a a deriving Generic
    instance C a => C (D a)
    
    data D2 m = D2 (m Int) (m Int) deriving Generic
    instance C (D2 D)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I want to have a class like Java Date . Its only data
Suppose that I have a class like this class Employee: pass I create two
Suppose that in a Haskell program I have some data whose type is something
Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();
Suppose I have a class named Data . Another class annotates one of its
Suppose I have 500 rows of data, each with a paragraph of text (like
Suppose I have the following types of data: class Customer { String id; //
Suppose I have a data type enum TreeTypes { TallTree, ShortTree, MediumTree } .
Suppose I have a bunch of Clojure data structures, all of the same type
Let's suppose I have a Winforms app with a Dictionary that looks like this:

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.