1
我正在學習一個編譯器類,並且我決定在haskell中完成它,但是我很難設置ast。我的問題是,我有一個Atom
類和一個Expr
類和Expr
的一個實例可以是Atom
,但當Expr
立即是一個Atom它有一個問題。以下是示例:Haskell找不到「間接」類型
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
編譯器由於預期Expr而在Print (Const 2)
上失敗。有沒有解決這個問題,是否有更好的詞彙來表達這個問題?
'Print(Atomic(Const 2))'''? –
它可以很方便地定義'IsString Atom'和'Num Atom'實例(第二個只在'Const'情況下工作)。然後,'打印(原子2)'會起作用,'打印(原子「hi」)''也會起作用。 – Alec
@BenjaminHodgson這很有效。想要更多,所以我可以接受它,並給你信譽? – Hovestar