A categorical framework for circuit construction