This is pretty simple with template haskell. Firstly, define the code in one module:
module Test where
--Purposely inefficient code for demonstration
fib 0=0
fib 1=1
fib n=fib (n-1) + fib (n-2)
Then create the value using that code with template haskell in another module. You have to do it in another module as template haskell definitions cannot call functions defined in the same module.
{-# LANGUAGE TemplateHaskell #-}
import Test
import Language.Haskell.TH
import Data.Ratio
goldRatio :: Double
goldRatio = $(litE (rationalL (toRational $ fib 21 / fib 20)))
Now compiling will take longer, however goldRatio
will now be a fixed value and compute instantly at runtime. It runs as if you typed goldRatio = 1.6180339985218033
in the source code. Example use:
> goldRatio
1.6180339985218033