3

我想试用 Haskelltypedata使用以下代码

type Program a = Maybe a -> (Operation a, Maybe Program a)
data Operation a = Read a | Write a

Program遵循延续传递风格 (CPS) 并Nothing表示终止。a可以用Int或其他东西实例化。

然而,GHC 抱怨说:

main.hs:1:1:
    Cycle in type synonym declarations:
      main.hs:1:1-58: type Program a =
                          Maybe a -> (Operation a, Maybe Program a)

我是 Haskell 的新手,所以我不明白为什么不允许这样做。如何在 Haskell 中表达这种类型?

4

1 回答 1

8

这里有两个问题:

  • 应该是Maybe (Program a),不是Maybe Program a
  • 类型同义词不能递归

一个工作版本是:

newtype Program a = Program { runProgram :: Maybe a -> (Operation a, Maybe (Program a)) }
data Operation a = Read a | Write a
于 2012-12-06T05:33:52.647 回答