Can type types of classes at the type level simulate with higher types?

I have a plug-in run-time type check that supports parametric but not ad-hoc polymorphism, since the compiler and type information are not deleted as soon as the type check decrypts.

Now I recently came up with the idea of ​​overriding type classes with an explicit type, so that I will get some of my advantages without having to completely include the type of the underlying mechanism in the validation mechanism:

data Functor f = Functor {fmap :: forall a b. (a -> b) -> f a -> f b}

mapList = Functor map
fmap (mapList) (+1) [1,2,3]

It seems that class types can be modeled with rank-2 types, at least at the type level, because of course there is still no static dispatching.

Is my assumption correct, and since I'm new to Haskell, does my explicit type of functor include any benefits only when used mapdirectly?

+6
source share
1 answer

The idea of ​​representing a class by data type is basically a dictionary, which is actually largely related to how the GHC implements class classes anyway: a boundedly polymorphic function / value

f :: Functor f => Y

is at run time represented by function

_f_ :: FunctorDict f -> Y

where FunctorDictessentially your FunctorRank2-ADT (or GADT).

, , , singleton: F instance Functor F, FunctorDict F . , , (GHC , ), .

+7

All Articles