Static types, polymorphism and specialization

前端 未结 3 1959
别那么骄傲
别那么骄傲 2021-01-01 09:49

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

3条回答
  •  伪装坚强ぢ
    2021-01-01 10:43

    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.

提交回复
热议问题