Task

"Simulate" a four-bit adder.

This design can be realized using four 1-bit full adders.
Each of these 1-bit full adders can be built with two half adders and an   or   gate. ;

Finally a half adder can be made using an   xor   gate and an   and   gate.

The   xor   gate can be made using two   nots,   two   ands   and one   or.

Not,   or   and   and,   the only allowed "gates" for the task, can be "imitated" by using the bitwise operators of your language.

If there is not a bit type in your language, to be sure that the   not   does not "invert" all the other bits of the basic type   (e.g. a byte)   we are not interested in,   you can use an extra   nand   (and   then   not)   with the constant   1   on one input.

Instead of optimizing and reducing the number of gates used for the final 4-bit adder,   build it in the most straightforward way,   connecting the other "constructive blocks",   in turn made of "simpler" and "smaller" ones.


Schematics of the "constructive blocks"


(Xor gate with ANDs, ORs and NOTs)        

   (A half adder)        

          (A full adder)            

                (A 4-bit adder)        












Solutions should try to be as descriptive as possible, making it as easy as possible to identify "connections" between higher-order "blocks".

It is not mandatory to replicate the syntax of higher-order blocks in the atomic "gate" blocks, i.e. basic "gate" operations can be performed as usual bitwise operations, or they can be "wrapped" in a block in order to expose the same syntax of higher-order blocks, at implementers' choice.

To test the implementation, show the sum of two four-bit numbers (in binary).




 
type Bit =
    | Zero
    | One
 
let bNot = function
    | Zero -> One
    | One -> Zero
 
let bAnd a b =
    match (a, b) with
    | (One, One) -> One
    | _ -> Zero
 
let bOr a b =
    match (a, b) with
    | (Zero, Zero) -> Zero
    | _ -> One
 
let bXor a b = bAnd (bOr a b) (bNot (bAnd a b))
 
let bHA a b = bAnd a b, bXor a b
 
let bFA a b cin =
    let (c0, s0) = bHA a b
    let (c1, s1) = bHA s0 cin
    (bOr c0 c1, s1)
 
let b4A (a3, a2, a1, a0) (b3, b2, b1, b0) =
    let (c1, s0) = bFA a0 b0 Zero
    let (c2, s1) = bFA a1 b1 c1
    let (c3, s2) = bFA a2 b2 c2
    let (c4, s3) = bFA a3 b3 c3
    (c4, s3, s2, s1, s0)
 
printfn "0001 + 0111 ="
b4A (Zero, Zero, Zero, One) (Zero, One, One, One) |> printfn "%A"
 