On Thu, Jan 21, 2021 at 11:50:01AM +0100, Stefan Höck wrote:
> Thanks for your quick reply. I am wondering, though, whether this is
> guaranteed to be safe across backends.
I'd say it is. The newtype optimisation is applied during translation to
CExp. Existing backends generate code from CExp or its further
simplifications.
In theory, you could imagine a backend that would work from TT instead
of CExp, where the newtype optimisation would not apply. Despite being
technically possible, I don't think that's the intended way of writing
backends.
> Also, I'd feel better (and safer) if this kind of guarantees could be
> provided by the typechecker.
Sadly, as far as I know, Idris has currently no means to talk about (the
equivalence of) the runtime representation of things.
How would you encode such information/knowledge in the type system?
Using some sort of Coercible typeclass? It would be an interesting thing
to figure out how the compiler could autogenerate instances depending on
the optimisations done on the IR...
> For instance, assume somebody changed the implementation of
> `Identity`, for instance by annotating it with `noNewtype`, the call
> to `believe_me` would now introduce a potential bug.
Indeed, believe_me is highly unsafe and should not be used ordinarily.
If we want to model the equivalence of runtime representation, we should
come up with something else that wraps believe_me safely (or replaces
it) for that particular purpose.
Matus