Hi. <br>I have a type inference related problem which causes me some confusion. The following is a simplified example capturing the key aspects. <br>First of all, consider a data type (here called) Elem and a type class IsElem.
<br><br>data Elem = E<br><br>class IsElem a where<br> toElem :: a -> Elem<br><br><br>instance IsElem Elem where<br> toElem = id<br><br>instance Show a => IsElem a where<br> toElem _ = E<br><br>Values of type Elem along with all instances of Show, instansitates the IsElem class.
<br>What I want to achieve is the enabling of definitions of classes/member functions corresponding to the following.<br><br>class SetId t v r | t -> r where<br> setId :: t -> v -> r<br> <br>And to instantiate the class, allowing 'setId' to produce values of type Elem as in
<br><br>instance SetId Elem String Elem where<br> setId x _ = x<br><br>Note that the fundep (t -> r) enables the type checker to uniquely determine the return type of setId, depending on it's first argument.<br>It is now possible to write function definitions according to
<br><br>myElem = E `setId` "id"<br><br>Checking the type of 'myElem' using ghci confirms that it has type 'Elem'. (Because of the E and the fundep)<br>So far, so good... However, problems arises trying to define the function :
<br><br>test = toElem myElem<br><br>Yielding the error message : 'No instance for (Show Elem) arising use of `toElem` at ...'<br><br>For some reason it seems like the type checker picks the *wrong* 'toElem', and that the type of 'myElem' can't be properly determined (without adding explicit type signatures). It seems strange though the the type of myElem really can be determined and that the instance (IsElem Elem) is more specific then the instance for Show types.
<br><br>Anyway, I just realised that turning off the 'monomorphism-restriction' solves the problem. (Don't know why though). <br>Is this *the* recommended solution here and is there any general policy regarding the flag ?
<br><br>Help/explanations are appreciated!<br><br>Thanks a lot<br>/Joel<br><br>