How does being mentally ill absolve one from criminal responsibility?

Anyway, this remark makes me think that the more violent the crime, the less likely prison becomes:
Justice John Scurfield said Li's attack was "grotesque" and "barbaric" but "strongly suggestive of a mental disorder."