Its a little more complicated than that.
Carbine is any barrel from 14.5 to 16 or so, or in some references 18".
M4 is actually a barrel profile, and can be 14.5 or 16". Its the one thats light under the handguards and has the M203 mounting turndown in front of the front sight base.
Mid length is a length of gas system and the corresponding handguards. 12.5, 14.5, 16 and 18" barrels can and sometimes do have midlength gas systems.
Handguards on a carbine or M4 are 7 inches long. On a midlength they are 9 inches. Full size rifles use 12 inch long handguards.
ETA: An M4 and a mid length are also considered carbines, but normally the distinction that they are "M4 carbines" or "Mid length carbines" is made. In the above list, consider the term carbine to mean a non M4 barreled rifle with a standard 7 inch gas system.