【问题标题】:polysemy does not detect a member多义词未检测到成员
【发布时间】:2020-12-18 08:07:57
【问题描述】:

我正在尝试在单个多义词 Sem Monad 中使用 Hspec 编写一堆语句:

{-# OPTIONS_GHC -fplugin=Polysemy.Plugin #-}

{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE GADTs               #-}
{-# LANGUAGE KindSignatures      #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeOperators       #-}

module StorageSpec (main, spec) where

import Test.Hspec

import Storage
import Data.Aeson
import Data.Maybe(fromJust)
import qualified Data.Text as T
import qualified Database.Bloodhound as B
import qualified Database.Bloodhound.Types as B
import GHC.Generics
import Polysemy (Embed, Member, Sem, makeSem, embed, interpret, reinterpret)
import Polysemy.IO
import Polysemy.State


data Storage (d :: *) (m :: * -> *) (a :: *) where
  CreateContainer :: B.IndexName -> B.IndexSettings -> Storage d m Bool

makeSem ''Storage

scenarii :: Member (Storage Book) r => Sem r (SpecWith ())
scenarii = do
  indexCreation <- it "create index should work the first time" . flip shouldBe True
    <$> (createContainer exampleIndex exampleMapping :: Member (Storage Book) r => Sem r Bool)
  return $ indexCreation

data Book = Book { name :: String, entries :: Int } deriving (Eq, Show, Generic, FromJSON, ToJSON)

在明确指定 Storage Book Member 时出现以下错误:

    • Ambiguous use of effect 'Storage'
      Possible fix:
        add (Member (Storage d1) r1) to the context of
          the type signature
      If you already have the constraint you want, instead
        add a type application to specify
          'd1' directly, or activate polysemy-plugin which
            can usually infer the type correctly.
    • In the second argument of ‘(<$>)’, namely
        ‘(createContainer exampleIndex exampleMapping ::
            Member (Storage Book) r => Sem r Bool)’
      In a stmt of a 'do' block:
        indexCreation <- it "create index should work the first time"
                           . flip shouldBe True
                           <$>
                             (createContainer exampleIndex exampleMapping ::
                                Member (Storage Book) r => Sem r Bool)
      In the expression:
        do indexCreation <- it "create index should work the first time"
                              . flip shouldBe True
                              <$>
                                (createContainer exampleIndex exampleMapping ::
                                   Member (Storage Book) r => Sem r Bool)
           return $ indexCreation
   |
39 |     <$> (createContainer exampleIndex exampleMapping :: Member (Storage Book) r => Sem r Bool)
   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我不知道我能做些什么来帮助类型检查器。

【问题讨论】:

    标签: haskell haskell-polysemy


    【解决方案1】:

    CreateContainer 的情况下,d 充当 Phantom 类型,为了修复它,我不得不使用类型应用程序:

    createContainer @Book exampleIndex exampleMapping
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-03
      • 2021-06-18
      • 2020-11-20
      • 2020-07-29
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多