# Const(pure)

**URL:** <https://internals.rust-lang.org/t/const-pure/13824>\
**Category:** Uncategorized\
**Created:** [January 13, 2021, 10:00pm UTC](https://internals.rust-lang.org/t/const-pure/13824 "2021-01-13T22:00:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![atagunov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/atagunov/32/5877_2.png) [@atagunov](https://internals.rust-lang.org/u/atagunov)\
**Post date:** [January 13, 2021, 10:00pm UTC](https://internals.rust-lang.org/t/const-pure/13824/1 "2021-01-13T22:00:27Z")

</div>

The topic suggests

```rust
    const(pure) fn foo() {...}
    impl const(pure) Trait ...

```

similar to but more permissive than vanilla `const`:

- can only call `fn`-s that are `const(pure)` themselves
- no access to static variables
- no assignments through references
- no `asm`
- ...
- can dereference pointers and use heap

Hopefully implementation can follow in footsteps of `const` `fn`-s and traits.

# Motivation

- make [Somewhat Random Idea: Deref patterns](https://internals.rust-lang.org/t/somewhat-random-idea-deref-patterns/13813) design simpler
- other cases when caller wants to be sure to get identical results while passing in identical args

# Future Possibilities

As written `const(pure) fn`-s have same limitation as `const fn`-s:

- can modify `mut` local bindings
- cannot modify through `&mut` references at all
- so, no iterators, sorry

To solve this would require coloring all `&mut` references throughout the type system in two colors - unique immutable and unique mutable. One idea here is to mark lifetimes as `const`..

Because of the size of the change it's left out as a future possibility. Initially `const(pure) fn`-s would treat all `&mut` references as immutable.

# Note

~~It is hoped present-day `const` `fn`-s/traits will be a subset of `const(pure)` `fn`-s and traits. However to leave more future possibilities open initially `const(pure)` and `const` can be marked separately:~~ It simplifies things to say that vanilla `const fn` will always be a stronger requirement than `const(pure) fn`.

# Objections

> [@Somewhat Random Idea: Deref patterns](https://internals.rust-lang.org/t/somewhat-random-idea-deref-patterns/13813/37):
>
> Rust used to have a `pure` modifier, but it was removed pre-1.0. I don't think we're going to double back again.
> 
> [github.com/rust-lang/rust](https://github.com/rust-lang/rust/pull/7310)
> 
> #### [Remove `ast::pure_fn` and all use of `pure`](https://github.com/rust-lang/rust/pull/7310)
> 
> `rust-lang:master` ← `emberian:various-cleanup`
> 
> opened Jun 22, 2013
> 
> [![emberian](https://avatars2.githubusercontent.com/u/704250?v=4) emberian ](https://github.com/emberian)
> 
> [+21 -39](https://github.com/rust-lang/rust/pull/7310/files)
> 
> [github.com/rust-lang/rust](https://github.com/rust-lang/rust/pull/7310)
> 
> #### [Remove `ast::pure_fn` and all use of `pure`](https://github.com/rust-lang/rust/pull/7310)
> 
> `rust-lang:master` ← `emberian:various-cleanup`
> 
> opened Jun 22, 2013
> 
> [![emberian](https://avatars2.githubusercontent.com/u/704250?v=4) emberian ](https://github.com/emberian)
> 
> [+21 -39](https://github.com/rust-lang/rust/pull/7310/files)
> 
> [github.com/rust-lang/rust](https://github.com/rust-lang/rust/pull/5412)
> 
> #### [Purity](https://github.com/rust-lang/rust/pull/5412)
> 
> `rust-lang:incoming` ← `pcwalton:purity`
> 
> opened Mar 16, 2013
> 
> [![pcwalton](https://avatars0.githubusercontent.com/u/157897?v=4) pcwalton ](https://github.com/pcwalton)
> 
> [+946 -1078](https://github.com/rust-lang/rust/pull/5412/files)

It is interesting why `pure` was dropped but the suggestion just keeps coming back..

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [January 13, 2021, 10:09pm UTC](https://internals.rust-lang.org/t/const-pure/13824/2 "2021-01-13T22:09:31Z")

</div>

~~As written, this is currently useless for anything that involves loops (no guaranteed TCO, and no mutation).~~ But there is a larger problem: purity polymorphism.

Ideally `Option::map` would be pure if the function/closure that is passed in is pure. If this wasn't true it would be much harder to write readable Rust. This is similar to `const`, and so it will require similar solutions to const. Currently, the best solution is `const Trait` and `?const TraitBound`. But these are wordy and can make writing correct generic core difficult and sometimes, unreadable. Adding `pure` exacerbates this problem, without sufficient motivation.

These are similar reasons to why pure was removed in the first place, and they should be addressed. `const` addressed these problems by providing sufficient motivation in compile time execution. Does `pure` provide this level of motivation. If not, I don't think it's worthwhile.

---

<div class="post-metadata">

**Author:** ![atagunov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/atagunov/32/5877_2.png) [@atagunov](https://internals.rust-lang.org/u/atagunov)\
**Post date:** [January 13, 2021, 10:28pm UTC](https://internals.rust-lang.org/t/const-pure/13824/3 "2021-01-13T22:28:03Z")

</div>

> [@RustyYato](#):
>
> ~~As written, this is currently useless for anything that involves loops (no guaranteed TCO, and no mutation).~~

Actually `const fn`-s can modify local `mut` variables now. But as noted iterators can't be used without a large-scale change introducing

```rust
// either
struct S<'a, T>{ v : &'a const mut T }
// or
fn foo<const 'a, 'b>(a : &'a mut .., b : &b' mut ..) {..}

```

Such change would benefit vanilla `const fn`-s as well so might carry its weight.

> [@RustyYato](#):
>
> But there is a larger problem: purity polymorphism

Oh, snap. Indeed.. Well... One could do poor-man's polymorphism

```rust
pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> { ... }
pub const(pure) fn mapPure<U, /* indicate F is const(pure) somehow here */ F: FnOnce(T) -> U>(self, f: F) -> Option<U> { ... }

```

but I agree that this is far from satisfying. Perhaps such extra methods could be added in popular cases as a stop-gap measure but one would indeed desire to be able to express purity polymorphism properly and then deprecate those methods.

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [January 13, 2021, 10:33pm UTC](https://internals.rust-lang.org/t/const-pure/13824/4 "2021-01-13T22:33:34Z")

</div>

> [@atagunov](#):
>
> Actually `const fn` -s can modify local `mut` variables now. But as noted iterators can't be used without a large-scale change introducing

~~Right, but you mentioned that `const(pure)` can't~~. But this is small potatoes, and doesn't really matter right now. Purity polymorphism is more important.

> [@atagunov](#):
>
> Well... One could do poor-man's polymorphism

As you mentioned, this isn't satisfying. This needs some way to hook into the trait system. If we're copying `const`, then we would need `const(pure) + Trait` and `?const(pure) + TraitBound` along side `const + Trait` and `?const + TraitBound`. Which can make things hard to track.

> Perhaps such extra methods could be added in popular cases as a stop-gap measure but one would indeed desire to be able to express purity polymorphism properly and then deprecate those methods.

I think we've denied proposals for additions that are known to be deprecated in the future.

---

<div class="post-metadata">

**Author:** ![atagunov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/atagunov/32/5877_2.png) [@atagunov](https://internals.rust-lang.org/u/atagunov)\
**Post date:** [January 13, 2021, 10:42pm UTC](https://internals.rust-lang.org/t/const-pure/13824/7 "2021-01-13T22:42:07Z")

</div>

> [@RustyYato](#):
>
> If we're copying `const` , then we would need `const(pure) + Trait` and `?const(pure) + TraitBound` along side `const + Trait` and `?const + TraitBound`

Magnificent! You have just mapped the path forward:

- guarantee that vanilla `const fn` will always stay a stronger requirement than `const(pure) fn`
- which introduces just 3 flavors of fn-s/traits: regular/pure/const

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [January 13, 2021, 10:43pm UTC](https://internals.rust-lang.org/t/const-pure/13824/8 "2021-01-13T22:43:15Z")

</div>

To be clear, I'm not in favor of this approach. It increases complexity significantly, without sufficient motivation.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [January 14, 2021, 5:59pm UTC](https://internals.rust-lang.org/t/const-pure/13824/11 "2021-01-14T17:59:16Z")

</div>

Oh no, not another effect system, please.

* * *

First, let's see some non-toy examples as to why this would have advantages significant enough to be worth overhauling one of the fundamental parts of the type system. I don't think "make another proposal (that was just written) work" counts as significant motivation.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [April 14, 2021, 5:59pm UTC](https://internals.rust-lang.org/t/const-pure/13824/12 "2021-04-14T17:59:24Z")

</div>

Closed on April 14, 2021, 5:59pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
