How to make a CAF not a CAF in Haskell?

前端 未结 7 1097
南旧
南旧 2020-12-01 05:23

How do I make a Constant Applicative Form into, well, not a Constant Applicative Form, to stop it being retained for the lifetime of the program?

I\'ve tried this ap

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 06:02

    The simplest possible solution might be to tell the compiler to inline it. (Note: this answer is untested. If it doesn't work, please comment below.)

    Even if (hypothetically) the compiler refuses to inline it for some reason, you can use cpp instead, by #defining it:

    #define twoTrues (map (++ (True : repeat False)) . trueBlock <$> [1..])
    

    (although with that approach, of course, it won't appear in the module's interface, so you can only use it within that module).

    The -cpp option tells GHC to preprocess the input file with cpp.

    Inlining would mean duplicating the code n times if you refer to twoTrues in n>1 places. However, while that's theoretically undesirable, it's probably not going to be a significant problem in practice.

提交回复
热议问题