const evaluation

101 views
Skip to first unread message

benjamin...@gmail.com

unread,
Aug 6, 2025, 4:00:44 PMAug 6
to elixir-lang-core
AFAIK, then the compiler does not perform const evaluation.

iex(9)> quote(do: 1+2*3)
{:+, [context: Elixir, imports: [{1, Kernel}, {2, Kernel}]],
 [1, {:*, [context: Elixir, imports: [{2, Kernel}]], [2, 3]}]}

iex(10)> quote(do: 1+2*3)
7

Are there any plans to implement this, if not what is the recommended approach to do this, when working on an AST.

José Valim

unread,
Aug 6, 2025, 4:19:44 PMAug 6
to elixir-l...@googlegroups.com
I believe we do it for certain functions in Elixir's stdlib and the Erlang compiler (which we invoke) does it too.


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/elixir-lang-core/b7b48090-7022-4f14-b92b-2732272fc151n%40googlegroups.com.
Message has been deleted
Message has been deleted

Jean Klingler

unread,
Aug 6, 2025, 5:29:13 PMAug 6
to elixir-l...@googlegroups.com
You can confirm it is done by the Erlang compiler using the beam_file lib for example (or `@compile :S`) and check the emitted byte code:

defmodule Foo do
  def foo, do: 1 + 2 * 3
end
|> BeamFile.byte_code!()
|> elem(5)
|> Enum.filter(&(elem(&1, 1) == :foo))

[
  {:function, :foo, 0, 11,
   [
     {:line, 1},
     {:label, 10},
     {:func_info, {:atom, Foo}, {:atom, :foo}, 0},
     {:label, 11},
     {:move, {:integer, 7}, {:x, 0}},
     :return
   ]}
]



Message has been deleted
Message has been deleted

benjamin...@gmail.com

unread,
Aug 8, 2025, 7:07:39 AMAug 8
to elixir-lang-core
Yeah, I should have been clearer, I'm looking for const evaluation of expressions e.g 

iex> Code.string_to_quoted!("1+2*3")
7

José Valim

unread,
Aug 8, 2025, 7:08:46 AMAug 8
to elixir-l...@googlegroups.com
That's what the compiler does, as long as the code is compiled. It is not the job of a parser to do this.


Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

benjamin...@gmail.com

unread,
Aug 15, 2025, 7:00:38 AMAug 15
to elixir-lang-core

Yeah, I’m looking for a compile snippet function as I’m only working with expression and not complete modules.
Reply all
Reply to author
Forward
0 new messages