Created
January 18, 2009 15:13
-
-
Save syoyo/48675 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
{-# LANGUAGE DeriveDataTypeable #-} | |
module Main where | |
import Data.Generics | |
data Expr = | |
Const Int | |
| Var String | |
| Add Expr Expr | |
deriving (Show, Eq, Typeable, Data) | |
data Stmt = | |
Let Expr Expr | |
| If Expr Expr Expr | |
deriving (Show, Eq, Typeable, Data) | |
replaceConst :: Expr -> Expr | |
replaceConst (Const i) = Var (show i) | |
replaceConst (Add lhs rhs) = Add (replaceConst lhs) (replaceConst rhs) | |
replaceConst expr = expr | |
trans :: Stmt -> Stmt | |
trans = everywhere (mkT replaceConst) | |
prog = Let (Var "x") (Add (Const 3) (Var "y")) | |
main = do putStrLn $ show prog | |
putStrLn $ show (trans 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