jgonet

jgonet

Popcorn Core Team

I was creating simple RPN calculator function, but I stumbled upon a problem in matching functions in guards.

Code looks like so:

def compute_RPN_expr(value, stack) when is_number(value), do: [value | stack]

def compute_RPN_expr(operator, stack) when operator in [&+/2, &-/2, &*/2, &//2] do
  [a, b | stack] = stack
  stack = [operator.(b, a) | stack]
end

After trying to define it inside IEX I’m getting invalid expression in guard, & is not allowed in guards.

Why? I can do:

a = &//2
a === &//2

Without much hassle, but I can’t use the same equality test via in.
Is there any Elixir/Erlang limitation or this was simply overlooked?

First 9 of 9 Posts Switch mode

NobbZ

NobbZ

In general you can’t compare functions for equality. So even if it were syntactically allowed, it wouldn’t make sense semantically.

Instead you should use atoms as operators and map them to functions.

jgonet

jgonet OP

Popcorn Core Team

Why so? This would work like a comparing function pointers in C - checking their address. I’m kinda expecting doing most operations on normal variables will work on functions too. So, for me it makes sense if I’m passing function f as argument and inside some function I’m checking if I actually passed exactly f. I know this thing isn’t broadly used, but in some applications can be useful. Mapping atoms to functions creates another level of indirection, but I’ll give it a go.

NobbZ

NobbZ

There are no function pointers in elixir.

&+/2 is different in every module you write this. Also, even though both of the following functions are obviously producing the same result for their respective domains, you can’t computationally compare them for equality without doing manual prooves:

def multiply_a(a, b), do: a * b

def multiply_b(0, _), do: 0
def multiply_b(_, 0), do: 0
def multiply_b(1, b), do: b
def multiply_b(a, b) when a > 0, do: b + multiply(a - 1, b)
def multiply_b(a, b) when a < 0, do: multiply(-a, -b)

Both of these functions are “equal” to */2, but for the latter it will be very hard to proof in a language like elixir.

Just rewrite your dispatcher to use atoms (or accept all functions with arity 2).

def f(op, [a, b | s]) when op in [:+, :-, :*, :/], do: [apply(:erlang, op, [a, b]) | s]
def f(v, s) when is_number(v), do: [v | s]

def g(op, [a, b | s]) when is_function(op, 2), do: [op.(a, b) | s]
def g(v, s), do: [v | s]
Qqwy

Qqwy

TypeCheck Core Team

One important reason is hot code reloading and the distributed kind of Erlang/Elixir programs: If you are running code on multiple nodes, then they both have separate versions of the functions that might be executed (and if you try to upgrade both nodes to a new version, they are not guaranteed to upgrade at exactly the same time). If you pass on a function reference from one node to the next, it has to be transformed to an intermediate representation on one side, and back on the other side.

In short: The reference on one node will not point to the same location as the one on the other node. This means that we cannot check for equality (which is stronger than, but would infer, equivalence, which is what your code actually requires) by comparing pointers.

In Erlang/Elixir, all data structures can be checked for structural equivalence by matching on them. However, functions are, for the reason outlined above, opaque, so we cannot check their internals.

And therefore, an extra symbolic intermediate step like what @NobbZ proposes is required.


In a more general sense, you can never prove the equivalence of two functions, because this would be the same as solving the Halting Problem. The only thing you could do is to prove that two functions are equivalent because they are equal (exactly the same instance), but this is something that is not possible in Elixir because of above-outlined restrictions imposed by the distributed nature of Elixir’s runtime system.

jgonet

jgonet OP

Popcorn Core Team

I don’t want to prove that function A is equals to function B. I Just want to compare them by address, e.g.

iex(1)> a = &Float.round(&1, &2)
&Float.round/2
iex(2)> b = &Float.round(&2, &1)
#Function<12.127694169/2 in :erl_eval.expr/5>

Those 2 functions above aren’t the same, even when they’re doing the same thing. So a != b.
is_function/2 may be good idea, I’ll try it, thanks.

@Qqwy, thanks for explanation; I’ve got one question: is any tutorial somewhere which covers Elixirs internals or I have to learn Erlang/BEAM low level stuff and then translate to Elixir from source code?

Qqwy

Qqwy

TypeCheck Core Team

@jgonet There are not many cases in which you actually touch the Erlang/BEAM low-level stuff directly or have to keep them in mind (most of the time, Elixir’s abstractions are very leak-free). However, I would recommend watching Solid Ground because it will give you a good high-level foundation of understanding what trade-offs have been made in Erlang/BEAM :slight_smile:.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The BEAM Book: Understanding the Erlang Runtime System is a good resource for looking at the internals of how the BEAM itself works. Decisions and capabilities like the one you’re describing are gonna either be present or absent on the basis of the BEAM itself, Elixir just gets compiled to BEAM byte code.

7stud

7stud

Why is :erlang the module in MFA.

LostKobrakai

LostKobrakai

You could use Kernel, but those just forward to the erlang implementations anyways: elixir/lib/elixir/lib/kernel.ex at 8ca85ed2a82019df1a2acf3238f24a56370464bb · elixir-lang/elixir · GitHub

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement