trpnd
trpnd

Reputation: 474

Writing haskell types pointfree (or other ways to write type-level functions)?

Are there combinators to write Haskell types in pointfree style?

I have a type synonym that is something like:

type FooT m a = StateT State (ReaderT (Params m) m)

I'd like to be able to write the right-hand-side of this in pointfree style in order to instantiate a typeclass that demands a parameter be a monad transformer. ie, there is some typeclass like:

class (MonadTrans t, Bar (t Monad)) => Baz t where -- where Bar is some other typeclass
 ...

And I would like to instantiate it with my stack of transformers. However, I need to provide it with something of kind (* -> *) -> * -> *, which means I would need to write a type-level function in order to pass my StateT State (ReaderT ...) transformer as a parameter to the typeclass.

I tried using type families but it seems like they need to be fully applied.

Upvotes: 0

Views: 146

Answers (1)

Li-yao Xia
Li-yao Xia

Reputation: 33519

You must use newtype. Type synonyms have to be fully applied, so they do not actually give you a way to rearrange type expressions to fit certain kinds.

The simplest is to turn FooT into a newtype:

newtype FooT m a = FooT (StateT State (ReaderT (Params m) m) a)

It's possible to develop a combinator style, but every combinator must be a newtype so that's kind of a pain to unwrap.

{-# LANGUAGE PolyKinds, TypeOperators #-}
newtype (:.:) f g a = (:.:) (f (g a))
newtype Join f a = Join (f a a)

type FooT = StateT State :.: Join (ReaderT :.: Params)
-- Good luck untangling that.

Upvotes: 5

Related Questions