[Haskell-cafe] typeclass and functional dependency problem

Martin DeMello martindemello at gmail.com
Tue Jan 10 09:28:30 CET 2012


I'm writing a Gtk2hs app, and I have several custom widgets that are
composite objects represented by records, one field of which is a
container widget. I am trying to write a replacement for gtk2hs's
boxPackStart

boxPackStart :: (BoxClass self, WidgetClass child) => self -> child ->
Packing -> Int -> IO ()

that will accept either a gtk widget or one of my custom widgets to
place in the box, and do the right thing. Here's my attempt at it;
what I want to know is why the commented out bit didn't work and I had
to individually add instances of widgets instead:

---------------------------------------------
-- packable objects
class WidgetClass w => Packable a w | a -> w where
    widgetOf :: a -> w

--instance WidgetClass w => Packable w w where
--    widgetOf = id

instance Packable Button Button where
    widgetOf = id

instance Packable Entry Entry where
    widgetOf = id

instance Packable Label Label where
    widgetOf = id

instance Packable Notebook Notebook where
    widgetOf = id

instance Packable HBox HBox where
    widgetOf = id

-- add widget to box
boxPackS :: (BoxClass b, WidgetClass w, Packable a w) => b -> a ->
Packing -> Int -> IO ()
boxPackS box child p i = boxPackStart box (widgetOf child) p i

---------------------------------------------

If I try to use

instance WidgetClass w => Packable w w where
    widgetOf = id

instead, I get the compilation error

Editor.hs:23:10:
    Functional dependencies conflict between instance declarations:
      instance Packable PairBox VBox -- Defined at Editor.hs:23:10-30
      instance WidgetClass w => Packable w w
        -- Defined at GuiUtils.hs:13:10-38

even though PairBox does not belong to WidgetClass.

martin



More information about the Haskell-Cafe mailing list