You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
HLS has quite a lot of use of partial functions. I'm sure many of these are benign, but we do occasionally get users reporting crashes with such (usually totally useless) errors.
Gradually reducing these is an easy, helpful thing for someone to do. Good new contributor issue!
Reacted by Junyoung/"Clare" Jang, Javier Neira , giladwo, Jan Hrcek, fendor, Hiromi Ishii, Sridhar Ratnakumar and Lauren Yim
I volunteer to work on this, as I wanted to contribute to hls in more substantial way.
Having looked at some of the partial functions usages, many of them require some domain-specific knowledge, e.g. about the Shake build stuff etc. For example I'm not at all sure about these:
Come up with a list of partial functions that we'd like to eventually replace with something better.
Apart from the above mentioned head, foldl1, (!!), fromJust i noticed quite a few Map.!, IntMap.!, Array.!, last
Can you think of others? Let's keep expanding the list in the description of this issue!
replace the obvious ones
when it's impossible to "prove" that given partial function will always succeeds, replace it with some variant that gives more informative exception (e.g. headNote from safe). This way we at least get more informative errors when one of these fails and we'll know from user reports which ones actually occur and should be fixed. Wdyt?
Reacted by giladwo and Junyoung/"Clare" Jang
Reacted by Javier Neira , fendor and Junyoung/"Clare" Jang
The plan sounds good to me too. A 4th bonus step would be to promote the .hlint.yaml that we have for ghcide to the top level, and set it to ban the various partial functions as we eliminate them.
Re: usesWithStale, I believe it could be simplified to take a NonEmpty NormalizedFilePath and return a NonEmpty list of results, which would allow avoiding the head. Or it could even work over an arbitrary Traversable if you wanted to get fancy, and then you could use Identity for the singleton cases you see there.
That's just from looking at the definitions of the functions! So I think you can probably get quite far without too much domain knowledge.
Re: usesWithStale, I believe it could be simplified to take a NonEmpty NormalizedFilePath and return a NonEmpty list of results, which would allow avoiding the head. Or it could even work over an arbitrary Traversable if you wanted to get fancy, and then you could use Identity for the singleton cases you see there.
usesWithStale doesn't use head so I assume you are referring to useWithStale. The use of head there is completely safe and cannot fail, by local equational reasoning on the call to usesWithStale [file]. So I don't really see the motivation fo the suggested change. Am I missing something?
when it's impossible to "prove" that given partial function will always succeeds, replace it with some variant that gives more informative exception (e.g. headNote from safe). This way we at least get more informative errors when one of these fails and we'll know from user reports which ones actually occur and should be fixed. Wdyt?
Let's make sure to validate these changes before applying them: headNote is not better than head nowadays. Same goes for other partial functions like fromJust, !!, etc.
usesWithStale doesn't use head so I assume you are referring to useWithStale. The use of head there is completely safe and cannot fail, by local equational reasoning on the call to usesWithStale [file]. So I don't really see the motivation fo the suggested change. Am I missing something?
Correct, it can't fail. However,
That could change, and we wouldn't notice.
Having exceptions to a "no head rule" makes it much harder to apply.
Using NonEmpty here removes no expressiveness and removes the need to do any reasoning at all (thanks typesystem).
So I agree it's safe, but if it's trivial to rewrite it so we don't even need to think about whether it's safe, that seems like a win to me.
Nota bene: As I was around this theme & two times promised to attend to a particular reduction of the head in the main doc retrieval path (#2539). HLS was always retrieving documentation doing the head [res]. Removing it also uncovered a bunch of other stuff around that code.
head removal is not the main agenda, PR is part of work towards provisioning argument documentation, the main agenda there is to provide argument documentation in a form that is neat for further processing.
IMO, partial functions are fine, if there is sufficient local proof that it is total in the given context (e.g. the input to a partial function is controlled by the outer function itself). #2672 (comment) is an example where no such local proof is possible and the callstack is actually really long. So the caller cannot make any reasonable assumption about env never being Nothing.
On the other hand, I don't think a function like this needs any adjustment:
foo::ShortByteString->ShortByteString
foo sbs
|null sbs = sbs
|last sbs == _space ="lol"|otherwise="shrug"
So I'm not too thrilled about enforcing hlint compliance. It's a nuisance.
I agree it is a nuisance but I would like to give here a different perspective: if hls crash for a beginner user, they are basically clueless what happened. Even if able to find Output in VSCode, finding the reason why hls crashed is hard (it is not necessarily the very last line in the output); and if the line contains just Prelude.head: empty list finding a workaround for such user is impossible.
So it's a nuisance for developer, but if the things go wrong way, for a user it is a deal breaker.
I think we need a hlint ratchet, even if it's going to be very tedious to whitelist all the existing usages. At the moment it's way too easy for people to introduce new partial function usage, even when it's very easy to avoid. We need something to slow that down.
I don't think a function like this needs any adjustment:
foo::ShortByteString->ShortByteString
foo sbs
|null sbs = sbs
|last sbs == _space ="lol"|otherwise="shrug"
So I'm not too thrilled about enforcing hlint compliance. It's a nuisance.
Why wouldn't you want to replace that with this?
foo::ShortByteString->ShortByteString
foo sbs =case unsnoc sbs ofNothing-> sbs
Just (_, x)
| x == _space ->"lol"|otherwise->"shrug"
Because that easily gets ridiculous if you want to look at the first 8 characters. I have a lot of those examples from the filepath package, but I'm not gonna list all of them here.
Activity
pepeiborra commentedon Dec 20, 2021
Could you share a few examples?
michaelpj commentedon Dec 20, 2021
30-odd usages of
head, 15 or sofoldl1s, 10 or so!!s.I think many of these are fine, but therefore usually trivial to avoid. It would be nice to have a blanket "no partial functions" rule.
michaelpj commentedon Dec 20, 2021
30-ish
fromJust...jhrcek commentedon Dec 21, 2021
I volunteer to work on this, as I wanted to contribute to hls in more substantial way.
Having looked at some of the partial functions usages, many of them require some domain-specific knowledge, e.g. about the Shake build stuff etc. For example I'm not at all sure about these:
haskell-language-server/ghcide/src/Development/IDE/Core/Shake.hs
Lines 854 to 867 in 070f16f
Can we prove these will never fail? What should the functions do in case of empty lisis? Should they maybe accept non-empty lists as inputs (as in https://www.parsonsmatt.org/2017/10/11/type_safety_back_and_forth.html)?
My preliminary plan is this:
Apart from the above mentioned
head,foldl1,(!!),fromJusti noticed quite a fewMap.!,IntMap.!,Array.!,lastCan you think of others? Let's keep expanding the list in the description of this issue!
jneira commentedon Dec 21, 2021
The plan sounds really great to me, given at minimum we will have more info about the error.
Many thanks for willing to contribute
michaelpj commentedon Dec 21, 2021
The plan sounds good to me too. A 4th bonus step would be to promote the
.hlint.yamlthat we have forghcideto the top level, and set it to ban the various partial functions as we eliminate them.Re:
usesWithStale, I believe it could be simplified to take aNonEmpty NormalizedFilePathand return aNonEmptylist of results, which would allow avoiding thehead. Or it could even work over an arbitraryTraversableif you wanted to get fancy, and then you could useIdentityfor the singleton cases you see there.That's just from looking at the definitions of the functions! So I think you can probably get quite far without too much domain knowledge.
pepeiborra commentedon Dec 21, 2021
usesWithStaledoesn't useheadso I assume you are referring touseWithStale. The use ofheadthere is completely safe and cannot fail, by local equational reasoning on the call tousesWithStale [file]. So I don't really see the motivation fo the suggested change. Am I missing something?pepeiborra commentedon Dec 21, 2021
Let's make sure to validate these changes before applying them:
headNoteis not better thanheadnowadays. Same goes for other partial functions likefromJust,!!, etc.michaelpj commentedon Dec 21, 2021
Correct, it can't fail. However,
headrule" makes it much harder to apply.NonEmptyhere removes no expressiveness and removes the need to do any reasoning at all (thanks typesystem).So I agree it's safe, but if it's trivial to rewrite it so we don't even need to think about whether it's safe, that seems like a win to me.
14 remaining items
Anton-Latukha commentedon Feb 10, 2022
Nota bene: As I was around this theme & two times promised to attend to a particular reduction of the
headin the main doc retrieval path (#2539). HLS was always retrieving documentation doing thehead [res]. Removing it also uncovered a bunch of other stuff around that code.headremoval is not the main agenda, PR is part of work towards provisioning argument documentation, the main agenda there is to provide argument documentation in a form that is neat for further processing.hasufell commentedon Feb 20, 2022
IMO, partial functions are fine, if there is sufficient local proof that it is total in the given context (e.g. the input to a partial function is controlled by the outer function itself). #2672 (comment) is an example where no such local proof is possible and the callstack is actually really long. So the caller cannot make any reasonable assumption about
envnever beingNothing.On the other hand, I don't think a function like this needs any adjustment:
So I'm not too thrilled about enforcing hlint compliance. It's a nuisance.
ttylec commentedon Feb 21, 2022
I agree it is a nuisance but I would like to give here a different perspective: if hls crash for a beginner user, they are basically clueless what happened. Even if able to find
Outputin VSCode, finding the reason why hls crashed is hard (it is not necessarily the very last line in the output); and if the line contains justPrelude.head: empty listfinding a workaround for such user is impossible.So it's a nuisance for developer, but if the things go wrong way, for a user it is a deal breaker.
michaelpj commentedon Jun 14, 2022
I think we need a hlint ratchet, even if it's going to be very tedious to whitelist all the existing usages. At the moment it's way too easy for people to introduce new partial function usage, even when it's very easy to avoid. We need something to slow that down.
michaelpj commentedon Jun 20, 2022
Ratchet started here: #2974
michaelpj commentedon Jun 24, 2022
Ratchet committed, you can now use
.hlint.yamlas a hit list of things to fix :)josephcsible commentedon Sep 22, 2022
Why wouldn't you want to replace that with this?
hasufell commentedon Sep 22, 2022
Because that easily gets ridiculous if you want to look at the first 8 characters. I have a lot of those examples from the filepath package, but I'm not gonna list all of them here.