Prison should be a balance between serving justice for victims/society through seeing offenders deprived of their liberty, keeping society safe where it's deemed some offenders are too dangerous to be left free, and rehabilitation of offenders so that they can become functional and contributing members of society.
Currently, it's failing in all these respects.
Prisons are so overcrowded and prison and probation staff so overworked that all but the most exceptionally dangerous prisoners escape with community-based sentences, and we've seen some awful tragedies as a result.
And despite the best efforts of very dedicated staff, rehabilitation in prisons is a joke. Educational and therapeutic opportunities are limited. Many are confined to their cells for 23 hours a day, with no opportunity to exercise or work on self development.
It's not making excuses but a fundamental requirement to understand what drives offending behaviours. There are very striking, way-above-average trends of substance dependency, mental illness, adverse childhood experiences, learning difficulties, lack of qualifications etc among this group. Many of them had appalling childhoods and have been let down by society long so badly that they no longer have any stake in society. The social contract no longer exists for them.
If we truly want rehabilitation, prison should be places of intense psychological therapy and education. Accompanied by good quality food and regular exercise (we all know the links between diet, exercise and behaviour). Those leaving need far better housing and job-finding support.
The fact that many people outside of prison are also denied this support is a damning indictment of society and not a justification to also deny it to prisoners.