Derive parse from print with reasonable constraints

or how to invert some functions

I will introduce simple motivation behind this whole story and shortly we will know how to derive parse :: s → Maybe a from print :: a → s, with reasonable constraints — BoundedEnum a and Ord s.

Motivation

Type system is a friend who’s got your back by making sure all inputs are encountered when writing functions. Usually print :: a → s takes some type which is smaller (has smaller number of values) than its resulting type. But when writing parse :: s -> Maybe a our friend can’t cover us anymore, as input type is bigger and we need to use _ → in our case expression (it might be impossible to write down all the cases, take String for example).