categorifier icon indicating copy to clipboard operation
categorifier copied to clipboard

Preprocessing step to make sure `categorify` calls are in the right form

Open zliu41 opened this issue 3 years ago • 0 comments

We use presimplifier to support partial applications of Categorify.expression hidden inside a wrapper function, like this:

preApply :: (a -> b) -> a `c` b
preApply = Categorify.expression
{-# INLINE preApply #-}

(see https://github.com/con-kitty/categorifier/blob/d72020b7f2c4534f94d7694b477e0dc8a3cb1f77/plugin/Categorifier/Test/PartialApplication.hs)

The presimplifier performs inlining, which inlines preApply and exposes Categorify.expression, whose applications are now fully saturated.

However, this approach doesn't work for slightly non-trivial cases, for example:

preApply :: (a -> b) -> a `c` b
preApply = if True then Categorify.expression else error "oops"
{-# INLINE preApply #-}

or direct partial application:

Categorify.expression . foo

Also, running the presimplifier has the disadvantage of unwanted inlining, e.g., sometimes fromIntegral is inlined and becomes fromInteger . toInteger, which may lead to categorification failures (e.g., the target category C.Cat does not support categorifying toInteger).

We should use a different approach to support (at least some) partial applications. At least Categorify.expression . foo seems easy to support.

zliu41 avatar Mar 12 '22 17:03 zliu41