Extending propositional logic to modal logic in Haskell
我在 Haskell 中编写了一些代码来建模命题逻辑
1 2 3 4 5 6 7 |
但是,由于数据类型是封闭的,因此无法自然地将其扩展到模态逻辑。
因此,我认为我应该改用类。这样,我以后可以很容易地在不同的模块中添加新的语言特性。问题是我不完全知道如何写它。我想要类似以下的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | type PropValue = (String,Bool) -- for example ("p",True) states that proposition p is true type Valuation = [PropValue] class Formula a where evaluate :: a -> Valuation -> Bool data Proposition = Prop String instance Formula Proposition where evaluate (Prop s) val = (s,True) `elem` val data Conjunction = Conj Formula Formula -- illegal syntax instance Formula Conjunction where evaluate (Conj ?? ??) v = evaluate ?? v && evaluate ?? v |
错误当然在于连词的定义。但是,我不清楚如何重写它以使其正常工作。
这应该可以工作:
1 2 3 4 | data Conjunction f = Conj f f instance Formula f => Formula (Conjunction f) where evaluate (Conj ?? ??) v = evaluate ?? v && evaluate ?? v |
但是,我不确定类型类是否是您想要实现的目标的正确工具。
也许你可以试一试使用显式类型级别的函子并重复它们:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | -- functor for plain formulae data FormulaF f = Prop {propName :: String} | Neg f | Conj f f | Disj f f | Impl f f | BiImpl f f -- plain formula newtype Formula = F {unF :: FormulaF Formula} -- functor adding a modality data ModalF f = Plain f | MyModality f -- modal formula newtype Modal = M {unM :: ModalF Modal} |
是的,这不是很方便,因为像
作为另一种选择,使用 GADT:
1 2 3 4 5 6 7 8 9 10 11 12 13 | data Plain data Mod data Formula t where Prop {propName :: String} :: Formula t Neg :: Formula t -> Formula t Conj :: Formula t -> Formula t -> Formula t Disj :: Formula t -> Formula t -> Formula t Impl :: Formula t -> Formula t -> Formula t BiImpl :: Formula t -> Formula t -> Formula t MyModality :: Formula Mod -> Formula Mod type PlainFormula = Formula Plain type ModalFormula = Formula Mod |