我已经在Reddit上询问过,但想向更广泛的圈子寻求帮助。
这是一个包含代码的存储库,您可以为最小的测试用例运行它:https ://github.com/cideM/co_log_issue
如果你跑步stack build
,你会得到:
• Could not deduce (HasLog
(AppEnv App) Message (Scotty.ActionT TL.Text m))
而且我不知道如何编写这个实例。
我正在尝试比较co-log
和Katip
。我有一个 Scotty 路由处理程序(更准确地说,它是处理程序的包装器)并且在该处理程序内部,我想在我的应用程序环境中修改日志操作。这里的用例是添加到记录器的上下文中,以便所有后续的日志操作都自动附加一个字符串或类似的东西。
这是处理程序的相关部分:
withSession ::
( WithLog (AppEnv App) Message m,
MonadIO m
) =>
SQLite.Connection ->
(Session -> Scotty.ActionT TL.Text m ()) ->
Scotty.ActionT TL.Text m () ->
Scotty.ActionT TL.Text m ()
withSession dbConn handler defaultAction =
withLog (cmap (\(msg :: Message) -> msg {msgText = "foo"})) $ do
log I "Hi"
sessionCookie <- Scotty.getCookie "lions-session"
...
该withLog
函数会导致错误:
• Occurs check: cannot construct the infinite type:
m ~ Scotty.ActionT TL.Text m
Expected type: Scotty.ActionT TL.Text m ()
Actual type: Scotty.ActionT TL.Text (Scotty.ActionT TL.Text m) ()
这是有道理的,因为do
之后块中的所有内容withLog
都是Scotty.ActionT TL.Text m()
并且我无法在同一范围内提升它。我有一个类似的问题katip
。
由于 GHC 错误,我无法派生实例:
The exact Name ‘f’ is not in scope
Probable cause: you used a unique Template Haskell name (NameU),
perhaps via newName, but did not bind it
If that's it, then -ddump-splices might be useful
即使没有那个错误,我也不确定它是否可以推导出来。我试图只使用转储的派生实例(即使生成的代码没有编译)但我最终无法让它工作:
deriving instance HasLog (AppEnv App) Message (Scotty.ActionT TL.Text App)
给我
instance HasLog (AppEnv App) Message (Scotty.ActionT TL.Text App) where
getLogAction
= coerce
@(AppEnv App -> LogAction (ExceptT (Scotty.ActionError TL.Text) (ReaderT Scotty.ActionEnv (StateT Scotty.ScottyResponse App))) Message)
@(AppEnv App -> LogAction (Scotty.ActionT TL.Text App) Message)
(getLogAction
@(AppEnv App) @Message
@(ExceptT (Scotty.ActionError TL.Text) (ReaderT Scotty.ActionEnv (StateT Scotty.ScottyResponse App)))) ::
AppEnv App -> LogAction (Scotty.ActionT TL.Text App.App) Message
哪个不见了
No instance for (HasLog
(AppEnv App)
Message
(ExceptT
(Scotty.ActionError TL.Text)
(ReaderT Scotty.ActionEnv (StateT Scotty.ScottyResponse App))))
我无法推导出
deriving instance HasLog (AppEnv App) Message (ExceptT (Scotty.ActionError TL.Text) (ReaderT Scotty.ActionEnv (StateT Scotty.ScottyResponse App)))
Can't make a derived instance of
‘HasLog
(AppEnv App)
Message
(ExceptT
(Scotty.ActionError TL.Text)
(ReaderT Scotty.ActionEnv (StateT Scotty.ScottyResponse App)))’
(even with cunning GeneralizedNewtypeDeriving):
cannot eta-reduce the representation type enough
我没主意了。