monadic绑定的显式签名中的约束(Constraints in explicit signatures for monadic bindings)
假设我有以下功能:
loadDialog :: String -> IO MyDialog loadDialog file = do Just ui <- xmlNew file MyDialog <$> xmlGetWidget ui castToWindow "w" <*> xmlGetWidget ui castToButton "b"
哪里
xmlGetWidget :: WidgetClass widget => GladeXML -> (GObject -> widget) -> String -> IO widget
现在我想捕获以下xmlNew / xmlGetWidget使用模式:
widgetBinder :: WidgetClass widget => FilePath -> IO ((GObject -> widget) -> String -> IO widget) widgetBinder file = do Just ui <- xmlNew file return $ xmlGetWidget ui
哪个应该允许我写:
loadDialog file = do bind <- widgetBinder file MyDialog <$> bind castToWindow "w" <*> bind castToButton "b"
问题是,它没有类型检查( 这里确切的错误)。 我认为可以明确地为绑定提供通用签名,但是对于monadic绑定似乎不是这种情况,因为以下也没有进行类型检查(即使有RankNTypes, 这里也是错误的):
loadDialog file = do bind :: WidgetClass w => (GObject -> w) -> String -> IO w <- widgetBinder file MyDialog <$> bind castToWindow "w" <*> bind castToButton "b"
有什么我可以做的吗?
Let's say I have the following function:
loadDialog :: String -> IO MyDialog loadDialog file = do Just ui <- xmlNew file MyDialog <$> xmlGetWidget ui castToWindow "w" <*> xmlGetWidget ui castToButton "b"
where
xmlGetWidget :: WidgetClass widget => GladeXML -> (GObject -> widget) -> String -> IO widget
Now I want to capture the following xmlNew/xmlGetWidget usage pattern:
widgetBinder :: WidgetClass widget => FilePath -> IO ((GObject -> widget) -> String -> IO widget) widgetBinder file = do Just ui <- xmlNew file return $ xmlGetWidget ui
Which should allow me to write:
loadDialog file = do bind <- widgetBinder file MyDialog <$> bind castToWindow "w" <*> bind castToButton "b"
The problem is, it doesn't typecheck (exact error here). I've thought it's possible to provide generic signatures to bindings explicitly, but it seems this is not the case for monadic bindings since the following doesn't typecheck as well (even with RankNTypes, error here):
loadDialog file = do bind :: WidgetClass w => (GObject -> w) -> String -> IO w <- widgetBinder file MyDialog <$> bind castToWindow "w" <*> bind castToButton "b"
Is there anything I can do?
原文:https://stackoverflow.com/questions/20803484