The purpose of prison is not to punish or rehabilitate specifically. Prison is part of the legal system, whose purpose is to provide stability and safety to society, with prison acting as a means to apply sentencing. Plenty of people (myself included) prefer the rehabilitation angle, plenty of others prefer the punishment angle - but both arguments see 'keeping society safe' as their target condition for how a prison system is run.