Hi Haskell Cafe,<br><br>I'm finding that I really like type families. For instance, the GHC.List.lookup and Data.Map.lookup functions annoy me because their names clash, yet their type are so similar. With type families, I could define a more generic lookup function like this:<br>
<br>import Data.Map as MAP<br>import GHC.List as LIST<br><br>class MapType ma where<br> type Key ma<br> type Item ma<br> lookup :: Key ma -> ma -> Maybe (Item ma)<br><br>instance (Ord ka) => MapType (MAP.Map ka a) where<br>
type Key (MAP.Map ka a) = ka<br> type Item (MAP.Map ka a) = a<br> lookup ka ma = MAP.lookup ka ma<br><br>instance (Eq ka) => MapType [(ka, a)] where<br> type Key [(ka, a)] = ka<br> type Item [(ka, a)] = a<br>
lookup ka ma = LIST.lookup ka ma<br><br>This lookup function works on both "Map ka a" and "[(ka, a)]" types and I no longer need to qualify my lookup function with the module name.<br><br>The downside I suppose is that lookup is no longer a function that can be manipulated freely:<br>
<br>*Main> let x = lookup<br>*Main> let y = Fx.Data.Map.lookup<br><br><interactive>:1:8:<br> Ambiguous type variable `ma' in the constraint:<br> `Fx.Data.Map.MapType ma'<br> arising from a use of `Fx.Data.Map.lookup' at <interactive>:1:8-25<br>
Probable fix: add a type signature that fixes these type variable(s)<br><br>A shame that. I had been hoping it would be possible to have a generic lookup function that could be used in every way the current collection of various lookup functions can be used.<br>
<br>So much nicer if 'y' above could be bound to the Fx.Data.Map.lookup with the same type:<br><br>*Main> :t Fx.Data.Map.lookup<br>Fx.Data.Map.lookup :: forall ma.<br> (Fx.Data.Map.MapType ma) =><br>
Fx.Data.Map.Key ma -> ma -> Maybe (Fx.Data.Map.Item ma)<br><br>And then have the ambiguity resolve later when 'y' is actually used.<br><br>-John<br><br>