Haskell 未找到 "indirect" 类型
Haskell not finding "indirect" type
我正在学习编译器 class,我决定在 haskell 中完成它,但我很难设置 ast。我的问题是我有一个 Atom
class 和一个 Expr
class 并且 Expr
的一个实例可以是 Atom
,但是当Expr
立即成为一个有问题的 Atom。这是示例:
data Atom -- cannot be reduced farther
= Const Int -- int is value
| Var String -- string is name
deriving (Show) -- So we can print it
data Expr -- add input and the like
= Add Expr Expr -- add is two exprs
| USub Expr -- negation
| Input -- call to input
| Atomic Atom -- or an atomic
deriving (Show) -- So we can print it
data Statement
= Print Expr
| Discard Expr
| Assign String Expr
deriving (Show) -- So we can print it
main = do
let test5 = Print (Const 2)
putStrLn $ show test5
编译器在 Print (Const 2)
上失败,因为它需要一个 Expr。有没有办法解决这个问题,有没有更好的词汇来表达这个问题?
Const 2
是一个 Atom
,但是 Print
接受一个 Expr
作为参数。幸运的是,每个 Atom
都可以用 Atomic
构造函数变成 Expr
。所以:
main = do
let test5 = Print (Atomic (Const 2))
print test5
我正在学习编译器 class,我决定在 haskell 中完成它,但我很难设置 ast。我的问题是我有一个 Atom
class 和一个 Expr
class 并且 Expr
的一个实例可以是 Atom
,但是当Expr
立即成为一个有问题的 Atom。这是示例:
data Atom -- cannot be reduced farther
= Const Int -- int is value
| Var String -- string is name
deriving (Show) -- So we can print it
data Expr -- add input and the like
= Add Expr Expr -- add is two exprs
| USub Expr -- negation
| Input -- call to input
| Atomic Atom -- or an atomic
deriving (Show) -- So we can print it
data Statement
= Print Expr
| Discard Expr
| Assign String Expr
deriving (Show) -- So we can print it
main = do
let test5 = Print (Const 2)
putStrLn $ show test5
编译器在 Print (Const 2)
上失败,因为它需要一个 Expr。有没有办法解决这个问题,有没有更好的词汇来表达这个问题?
Const 2
是一个 Atom
,但是 Print
接受一个 Expr
作为参数。幸运的是,每个 Atom
都可以用 Atomic
构造函数变成 Expr
。所以:
main = do
let test5 = Print (Atomic (Const 2))
print test5