(def nand (a b) (r) ( (let (a_and_b) (relay_default_off a b)) (set r (relay_default_on a_and_b vin)) )) (def not (a) (r) ( (set r (nand a a)) )) (def and (a b) (r) ( (set r (not (nand a b))) )) (def or (a b) (r) ( (set r (nand (not a) (not b))) )) (def xor (a b) (r) ( (set r (and (or a b) (not (and a b)))) )) (def xor_optimized (a b) (r) ( (let (c) (nand a b)) (set r (nand (nand a c) (nand b c))) )) (def half_add (a b) (r0 r1) ( (set r0 (xor a b)) (set r1 (and a b)) )) (def add (a b carry) (r0 r1) ( (let (d0 d1) (half_add a b)) (let (e0 e1) (half_add carry d0)) (set r0 e0) (set r1 (or d1 e1)) )) (def add2 (a0 a1 b0 b1 carry_in) (r0 r1 carry_out) ( (let (d0 d1) (add a0 b0 carry_in)) (let (e0 e1) (add a1 b1 d1)) (set r0 d0) (set r1 e0) (set carry_out e1) ))