-1

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.

Legion Daeth
  • 329
  • 1
  • 5
  • 15
  • 2
    So if "a" is zero and "b" is zero shouldn't your circuit output TRUE since they are equal? – Thomas M. DuBuisson Jan 16 '18 at 04:17
  • It would, 0 AND 0 is TRUE – Legion Daeth Jan 16 '18 at 04:25
  • 1
    How does `IN` determine truthiness? – Adam Smith Jan 16 '18 at 04:29
  • Could you elaborate @AdamSmith? I understand IN to be an operator that replaces the variable with its literal value at runtime. So my thought was that it would simply pick the string provided to the program. – Legion Daeth Jan 16 '18 at 04:37
  • @LegionDaeth That's what I'm asking about. How do you determine the value of `IN "A"` and `IN "B"` in your example? – Adam Smith Jan 16 '18 at 04:40
  • 1
    IN simply replaces the variable with the literal value it holds. So for example if both were FALSE then IN "A" -> FALSE and IN "B" -> FALSE. – Legion Daeth Jan 16 '18 at 04:43
  • @LegionDaeth I'm confused when you say variable, because these seem to be string constants. Are they supposed to be referring to variables with those names in the global scope? (i.e. `(A, B) = (False, False); (p1, p2) = (IN "A", IN "B")` or etc? – Adam Smith Jan 16 '18 at 04:46
  • 1
    Yes, all IN does is replace the string constants with the values they hold in the program. So A is defined to hold TRUE or FALSE and IN changes them to that value instead of being "A" or "B". Sorry, really new to this. – Legion Daeth Jan 16 '18 at 04:51
  • @LegionDaeth the easiest way to implement that is with a `Data.Map.Map` from the [`containers`](https://hackage.haskell.org/package/containers) package, but you could use an association list too `:: [(String, Prop)]` and use [`lookup`](https://hackage.haskell.org/package/base-4.10.1.0/docs/Prelude.html#v:lookup) – Adam Smith Jan 16 '18 at 04:55
  • 4
    If 0 AND 0 is TRUE then why the heck is it called AND? I can't truthfully say "I went to the store AND I got some milk" if I did neither - only if I did both. The same thing applies to mathematical logic. – user253751 Jan 16 '18 at 05:08

1 Answers1

3

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   |
--------------------------------------------------------------------------
Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172