# Purity validation

**URL:** <https://forum.elixirforum.com/t/purity-validation/30243>\
**Category:** Questions\
**Tags:** pure-functions\
**Created:** [March 31, 2020, 10:24am UTC](https://forum.elixirforum.com/t/purity-validation/30243 "2020-03-31T10:24:30Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim2CF](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@tim2CF](https://forum.elixirforum.com/u/tim2CF)\
**Post date:** [March 31, 2020, 10:24am UTC](https://forum.elixirforum.com/t/purity-validation/30243/1 "2020-03-31T10:24:30Z")

</div>

Hi again, I have question related to previous. To find answer I probably have to read compiler source code, but maybe you know answer already. I want some utility function which will do validation of ast, something like:

`valid_guard?(ast) :: boolean`

which I want to reuse to assume that given expression is pure. This function, or similar equivalent should exist in Erlang/Elixir compiler for sure, because it’s doing this purity validation well.

Writing own custom equivalent is not the best option, because you have to deal with possible things like

```elixir
import Kernel, except: [{:'-', 1}]
import UnpureOverride, only: [{:'-', 1}]

```

But compiler is already doing all this smart stuff and can recognise this impurity

---

<div class="post-metadata">

**Author:** ![lpil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lpil/32/17492_2.png) [@lpil](https://forum.elixirforum.com/u/lpil)\
**Post date:** [March 31, 2020, 10:30am UTC](https://forum.elixirforum.com/t/purity-validation/30243/2 "2020-03-31T10:30:45Z")

</div>

If you want to know if a macro is a valid guard you can use [Macro — Elixir v1.20.2](https://hexdocs.pm/elixir/Macro.html#expand/2) and then traverse the AST, checking if all the calls in that AST are guards themselves.

If you wish to know whether a function it pure it is a much more difficult task, one not possible with the Elixir compiler today.

> [@tim2CF](#):
>
> because it’s doing this purity validation well.

What do you mean by this? The Elixir compiler does not know whether functions are pure or not.

---

<div class="post-metadata">

**Author:** ![tim2CF](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@tim2CF](https://forum.elixirforum.com/u/tim2CF)\
**Post date:** [March 31, 2020, 10:34am UTC](https://forum.elixirforum.com/t/purity-validation/30243/3 "2020-03-31T10:34:02Z")

</div>

> [@lpil](#):
>
> What do you mean by this?

I mean that both Erlang and Elixir compilers don’t allow application of impure functions in guard expressions. And I just want to reuse this utility. Check that given AST is pure for sure, either that it might be impure.

---

<div class="post-metadata">

**Author:** ![lpil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lpil/32/17492_2.png) [@lpil](https://forum.elixirforum.com/u/lpil)\
**Post date:** [March 31, 2020, 10:35am UTC](https://forum.elixirforum.com/t/purity-validation/30243/4 "2020-03-31T10:35:53Z")

</div>

They don’t track if functions are pure, instead they have a small list of permitted functions and operators, and if any other functions are used then the code is rejected by the compiler. In Elixir it is the same process I outlined above with macro expansion.

Purity inference isn’t need as being pure isn’t enough for a function to be valid in a guard.

---

<div class="post-metadata">

**Author:** ![tim2CF](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@tim2CF](https://forum.elixirforum.com/u/tim2CF)\
**Post date:** [March 31, 2020, 10:40am UTC](https://forum.elixirforum.com/t/purity-validation/30243/5 "2020-03-31T10:40:36Z")

</div>

> [@lpil](#):
>
> they have a small list of permitted functions

Thanks, I understand, that’s completely fine for my proposes. I maintain this library

> **[ExEnv — ExEnv v0.3.4](https://ex-env.hexdocs.pm/readme.html)**

And atm it don’t allow any function application. But I want to allow application of functions which are pure for sure. It can be small subset of pure functions, that’s fine. Because atm expressions like `foo: -42` are rejected by library because there is `&Kernel.-/1` function application there.

---

<div class="post-metadata">

**Author:** ![tim2CF](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@tim2CF](https://forum.elixirforum.com/u/tim2CF)\
**Post date:** [March 31, 2020, 10:43am UTC](https://forum.elixirforum.com/t/purity-validation/30243/6 "2020-03-31T10:43:14Z")

</div>

I just don’t want to re-invent the wheel, and deal with tricky possibilities like

```elixir
import Kernel, except: [{:'-', 1}]
import UnpureOverride, only: [{:'-', 1}]

```

Erlang/Elixir compiler already have utility to deal with this kind of stuff and reject it in guards because of possible impurity

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [March 31, 2020, 10:44am UTC](https://forum.elixirforum.com/t/purity-validation/30243/7 "2020-03-31T10:44:42Z")

</div>

No, not really. Erlang/Elixir just have defined set of “guard safe” functions which are allowed. There is much broader set of pure functions that will not be allowed in guards.

---

<div class="post-metadata">

**Author:** ![lpil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lpil/32/17492_2.png) [@lpil](https://forum.elixirforum.com/u/lpil)\
**Post date:** [March 31, 2020, 10:46am UTC](https://forum.elixirforum.com/t/purity-validation/30243/8 "2020-03-31T10:46:35Z")

</div>

> [@tim2CF](#):
>
> Erlang/Elixir compiler already have utility to deal with this kind of stuff and reject it in guards because of possible impurity

They do not have this functionality I’m afraid.

You’re effectively wanting to write a kind of static type system for Erlang- it would be a large piece of work.

---

<div class="post-metadata">

**Author:** ![tim2CF](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@tim2CF](https://forum.elixirforum.com/u/tim2CF)\
**Post date:** [March 31, 2020, 10:50am UTC](https://forum.elixirforum.com/t/purity-validation/30243/9 "2020-03-31T10:50:39Z")

</div>

> [@lpil](#):
>
> checking if all the calls in that AST are guards themselves.

I think it will be quite enough for purposes of my library. Do you know name of utility function which checks that given AST is valid guard? Or maybe some guesses where to search it?

---

<div class="post-metadata">

**Author:** ![lpil](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lpil/32/17492_2.png) [@lpil](https://forum.elixirforum.com/u/lpil)\
**Post date:** [March 31, 2020, 10:53am UTC](https://forum.elixirforum.com/t/purity-validation/30243/10 "2020-03-31T10:53:32Z")

</div>

> [@tim2CF](#):
>
> I think it will be quite enough for purposes of my library.

I suspect it will not be, you will be limited to just these functions:

- [!=/2](https://hexdocs.pm/elixir/Kernel.html#!=/2)
- [!==/2](https://hexdocs.pm/elixir/Kernel.html#!==/2)
- [\*/2](https://hexdocs.pm/elixir/Kernel.html#*/2)
- [+/1](https://hexdocs.pm/elixir/Kernel.html#+/1)
- [+/2](https://hexdocs.pm/elixir/Kernel.html#+/2)
- [-/1](https://hexdocs.pm/elixir/Kernel.html#-/1)
- [-/2](https://hexdocs.pm/elixir/Kernel.html#-/2)
- [//2](https://hexdocs.pm/elixir/Kernel.html#//2)
- [\<=/2](https://hexdocs.pm/elixir/Kernel.html#%3C=/2)
- [==/2](https://hexdocs.pm/elixir/Kernel.html#==/2)
- [===/2](https://hexdocs.pm/elixir/Kernel.html#===/2)
- [\>/2](https://hexdocs.pm/elixir/Kernel.html#%3E/2)
- [\>=/2](https://hexdocs.pm/elixir/Kernel.html#%3E=/2)
- [abs/1](https://hexdocs.pm/elixir/Kernel.html#abs/1)
- [and/2](https://hexdocs.pm/elixir/Kernel.html#and/2)
- [binary\_part/3](https://hexdocs.pm/elixir/Kernel.html#binary_part/3)
- [bit\_size/1](https://hexdocs.pm/elixir/Kernel.html#bit_size/1)
- [byte\_size/1](https://hexdocs.pm/elixir/Kernel.html#byte_size/1)
- [ceil/1](https://hexdocs.pm/elixir/Kernel.html#ceil/1)
- [div/2](https://hexdocs.pm/elixir/Kernel.html#div/2)
- [elem/2](https://hexdocs.pm/elixir/Kernel.html#elem/2)
- [floor/1](https://hexdocs.pm/elixir/Kernel.html#floor/1)
- [hd/1](https://hexdocs.pm/elixir/Kernel.html#hd/1)
- [in/2](https://hexdocs.pm/elixir/Kernel.html#in/2)
- [is\_atom/1](https://hexdocs.pm/elixir/Kernel.html#is_atom/1)
- [is\_binary/1](https://hexdocs.pm/elixir/Kernel.html#is_binary/1)
- [is\_bitstring/1](https://hexdocs.pm/elixir/Kernel.html#is_bitstring/1)
- [is\_boolean/1](https://hexdocs.pm/elixir/Kernel.html#is_boolean/1)
- [is\_float/1](https://hexdocs.pm/elixir/Kernel.html#is_float/1)
- [is\_function/1](https://hexdocs.pm/elixir/Kernel.html#is_function/1)
- [is\_function/2](https://hexdocs.pm/elixir/Kernel.html#is_function/2)
- [is\_integer/1](https://hexdocs.pm/elixir/Kernel.html#is_integer/1)
- [is\_list/1](https://hexdocs.pm/elixir/Kernel.html#is_list/1)
- [is\_map/1](https://hexdocs.pm/elixir/Kernel.html#is_map/1)
- [is\_map\_key/2](https://hexdocs.pm/elixir/Kernel.html#is_map_key/2)
- [is\_nil/1](https://hexdocs.pm/elixir/Kernel.html#is_nil/1)
- [is\_number/1](https://hexdocs.pm/elixir/Kernel.html#is_number/1)
- [is\_pid/1](https://hexdocs.pm/elixir/Kernel.html#is_pid/1)
- [is\_port/1](https://hexdocs.pm/elixir/Kernel.html#is_port/1)
- [is\_reference/1](https://hexdocs.pm/elixir/Kernel.html#is_reference/1)
- [is\_struct/1](https://hexdocs.pm/elixir/Kernel.html#is_struct/1)
- [is\_tuple/1](https://hexdocs.pm/elixir/Kernel.html#is_tuple/1)
- [length/1](https://hexdocs.pm/elixir/Kernel.html#length/1)
- [map\_size/1](https://hexdocs.pm/elixir/Kernel.html#map_size/1)
- [node/0](https://hexdocs.pm/elixir/Kernel.html#node/0)
- [node/1](https://hexdocs.pm/elixir/Kernel.html#node/1)
- [not/1](https://hexdocs.pm/elixir/Kernel.html#not/1)
- [or/2](https://hexdocs.pm/elixir/Kernel.html#or/2)
- [rem/2](https://hexdocs.pm/elixir/Kernel.html#rem/2)
- [round/1](https://hexdocs.pm/elixir/Kernel.html#round/1)
- [self/0](https://hexdocs.pm/elixir/Kernel.html#self/0)
- [tl/1](https://hexdocs.pm/elixir/Kernel.html#tl/1)
- [trunc/1](https://hexdocs.pm/elixir/Kernel.html#trunc/1)
- [tuple\_size/1](https://hexdocs.pm/elixir/Kernel.html#tuple_size/1)

There’s no way to iterate or recurse, and there is no flow control.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [March 31, 2020, 10:54am UTC](https://forum.elixirforum.com/t/purity-validation/30243/11 "2020-03-31T10:54:01Z")

</div>

`erl_internal:guard_bif/2` or `erl_lint:is_guard_test/1`

---

<div class="post-metadata">

**Author:** ![tim2CF](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@tim2CF](https://forum.elixirforum.com/u/tim2CF)\
**Post date:** [March 31, 2020, 10:57am UTC](https://forum.elixirforum.com/t/purity-validation/30243/12 "2020-03-31T10:57:17Z")

</div>

@lpil and @hauleth  
Thanks, I know how to do it now!
