TL;DR: What can cause a type-mismatch error in GHCi purely as a result of function composition? It's odd to see GHCi evaluate the following code:
foldl (a . b . c) crackle pop <- GHCi evaluates this`
...only to give an error after we try to evaluate the following:
let snap = a . b . c <- GHCi evaluates this
foldl snap crackle pop <- GHCi reports an error (!)
The longer version:
I am baffled by what I'm observing here in GHCi and am hoping someone can explain it (description included below image):
What do we see above?:
First, we have a variable
b
which is bound to the following list:[(2,["Dipak"]), (2,["Andrew"]),(2,["Keone"])]
.b
is of type[(Int,[String])]
. (See the firstghci>
prompt and the resulting output in the screenshot above.)We then perform a fold on
b
, transforming it into the following type:Map (Integer, [String])
. We do so by using a folding function based oninsertWith (++)
a starting accumulator that is anempty
map. The function is as follows (same as what follows the secondghci>
prompt in the screenshot above. (See secondghci>
prompt above.)foldl' (flip $ uncurry (Map.insertWith (++))) (Map.fromList []) b
Okay, cool; so far, so good
Given the
foldl'
function above was a mouthful, I decided to compose a folding function (namedfoldingFunc
) which was equal toflip $ uncurry (Map.insertWith (++))
. This is simply the first argument offoldl'
in the expression above. (Seelet
expression in the thirdghci>
prompt above.)This is where I get confused: as a routine check, I perform the same
foldl'
as above, except withfoldingFunc
(in lieuflip $ uncurry (Map.insertWith (++))
) which should simply be a cosmetic change...and now GHCi reports a type mismatch error (details above).
Can someone please help me understand why function composition led to an error (as a result of a type change) in this case? And what should I be doing differently?
MonomorphismRestriction
turned on? What are the types ofb
andfoldingFunc
? (Inspect the types with:t
in ghci.) – Rufflewind