1 {-# LANGUAGE UndecidableInstances, EmptyDataDecls #-}

2 {-# LANGUAGE RankNTypes, KindSignatures, MultiParamTypeClasses, FlexibleInstances #-}

4 -- Works with new constraint solver

10 -- minimal Data/Rep classes

17 --------- Version A: failed in 6.12.3 -----------

18 -- Substitution class

19 -- substitute [a -> t] t'.

25 -- Allow override dictionary verion with implementation of type class Subst

29 -- Generic instance

33 --------- Version B: passed in 6.12.3 -----------

34 -- Substitution class

35 -- substitute [a -> t] t'.

41 -- allow override dictionary verion with implementation of type class Subst

45 -- generic instance

50 {- Commentary from Trac #3018

52 Here are the key lines of code:

54 class Subst a t t' where

55 subst :: (Monad m) => a -> t -> t' -> m t'

57 data SubstD a t t'

58 = SubstD (forall m. Monad m => a -> t -> t' -> m t')

60 instance Data (SubstD a t) t' => Subst a t t' -- (1)

62 instance Subst a t t' => Sat (SubstD a t t') where -- (2)

63 dict = SubstD subst

65 The call to 'subst' on the last line gives rise to a constraint (Subst

66 a t t'). But that constraint can be satisfied in two different ways:

68 Using the instance declaration for Subst (which matches anything!)

69 Using the context of the Sat (SubstD ..) instance declaration itself

71 If GHC uses (1) it gets into a corner it can't get out of, because now

72 it needs (Data (SubstD a t) t'), and that it can't get. The error

73 message is a bit misleading:

75 T3018.hs:29:28:

76 Could not deduce (Data (SubstD a t) t') from the context (Monad m)

77 arising from a use of `subst' at T3018.hs:29:28-32

79 it should really say

81 ...from the context (Subst a t t', Monad m)

83 but that's a bit of a separate matter.

85 Now, you are hoping that (2) will happen, but I hope you can see that

86 it's delicate. Adding the (Monad m) context just tips things over the

87 edge so that GHC doesn't "see" the (Subst a t t') in the context until

88 too late. But the real problem is that you are asking too much. Here

89 is a simpler example:

91 f :: Eq [a] => a -> blah

92 f x = let g :: Int -> Int

93 g = ....([x]==[x])...

94 in ...

96 The use of == requires Eq [a], but GHC will probably use the list

97 equality instance to simplify this to Eq a; and then it can't deduce

98 Eq a from Eq [a]. Local constraints that shadow or override global

99 instance declarations are extremely delicate.

101 All this is perhaps soluble if GHC were to be lazier about solving

102 constraints, and only makes the attempt when it has all the evidence

103 in hand. I'm thinking quite a bit about constraint solving at the

104 moment and will bear that in mind. But I can't offer you an immediate

105 solution. At least I hope I've explained the problem.

106 -}