我目前正在編寫一些代碼來以特定的方式來描述邏輯形式。在追求這個,我寫了下面的代碼,這使得使用的鏡頭:關於從另一個異構構建一個Iso的困惑
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE Rank2Types #-}
module Logic.Internal.Formula where
import BasicPrelude hiding (empty, negate)
import qualified Data.Set as Set
import Control.Lens
data Atom = Pos { i :: Integer}
| Neg { i :: Integer}
deriving (Eq, Ord, Show, Read)
negatingAtom :: Iso' Atom Atom
negatingAtom = iso go go
where go (Pos x) = Neg x
go (Neg x) = Pos x
newtype Conjunction a = Conjunction (Set a)
deriving (Eq, Ord, Read, Show)
conjuncts :: Conjunction a -> [a]
conjuncts (Conjunction x) = Set.toList x
newtype Disjunction a = Disjunction (Set a)
deriving (Eq, Ord, Read, Show)
disjuncts :: Disjunction a -> [a]
disjuncts (Disjunction x) = Set.toList x
negatingClause :: Iso' (Conjunction Atom) (Disjunction Atom)
negatingClause = liftClause negatingAtom
type CNF = Conjunction (Disjunction Atom)
type DNF = Disjunction (Conjunction Atom)
-- Helper stuff
liftClause :: (Ord a) => Iso' a a -> Iso' (Conjunction a) (Disjunction a)
liftClause x = let pipeline = Set.fromList . fmap (view x) in
iso (Disjunction . pipeline . conjuncts) (Conjunction . pipeline . disjuncts)
於是,我試圖寫在下面(我認爲是)自然的方式:
type CNF = Conjunction (Disjunction Atom)
type DNF = Disjunction (Conjunction Atom)
negatingForm :: Iso' CNF DNF
negatingForm = liftClause negatingClause
但是,typechecker是肯定不滿意這一點,我一點也不知道爲什麼。確切的輸出低於:
Couldn't match type ‘Conjunction Atom’ with ‘Disjunction Atom’
Expected type: p1 (Disjunction Atom) (f1 (Disjunction Atom))
-> p1 (Disjunction Atom) (f1 (Disjunction Atom))
Actual type: p1 (Disjunction Atom) (f1 (Disjunction Atom))
-> p1 (Conjunction Atom) (f1 (Conjunction Atom))
In the first argument of ‘liftClause’, namely ‘negatingClause’
In the expression: liftClause negatingClause
我是一個有點新的鏡頭,並且真的想明白了我的誤解在這裏,以及我將如何實現所需的Iso
。
想必你想'liftClause ::(ORD一,奧德B)=>異 'AB - >異' (連接a)(分離b)'。 –