我正在尝试在Haskell中实现EDSL。我想用绑定的变量名来打印AST(如果我不能得到实名,那么一些生成的名称就可以了)。
这是我用一个简单的例子得到的:
import Control.Monad.State
data Free f a = Roll (f (Free f a))
| Pure a
instance Functor f => Monad (Free f) where
return = Pure
(Pure a) >>= f = f a
(Roll f) >>= g = Roll $ fmap (>>= g) f
data Expr a = I a
| Plus (Expr a) (Expr a)
deriving (Show)
data StackProgram a next = Pop (a -> next)
| Push a next
instance Functor (StackProgram a) where
fmap f (Pop k) = Pop (f.k)
fmap f (Push i x) = Push i (f x)
liftF :: Functor f => f a -> Free f a
liftF l = Roll $ fmap return l
push :: a -> Free (StackProgram a) ()
push i = liftF $ Push i ()
pop :: Free (StackProgram a) a
pop = liftF $ Pop id
prog3 :: Free (StackProgram (Expr Int)) (Expr Int)
prog3 = do
push (I 3)
push (I 4)
a <- pop
b <- pop
return (Plus a b)
showSP' :: (Show a, Show b) => Free (StackProgram a) b -> [a] -> State Int String
showSP' (Pure a) _ = return $ "return " ++ show a
showSP' (Roll (Pop f)) (a:stack) = do
i <- get
put (i+1)
rest <- showSP' (f a) stack
return $ "var" ++ show i ++ " <- pop " ++ show (a:stack) ++ "\n" ++ rest
showSP' (Roll (Push i n)) stack = do
rest <- showSP' n (i:stack)
return $ "push " ++ show i ++ " " ++ show stack ++ "\n" ++ rest
showSP :: (Show a, Show b) => Free (StackProgram a) b -> [a] -> String
showSP prg stk = fst $ runState (showSP' prg stk) 0
运行此命令:
*Main> putStrLn $ showSP prog3 []
push I 3 []
push I 4 [I 3]
var0 <- pop [I 4,I 3]
var1 <- pop [I 3]
return Plus (I 4) (I 3)
所以我想要的是用Plus (I 4) (I 3)
替换Plus var0 var1
。我已经考虑过遍在树的其余部分并用名称值元组替换绑定变量,但我不是100%确定是否/如何工作。我也更喜欢保留原始变量名称,但我想不出这样做的简单方法。我更喜欢在haskell中使用相当轻量级的语法(如上所述)。
我也非常感谢能够教会我如何最好地完成这些事情的材料。我已经阅读了一些关于免费monad和GADT的内容,但我想我错过了如何将它们放在一起。
答案 0 :(得分:5)
使用您拥有的结构,您无法在“纯”Haskell代码中执行此操作,因为一旦编译了代码,您就无法区分(Plus a b)
和(Plus (I 4) (I 3))
并保持“引用透明度” “ - 变量及其值的可互换性。
然而,有些不安全的黑客 - 即无法保证工作 - 可以让你做这种事情。它们通常以“可观察共享”的名称命名,并基于使用StableName访问值的表示方式。本质上,它为您提供了一个指针相等操作,允许您区分对a
的引用和值(I 4)
的新副本。
有助于整理此功能的一个软件包是data-reify。
在编译期间,源中使用的实际变量名称将无法挽回地丢失。在Paradise中,我们使用预处理器在编译之前将foo <~ bar
转换为foo <- withName "foo" $ bar
,但它很笨拙,并且它会使构建速度变慢。
答案 1 :(得分:4)
我根据@Gabriel Gonzales'linked answer计算出来了。基本思想是在Expr类型中引入一个新的变量构造函数,并在解释树时为它们分配一个唯一的id。那和清理代码有点:
import Control.Monad.Free
import Data.Map
newtype VInt = VInt Int
data Expr = IntL Int
| IntV VInt
| Plus Expr Expr
instance Show Expr where
show (IntL i) = show i
show (IntV (VInt i)) = "var" ++ show i
show (Plus e1 e2) = show e1 ++ " + " ++ show e2
data StackProgF next = Pop (VInt -> next)
| Push Expr next
instance Functor StackProgF where
fmap f (Pop k) = Pop (f.k)
fmap f (Push e x) = Push e (f x)
type StackProg = Free StackProgF
type Stack = [Expr]
push :: Expr -> StackProg ()
push e = liftF $ Push e ()
pop :: StackProg Expr
pop = liftF $ Pop IntV
prog3 :: StackProg Expr
prog3 = do
push (IntL 3)
push (IntL 4)
a <- pop
b <- pop
return (Plus a b)
showSP :: StackProg Expr -> String
showSP prg = go 0 prg []
where
go i (Pure a) _ = show a
go i (Free (Pop n)) (h:t) = "var" ++ show i ++ " <- pop " ++ show (h:t) ++ "\n" ++
go (i+1) (n (VInt i)) t
go i (Free (Pop _)) [] = "error: pop on empty stack\n"
go i (Free (Push e n)) stk = "push " ++ show e ++ ", " ++ show stk ++ "\n" ++ go i n (e:stk)
type Env = Map Int Expr
evalExpr :: Expr -> Env -> Int
evalExpr (IntL i) _ = i
evalExpr (IntV (VInt k)) env = evalExpr (env ! k) env
evalExpr (Plus e1 e2) env = evalExpr e1 env + evalExpr e2 env
evalSP :: StackProg Expr -> Int
evalSP prg = go 0 prg [] empty
where
go i (Free (Pop _)) [] env = error "pop on empty stack\n"
go i (Free (Pop n)) (h:t) env = go (i+1) (n (VInt i)) t (insert i h env)
go i (Free (Push e n)) stk env = go i n (e:stk) env
go i (Pure a) _stk env = evalExpr a env
漂亮的打印和运行:
*Main> putStrLn $ showSP prog3
push 3, []
push 4, [3]
var0 <- pop [4,3]
var1 <- pop [3]
var0 + var1
*Main> evalSP prog3
7