A Theory of Composing Protocols