1

查看来自freer-simple的示例代码

{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}
module Console where

import Control.Monad.Freer
import Control.Monad.Freer.Error
import Control.Monad.Freer.State
import Control.Monad.Freer.Writer
import System.Exit hiding (ExitCode(ExitSuccess))

--------------------------------------------------------------------------------
                                                             -- Effect Model --
--------------------------------------------------------------------------------
data Console r where
    PutStrLn    :: String -> Console ()
    GetLine     :: Console String
    ExitSuccess :: Console ()

putStrLn' :: Member Console effs => String -> Eff effs ()
putStrLn' = send . PutStrLn

getLine' :: Member Console effs => Eff effs String
getLine' = send GetLine

exitSuccess' :: Member Console effs => Eff effs ()
exitSuccess' = send ExitSuccess

--------------------------------------------------------------------------------
                                                    -- Effectful Interpreter --
--------------------------------------------------------------------------------
runConsole :: Eff '[Console, IO] a -> IO a
runConsole = runM . interpretM (\case
    PutStrLn msg -> putStrLn msg
    GetLine -> getLine
    ExitSuccess -> exitSuccess)

我不明白 Eff '[Console, IO] 中好奇的 '[...] 来自哪里。

我知道它为解释器声明了一个效果列表,但它在哪里声明?

我浏览了库的源代码,但看不到任何我认为是“构造函数”的东西。

它是 Haskell 的一个特性吗?它来自低级的 type-fu 库吗?

对其目的更严格的描述是什么?

4

0 回答 0