【问题标题】:Extending propositional logic to modal logic in Haskell在 Haskell 中将命题逻辑扩展到模态逻辑
【发布时间】:2014-09-28 10:31:27
【问题描述】:

我在 Haskell 中编写了一些代码来建模命题逻辑

data Formula = Prop {propName :: String} 
            | Neg Formula 
            | Conj Formula Formula 
            | Disj Formula Formula
            | Impl Formula Formula 
            | BiImpl Formula Formula 
    deriving (Eq,Ord)

但是,由于数据类型是封闭的,因此无法自然地将其扩展到模态逻辑。 因此,我认为我应该改用类。这样,我以后可以很容易地在不同的模块中添加新的语言特性。问题是我不完全知道如何写它。我想要类似下面的东西

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

错误当然在于连词的定义。但是,我不清楚如何重写它以使其正常工作。

【问题讨论】:

  • 如果您喜欢阅读,您可能会发现this 很有帮助。

标签: haskell modal-logic


【解决方案1】:

这应该可行:

data Conjunction f = Conj f f

instance Formula f => Formula (Conjunction f) where
    evaluate (Conj φ ψ) v = evaluate φ v && evaluate ψ v

但是,我不确定类型类是否适合您想要实现的目标。


也许您可以试一试使用显式类型级别的函子并在它们上重复:

-- 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}

是的,这不是很方便,因为 F,M,Plain 这样的构造函数有时会妨碍。但是,与类型类不同,您可以在此处使用模式匹配。


作为另一种选择,使用 GADT:

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

【讨论】:

  • 谢谢,您的第一个解决方案似乎有效。但是,我也会研究您的其他解决方案,因为我相信数据类型上下文将在不久的将来被弃用,请参阅stackoverflow.com/questions/7438600/…。 GADT 似乎不是我需要的解决方案,因为我希望能够在不同的模块中定义不同的运算符。例如,PropLogic.hs 中的 Conj 和 Disj 运算符、ModalLogic.hs 中的 Box 运算符和 PredLogic.hs 中的 ForAll 量词。
  • @Anonymous 数据类型上下文确实要避免,但我没有使用它们(你也没有)。这些上下文是data (Ord a) => Set a = ... 中的上下文。 classinstance 中出现的上下文不是数据类型上下文,不会消失。
猜你喜欢
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多