Skip to content

List: make head/tail's expectation of a non-empty list visible in the type system #1105

Closed

Description

@kud1ing

Currently list::head/tail call "fail" when the list is empty.

Haskell's head/tail do the same and it causes a lot of grief: http://www.reddit.com/r/haskell/comments/lf71l/deprecate_preludehead_and_partial_functions/

I suggest to either
a) make the return type an "option"
b) add a "is_not_empty" predicate, similar to vec::head/tail

b) is probably more Rust-nic.

Activity

  1. brson commented on Oct 31, 2011

    @brson
    Contributor

    Unfortunately we've been getting in the sense lately that predicates are not as useful as we would like. They just end up moving the assertion from inside the function to before every call. In fact, I'm probably going to remove all the predicates from std this week.

    We can try making these return option.

  2. jckarter commented on Oct 31, 2011

    @jckarter

    I'd be interested to hear why you guys are deciding to give up on predicates. From the peanut gallery, I thought "moving the assertion from inside the function to before every call" was at least part of the point, to let you get away with writing unsafe inner loops as long as someone in the call chain makes the necessary checks. Is it just that, in practice, that "someone" only ever ends up being the immediate caller?

  3. brson commented on Oct 31, 2011

    @brson
    Contributor

    So far, in practice, the check tends to get done by the immediate caller, without regard to whether the check is actually true are false. The result is that functions get a little better documentation, but are much more annoying to use.

    Part of the problem might be that we just haven't developed typestate far enough to make it pleasant. Function postconditions, for example, along with carefully designed APIs, could make some predicates hold across longer regions of code. As it is though, there aren't a lot of fans right now among the core developers.

    It does make me sad. So much of error handling is coping with bad input, and I was really excited by the prospect of not worring about it. Unfortunately, with the current state of typestate, it makes the implementor's life easy while making the user's life absolutely terrible.

  4. doublec commented on Nov 7, 2011

    @doublec
    Contributor

    Is there an example of std code that uses predicates that have been showing the issue of making it annoying to use?

  5. brson commented on Nov 7, 2011

    @brson
    Contributor

    Not really in std, since most of the heavily used standard functions haven't been given preconditions. There is heavy use of preconditions in a few places in rustc though. The function rustc::middle::trans::GEP_tup_like is an oft-used function with a precondition, and everywhere it's used it is preceded immediately by the check (and often a comment about the check being silly).

    https://github.com/graydon/rust/blob/master/src/comp/middle/trans.rs#L689

  6. kud1ing commented on Dec 26, 2011

    @kud1ing
    Author

    For those not subscribed to "rust-dev":
    I've written up some thoughts regarding Typestate: https://mail.mozilla.org/pipermail/rust-dev/2011-December/001149.html

  7. boggle commented on Dec 28, 2011

    @boggle
    Contributor

    Coming here late, I'm not happy about predicates being removed, this was one of the things that brought me to rust.
    I'd rather see us to first to really have preconditions, invariants, and postconditions, and to push hard and try to use them in std before dropping them. Lots of the problems with typestate as it is now may be process issues, i.e. you need to develop/design differently to make full use of it (wonder what eiffelists and DbC people have written about this).

    It may also be a good idea to take the extra step and allow limited forms of inference to make typestate more useful though this first would require more experience with using them (inference opportunities: length predicates on vecs, set intersection for (remaining) possible tag variants of a value, logical connectives)

  8. graydon commented on Dec 28, 2011

    @graydon
    Contributor

    I agree we haven't pushed on typestate nearly hard enough to remove it wholesale (function postconditions don't work yet!) much less played with simple forms of inference.

  9. kud1ing commented on Dec 29, 2011

    @kud1ing
    Author

    I think using either Typestate or "option" would make list's "head"/tail" equally unpleasant to use.
    But Haskell has shown, that this is just necessary.
    Should we go for Typestate (analogous to vec's head/tail) then and hope Typestate makes our lives better in the future?

  10. kud1ing commented on Jan 3, 2012

    @kud1ing
    Author

    Fixed

  11. added a commit that references this issue on Nov 3, 2020
  12. added 2 commits that reference this issue on Aug 21, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions