# Suggestion: Fortran-like Keyword to Assert Function is Pure

**URL:** <https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047>\
**Category:** Internals & Design\
**Created:** [June 16, 2021, 8:34pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047 "2021-06-16T20:34:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [June 16, 2021, 8:34pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/1 "2021-06-16T20:34:37Z")

</div>

I don’t know if this is possible or how much work it would be, but I’d like it if there was some kind of keyword like `pure function f()` that throws an error if the function being compiled is not pure. I’ve definitely faced frustrating bugs where I’ve accidentally mutated a variable that was passed, or otherwise changed a program’s state without realizing it.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [June 16, 2021, 8:52pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/2 "2021-06-16T20:52:28Z")

</div>

It’s not what you’re looking for, but there’s `Base.@pure`, which tells the compiler the function is pure **but does not check for purity**. Julia’s mutable method tables also mean that `@pure` cannot be used for methods that call generic functions. For that reason, its use is generally discouraged for anyone without deep knowledge of Julia’s internals.

```nohighlight
help?> Base.@pure
  @pure ex
  @pure(ex)

  @pure gives the compiler a hint for the definition of a pure function, helping for type inference.

  A pure function can only depend on immutable information. This also means a @pure function cannot use any global
  mutable state, including generic functions. Calls to generic functions depend on method tables which are mutable
  global state. Use with caution, incorrect @pure annotation of a function may introduce hard to identify bugs.
  Double check for calls to generic functions. This macro is intended for internal compiler use and may be subject to
  changes.

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [June 16, 2021, 8:58pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/3 "2021-06-16T20:58:12Z")

</div>

> [@ParadaCarleton](#):
>
> that throws an error if the function called is not pure. … Enforcing purity would also be useful for future optimization/compiler improvements, e.g. for type inference.

I think these two goals are mutually exclusive (though I could be wrong).  
Either you are asserting purity in a place the compiler can check and thus error if you violate this (and can/does already use that purity for optizations).  
Or it is asserting purity in a place the compiler can’t prove (and thus doesn’t use for optimizations) in which case it can’t error if you violate it.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 16, 2021, 9:00pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/4 "2021-06-16T21:00:50Z")

</div>

The compiler (or runtime) can still prove it’s not pure and raise an error.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 16, 2021, 9:15pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/5 "2021-06-16T21:15:40Z")

</div>

But if it can do that, it could have just figured out that the function is pure and optimized, sans any annotation. ~~The difference between Julia and Fortran is that Julia’s compiler is not entirely potato.~~

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 16, 2021, 9:47pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/6 "2021-06-16T21:47:55Z")

</div>

> [@ParadaCarleton](#):
>
> I’ve definitely faced frustrating bugs where I’ve accidentally mutated a variable that was passed, or otherwise changed a program’s state without realizing it.

OP’s goal is to raise an error for a function that is impure but labeled `@purefunction`, to help with debugging. Julia should be able to do this.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [June 17, 2021, 2:18am UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/7 "2021-06-17T02:18:21Z")

</div>

This is a good point and I withdraw my claim about this potentially helping with performance. I’ll settle for less bugs, then.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 17, 2021, 2:36am UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/8 "2021-06-17T02:36:39Z")

</div>

Not to be a broken record but… don’t use `Base.@pure` if you’re getting bitten by bugs? Seriously, the purity requirements are _very high_ for any meaningful optimizations to occur beyond what Julia normally does.

Often forcing inlining is enough to get the optimizations you want beyond the normal.

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 17, 2021, 3:03am UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/9 "2021-06-17T03:03:10Z")

</div>

I don’t think this issue is about `Base.@pure` – that macro just has a naming collision with the functionality being requested here.

---

<div class="post-metadata">

**Author:** ![tchr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tchr/32/15138_2.png) [@tchr](https://discourse.julialang.org/u/tchr)\
**Post date:** [June 17, 2021, 3:08am UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/10 "2021-06-17T03:08:41Z")

</div>

Not really important, but proving something _is not_ pure (or any property) doesn’t require being able to prove something _is_ pure.

(E.g., if I can only see the color red, hypothetically, I can prove that something is not green by seeing that it is red - but I cannot prove that anything is green; it could just as well be blue)

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 17, 2021, 3:26am UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/11 "2021-06-17T03:26:31Z")

</div>

> [@tchr](#):
>
> Not really important, but proving something _is not_ pure (or any property) doesn’t require being able to prove something _is_ pure.

I think that distinction is important, since it makes this feature possible.

---

<div class="post-metadata">

**Author:** ![tchr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tchr/32/15138_2.png) [@tchr](https://discourse.julialang.org/u/tchr)\
**Post date:** [June 17, 2021, 3:32am UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/12 "2021-06-17T03:32:50Z")

</div>

Sure, I merely meant that this was in response to somewhat of a tangent relative to the original question.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 17, 2021, 4:21am UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/13 "2021-06-17T04:21:55Z")

</div>

I think it would be Bad to have an annotation that throws an error _only if_ (but not _if_) a function is impure.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [June 17, 2021, 2:54pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/14 "2021-06-17T14:54:28Z")

</div>

I’m not using Base.@pure. I’m not sure how you’re being a broken record, since nobody else suggested that because that’s not at all related to what I’m proposing. Bugs of the type I’m referring to come from coders accidentally writing functions that have side effects, e.g. taking an array `x` as input and using `@. x = 2 * x ` in the body, which mutates the input, instead of `x = 2 .* x`, which just changes what the _local_ variable refers to.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 17, 2021, 3:00pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/15 "2021-06-17T15:00:25Z")

</div>

Ah, gotcha, sorry about that. I misunderstood where your bugs were coming from! And yeah, the tracks on this broken record are skipping between many topics over the years — it’s not a targeted slight against you.

Interestingly, I think that slant on the question significantly changes it — instead of being a compiler tool it becomes a dev tool for QA. In some ways I think this makes the request _more_ feasible because it doesn’t need to turn on any additional optimizations.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 17, 2021, 3:02pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/16 "2021-06-17T15:02:28Z")

</div>

> [@ParadaCarleton](#):
>
> Bugs of the type I’m referring to come from coders accidentally writing functions that have side effects,

This would be solved by introducing freeze/thaw semantics (i.e. marking a mutable variable temporarily as read-only and making writes to them a compiler error) and is not necessarily connected to declaring/proving purity. There was some discussion about this in various issues on the issue tracker, but nothing concrete yet.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [June 17, 2021, 3:10pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/17 "2021-06-17T15:10:44Z")

</div>

Yeah, as mentioned above, I originally thought this would be useful for performance since the compiler could apply whatever kinds of optimizations it currently applies using `@pure`; presumably, though, the compiler is already optimizing anything that it can prove is pure, so `@pure` only makes a difference if the compiler can’t prove the function is pure. I think a `pure function` keyword might still be slightly useful to the compiler as a hint that these optimizations are feasible, so the compilation might be a bit faster. (For instance, if the function isn’t marked with `pure function`, that’s a hint that checking for purity to optimize might not be worth it, especially on lower compilation settings). I’ll rewrite the OP to remove references to optimization, though, since that’s far and away a lesser concern.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [June 17, 2021, 3:15pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/18 "2021-06-17T15:15:58Z")

</div>

That sounds great, but doesn’t fix some other problems, like accidentally writing functions that depend on global state.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 17, 2021, 3:20pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/19 "2021-06-17T15:20:10Z")

</div>

Yes, that’s true - I don’t see that as much of a problem though, since non-constant global state/variables is kind of a code smell in julia anyway 🤷‍♂️ The associated drop in performance is usually enough to persuade people to put everything in functions and pass state explicitly, focusing the problem on mutating variables.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [June 17, 2021, 4:46pm UTC](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047/20 "2021-06-17T16:46:52Z")

</div>

This is an appealing idea. The question, however, is this: how do you enforce this?

[Next page](https://discourse.julialang.org/t/suggestion-fortran-like-keyword-to-assert-function-is-pure/63047.md?page=2)
