免責宣告:我是使用 haskell 的新手。
我正在用haskell證明邏輯公式。我很難理解如何正確使用newtypes 和datas 。
我定義了以下型別來表示具有以下結構的邏輯公式:(a or b or c) and (d or e) and (f)等。
data Literal x = Literal x | Negation x
deriving (Show, Eq)
newtype Or x = Or [Literal x]
deriving (Show, Eq)
newtype And x = And [Or x]
deriving (Show, Eq)
我想撰寫一個可以過濾文字的函式(即取出某些a b或c基于某些條件)。我天真地認為這應該類似于過濾,[[Literal x]]但我似乎無法讓它作業。
我目前的方法是這樣的:
filterLit :: Eq x => And x -> And x
filterLit = map (\(Or x) -> (filter (\(Lit l) -> condition l) x))
這不打字。我覺得我在這里缺少一些語法規則。如果您對我應該如何處理它有任何建議,請告訴我。
uj5u.com熱心網友回復:
\(Or x) -> filter (\(Lit l) -> condition l) x
讓我們檢查一下這個函式的型別。
域必須具有 type Or x。沒關系。
共域是 的結果filter,因此它是一個串列。讓我們只[....]為此而寫。
因此,函式為Or x -> [....]。
如果我們map這樣做,我們得到[Or x] -> [[....]]. 這與宣告的型別不同And x -> And x——引發型別錯誤。
首先,您希望您的 lambda 具有 type Or x -> Or x。為此,您可以使用\(Or x) -> Or (filter .....).
然后,你想filterLit成為
filterLit (And ys) = And (map ....)
使其具有正確的型別。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/517159.html
標籤:哈斯克尔
上一篇:在AST中包含注釋
