tim2CF

tim2CF

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

Marked As Solved

hauleth

hauleth

erl_internal:guard_bif/2 or erl_lint:is_guard_test/1

Also Liked

lpil

lpil

Creator of Gleam

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

lpil

lpil

Creator of Gleam

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.

hauleth

hauleth

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.

Last Post!

tim2CF

tim2CF

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

Where Next?

Popular in Questions Top

New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement