Is it possible to introduce extra constraints using e.g. unsafeCoerce?

111 Views Asked by At

Is it possible to use unsafeCoerce (or some other unsafe functionality) to 'make up' e.g. a Typeable constraint for a type? I.e., to change a type from a forall v. v into an existential forall v. Typeable v => v?

I am in a situation where I have some datatypes/functions that can handle all types a but do require to call typeOf and/or toDyn on them. The result will not break parametricity, but it is impossible to convince the typechecker of this.

To be precise, I need to convert some types to Dynamic and later back. However, currently the requirement of Typeable prevents the implementation of typeclasses such as Functor, Applicative, etc.

(I know that another technique called 'constrained normalized deep embeddings' exists to allow types that require a constraint to still implement these kinds of typeclasses. However, this technique will not work on datatypes containing cycles, as the lowering operation will loop infinitely.)

So is there a way to convince the typechecker to trust me here? Even if it is very unsafe?

0

There are 0 best solutions below