When I first learned Haskell, I very quickly came to love parametric polymorphism. It\'s a delightfully simple idea that works astonishingly well. The whole \"if it compiles
Haskell (with no extensions) permits polymorphic recursion, and this feature alone makes it impossible to statically specialize a program to a completely monomorphic one. Here is a program that will print an N-deep nested list, where N is a command-line parameter:
import System
foo :: Show a => Int -> a -> IO ()
foo 0 x = print x
foo n x = foo (n-1) [x]
main = do [num_lists] <- getArgs
foo (read num_lists) 0
In the first call to foo
, a
has type Int
. In the next recursive call, it has type [Int]
, then [[Int]]
, and so forth.
If polymorphic recursion is prohibited, then I believe it's possible to statically specialize a program.