Free Monads中的抽象结果类型

时间:2016-11-08 19:52:42

标签: scala haskell functional-programming free-monad

假设我们要定义一个简单的DSL来定义UI交互,我们可以在其中创建对象然后选择它们:

object TestCommand {

  sealed trait EntityType
  case object Project extends EntityType
  case object Site extends EntityType

  sealed trait TestCommand[A, E]
  case class Create[A, E](entityType: EntityType, withEntity: E => A) extends  TestCommand[A, E]
  case class Select[A, E](entity: E, next: A) extends TestCommand[A, E]

} 

我遇到的问题是我不想指定创建命令的返回类型(上面E)。我想让这个决定由口译员决定。例如,E可以是字符串,如果我们使用异步REST调用创建对象,则Future

如果我尝试使用liftF以常规方式定义DSL,如下所示:

object TestDSL {

  def create[E](entityType: EntityType): Free[TestCommand[?, E], E] =
    Free.liftF(Create(entityType, identity: E => E): TestCommand[E, E])

  def select[E](entity: E): Free[TestCommand[?, E], Unit] =
    Free.liftF(Select[Unit, E](entity, ()))

}

我收到以下错误:

Error:(10, 10) no type parameters for method liftF: (value: S[A])scalaz.Free[S,A] exist so that it can be applied to arguments (dsl.TestCommand.TestCommand[E,E])
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : dsl.TestCommand.TestCommand[E,E]
 required: ?S[?A]
    Free.liftF(Create(entityType, identity: E => E): TestCommand[E, E])

我无法理解上面代码中出现了什么问题,但更重要的一个问题是,这是否是对自由monad中出现的类型进行抽象的正确方法。如果没有,什么是正确的(功能)方法?

修改

在Haskell中,上述方法没有问题:

{-# LANGUAGE DeriveFunctor #-}
-- |

module TestDSL where

import           Control.Monad.Free

data EntityType = Project | Site

data TestCommand e a = Create EntityType (e -> a) | Select e a
  deriving Functor

-- | The DSL
create :: EntityType -> Free (TestCommand e) e
create et = liftF $ Create et id

select :: e -> Free (TestCommand e) ()
select e = liftF $ Select e ()


-- | A sample program:
test :: Free (TestCommand e) ()
test = do
  p <- create Project
  select p
  _ <- create Site
  return ()

-- | A trivial interpreter.
interpTestCommand :: TestCommand String a -> IO a
interpTestCommand (Create Project withEntity) = do
  putStrLn $ "Creating a project"
  return (withEntity "Project X")
interpTestCommand (Create Site withEntity) = do
  putStrLn $ "Creating a site"
  return (withEntity "Site 51")
interpTestCommand (Select e next) = do
  putStrLn $ "Selecting " ++ e
  return next

-- | Running the interpreter
runTest :: IO ()
runTest = foldFree interpTestCommand test

运行测试将产生以下输出:

λ> runTest
Creating a project
Selecting Project X
Creating a site

1 个答案:

答案 0 :(得分:3)

现在你有test :: Free (TestCommand e) ()。这意味着实体e的类型可以是调用者想要的任何类型,但它在整个计算过程中都是固定的。

但那不对!在现实世界中,为响应Create命令而创建的实体类型取决于命令本身:如果您创建了Project,那么e应为Project ;如果您创建了Site,则e应为Site。因此e不应修复整个计算(因为我可能想创建Project s Site s),它不应该是由来电者选择e

这是一种解决方案,其中实体的类型取决于命令的值。

data Site = Site { {- ... -} }
data Project = Project { {- ... -} }

data EntityType e where
    SiteTy :: EntityType Site
    ProjectTy :: EntityType Project

这里的想法是EntityType e上的模式匹配会告诉您e是什么。在Create命令中,我们将存储一个实体e和一些GADT证据,其形式为EntityType e,您可以将其模式匹配,以了解e data CommandF r where Create :: EntityType e -> (e -> r) -> CommandF r Select :: EntityType e -> e -> r -> CommandF r instance Functor CommandF where fmap f (Create t next) = Create t (f . next) fmap f (Select t e next) = Select t e (f next) type Command = Free CommandF create :: EntityType e -> Command e create t = Free (Create t Pure) select :: EntityType e -> e -> Command () select t e = Free (Select t e (Pure ())) myComputation :: Command () myComputation = do p <- create ProjectTy -- p :: Project select ProjectTy p s <- create SiteTy -- s :: Site return () 是的。

Create

当解释器到达EntityType指令时,其作用是返回与包装EntityType匹配的类型的实体。它必须检查e才能知道-- assuming createSite :: IO Site and createProject :: IO Project interp :: CommandF a -> IO a interp (Create SiteTy next) = do site <- createSite putStrLn "created a site" return (next site) interp (Create ProjectTy next) = do project <- createProject putStrLn "created a project" return (next project) -- plus clauses for Select 是什么,并且行为恰当。

curl -u 'USERNAME' https://api.github.com/user/repos -d '{"name":"REPONAME"}'

我不知道这会如何完全转化为Scala,但这是Haskell中的主旨。