lambda 抽象 ADT 的显示实例

Instance of Show for lambda-abstraction ADT

所以,我已经这样定义了 lambda 数据类型:

data LExpr
    = Variable String         -- variable
    | Apply LExpr LExpr       -- function application
    | Lambda String LExpr     -- Lambda abstraction 
    deriving (Eq, Show)  

现在我想自己实现一个 Show 的实例。我已经有了完成大部分工作的函数 show',但没有使用实例:

 show' :: LExpr -> String
 show' (Variable a) = a
 show' (Apply e1 e2) = "(" ++ show' e1 ++ " " ++ show' e2 ++ ")"                   
 show' (Lambda x e) = "(λ " ++ x ++ ". " ++ show' e ++ ")"    

我如何实现它,在不显式使用 show' 函数的情况下获得以下输出:

Main> (Apply (Lambda "x" (Apply (Variable "x") (Variable "y"))) (Variable "z"))
((λ x. x y) y)       

Show class 添加实例声明。

instance Show LExpr where
  show = show'

并删除 deriving(Show) 部分

data LExpr
    = Variable String         -- variable
    | Apply LExpr LExpr       -- function application
    | Lambda String LExpr     -- Lambda abstraction 
    deriving (Eq)