1

すでに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

私はアイデアがありません。

4

1 に答える 1