如何在Spock请求处理程序中使用`IO String`?

时间:2016-02-13 15:47:18

标签: haskell io-monad haskell-spock

我有以下函数生成1024的倍数的随机字符串:

import System.Random

rchars :: Int -> [IO Char]
rchars n = map (\_ -> randomRIO ('a', 'z')) [n | n <- [0..n]] -- a wasteful "iteration"-like func

rstr :: Int -> IO String
rstr n = sequence $ rchars (1024 * n)

我想使用Spock将其暴露给网络,例如:

import Data.Monoid
import Data.Text
import Lib
import Web.Spock.Safe

main :: IO ()
main =
    runSpock 8080 $ spockT id $
    do get root $
           redirect "/data/1"
       get ("data" <//> var) $ \n ->
          do
           str <- rstr n
           text ("boo:" <> str <> "!")

但这是不正确的,因为最里面的do - 块产生IO b0,而不是来自Spock的预期类型:

Couldn't match type ‘ActionT IO ()’ with ‘IO b0’
Expected type: Int -> IO b0
  Actual type: hvect-0.2.0.0:Data.HVect.HVectElim
                 '[Int] (ActionT IO ())
The lambda expression ‘\ n -> ...’ has one argument,
but its type ‘hvect-0.2.0.0:Data.HVect.HVectElim
                '[Int] (ActionT IO ())’
has none
In the second argument of ‘($)’, namely
  ‘\ n
     -> do { str <- rstr n;
             text ("boo:" <> str <> "!") }’
In a stmt of a 'do' block:
  get ("data" <//> var)
  $ \ n
      -> do { str <- rstr n;
              text ("boo:" <> str <> "!") }

如何在Spock get请求处理程序中使用我的IO驱动的随机字符串函数?

1 个答案:

答案 0 :(得分:6)

ActionT类型是MonadIO类型类的实例。这意味着您可以使用liftIO在此monad中执行IO操作。在您的情况下,您似乎需要liftIO $ rstr n而不是普通的rstr n

这表明了我所指的:

import Control.Monad.IO.Class
...
main :: IO ()
main =
    runSpock 8080 $ spockT id $
    do get root $
           redirect "/data/1"
       get ("data" <//> var) $ \n ->
          do
           str <- liftIO $ rstr n
           text $ pack str