# Referential transparency

**URL:** <https://forum.elixirforum.com/t/referential-transparency/44806>\
**Category:** Questions\
**Tags:** memory\
**Created:** [December 24, 2021, 2:40am UTC](https://forum.elixirforum.com/t/referential-transparency/44806 "2021-12-24T02:40:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [December 24, 2021, 2:40am UTC](https://forum.elixirforum.com/t/referential-transparency/44806/1 "2021-12-24T02:40:08Z")

</div>

From what I understand, Elixir does not have referential transparency. I was still curious about it, though.

I discovered `:erts_debug.same/2` in [this reply](https://forum.elixirforum.com/t/is-there-a-way-to-test-reference-equivalence/26051/16) which tells you if two pieces of data point to the same memory location or not. So I did a couple of tests:

```elixir
iex(1)> defmodule Fry, do: def about, do: %{name: "Philip"}
iex(2)> fry = Fry.about()
iex(3)> also_fry = Fry.about()
iex(4)> :erts_debug.same(fry, also_fry)
true # 🤔

```

And then in a second test:

```elixir
iex(1)> defmodule Employee, do: def about(name), do: %{name: name}
iex(2)> leela = Employee.about("Leela")
iex(3)> also_leela = Employee.about("Leela")
iex(4)> :erts_debug.same(leela, also_leela)
false # 🧐

```

The second example has me pretty dang satisfied that there is no referential transparency in Elixir (not that I didn’t believe it…), but what’s going on in the first one? Is that the compiler inlining the function?

---

<div class="post-metadata">

**Author:** ![ityonemo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/32/11341_2.png) [@ityonemo](https://forum.elixirforum.com/u/ityonemo)\
**Post date:** [December 24, 2021, 2:51am UTC](https://forum.elixirforum.com/t/referential-transparency/44806/2 "2021-12-24T02:51:21Z")

</div>

I’m not at my computer now, but I recommend checking `:beam_disasm.file(module)`. The map in the first one is almost certainly a literal stored as in a single place, so two calls will return the same reference to that literal.

The leela function probably new’s a new map each time it’s called, so there’s no way for the BEAM to know if they _should_ be the same, since it could have been passed anything, let alone a string.

---

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [December 24, 2021, 3:05am UTC](https://forum.elixirforum.com/t/referential-transparency/44806/3 "2021-12-24T03:05:46Z")

</div>

Thanks for the reply!

I’m playing around with `:beam_disasm.file` but can’t seem to get it to return anything other than

```elixir
{:error, :beam_lib,
 {:not_a_beam_file, "_build/dev/lib/disasm_test/ebin/Elixir.DisasmTest.beam"}}

```

(I’ve tried several other inputs including bare code, code as strings, and different file paths)

What you are describing is what I assume is happening, but I was wondering how exactly it makes those decisions. I’m assuming the compiler can tell if the body of a function is static or not, but I just wanted to know for sure what’s going on. I’m guessing `:beam_disasm` can help me with this! The docs are a little sparse, but I’ll keep plugging away at it.

---

<div class="post-metadata">

**Author:** ![ityonemo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ityonemo/32/11341_2.png) [@ityonemo](https://forum.elixirforum.com/u/ityonemo)\
**Post date:** [December 24, 2021, 3:08am UTC](https://forum.elixirforum.com/t/referential-transparency/44806/4 "2021-12-24T03:08:32Z")

</div>

You should pass the actual binary of the file and not the path to the file. So, File.read! that path!

---

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [December 24, 2021, 3:35am UTC](https://forum.elixirforum.com/t/referential-transparency/44806/5 "2021-12-24T03:35:51Z")

</div>

Ohhhh, cool! …and I now realize “disasm” means “disassemble”, doi.

Thanks for this!

---

<div class="post-metadata">

**Author:** ![LostKobrakai](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lostkobrakai/32/3072_2.png) [@LostKobrakai](https://forum.elixirforum.com/u/LostKobrakai)\
**Post date:** [December 24, 2021, 7:16am UTC](https://forum.elixirforum.com/t/referential-transparency/44806/6 "2021-12-24T07:16:25Z")

</div>

> [@sodapopcan](#):
>
> What you are describing is what I assume is happening, but I was wondering how exactly it makes those decisions. I’m assuming the compiler can tell if the body of a function is static or not, but I just wanted to know for sure what’s going on.

> **[Macro — Elixir v1.12.3](https://elixir.hexdocs.pm/1.12/Macro.html)**

---

<div class="post-metadata">

**Author:** ![josevalim](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/32/1787_2.png) [@josevalim](https://forum.elixirforum.com/u/josevalim)\
**Post date:** [December 24, 2021, 12:36pm UTC](https://forum.elixirforum.com/t/referential-transparency/44806/7 "2021-12-24T12:36:25Z")

</div>

> [@sodapopcan](#):
>
> From what I understand, Elixir does not have referential transparency.

Elixir does not have referential transparency but I don’t think those examples prove or deny it.

Referential transparency means the compiler is able to inline those calls - but the compiler doesn’t necessarily have to. After all, you can think some calls may actually perform long running computations that would make them expensive to inline.

As you likely know, for a system to be referentially transparent, the compiler (and the developer) need to be able to recognize and separate code with side-effects from pure code.

Elixir and Erlang does not really tag functions with side-effects. The compiler also doesn’t look across modules, because of hot code reloading, so we can’t peak inside either. But the Erlang compiler does optimize literals and several functions from Erlang’s stdlib if those particular functions are guaranteed to be pure.

---

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [December 24, 2021, 2:46pm UTC](https://forum.elixirforum.com/t/referential-transparency/44806/8 "2021-12-24T14:46:31Z")

</div>

Awesome, thanks for all that! That helps a bunch, especially around if the compiler is able to tag something as having side-effects or not. I don’t know a lot about compilers but am slowly learning a thing or two and I just like to know these things. I am still a little curious how the first example knows to point to the same memory after a function call, unless `:erts_debug.same/2` doesn’t do exactly what I think it does.

---

<div class="post-metadata">

**Author:** ![josevalim](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/32/1787_2.png) [@josevalim](https://forum.elixirforum.com/u/josevalim)\
**Post date:** [December 24, 2021, 2:48pm UTC](https://forum.elixirforum.com/t/referential-transparency/44806/9 "2021-12-24T14:48:52Z")

</div>

The answers above explain it. It is tagged as a literal and all references to it point to the same place in memory.

---

<div class="post-metadata">

**Author:** ![sodapopcan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sodapopcan/32/34668_2.png) [@sodapopcan](https://forum.elixirforum.com/u/sodapopcan)\
**Post date:** [December 24, 2021, 4:01pm UTC](https://forum.elixirforum.com/t/referential-transparency/44806/10 "2021-12-24T16:01:49Z")

</div>

Ah ok! I thought you were only talking functions from the standard lib so was a little confused but I got it now. Thanks so much for taking the time to explain!
