FullAdder, using only the public API. The result is 60 NAND gates, organized as a hierarchy you can inspect.
1
Define the ports
Two four-bit operands (
a0–a3 and b0–b3, least significant bit first), a carry in, four sum bits and a carry out.2
Chain the full adders
Each stage’s carry out feeds the next stage’s carry in. A The template literal keys like
SourcePort variable tracks the carry as the loop runs.inputs[`a${i}`] are still type-checked, because i is the literal union 0 | 1 | 2 | 3.3
Add some numbers
4
Test every combination
With 4 + 4 + 1 input bits there are only 512 cases, so you can check them all:The library’s own test suite runs exactly this check.
5
Look inside
elaborate() flattens the hierarchy into primitive gates: