2
我在試圖瞭解如何構建重言式和可滿足的公式時遇到一些困難。我正在研究需要我使用這種方法模擬NAND門和NOR門的問題。在haskell中構建重言式和可滿足的公式
問題:
通過擴展文件proplog.hs
A中的代碼 - 模擬NAND,NOR中沒有,或者與和條款。 NAND爲真時的其輸入中的至少一個爲假
也不是真時它的兩個輸入均爲假
乙 - 通過使用NAND,NOR,XOR,IMPL,T,F建立
一個)2重言
b)中令人滿意的式
c)一種不可滿足式
Proplog.hs:
-- definition of basic gates
data Prop = T | F |
Not Prop |
And Prop Prop |
Or Prop Prop
deriving (Eq,Read,Show)
-- truth tables of basic gates
tt (Not F) = T
tt (Not T) = F
tt (And F F) = F
tt (And F T) = F
tt (And T F) = F
tt (And T T) = T
tt (Or F F) = F
tt (Or F T) = T
tt (Or T F) = T
tt (Or T T) = T
-- giving the tt of a derived gate
xor' F F = F
xor' F T = T
xor' T F = T
xor' T T = F
-- building the derived gate from Not, And, Or
xor x y = eval (And (Or x y) (Not (And x y)))
-- evaluating expressions made of logic gates
eval T = T
eval F = F
eval (Not x) = tt (Not (eval x))
eval (And x y) = tt (And (eval x) (eval y))
eval (Or x y) = tt (Or (eval x) (eval y))
ite c t e = eval (Or (And c t) (And (Not c) e))
truthTable1 f = [(x,f x)|x<-[F,T]]
tt1 f = mapM_ print (truthTable1 f)
truthTable2 f = [((x,y),f x y)|x<-[F,T],y<-[F,T]]
tt2 f = mapM_ print (truthTable2 f)
truthTable3 f = [((x,y),f x y z)|x<-[F,T],y<-[F,T],z<-[F,T]]
tt3 f = mapM_ print (truthTable3 f)
or' x y = eval (Or x y)
and' x y = eval (And x y)
not' x = eval (Not x)
impl x y = eval (Or (Not x) y)
eq x y = eval (And (impl x y) (impl y x))
deMorgan1 x y = eq (Not (And x y)) (Or (Not x) (Not y))
deMorgan2 x y = eq (Not (Or x y)) (And (Not x) (Not y))
-- tautologies, satisfiable and unsatisfiable formulas
taut1 f = all (==T) [f x|x<-[F,T]]
taut2 f = all (==T) [f x y|x<-[F,T],y<-[F,T]]
sat1 f = any (==T) [f x|x<-[F,T]]
sat2 f = any (==T) [f x y|x<-[F,T],y<-[F,T]]
unsat1 f = not (sat1 f)
unsat2 f = not (sat2 f)
-- examples of tautologies: de Morgan1,2
-- examples of satisfiable formulas: xor, impl, ite
-- example of contradiction (unsatisfiable formulas): contr1
contr1 x = eval (And x (Not x))
謝謝!