UndecidableInstances and newtypes

I have a problem with UndecidableInstances that I could not figure out how to avoid using newtype . Here is what I originally:

 {-# LANGUAGE TypeFamilies, FlexibleContexts #-} class Record r where key :: r -> String class (Record r) => SizedRecord r where size :: r -> Int class Database d where type DBRecord d class (Record a) => Agent a where agentId :: a -> String agentId = key class (Database (UAgentDB u), Agent (UAgent u), Record (UAgent u)) => Universe u where type UAgent u type UAgentDB u -- plus other stuff data SimpleUniverse d = SimpleUniverse { suDB :: d -- plus other stuff } deriving (Show, Eq) instance (Record (DBRecord d)) => Universe (SimpleUniverse d) where -- line 28 type UAgent (SimpleUniverse d) = DBRecord d type UAgentDB (SimpleUniverse d) = d -- plus other stuff 

The message I receive is

 amy9.hs:28:10: Constraint is no smaller than the instance head in the constraint: Record (DBRecord d) (Use -XUndecidableInstances to permit this) In the instance declaration for `Universe (SimpleUniverse d)' 

I want to avoid UndecidableInstances because this code will be in a reusable library, so I will try to declare newtype :

 newtype SimpleUniverse2 u = SimpleUniverse2 { fromAdditiveGroup :: u } instance (Record (DBRecord u)) => Universe (SimpleUniverse2 u) where type UAgent (SimpleUniverse2 u) = DBRecord u type UAgentDB (SimpleUniverse2 u) = u -- plus other stuff 

But I get the same error. I read the answers to other questions about UndecidableInstances , but I could not solve this problem.

+7
haskell
source share
1 answer

Like an awful kludge, double packaging and using FlexibleInstances seems to do the trick:

 import Control.Monad.Identity instance (Database u, Agent (DBRecord u), Record (DBRecord u)) => Universe (Identity (Identity u)) where type UAgent (Identity (Identity u)) = DBRecord u type UAgentDB (Identity (Identity u)) = u 
+1
source share

All Articles