10

I was browsing the source of Data.Foldable, where I came accross Endo #. f, and upon clicking this link, was confronted with:

(#.) _f = coerce

Now, first, I don't know what coerce and the before-that mentioned Coercible is, however, it baffles me more what the _f could mean. I have searched "Haskell underscore before variable" and similar and have only found discussions about the _ pattern matching syntax.

1

2 Answers 2

25

According to the Haskell spec, it is just another possible name for a variable. But the truth is a bit longer, because most Haskell developers write code specifically for GHC, and this is one of those times.

GHC has lots of helpful warnings; one is to warn you when you write a pattern that binds a variable that isn't used in the body of the function. It's pretty handy, and has caught a couple bugs of mine. However, it has a knock-on effect: if you write a function that uses some of the variables only in one clause or the other, you get a warning. For example, here is a very natural definition of foldr on lists:

 foldr f z [] = z
 foldr f z (x:xs) = x `f` foldr f z xs

Oops! We didn't use f in the first clause, and we'll get a warning. Okay, so it's easy enough to fix:

foldr _ z [] = z
foldr f z (x:xs) = x `f` foldr f z xs

Unfortunately now we've lost the information about what that first variable's role in the code is supposed to be. In this case, foldr is so familiar that it's no big loss, but in unfamiliar codebases with functions that take a lot of arguments, it can be nice to know what data is ignored by each "hole". So GHC adds a special rule: the warning about unused variables doesn't warn you about variable names that start with _ -- by analogy to the _ pattern that it also doesn't warn you about. So now I can write:

foldr _f z [] = z
foldr  f z (x:xs) = x `f` foldr f z xs

Now I get the best of both worlds: I get a nice warning if I forget to use a variable I bound, and I can still give the reader information about the meaning of the holes in patterns that I don't need for the current clause. (As a side note, I'd love an additional warning that reported if I do mistakenly use a variable starting with _, but I don't think it exists at the moment! It sounds stupid ("just don't type _ in a function body"), but I've found my editor's tab-completion occasionally inserting them for me, and it would be easy not to notice if you were coding quickly.)

3
  • To me it looks like the single underscore name has something more than names starting with underscore. The former can be used more than once in a single match without triggering the Conflicting definitions for ‘_’ error; furthermore, it cannot be used in the rhs of =.
    – Enlico
    Nov 27, 2019 at 18:08
  • @EnricoMariaDeAngelis Correct, a single underscore is special: it is not a valid variable name at all, in fact! It is a pattern, which matches anything, and binds no names. (And this pattern is even mentioned off-hand in the answer.) Nov 27, 2019 at 20:22
  • Ok, it was not apparent to me from your (nonetheless excellent) answer.
    – Enlico
    Nov 27, 2019 at 20:25
4

It's just a convention to mark an unused variable. As you see, _f isn't used in the definition of #., so it is marked with the underscore.

1
  • 5
    It's a tad bit more than human convention though - GHC won't give unused variable warnings for underscore-prefixed variables. Jun 5, 2018 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.