Question

Is it possible in Haskell to apply the function arrow type constructor (->) to only its right-side type (for instance: (-> a)) to get a type constructor of kind * -> *?

Was it helpful?

Solution

No, it is currently impossible. There are certain limitations to Haskell's type system that allow it to be useful and convenient for most cases, and this is one of those limitations. Your best alternative is to use a newtype.

newtype FuncFlip r a = FuncFlip { unFuncFlip :: a -> r }

Newtypes are simply tags to help the compiler know how to typecheck and perform type-directed dispatch (typeclasses) properly. Presumably you wanted to flip the type arguments to provide some typeclass instance. That just means that whenever you want to make use of that typeclass's functions, you have to decorate any specific inputs with FuncFlip, and undecorate any specific outputs with unFuncFlip. This is slightly more verbose than desired, but it's not actually that bad, because it forces you to explicitly identify which instance of the typeclass you want to use.

You can create an instance of Newtype for this, which may or may not turn out to be convenient for you.

instance Newtype (FuncFlip r a) (a -> r) where
  pack = FuncFlip
  unpack = unFuncFlip

Further reading: Are there "type-level combinatos?"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top