Given this line of code (which I first saw in this answer):
pVal :: Num a => a
pVal = sum . map fromIntegral $ ([1..10000000] :: [Int])
If it’s used as multiple types, is the expression completely reevaluated for each type? Is one result for each type kept around?
For example:
pInt :: Int
pInt = pVal
pDub :: Double
pDub = pVal
Technically, the Haskell standard doesn’t specify. In practice, it’s re-evaluated each time. You can verify this yourself using
Debug.Trace:Try it in GHCi:
However, binding this value to a concrete type will allow it to be re-used.
Notice there’s no “evaluating…” the second time: