Skip to content

Instantly share code, notes, and snippets.

@syoyo
Created January 18, 2009 15:13
Show Gist options
  • Save syoyo/48675 to your computer and use it in GitHub Desktop.
Save syoyo/48675 to your computer and use it in GitHub Desktop.
{-# 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