Skip to content

Pragma to SPECIALISE on value arguments

I've sometimes found myself wishing for this pragma to get some "partial evaluation on the cheap". The idea is to allow something like:

defaultOpts :: Options
defaultOpts = ...

{-# SPECIALISE f defaultOpts :: Int -> Int #-}
f :: Options -> Int -> Int
f opts x = ... f opts ...

This would desugar into this additional code:

{-# RULES "f/spec" f defaultOpts = f_spec_1 #-}
f_spec_1 = (\opts x -> ... ... f opts ...) defaultOpts -- NB: body of f duplicated

The hope is that the simplifier and RULE matcher will tidy this up so we get a nice loop back to f_spec_1 with the body of the function specialised for the particular opts.

This is useful when functions are called often with particular arguments. An example would be where "f" is an edit-distance function which takes costs to be assigned to each edit, strings to be compared and returns an integer distance. In my library, the costs are given almost always going to be the default ones so I want to make that case fast, but I want to allow the user to supply their own set.

This pragma is somewhat subsumed by:

  1. SpecConstr, if the options are algebraic data/literals that are also scrutinised by the body of f
  2. Static argument transformation, except that the RULE based strategy achieves more code sharing compared to SAT

I think that pragma might be a relatively simple to implement nice-to-have feature.

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