Skip to content

Instantly share code, notes, and snippets.

@syoyo
Created January 18, 2009 14:51
Show Gist options
  • Save syoyo/48669 to your computer and use it in GitHub Desktop.
Save syoyo/48669 to your computer and use it in GitHub Desktop.
module Main where
data Expr =
Const Int
| Var String
| Add Expr Expr
deriving (Show, Eq)
data Stmt =
Let Expr Expr
| If Expr Expr Expr
deriving (Show, Eq)
class Main a where
replaceConst :: a -> a
instance Main Stmt where
replaceConst (Let lhs rhs) = Let (replaceConst lhs) (replaceConst rhs)
replaceConst (If cond thenE elseE) = If (replaceConst cond) (replaceConst thenE) (replaceConst elseE)
instance Main Expr where
replaceConst (Const i) = Var (show i)
replaceConst (Add lhs rhs) = Add (replaceConst lhs) (replaceConst rhs)
replaceConst expr = expr
prog = Let (Var "x") (Add (Const 3) (Var "y"))
main = do putStrLn $ show prog
putStrLn $ show (replaceConst prog)
$ runhaskell Main.hs
Let (Var "x") (Add (Const 3) (Var "y"))
Let (Var "x") (Add (Var "3") (Var "y"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment