haskell - 在 haskell 中构建重言式和可满足的公式

标签 haskell

我在尝试理解如何构建重言式和可满足的公式时遇到了一些困难。我正在研究需要我使用此类方法模拟 NAND 门和 NOR 门的问题。

问题:

通过扩展文件 proplog.hs 中的代码

A - 模拟 nand,也不是 Not、Or 和 And。 当至少一个输入为假时,nand 为真

当它的两个输入都为假时也不为真

B - 通过使用 nand,nor,xor,impl,T,F 构建

a) 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))

谢谢!

最佳答案

您可以根据其他门来编写 nand,但更好的方法可能是直接定义它:

-- Nand 
nand x y = not' (and' x y)

-- Nand - by definition
nand' F _ = T
nand' _ F = T
nand' _ _ = F

什么是最伟大的逻辑重言式?当然是 Modus ponens!

modusPonens p q = (p `and'` (p `impl` q)) `impl` q
prove_modusPonens = taut2 modusPonens

这里有一些简单的公式:

f0 p q = p `and'` q 
satisfy_f0 = sat2 f0

f1 p = p `and'` (not' p)
satisfy_f1 = sat1 f1 

关于haskell - 在 haskell 中构建重言式和可满足的公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23147463/

相关文章:

haskell - Haskell 中的循环列表和无限列表有什么区别?

haskell - 无法使用镜头从嵌套 JSON 收集值

haskell - 如何在 NixOS 上使用新的 haskell-ng 基础架构?

haskell - 使用 DataKind 在类型签名中绑定(bind)名称

haskell - 稍微复杂 [String] -> [UTCTime]

haskell - 解析 attoparsec 中不以某些字符结尾的标识符

haskell - 为什么这个 Parsec 解析器会进入无限循环?

haskell - J 风格的副词、 fork 等是否通过主流函数式语言的库进行了模拟?

database - Happstack 状态概念和文档?

haskell - 如何将函数映射到嵌套数据结构的一部分?