Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Created March 21, 2015 16:33
Show Gist options
  • Save lovasoa/e6806f1d7111c8c4ca44 to your computer and use it in GitHub Desktop.
Save lovasoa/e6806f1d7111c8c4ca44 to your computer and use it in GitHub Desktop.
Simplificateur d'équations booléennes
type ProcNum = Int
type Sort = String
data Op = And | Or deriving (Show,Eq)
data Ex = Proc Sort ProcNum | BoolOp Op Ex Ex | Not Ex
instance Show Ex where
show (BoolOp op a b) = "("++show a++" "++show op++" "++show b++")"
show (Proc name n) = name++show n
show (Not e) = '!':show e
e = BoolOp And (Proc "a" 0) (Not (BoolOp Or (Proc "b" 1) (BoolOp And (Proc "c" 2) (Proc "d" 2))))
maxProc::Sort->ProcNum
maxProc _ = 1
other op = if op==And then Or else And
nonot:: Ex -> Ex
nonot (Not (Not a)) = a
nonot (Not (Proc a b)) = foldr1 (BoolOp Or) $
map (Proc a) $ filter (/=b) [0..maxProc a]
nonot (Not (BoolOp op a b)) = nonot $ BoolOp (other op) (nonot (Not a)) (nonot (Not b))
nonot (BoolOp And a b) = let t = (nonot a, nonot b)
in case t of
(BoolOp Or a b, d) -> BoolOp Or (nonot $ BoolOp And a d) (nonot $ BoolOp And b d)
(d, BoolOp Or a b) -> BoolOp Or (nonot $ BoolOp And a d) (nonot $ BoolOp And b d)
(a, b) -> BoolOp And a b
nonot x = x
mklist:: Ex -> [[(Sort,ProcNum)]]
mklist (Proc name num) = [[(name,num)]]
mklist (BoolOp Or a b) = mklist a ++ mklist b
mklist (BoolOp And a b) = [foldr (\cur old -> if null old then old
else case lookup (fst cur) old of
Just a | a == (snd cur) -> old
Just _ -> []
Nothing -> cur:old)
(head $ mklist a) (head $ mklist b)]
main = print $ mklist $ nonot $ (BoolOp And (BoolOp Or (Proc "c" 1) (Proc "d" 1)) (BoolOp Or (Proc "a" 1) (Not $ Proc "b" 1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment