Skip to content

The () constraint should perhaps not be optimised.

Summary

Optimising the constraint () away can change the behavior of programs. Sometimes you want to use global variables that are recomputed each time they are used, because storing the entire structure takes too much space.

Steps to reproduce

x :: () => [Int]
x = [0..]

main = do
  print $ x !! 1000000000
  print $ x !! 1000000001

Expected behavior

This program should run in constant memory because the list [0..] should be recomputed.

To get the expected behavior we have to trick the compiler with a more complicated constraint like:

x :: ()~() => [Int]

or

x :: (() :: Constraint) => [Int]

But what if GHC also optimises these away in the future?

Environment

  • GHC version used: 8.10.4
Edited by Jaro Reinders
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information