Skip to content

Reduce use of partial functions from HLS #2514

Open

Description

@michaelpj

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!

Activity

  1. pepeiborra commented on Dec 20, 2021

    @pepeiborra
    Collaborator

    Could you share a few examples?

  2. michaelpj commented on Dec 20, 2021

    @michaelpj
    CollaboratorAuthor

    30-odd usages of head, 15 or so foldl1s, 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.

  3. michaelpj commented on Dec 20, 2021

    @michaelpj
    CollaboratorAuthor

    30-ish fromJust...

  4. jhrcek commented on Dec 21, 2021

    @jhrcek
    Collaborator

    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:

    use :: IdeRule k v
    => k -> NormalizedFilePath -> Action (Maybe v)
    use key file = head <$> uses key [file]
    -- | Request a Rule result, it not available return the last computed result, if any, which may be stale
    useWithStale :: IdeRule k v
    => k -> NormalizedFilePath -> Action (Maybe (v, PositionMapping))
    useWithStale key file = head <$> usesWithStale key [file]
    -- | Request a Rule result, it not available return the last computed result which may be stale.
    -- Errors out if none available.
    useWithStale_ :: IdeRule k v
    => k -> NormalizedFilePath -> Action (v, PositionMapping)
    useWithStale_ key file = head <$> usesWithStale_ key [file]

    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:

    1. 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!
    2. replace the obvious ones
    3. 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?
  5. jneira commented on Dec 21, 2021

    @jneira
    Member

    The plan sounds really great to me, given at minimum we will have more info about the error.
    Many thanks for willing to contribute

  6. michaelpj commented on Dec 21, 2021

    @michaelpj
    CollaboratorAuthor

    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.

  7. pepeiborra commented on Dec 21, 2021

    @pepeiborra
    Collaborator

    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?

  8. pepeiborra commented on Dec 21, 2021

    @pepeiborra
    Collaborator

    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.

  9. michaelpj commented on Dec 21, 2021

    @michaelpj
    CollaboratorAuthor

    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.

  10. 14 remaining items

  11. Anton-Latukha commented on Feb 10, 2022

    @Anton-Latukha
    Collaborator

    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.

  12. hasufell commented on Feb 20, 2022

    @hasufell
    Member

    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.

  13. ttylec commented on Feb 21, 2022

    @ttylec
    Contributor

    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.

  14. michaelpj commented on Jun 14, 2022

    @michaelpj
    CollaboratorAuthor

    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.

  15. michaelpj commented on Jun 20, 2022

    @michaelpj
    CollaboratorAuthor

    Ratchet started here: #2974

  16. michaelpj commented on Jun 24, 2022

    @michaelpj
    CollaboratorAuthor

    Ratchet committed, you can now use .hlint.yaml as a hit list of things to fix :)

  17. josephcsible commented on Sep 22, 2022

    @josephcsible

    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 of
      Nothing -> sbs
      Just (_, x)
        | x == _space -> "lol"
        | otherwise -> "shrug"
  18. hasufell commented on Sep 22, 2022

    @hasufell
    Member

    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 of
      Nothing -> 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.

  19. added
    Hackathon
    on May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Hackathonlevel: easytype: enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions