[Haskell-cafe] Re: overlapping instances, selecting if type a does not belong to class?

Marc Weber marco-oweber at gmx.de
Tue Feb 27 12:10:27 EST 2007


> However, it seems that your particular problem can be solved with
> simpler means:
> 
> > instance (HList a) => HListAppendArbitrary a HNil a where
> >   hAppendArbitrary a _ = a
> > instance (HList a, HList b, HList c) 
> >     => HListAppendArbitrary a (HCons b d) c where
> >   hAppendArbitrary a b = hAppend a b -- or use HCons with recursion
> >
> > -- overlapping instance HList + value (2)
> > instance (HList a, HList c)
> >   => HListAppendArbitrary a b c where
> >   hAppendArbitrary a b = HCons b a

It looks like this, doesn't it ?
(Some class isntance declarations are still missing)
============= code ===================================================
class (HList c) => HListAppendArbitrary a b c | a b -> c where
  hAppendArbitrary :: a -> b -> c

-- instances HList + HList
instance HListAppendArbitrary HNil        HNil        HNil where   -- line  134
  hAppendArbitrary _ _ = HNil
instance HListAppendArbitrary (HCons a b) HNil        (HCons a b) where
  hAppendArbitrary a _ = a
instance HListAppendArbitrary HNil        (HCons a b) (HCons a b) where
  hAppendArbitrary _ a = a
instance HListAppendArbitrary (HCons a b) (HCons b c) (HCons a l) where
  hAppendArbitrary a b = hAppend a b

-- instance HList + value
instance HListAppendArbitrary HNil         b (HCons b HNil) where  -- line 143
  hAppendArbitrary _ b = HCons b HNil
instance HListAppendArbitrary (HCons a b)  c d where
  hAppendArbitrary a b = hAppend a (HCons b HNil)

-- instance value + HList
instance HListAppendArbitrary b            HNil c where
  hAppendArbitrary b _ = HCons b HNil
instance HListAppendArbitrary a            (HCons b c) d where
  hAppendArbitrary a b = hAppend (HCons a HNil) b
============= end code ===============================================

But I'm getting the same error twice:

hps-lib/HPS/Utils.hs|134| 0:
||     Functional dependencies conflict between instance declarations:
||       instance [incoherent] HListAppendArbitrary HNil HNil HNil
-- Defined at hps-lib/HPS/Utils.hs|134| 0
||       instance [incoherent] HListAppendArbitrary HNil b (HCons b HNil)
-- Defined at hps-lib/HPS/Utils.hs|143| 0

If you compare those two lines g
instance HListAppendArbitrary HNil        HNil        HNil where   -- line  134
instance HListAppendArbitrary HNil         b          (HCons b HNil) where  -- line 143

I see that HNil HNil -> HNil 
doesn't fit
	   HNil b (thus maybe HNil) -> HCons b HNil

But I don't want to omit the funtcional dependency because the resulting
type should be determined by the first two parameters.

As I don't know how to solve this right now I'll use HLists anywhere.

Marc


More information about the Haskell-Cafe mailing list