Haskell:AST减少导致类型问题

时间:2018-03-11 05:38:51

标签: haskell

我有一个可扩展AST的代码。我的目标是允许稍后使用Expr类添加更多表达式类型,而不是在单个数据块中包含所有类型。

data Constant o = Constant o deriving (Show)
data Add l r o = Add (l o) (r o) deriving (Show)

class Expr e where
  simplify :: (Expr a, Num o) => (e o) -> (a o)

instance Expr Constant where
  simplify (Constant a) = Constant a

instance (Expr l, Expr r) => Expr (Add l r) where
  simplify (Add l r) = case (simplify l, simplify r) of
    (Conatant a, Constant b) -> Constant $ a + b
    (sl@_, sr@_) -> Add sl sr

我遇到了这些错误:

math.hs:10:27: error:
    • Couldn't match type ‘a’ with ‘Constant’
      ‘a’ is a rigid type variable bound by
        the type signature for:
          simplify :: forall (a :: * -> *) o.
                      (Expr a, Num o) =>
                      Constant o -> a o
        at math.hs:10:3-10
      Expected type: a o
        Actual type: Constant o
    • In the expression: Constant a
      In an equation for ‘simplify’: simplify (Constant a) = Constant a
      In the instance declaration for ‘Expr Constant’
    • Relevant bindings include
        simplify :: Constant o -> a o (bound at math.hs:10:3)

math.hs:15:21: error:
    • Couldn't match type ‘a’ with ‘Add l0 r0’
      ‘a’ is a rigid type variable bound by
        the type signature for:
          simplify :: forall (a :: * -> *) o.
                      (Expr a, Num o) =>
                      Add l r o -> a o
        at math.hs:13:3-10
      Expected type: a o
        Actual type: Add l0 r0 o
    • In the expression: Add sl sr
      In a case alternative: (sl@_, sr@_) -> Add sl sr
      In the expression:
        case (simplify l, simplify r) of { (sl@_, sr@_) -> Add sl sr }
    • Relevant bindings include
        sr :: r0 o (bound at math.hs:15:12)
        sl :: l0 o (bound at math.hs:15:6)
        simplify :: Add l r o -> a o (bound at math.hs:13:3)

我无法理解为什么这不起作用。再说一次,我对Haskell相当新,在OOP土地上花了将近十年。我希望有人可以告诉我这方面的见解。

我试图在没有任何GHC扩展的情况下这样做,因为我想在开始添加更多内容之前了解vanilla语言。

1 个答案:

答案 0 :(得分:2)

class ... where
  simplify :: (Expr a, ...) => ... -> (a o)

此声明意味着您需要能够为使用a o选择的Expr类中的任何类型构造simplify。您不能在实例中指定具体类型。

编辑:您可以在Expr中引入一个构造:

class Expr e where
    ...
    constant :: a -> e a
    add :: e a -> e a -> e a
    ...

并使用那些。虽然像(add _ _ :: Constant Int)这样的表达式没有意义。

你能做什么指定具有依赖性的类

class Simplify e1 e2 | e1 -> e2 where ...

与类型系列

class Simplify e where
    data Simplified e :: *
    ....

他们需要相应的扩展,但你不应该害怕它。那就是现代的Haskell。