Created
January 18, 2009 14:51
-
-
Save syoyo/48669 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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