首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在haskell中建立重言式和可满足公式

在haskell中建立重言式和可满足公式
EN

Stack Overflow用户
提问于 2014-04-18 04:40:31
回答 1查看 658关注 0票数 3

我遇到了一些困难,试图理解如何建立重言式和可满足公式。我正在处理的问题,要求我模拟一个NAND门和一个NOR门使用这些方法。

问题:

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

A-模仿nand,也不表示Not,或和和.当至少一个输入为假时,nand为真。

当两个输入都为假时,也不正确。

B-通过使用nand,nor,xor,impl,T,F生成

( a) 2个重言式

( b)可满足公式

( c)不可满足的公式

Proplog.hs:

代码语言:javascript
复制
-- 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))

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-18 05:30:52

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

代码语言:javascript
复制
-- Nand 
nand x y = not' (and' x y)

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

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

代码语言:javascript
复制
modusPonens p q = (p `and'` (p `impl` q)) `impl` q
prove_modusPonens = taut2 modusPonens

以下是一些简单的公式:

代码语言:javascript
复制
f0 p q = p `and'` q 
satisfy_f0 = sat2 f0

f1 p = p `and'` (not' p)
satisfy_f1 = sat1 f1 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23147463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档