Search code examples
haskellcircuit

Build a circuit to test if two inputs are equal


So I'm doing a question that reads as follows

Build a circuit using the abstract syntax for Prop to test if two inputs are equal. Justify that your circuit is correct.

This is the Prop in question.

 data Prop = FALSE | TRUE | IN String | NOT Prop | OR Prop Prop | AND Prop Prop

Instinctively I am tempted to write AND(IN "A")(IN "B") and give a truth table to prove it but this seems to simple. Am I missing something?

EDIT: My bad, I ended up making a XNOR gate that solved the problem. I mistook AND for XNOR which was the root cause of the confusion. The solution in the answer field is more elegant than mine so please refer to that.


Solution

  • The two inputs would be equal in two cases: either both are true or both are false. It seems that the given language allows to encode that: you can check if an input is true simply by referencing it, i.e. IN "A", and you can check if an input is false by negating it, i.e. NOT (IN "A"). Then combine these checks with ANDs and an OR, and you're done:

    OR 
       (AND           -- Both "A" and "B" are true
           (IN "A")       -- "A" is true
           (IN "B")       -- "B" is true
       )
       (AND                -- Both "A" and "B" are false
           (NOT (IN "A"))      -- "A" is false  
           (NOT (IN "B"))      -- "B" is false
       ) 
    

    .

    --------------------------------------------------------------------------
    |  A    |  B    | NOT A | NOT B | AND A B | AND (NOT A) (NOT B) | Result |
    --------------------------------------------------------------------------
    | false | false | true  | true  | false   | true                | true   |
    | false | true  | true  | false | false   | false               | false  |
    | true  | false | false | true  | false   | false               | false  |
    | true  | true  | false | false | true    | false               | true   |
    --------------------------------------------------------------------------