Type error with rank-2 types and function composition

后端 未结 2 1473
既然无缘
既然无缘 2020-12-10 12:13

Here are some pragmas and some imports:

{-# LANGUAGE ScopedTypeVariables #-}

import Control.Monad.ST
import Data.Array.ST
import Data.Array
<
2条回答
  •  醉话见心
    2020-12-10 12:25

    As you already hint at in the title of your post, the problem has to do with runSTArray having a polymorphic type of rank 2.

    runSTArray :: Ix i => (forall s. ST s (STArray s i e)) -> Array i e
    

    With

    elems :: Ix i => Array i e -> [e]
    

    and

    ($) :: (a -> b) -> a -> b
    

    writing runSTArray $ ... means that the type variable a in the type schema of ($) needs to be instantiated with a polymorphic type rather than a monomorphic type. This requires so-called impredicative polymorphism. How GHC implements impredicative polymorphism is explained in the ICFP 2008 paper by Dimitrios Vytiniotis, Stephanie Weirich, and Simon Peyton Jones: FPH : First-class Polymorphism for Haskell. The bottom line is that while FPH often gives you the behaviour that you expect, typeability is sometimes not preserved under simple transformations like the ones you describe in your question: see Section 6.2 of the aforementioned paper.

提交回复
热议问题