如何构建自定义阅读器monad以及自定义类型类?

时间:2016-06-26 05:33:28

标签: haskell monads monad-transformers reader-monad

我试图将http://lexi-lambda.github.io/blog/2016/06/12/four-months-with-haskell/(标题为" Typeclasses可以效仿效果")的方法与某种本土的读者monad结合起来。

我试图解决的整体问题是避免传递配置变量几乎在我的应用程序中运行。我无法使用ReaderT的原因是因为我的很多函数都在SqlPersistT中,它本身在内部使用了ReaderT。另一个原因是更好地学习所有这些心理体操。

我的两个问题在下面的代码中作为评论给出。在这里再现它们:

  • 定义NwMonad的最合适方式是什么?
  • 因此,如何将NwMonad定义为HasNwConfig的实例?如何编写askNwConfig的函数体?
  • 我如何最终致电runNwMonad?它的论点是什么?

以下是代码:

data NwConfig = NwConfig {
  _googleClientId :: T.Text,
  _googleClientSecret :: T.Text,
  _tgramBotToken :: String,
  _aria2Command :: String,
  _aria2DownloadDir :: String
  }
$(makeLenses ''NwConfig)

instance Default NwConfig where
  def = NwConfig{}

class MonadIO m => HasNwConfig m where
  askNwConfig :: m NwConfig

startAria2 :: (HasNwConfig m) => m Sytem.Process.ProcessHandle
  cfg <- askNwConfig
  (_, _, _, processHandle) <- createProcess $ proc (cfg ^. aria2Command) []
return processHandle


-- QUESTION: Is this correct?
data NwMonad a = NwMonad{runNwMonad :: (NwConfig -> IO a)}
               deriving (Functor, Applicative, Monad, MonadIO)

-- Or is this the way to do it?
data NwMonad a = NwMonad{runNwMonad :: IO a, nwConfig :: NwConfig}
               deriving (Functor, Applicative, Monad, MonadIO)

instance HasNwConfig NwMonad where
  askNwConfig = return . nwConfig -- QUESTION: How to write this?

main :: IO ()
main = do
  [cId, cSecret, botToken] <- sequence [getEnv "GOOGLE_CLIENT_ID", getEnv "GOOGLE_CLIENT_SECRET", getEnv "TELEGRAM_TOKEN"]
  let cfg = (def :: NwConfig)
        & googleClientId .~ (T.pack cId)
        & googleClientSecret .~ (T.pack cSecret)
        & tgramBotToken .~ botToken
        & aria2Command .~ "/Users/saurabhnanda/projects/nightwatch/aria2-1.19.3/bin/aria2c"
  -- QUESTION: How do I use this now?
  runNwMonad $ (?????) $ startAria2

1 个答案:

答案 0 :(得分:1)

这里有一些代码,展示了如何在同一个变换器堆栈中使用多个Reader环境。此处BaseMonad与您的SqlPersistT

类似
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.IO.Class

type BaseMonad = ReaderT String IO

type NwMonad = ReaderT Int BaseMonad

askString :: NwMonad String
askString =  lift ask

askInt :: NwMonad Int
askInt = ask

startAria :: NwMonad ()
startAria = do
  i <- askInt
  s <- askString
  liftIO $ putStrLn $ "i: " ++ show i ++ " s: " ++ s

main = do
  let cfg = 10       -- i.e. your google client data
      s = "asd"      -- whatever is needed for SqlPersistT
  runReaderT (runReaderT startAria cfg) s

以下是使用SqlPersisT类型和runSqlConn的一些代码:

import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.IO.Class
import Database.Persist.Sql

data Config = Config { _clientId :: String }

type BaseMonad = SqlPersistT IO

type NwMonad = ReaderT Config BaseMonad

askBackend:: NwMonad SqlBackend
askBackend =  lift ask

askConfig :: NwMonad Config
askConfig = ask

startAria :: NwMonad ()
startAria = do
  cfg <- askConfig
  liftIO $ putStrLn $ "client id: " ++ (_clientId cfg)

main = do
  let cfg = Config "foobar"
      backend = undefined :: SqlBackend -- however you get this
      sqlComputation = runReaderT startAria cfg :: SqlPersistT IO ()
  runSqlConn sqlComputation backend :: IO ()

<强>更新

环境的类型并不重要。

import Control.Monad.Reader
import Control.Monad.IO.Class

type Level1  =  ReaderT Int IO
type Level2  =  ReaderT Int Level1
type Level3  =  ReaderT Int Level2

ask3 :: Level3 Int
ask3 = ask

ask2 :: Level3 Int
ask2 =  lift ask

ask1 :: Level3 Int
ask1 =  lift $ lift $ ask

doit :: Level3 ()
doit = do
  r1 <- ask1
  r2 <- ask2
  r3 <- ask3
  liftIO $ print (r1, r2, r3)

main = do
  runReaderT (runReaderT (runReaderT doit 333) 222) 111