Skip to content

Data constructors should not have RULES

GHC has never (knowingly) supported rules for data constructors, like

{-# RULES 
  "BadBadBad"   Just [x,y] = Just []
 #-}

Notice that the rule is for the constructor itself. Presumably the intent is that any occurrence of Just appplied to a two-element list will rewrite to Just [].

But currently they are accidentally allowed through, and then behave in mysterious ways because of constructor wrappers. Duncan Coutts says

> Well I've certainly tried to use that in the past.
> A previous version of the cbor lib which used a different 
> representation did a lot of matching on constructors to re-arrange 
> input to an interpreter, until I discovered that GHC actually uses 
> constructor wrappers and that matching on constructors was thus not 
> reliable

I think we should simply make it illegal for now. If you really want it, use a smart constructor thus

mkJust x = Just x
{-# INLINE [0] mkJust #-}
{-# RULES “Good” mkJust [x,y] = mkJust [] #-}

So let us

  • Check in that you don't try to write a rule for a data constructor.
  • Document in the user manual
Edited by Simon Peyton Jones
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information