我已經寫在Haskell一些代碼用於建模命題邏輯擴展命題邏輯模態邏輯在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
這個錯誤當然是在連詞的定義中。但是,我不清楚如何重寫它,以便它能夠正常工作。
如果你喜歡閱讀,你會發現[這](http://okmij.org/ftp/tagless-final/course/lecture .pdf)很有幫助。 – user2407038 2014-09-28 11:25:33