循环类型同义词声明

时间:2012-12-06 05:23:48

标签: haskell types

我想使用以下代码尝试Haskell typedata

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中表达这种类型?

1 个答案:

答案 0 :(得分:7)

这里有两个问题:

  • 应该是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