This value squares
is potentially polymorphic:
Prelude> :t [ x ** 2 | x <- [1 ..] ]
[ x ** 2 | x <- [1 ..] ] :: (Floating t, Enum t) => [t]
AFAIK, whether or not it will be recalculated (in GHC) depends on whether the top-level value squares
is given a polymorphic type. I believe that GHC does not do any memoization of polymorphic values involving type classes (functions from types to values), just as it does not do any memoization of ordinary functions (functions from values to values).
That means if you define squares
by
squares :: [Double]
squares = [ x ** 2 | x <- [1 ..] ]
then squares
will only be computed once, while if you define it by
squares :: (Floating t, Enum t) => [t]
squares = [ x ** 2 | x <- [1 ..] ]
then it will likely be computed each time it is used, even if it's used repeatedly at the same type. (I haven't tested this, though, and it's possible that GHC, if it sees several uses of squares :: [Double]
, could specialize the squares
value to that type and share the resulting value.) Certainly if squares
is used at several different types, like squares :: [Double]
and squares :: [Float]
, it will be recalculated.
If you don't give any type signature for squares
, then the monomorphism restriction will apply to it, unless you have it disabled. The result will be that squares
is assigned a monomorphic type, inferred from the rest of your program (or according to the defaulting rules). The purpose of the monomorphism restriction is exactly to ensure that values that look like they will only be evaluated once, such as your squares
, really will only be evaluated once.