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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:17:43+00:00 2026-06-15T00:17:43+00:00

There are several generics libraries with numerous overlapping modules in just the Haskell Platform

  • 0

There are several generics libraries with numerous overlapping modules in just the Haskell Platform alone (syb, Data.Typeable, Data.Data, GHC.Generics), but I’m having trouble with a very basic generic programming task.

I want to be able to convert between types of the same shape, i.e. I want a polymorphic, typed conversion function between isomorphic types, essentially what is offered at the end of this paper(PDF) where indexed type families are mentioned.

I’m not concerned with scrapping my boilerplate, but rather with being able to build new libraries around sum and product abstractions.

The question below is in terms of GHC.Generic which I thought was closest to what I needed, but other solutions are welcome.


The following two types have the same shape

data Pair = Pair Char Int deriving (Generic, Show)
data Pair2 = Pair2 Char Int deriving (Generic, Show)

I want to convert values between them using GHC.Generics. The following fails to typecheck because of all the phantom parameters and other nonsense:

f :: Pair -> Pair2
f = to . from

Ultimately I want a function akin to fromInteger that has a polymorphic return value for any Generic (or whatever other class could support this) instance. I guess I’m looking for something like GHC.Generics:

--class:
type family NormalForm a
class ToGeneric a where
    to :: a -> NormalForm a
class FromGeneric b where
    from :: NormalForm b -> b

--examples:
data A = A Char Int deriving Show
data B = B Char Int deriving Show

type instance NormalForm A = (Char,Int)
instance ToGeneric A where
    to (A a b) = (a,b)
instance FromGeneric A where
    from (a,b) = A a b

type instance NormalForm B = (Char,Int)
instance ToGeneric B where
    to (B a b) = (a,b)
instance FromGeneric B where
    from (a,b) = B a b

-- the function I'm looking for
coerce :: (ToGeneric a, FromGeneric b, NormalForm a ~ NormalForm b)=> a -> b
coerce = from . to

With the above we can do everything I want:

*Main> (coerce $A 'a' 1) :: B
B 'a' 1
*Main> (coerce $A 'a' 1) :: A
A 'a' 1

EDIT: This is how Nathan Howell’s f function seems to work below, actually.

Questions

  1. Is this possible to do with libraries currently in the haskell platform?

  2. If not, could a library be defined that leveraged the existing deriving mechanism for Generic, Data, etc. without resorting to TH?

  • 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-15T00:17:44+00:00Added an answer on June 15, 2026 at 12:17 am

    If “of the same shape” means that datatypes are equal up to constructor names, record selectors and type synonyms then datatype conversion is as simple as traversing representation.

    {-# LANGUAGE DeriveGeneric #-}
    {-# LANGUAGE TypeOperators #-}
    {-# LANGUAGE MultiParamTypeClasses #-}
    {-# LANGUAGE FlexibleContexts, FlexibleInstances #-}
    
    import GHC.Generics
    
    conv
      :: (Generic a, Generic b, Conv (Rep a) (Rep b))
      => a -> b
    conv = to . cv . from
    
    class Conv a b where
      cv :: a x -> b x
    
    -- skip irrelevant parts: datatype name, constructor name, selector
    instance Conv f1 f2 => Conv (M1 i1 c1 f1) (M1 i2 c2 f2) where
      cv = M1 . cv . unM1
    
    instance (Conv a1 a2, Conv b1 b2) => Conv (a1 :*: b1) (a2 :*: b2) where
      cv ~(a :*: b) = cv a :*: cv b
    
    instance (Conv a1 a2, Conv b1 b2) => Conv (a1 :+: b1) (a2 :+: b2) where
      cv (L1 a) = L1 $ cv a
      cv (R1 b) = R1 $ cv b
    
    -- copy values
    instance Conv U1 U1 where cv = id
    instance Conv (K1 R c) (K1 R c) where cv = id
    

    Test case:

    data A = A1 String Int | A2 (Int,Int) deriving (Generic, Show)
    data B = B1 [Char] Int | B2 { xy :: (Int,Int) } deriving (Generic, Show)
    data X = X Int Int deriving (Generic, Show)
    
    *Main> conv $ X 3 14 :: (Int,Int)
    (3,14)
    *Main> conv $ A1 "hello" 42 :: B
    B1 "hello" 42
    *Main> conv $ A2 (13,42) :: B
    B2 {xy = (13,42)}
    

    Update

    A few more instances allow more interesting conversions:

    instance Conv U1 (M1 S s (K1 R ())) where
      cv _ = M1 $ K1 ()
    -- *> conv (Nothing :: Maybe Int) :: Either () Int
    -- Left ()
    
    instance Conv (M1 S s (K1 R ())) U1 where
      cv _ = U1
    -- *> conv (Left () :: Either () Int) :: Maybe Int
    -- Nothing
    
    -- this one requires OverlappingInstances
    instance (Generic c1, Generic c2, Conv (Rep c1) (Rep c2))
      => Conv (K1 R c1) (K1 R c2)
      where
        cv (K1 x) = K1 $ conv x
     -- *> conv (Right Nothing :: Either () (Maybe Int)) :: Maybe (Either () Int)
     -- Just (Left ())
    
     -- data List a = Empty | Cons a (List a) deriving (Generic, Show)
     -- *> conv [1,2,3::Int] :: List Int
     -- Cons 1 (Cons 2 (Cons 3 Empty))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every time I make a project I develop several generic routines/modules/libraries that I expect
There are several other SO questions talking about generics compiling OK w/ Eclipse's compiler
There are several threads on this here at SO but I didn't find one
There are several other questions about this topic that I have gone through, but
There are several variant questions on this on SO, but I did not find
There are several places on the internet that talk about having multy key dictionary
There are several ways google throws at me for checking if a file is
There are several good Javascript editors for Markdown / Textile (e.g.: http://attacklab.net/showdown/ , the
There are several resources out there that explain how the sandbox in Chrome works
There are several useful answers on SO regarding prevention of brute forcing a password

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.