haskell – 酸状态查询的意外返回类型(Happstack)

我正在尝试使用一些附加功能扩展
Happstack crash course博客:在主页上显示所有标签的列表.

我的博客记录如下:

data Blog = Blog
    { nextPostId :: PostId
    , posts      :: IxSet Post
    , allTags    :: [Text] 
    }
    deriving (Data, Typeable)

我通过以下方式获取id的博客帖子(从崩溃课程中复制):

-- Models.hs

postById :: PostId -> Query Blog (Maybe Post)
postById pid =
     do Blog{..} <- ask
        return $getOne $posts @= pid

-- Controller.hs

viewPage :: AcidState Blog -> ServerPart Response
viewPage acid = 
     do pid <- PostId <$> lookRead "id"
        mPost <- query' acid (PostById pid)
        ...
        -- mPost has type Maybe Post here
        ...

它工作正常.

当我尝试以类似的方式查询所有标签时:

-- Models.hs 

getTags :: Query Blog [Text]
getTags = 
    do Blog{..} <- ask
       return allTags

-- Controller.hs

serveTags :: AcidState Blog -> [Text]
serveTags acid = query' acid GetTags

它不会起作用.错误堆栈跟踪是:

Blog/Controllers.hs:154:18:
   Couldn't match type `[Text]' with `Text'
   Expected type: [Text]
   Actual type: [acid-state-0.8.1:Data.Acid.Common.EventResult
                                       GetTags]
   In the return type of a call of query'
   In the expression: query' acid GetTags

我无法弄清楚为什么返回类型的查询’是[EventResult GetTags],而它应该是[Text].

这个错误的原因是什么?有办法解决吗?

最佳答案 问题是你在serveTags函数上的类型签名,它应该是monadic:

serveTags :: MonadIO m => AcidState Blog -> m [Text]
serveTags acid = query' acid GetTags

EventResult是一个类型系列,在此解析为[Text].由于查询’是monadic,你的类型签名被解析为列表monad,即m text,其中m是列表monad,因此是混乱的错误消息.

点赞