在Haskell中处理相当大的代数数据类型时,有一种特殊的递归遍历没有通过折叠数据类型来捕获。例如,假设我有一个简单的数据类型表示命题逻辑中的公式,并在其上定义了一个折叠:
type FAlgebra α φ =
(φ, φ, -- False, True
α -> φ, -- Atom
φ -> φ, -- Negation
φ -> φ -> φ, -- Conjunction
φ -> φ -> φ, -- Disjunction
φ -> φ -> φ, -- Implication
φ -> φ -> φ) -- Bi-implication
fold :: FAlgebra α φ -> Form α -> φ
fold (f,t,lit,not,con,dis,imp,iff) = fold' where
fold' (Fls) = f
fold' (Tru) = t
fold' (Lit α) = lit α
fold' (Not φ) = not (fold' φ)
fold' (Con φ ψ) = con (fold' φ) (fold' ψ)
fold' (Dis φ ψ) = dis (fold' φ) (fold' ψ)
fold' (Imp φ ψ) = imp (fold' φ) (fold' ψ)
fold' (Iff φ ψ) = iff (fold' φ) (fold' ψ)
这种递归方案为评估或查找文字等递归提供了简洁的答案:
eval :: (Ord α) => Map α Bool -> Form α -> Bool
eval v = fold (False, True, (fromJust . flip M.lookup v),
not, (&&), (||), ((||) . not), (==))
literals :: (Ord α) => Form α -> Set α
literals = fold (S.empty, S.empty, S.singleton, id,
S.union, S.union, S.union, S.union)
但是,当我希望“扫描”数据类型时,它的表现并不好。在下文中,simp是由必要的模式匹配定义的辅助函数:
simplify :: Form α -> Form α
simplify (Not φ) = simp (Not (simplify φ))
simplify (Con φ ψ) = simp (Con (simplify φ) (simplify ψ))
simplify (Dis φ ψ) = simp (Dis (simplify φ) (simplify ψ))
simplify (Imp φ ψ) = simp (Imp (simplify φ) (simplify ψ))
simplify (Iff φ ψ) = simp (Imp (simplify φ) (simplify ψ))
simplify φ = φ
使用折叠定义简化当然会产生不正确的结果。例如,以下内容不等同:
simplify = fold (Fls, Tru, Lit, (simp . Not), con Con, con Dis, con Imp, con Iff)
where con f φ ψ = simp (f φ ψ)
简化等递归的最佳解决方案是什么?我应该定义一个类似于数据类型的折叠的通用遍历,还是有一个标准的递归模式来定义这样的函数?
答案 0 :(得分:8)
你试过Uniplate吗?对于仅适用于单一类型的操作,它可以执行自下而上的重写并重写直到固定点。
例如:
import Data.Generics.Uniplate.Direct
import qualified Data.Map as M
data Form a = Fls | Tru | Lit a
| Not (Form a)
| Con (Form a) (Form a)
| Dis (Form a) (Form a)
-- etc.
deriving (Show, Eq)
instance Uniplate (Form a) where
uniplate (Not f) = plate Not |* f
uniplate (Con f1 f2) = plate Con |* f1 |* f2
uniplate (Dis f1 f2) = plate Dis |* f1 |* f2
-- one case for each constructor that may contain nested (Form a)s
uniplate x = plate x
simplify :: Form a -> Form a
simplify = transform simp
where
simp (Not (Not f)) = f
simp (Not Fls) = Tru
simp (Not Tru) = Fls
simp x = x
test =
simplify (Not (Not (Not (Not (Lit "a"))))) == Lit "a"
有关Uniplate的更深入的文档,还有a paper (PDF)。