Skip to content

Locally-scoped RULES

I'd like the following to compile:

module A where

f = g where
  {-# RULES "g/h" g = h #-}
  g = undefined
  h = undefined

This doesn't currently compile, because all RULES have to be top-level.

However, GHC permits SPECIALISE pragmas at local scope; the following compiles:

module Test where

f :: Num a => a -> a
f = g where
  {-# SPECIALISE g :: Int -> Int #-}
  g :: Num b => b -> b
  g x = x + x

(Admittedly, this example discards the specialisation, but more interesting examples don't.)

I only point out that the SPECIALISE pragma succeeds, because the user manual has the comment that rules subsume the 'Obsolete SPECIALIZE syntax', but in this case they don't.

I've got a real example where I would like locally-scoped RULES, but it is rather long, so I will post it as a comment.

Trac metadata
Trac field Value
Version 7.0.3
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information