Skip to content

Add a field in SimplMode to turn off built-in rules

Motivation

Currently, built-in rules simply cannot be turned off during simplification:

-- GHC.Core.Rules
matchRule opts rule_env _is_active fn args _rough_args
          (BuiltinRule { ru_try = match_fn })
-- Built-in rules can't be switched off, it seems
  = case match_fn opts rule_env fn args of
        Nothing   -> Nothing
        Just expr -> Just expr

This poses a problem for what we are trying to do: we are developing a Core plugin that converts certain CoreExprs of type a -> b into CoreExprs of type M a -> M b for some M. Our plugin needs to call simplExpr on various parts of the expression, for purposes like inlining, case-of-case transformation, beta reduction etc. The one thing we do not want simplExpr to do is specialization - it is much easier and more efficient to directly convert == $fEqFoo ... than $fEqFoo5 .... The latter (1) has a strange name (2) has a different name for each Foo (3) may contain primops which are difficult to deal with.

I don't think our use case is the only one that would benefit from a simplExpr that allows turning off built-in rules. This would allow one to precisely control its behavior, e.g., only performing case-of-case and inlining, but nothing else, which seems useful in general.

Proposal

I think this can be easily achieved by adding a new field to SimplMode, say sm_builtin_rules, and then modifying the above code snippet to check this field. Its value would normally be True, but can be set to False if one knows what they are doing.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information