Is this a bug in my brain or the compiler?

116 views
Skip to first unread message

pragdave

unread,
Feb 22, 2017, 8:57:25 PM2/22/17
to elixir-lang-core
iex(16)> defmodule A do def _fib(n, %{ ^n => result }), do: result end
** (MatchError) no match of right hand side value: :error

Notice this is a compilation error.

Some background. This is part of a Fibonacci function that uses a cache (the second parameter). If I get a cache hit, I want to return the corresponding value.

The concept works outside the context of a function parameter list:

iex(16)> cache = %{ 0 => 0, 10 => 55 }
%{0 => 0, 10 => 55}
iex(17)> n = 10; %{ ^n => result } = cache; result
55

Elixir 1.5.0-dev (3094471)

If it's a bug, I'll submit it.

Cheers

Dave

Allen Madsen

unread,
Feb 22, 2017, 10:44:09 PM2/22/17
to elixir-l...@googlegroups.com
Your non-method example isn't the same as the method example. The equivalent would be:

[n, %{^n => result}] = [10, %{ 0 => 0, 10 => 55 }]

In both cases I get `unbound variable ^n` on elixir 1.4.1.

The behavior is surprising since this works:

[n, ^n] = [10, 10]

--
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-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/6c948795-eafd-4a05-af81-857cf470daa5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paul Schoenfelder

unread,
Feb 22, 2017, 11:11:26 PM2/22/17
to elixir-l...@googlegroups.com
Isn't the pin operator unnecessary/invalid here? Two bindings in a match with the same name asserts that the two values are the same.

Paul

On Wed, Feb 22, 2017 at 9:44 PM Allen Madsen <allen.c...@gmail.com> wrote:
Your non-method example isn't the same as the method example. The equivalent would be:

[n, %{^n => result}] = [10, %{ 0 => 0, 10 => 55 }]

In both cases I get `unbound variable ^n` on elixir 1.4.1.

The behavior is surprising since this works:

[n, ^n] = [10, 10]

On Wed, Feb 22, 2017 at 8:57 PM, pragdave <prag...@gmail.com> wrote:
iex(16)> defmodule A do def _fib(n, %{ ^n => result }), do: result end
** (MatchError) no match of right hand side value: :error

Notice this is a compilation error.

Some background. This is part of a Fibonacci function that uses a cache (the second parameter). If I get a cache hit, I want to return the corresponding value.

The concept works outside the context of a function parameter list:

iex(16)> cache = %{ 0 => 0, 10 => 55 }
%{0 => 0, 10 => 55}
iex(17)> n = 10; %{ ^n => result } = cache; result
55

Elixir 1.5.0-dev (3094471)

If it's a bug, I'll submit it.

Cheers

Dave

--
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.

--
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 on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAK-y3Cs_N%3DG1TEhAk6LC5uAeeiy9o_gYix8%3DgzSK1icLEPjnFQ%40mail.gmail.com.

José Valim

unread,
Feb 23, 2017, 3:05:59 AM2/23/17
to elixir-l...@googlegroups.com
It is definitely a compiler bug, as that error message is not clear what is happening.

I will fix it on master. Thank you.



José Valim
Skype: jv.ptec
Founder and Director of R&D

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.

--
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-core+unsubscribe@googlegroups.com.

--
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-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAK%3D%2B-Ttp5MuRotrkP8LhhVx9nHRMbXbSZ4%3DFRnTBxV08tu1UNg%40mail.gmail.com.

Allen Madsen

unread,
Feb 23, 2017, 8:38:21 AM2/23/17
to elixir-l...@googlegroups.com
Paul,

I thought so as well, but ^n is necessary in a map. Otherwise it produces this error:

illegal use of variable n inside map key match, maps can only match on existing variable by using ^n

pragdave

unread,
Feb 23, 2017, 11:17:29 AM2/23/17
to elixir-lang-core, jose....@plataformatec.com.br


On Thursday, February 23, 2017 at 2:05:59 AM UTC-6, José Valim wrote:
It is definitely a compiler bug, as that error message is not clear what is happening.

I will fix it on master. Thank you.

Does that mean I won't be able to write this? 

Michał Muskała

unread,
Feb 23, 2017, 11:22:24 AM2/23/17
to elixir-lang-core, jose....@plataformatec.com.br
Pattern matching on something like the example below is a bit tricky, because of the semantics of pattern matching:

[n, %{^n => result}] = [10, %{ 0 => 0, 10 => 55 }]


^n means take the previous value of the variable "n" - but since we're in the pattern there's no previous value (unless it was bound earlier). The pattern is not evaluated left to right, but considered as a whole, e.g. you write {x, x} to match on a tuple with two identical values. This could leave us with another possibility:

[n, %{n => result}] = [10, %{ 0 => 0, 10 => 55 }]

This, in turn, presents a rather nasty performance issues. Since the pattern is evaluated as a whole, we could write [n, %{n => n}] or even worse [n, %{x => n}] - matching becomes extremely complex in those situations. Because of this, matching on a map keys with non-literals is not allowed.

Michał.
--
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 on the web visit https://groups.google.com/d/msgid/elixir-lang-core/a5434bc2-b63d-4038-b51d-ce7c780eac89%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages