Law doesn't seek to punish but to rehabilitate. Act of taking freedom away from the criminal is violent enough. Treating them badly is just a sign of unfair/poor society that cannot maintain (afford to keep) it's promise to be civil to all citizens.
Your confounding how things should be with how they are. These are two distinct philosophies, only one of which is relevant in most of the real world, unfortunately.
Punishment and rehabilitation aren’t mutually exclusive. Arguably, the punishment aspect is served by the removal of freedoms that being locked in prison entails. Rehabilitation can be any number of things that the prisoner does while in prison.