aboutsummaryrefslogtreecommitdiffstats
path: root/src/Main.hs
blob: e25986cf6fe42581f3bff54020de4c0223579f26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Main where

import Data.Bits

newtype Input = Input { getInput :: Bool }
              deriving (Show)

data Gate = And
          | Or
          | Xor
          | Nand
          | Nor
          deriving (Show)

data Circuit = In Input
             | Node Gate Circuit Circuit
             deriving (Show)

eval :: Circuit -> Bool
eval (In val) = getInput val
eval (Node And c1 c2) = eval c1 .&. eval c2
eval (Node Or c1 c2) = eval c1 .|. eval c2
eval (Node Xor c1 c2) = eval c1 `xor` eval c2
eval (Node Nand c1 c2) = complement $ eval c1 .&. eval c2
eval (Node Nor c1 c2) = complement $ eval c1 .|. eval c2

main :: IO ()
main = print . eval $ Node And (In . Input $ True) (In . Input $ True)