c13e

c13e

Eager evaluation and optimizing computation

I’m learning Haskell at the moment and contrasting its semantics with Elixir as I go.

In Haskell, if I create the following naive & inefficient prime number checker:

factors n = [x | x <- [1..n], n `mod` x == 0]
prime n = factors n == [1, n]

and run it against a large number like 10^9, it returns False immediately thanks to lazy evaluation as soon as a mismatch occurs.

> prime 1000000000
False

If I do the same in Elixir, the eager evaluation will try to compute all the factors of 10^9 first, which is very slow with this implementation.

factor = &(for x <- 1..&1, rem(&1, x) == 0, do: x)
prime = &(factor.(&1) == [1, &1])
prime.(1000000000)
# hangs

The two evaluation strategies make sense to me but I somehow still find it surprising that the BEAM runtime can’t optimize away the matching against 2-element list and instead carries on the computation past the (sufficient) first 2 evaluations of factor.(1000000000).

I guess despite the eager evaluation, I was still expecting some sort of treacherous optimization from the BEAM :slight_smile:

Can someone enlighten me as to why this kind of constrained optimization can’t or doesn’t happen in languages with eager evaluation? Thanks!

Marked As Solved

Qqwy

Qqwy

TypeCheck Core Team

Here is the code one more time, but now as more idiomatic Elixir with named functions in a module (rather than anonymous functions in IEx).

defmodule StrictExample do
  def factor(val) do
    for x <- 1..val, rem(val, x) == 0 do
      x
    end
  end

  def prime(val) do
    factor(val) == [1, val]
  end

  def is_large_number_prime() do
    prime(1000000000)
  end
end

It indeed has to do with the order of evaluation.
Haskell, being powered by a STG (Spineless Tagless graph-reduction machine), will move execution from the ‘goal’ back to where it originates.

In this example, it will indeed try to match [1, n] with the outcome of factors n. Indeed because of laziness is Haskell able to stop once two elements have been read from the output list of factor.

However, on the BEAM, we do not have this context-switching between looking at multiple parts of the output. Instead, for an operation like == we first evaluate both operands, and then check whether they are equal.

The optimization you expect here is (in eager languages) both rather limited in usefulness for practical applications, as well as rather high-level in the sense that as a human it seems obvious but for a compiler it would require many steps of intermediate reasoning.

To make an explicitly lazy implementation in Elixir, you can do this:

defmodule LazyExample do
  def factor(val) do
    # A lazy enumeration of all prime factors of `val`
    Stream.filter(1..val, &rem(val, &1) == 0)
  end

  def prime(val) do
    # We force the stream here, but only look at the first two elements.
    Enum.take(factor(val), 2) == [1, val]
  end

  def is_large_number_prime() do
    prime(1000000000)
  end
end

In this implementation, prime(1000000000) will indeed execute very fast.

Also Liked

kip

kip

ex_cldr Core Team

For those like my treading this path for the first time, here’s a good starting point: https://www.microsoft.com/en-us/research/wp-content/uploads/1992/04/spineless-tagless-gmachine.pdf

c13e

c13e

Thank you so much for the clear explanation!

It makes more sense now given that such context switching strategy doesn’t happen in the BEAM or play well with eager evaluation in general.

Haskell’s STG sounds fascinating, will look into it :thinking:

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement