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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:40:12+00:00 2026-06-09T01:40:12+00:00

I would like to do some magic in a library, allowing a product type

  • 0

I would like to do some magic in a library, allowing a product type to be destructured polymorphically. This is a more or less working mockup illustrating what I’d like to do:

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances, UndecidableInstances #-} 
newtype Wrapped a = Wrapped { unwrap :: a } 

-- our example structure
ex :: (Int, (Int, Int))
ex = (1,(2,3))

class WrapDecomp x y | y -> x where
    decomp :: x -> y

instance (WrapDecomp x x', WrapDecomp y y')=> WrapDecomp (x,y) (x',y') where
    decomp (x,y) = (decomp x, decomp y)

instance WrapDecomp x (Wrapped x) where
    decomp = Wrapped


example = let w = decomp ex
              (w0, w1) = decomp ex
              (w0', (w1', w2')) = decomp ex :: (Wrapped Int, (Wrapped Int, Wrapped Int))
           in print $ ( unwrap w, unwrap w0, unwrap $ snd w1, unwrap $ fst w1 )
         -- Also works:
         -- in print $ ( unwrap w, unwrap w0, unwrap w1 )

My actual application is a library, and will have two properties that make the warts I noticed in the above acceptable:

  1. the Wrapped type constructor is not exported

  2. the user will always be calling unwrap on all Wrapped data in a binding (because of boring details of my application), so there shouldn’t be ambiguity in practice

The consensus seems to be that UndecidableInstances isn’t really bad, but I’d like to be sure the above is kosher before I proceed.


Update w/ solution

I puzzled with this for a bit, but I was able to solve my problem with TypeFamilies as follows:

{-# LANGUAGE TypeFamilies #-}
class Out a where
    type In a :: *
    decomp :: In a -> a

instance Out (Wrapped a) where
    type In (Wrapped a) = a
    decomp = Wrapped

instance (Out a, Out b)=> Out (a,b) where
    type In (a,b) = (In a,In b)
    decomp (x,y) = (decomp x, decomp y)
  • 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-09T01:40:14+00:00Added an answer on June 9, 2026 at 1:40 am

    The use of UndecidableInstances alone is kosher in general, what UndecidableInstances does is allow the type checker to try to resolve instances when it can’t prove in advance that it will finish. If it does, the code is no less safe than if termination could have been proved in advance.

    However, with your instances, you can create a situation where the type checker would loop with an expression that causes a constraint WrapDecomp x (x,y), for example

    foo x = [fst $ decomp x, x]
    

    The use of fst requires decomp x to have type (a,b) for some types a and b, thus an instance WrapDecomp t (a,b) where t is the type of x. Being in the same list requires that x have tape a too, so an

    instance WrapDecomp a (a,b)
    

    The only instance with a pair for the second parameter is

    instance (Wrapdecomp x x', WrapDecomp y y') => WrapDecomp (x,y) (x',y')
    

    hence a = (x,y) for some types x and y and the constraint of foo becomes

    WrapDecomp (x,y) ((x,y),b)
    

    The pairs instance says there is such an instance if there are instances

    WrapDecomp y b
    

    and

    WrapDecomp x (x,y)
    

    which is the exact form of instance we started with.

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

Sidebar

Related Questions

When I'm working on Android project I would like to use some magic shortcut
I would like to do some magic in the moment instance and class methods
I would like some help about an Android Application. I used the new Google
I would like some help parsing a dynamic (folder.subfolders) xml with xslt, I tried
I would like some container that I can very efficiently append a variable amount
I would like some sugestion on how to design a table that gets like
I would like some assistance on creating a depreciation run in X++ the variables
I would like some advice on the best approach to use in the following
I would like some of my preferences to have icons, like the Settings app.
I am developing a site and i would like some simple markup. I would

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.