Compiler infers less polymorphism for local bindings

At first I thought it is a bug, but after reading some issues I got to think that it is a feature. Nevertheless, instead of filing a bug/CR I decided to ask first.

As in title: the compiler infers less polymorphism for local bindings as opposed to top-level bindings. For example, this code compiles:

deleteAt' idx arr = fromMaybe arr $ deleteAt idx arr

main = do
  logShow $ deleteAt' 0 [1,2,3]
  logShow $ deleteAt' 1 ["Ala", "ma", "kota"]

and this does not:

main = do
  logShow $ deleteAt' 0 [1,2,3]
  logShow $ deleteAt' 1 ["Ala", "ma", "kota"]
  where
    deleteAt' idx arr = fromMaybe arr $ deleteAt idx arr

(Could not match type String with Int). It works only after explicitly giving it a polymorphic type signature. Is it a deliberate behaviour?

2 Likes

Yeah this is deliberate, it was mused at one point but we ended up closing it: Let Generalization · Issue #423 · purescript/purescript · GitHub

There’s not much discussion in that issue about specifically why we opted not to do it, but here’s a paper arguing against it that I think informed the decision: https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tldi10-vytiniotis.pdf

5 Likes