Skip to content

Change the pure keyword #2753

Closed
Closed

Description

@brson

It's not obvious what pure means in rust, but it doesn't seem to have much to do with common notions of purity.

For example, pure functions can call stateful closures.

Activity

  1. catamorphism commented on Jun 29, 2012

    @catamorphism
    Contributor

    Just to elaborate, "pure" means "as pure as the borrow check needs a pure function to be"... which is a weaker notion than referential transparency, and isn't the same kind of "pure" that typestate would want either.

  2. kud1ing commented on Jun 29, 2012

    @kud1ing

    I was surprised to learn that vec::append*() are marked pure while effectively altering the lhs (after overtaking ownership).
    That's more like D's "weak purity" than Haskell's purity: http://leonardo-m.livejournal.com/99194.html

  3. nikomatsakis commented on Jul 5, 2012

    @nikomatsakis
    Contributor

    I am not persuaded that a new keyword is needed. Pure means that the function itself does not make changes to data it does not own. We have a slight generalization because we allow the function to invoke closures that are given as arguments, so in fact a function declared as a pure is parametrically, or conditionally, pure---"as pure as the closures it is given as arguments".

    @catamorphism is correct though that this is insufficiently strict for typestate. To be correct, typestate must prohibit reads from non-owned, mutable state. (Also, of course, for a type state predicate to be considered pure it is not enough that it calls a pure function, it must also not supply only pure closures as arguments)

  4. catamorphism commented on Jul 5, 2012

    @catamorphism
    Contributor

    Two points from discussion on IRC: 1. It seems like "does not make changes to data it does not own" does amount to referentially transparent, as long as all higher-order function arguments are also referentially transparent (conditionally pure, as @nikomatsakis says). But this is fairly subtle and should be documented, or else people are going to look at a function and ask "how can this be pure, it mutates data?!" 2. The "conditionally pure" part is probably the most confusing thing. I'm not sure most people think "pure" means "as pure as its arguments". So I'm still in favor of changing the keyword.

  5. catamorphism commented on Nov 24, 2012

    @catamorphism
    Contributor

    There doesn't seem to be much interest in changing the keyword, and now that we no longer have typestate we no longer have the motivating reason that there are two notions of purity. So I'm closing.

  6. added a commit that references this issue on Jan 9, 2023
  7. added a commit that references this issue on Feb 2, 2026
  8. added a commit that references 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