循环类型同义词声明

我想用下面的代码尝试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中表达这种类型?


这里有两个问题:

  • 它应该是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
    
    链接地址: http://www.djcxy.com/p/33151.html

    上一篇: Cycle in type synonym declarations

    下一篇: How to choose right Haskell C type?