Sco*_*ott 48 haskell functional-programming
我目前正在为一种编程语言开发一个简单的解释器,并且我的数据类型如下:
data Expr
= Variable String
| Number Int
| Add [Expr]
| Sub Expr Expr
Run Code Online (Sandbox Code Playgroud)
我有许多函数可以执行简单的操作,例如:
-- Substitute a value for a variable
substituteName :: String -> Int -> Expr -> Expr
substituteName name newValue = go
where
go (Variable x)
| x == name = Number newValue
go (Add xs) =
Add $ map go xs
go (Sub x y) =
Sub (go x) (go y)
go other = other
-- Replace subtraction with a constant with addition by a negative number
replaceSubWithAdd :: Expr -> Expr
replaceSubWithAdd = go
where
go (Sub x (Number y)) =
Add [go x, Number (-y)]
go (Add xs) =
Add $ map go xs
go (Sub x y) =
Sub (go x) (go y)
go other = other
Run Code Online (Sandbox Code Playgroud)
但是在所有这些函数中,我都必须重复执行该代码的部分,而只需对函数的一部分进行少量更改即可。有没有更通用的现有方法?我宁愿不必复制并粘贴此部分:
go (Add xs) =
Add $ map go xs
go (Sub x y) =
Sub (go x) (go y)
go other = other
Run Code Online (Sandbox Code Playgroud)
而且每次都只更改一个大小写,因为复制这样的代码似乎效率低下。
我唯一能想到的解决方案是拥有一个函数,该函数首先在整个数据结构上调用一个函数,然后递归地对结果进行如下调用:
recurseAfter :: (Expr -> Expr) -> Expr -> Expr
recurseAfter f x =
case f x of
Add xs ->
Add $ map (recurseAfter f) xs
Sub x y ->
Sub (recurseAfter f x) (recurseAfter f y)
other -> other
substituteName :: String -> Int -> Expr -> Expr
substituteName name newValue =
recurseAfter $ \case
Variable x
| x == name -> Number newValue
other -> other
replaceSubWithAdd :: Expr -> Expr
replaceSubWithAdd =
recurseAfter $ \case
Sub x (Number y) ->
Add [x, Number (-y)]
other -> other
Run Code Online (Sandbox Code Playgroud)
但是我觉得应该已经可以有一种更简单的方法来做到这一点。我想念什么吗?
chi*_*chi 37
恭喜,您刚刚发现了变形!
将您的代码改写为可与recursion-schemes包一起使用。las,它并不短,因为我们需要一些样板来使机械正常工作。(可能有一些自动的方法来避免样板,例如使用泛型。我只是不知道。)
在下面,您的recurseAfter被替换为standard ana。
我们首先定义您的递归类型,以及它的固定点。
{-# LANGUAGE DeriveFunctor, TypeFamilies, LambdaCase #-}
{-# OPTIONS -Wall #-}
module AnaExpr where
import Data.Functor.Foldable
data Expr
= Variable String
| Number Int
| Add [Expr]
| Sub Expr Expr
deriving (Show)
data ExprF a
= VariableF String
| NumberF Int
| AddF [a]
| SubF a a
deriving (Functor)
Run Code Online (Sandbox Code Playgroud)
然后,我们将它们与几个实例连接起来,以便我们可以展开Expr为同构ExprF Expr,然后将其折回。
type instance Base Expr = ExprF
instance Recursive Expr where
project (Variable s) = VariableF s
project (Number i) = NumberF i
project (Add es) = AddF es
project (Sub e1 e2) = SubF e1 e2
instance Corecursive Expr where
embed (VariableF s) = Variable s
embed (NumberF i) = Number i
embed (AddF es) = Add es
embed (SubF e1 e2) = Sub e1 e2
Run Code Online (Sandbox Code Playgroud)
最后,我们改编您的原始代码,并添加一些测试。
substituteName :: String -> Int -> Expr -> Expr
substituteName name newValue = ana $ \case
Variable x | x == name -> NumberF newValue
other -> project other
testSub :: Expr
testSub = substituteName "x" 42 (Add [Add [Variable "x"], Number 0])
replaceSubWithAdd :: Expr -> Expr
replaceSubWithAdd = ana $ \case
Sub x (Number y) -> AddF [x, Number (-y)]
other -> project other
testReplace :: Expr
testReplace = replaceSubWithAdd
(Add [Sub (Add [Variable "x", Sub (Variable "y") (Number 34)]) (Number 10), Number 4])
Run Code Online (Sandbox Code Playgroud)
另一种选择是ExprF a仅定义,然后派生type Expr = Fix ExprF。这节省了上面的一些样板文件(例如,两个实例),但以不得不使用Fix (VariableF ...)代替的代价Variable ...,以及其他构造函数的类似方法。
可以进一步减轻使用模式同义词的负担(不过要花更多的时间来做)。
更新:我终于使用模板Haskell找到了自动魔术工具。这使得整个代码相当短。注意,ExprF函子和上面的两个实例仍然存在于引擎盖下,我们仍然必须使用它们。我们仅省去了手动定义它们的麻烦,但是仅此一项就节省了很多工作。
{-# LANGUAGE DeriveFunctor, DeriveTraversable, TypeFamilies, LambdaCase, TemplateHaskell #-}
{-# OPTIONS -Wall #-}
module AnaExpr where
import Data.Functor.Foldable
import Data.Functor.Foldable.TH
data Expr
= Variable String
| Number Int
| Add [Expr]
| Sub Expr Expr
deriving (Show)
makeBaseFunctor ''Expr
substituteName :: String -> Int -> Expr -> Expr
substituteName name newValue = ana $ \case
Variable x | x == name -> NumberF newValue
other -> project other
testSub :: Expr
testSub = substituteName "x" 42 (Add [Add [Variable "x"], Number 0])
replaceSubWithAdd :: Expr -> Expr
replaceSubWithAdd = ana $ \case
Sub x (Number y) -> AddF [x, Number (-y)]
other -> project other
testReplace :: Expr
testReplace = replaceSubWithAdd
(Add [Sub (Add [Variable "x", Sub (Variable "y") (Number 34)]) (Number 10), Number 4])
Run Code Online (Sandbox Code Playgroud)
K. *_*uhr 18
作为一种替代方法,这也是该uniplate包的典型用例。它可以使用Data.Data泛型而不是Template Haskell来生成样板,因此,如果要Data为您派生实例Expr:
import Data.Data
data Expr
= Variable String
| Number Int
| Add [Expr]
| Sub Expr Expr
deriving (Show, Data)
Run Code Online (Sandbox Code Playgroud)
然后,transform函数from Data.Generics.Uniplate.Data将函数递归地应用于每个嵌套的函数Expr:
import Data.Generics.Uniplate.Data
substituteName :: String -> Int -> Expr -> Expr
substituteName name newValue = transform f
where f (Variable x) | x == name = Number newValue
f other = other
replaceSubWithAdd :: Expr -> Expr
replaceSubWithAdd = transform f
where f (Sub x (Number y)) = Add [x, Number (-y)]
f other = other
Run Code Online (Sandbox Code Playgroud)
replaceSubWithAdd特别要注意的是,f编写函数是为了执行非递归替换;transform使它在中递归x :: Expr,因此它对辅助函数的作用与ana@chi的答案相同:
> substituteName "x" 42 (Add [Add [Variable "x"], Number 0])
Add [Add [Number 42],Number 0]
> replaceSubWithAdd (Add [Sub (Add [Variable "x",
Sub (Variable "y") (Number 34)]) (Number 10), Number 4])
Add [Add [Add [Variable "x",Add [Variable "y",Number (-34)]],Number (-10)],Number 4]
>
Run Code Online (Sandbox Code Playgroud)
这不短于@chi的Template Haskell解决方案。一个潜在的优势是uniplate提供了一些可能有用的附加功能。例如,如果您使用descend代替transform,它将仅转换直接子代,从而可以控制递归发生的位置,或者您可以rewrite用来重新转换转换的结果,直到达到固定点为止。一个潜在的缺点是“变形”听起来比“单板”酷。
完整程序:
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Data -- in base
import Data.Generics.Uniplate.Data -- package uniplate
data Expr
= Variable String
| Number Int
| Add [Expr]
| Sub Expr Expr
deriving (Show, Data)
substituteName :: String -> Int -> Expr -> Expr
substituteName name newValue = transform f
where f (Variable x) | x == name = Number newValue
f other = other
replaceSubWithAdd :: Expr -> Expr
replaceSubWithAdd = transform f
where f (Sub x (Number y)) = Add [x, Number (-y)]
f other = other
replaceSubWithAdd1 :: Expr -> Expr
replaceSubWithAdd1 = descend f
where f (Sub x (Number y)) = Add [x, Number (-y)]
f other = other
main = do
print $ substituteName "x" 42 (Add [Add [Variable "x"], Number 0])
print $ replaceSubWithAdd e
print $ replaceSubWithAdd1 e
where e = Add [Sub (Add [Variable "x", Sub (Variable "y") (Number 34)])
(Number 10), Number 4]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6125 次 |
| 最近记录: |