How can eta-reduction of a well typed function result in a type error?

后端 未结 2 2115

I was playing around with van Laarhoven lenses and ran into a problem where the type-checker rejects the eta-reduced form of a well-typed function:

{-# LANGUAGE          


        
2条回答
  •  梦毁少年i
    2021-02-19 02:28

    I'd say that the reason isn't in the η-reduction itself, the problem is that with RankNTypes you lose principal types and type inference.

    The problem with type inference with higher-order ranks is when inferring the type of λx.M to obey the rule

         Γ, x:σ |- M:ρ
    ----------------------
      Γ |- λx:σ.M : σ→ρ
    

    we don't know what type σ we should choose for x. In the case of Hindley-Milner type system, we limit ourselves to type-quantifier-free types for x and the inference is possible, but not with arbitrary ranked types.

    So even with RankNTypes, when the compiler encounters a term without explicit type information, it resorts to Hindley-Milner and infers its rank-1 principal type. However, in your case the type you need for getWith id is rank-2 and so compiler can't infer it by itself.

    Your explicit case

    get lens = getWith id lens
    

    corresponds to the situation where the type of x is already given explicitly λ(x:σ).Mx. The compiler knows the type of lens before type-checking getWith id lens.

    In the reduced case

    get = getWith id
    

    the compiler has to infer the type of getWidth id on it's own, so it sticks with Hindley-Milner and infers the inadequate rank-1 type.

提交回复
热议问题