Purity validation

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

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

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

If you want to know if a macro is a valid guard you can use 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.

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

1 Like

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.

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.

1 Like

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


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.

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

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

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.

1 Like

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.

1 Like

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?

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

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

1 Like

erl_internal:guard_bif/2 or erl_lint:is_guard_test/1

1 Like

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