We're planting a tree for every job application! Click here toĀ learn more

Metaprogramming in Elixir

Serokell

7 Sep 2021

ā€¢

8 min read

Metaprogramming in Elixir
  • Elixir

Usually, we think of a program as something that manipulates data to achieve some result.

But what is data?

Can we use the programs themselves as data? šŸ¤”

In todayā€™s article, weā€™ll go down the rabbit hole with the assistance of Elixir, a programming language that is permeated by metaprogramming.

Iā€™ll introduce you to metaprogramming in Elixir and show you how to create a macro for defining curried functions.

What is metaprogramming?

Metaprogramming is just writing programs that manipulate programs. Itā€™s a wide term that can include compilers, interpreters, and other kinds of programs.

In this article, we will focus on metaprogramming as it is done in Elixir, which involves macros and compile-time code generation.

Metaprogramming in Elixir

To understand how metaprogramming works in Elixir, it is important to understand a few things about how compilers work. During the compilation process, every computer program is transformed into an abstract syntax tree (AST) ā€“ a tree structure that enables the computer to understand the contents of the program.

ob9gpjsm.ast.png

Quick maths.

In Elixir, each node of the AST (except basic values) is a tuple of three parts: function name, metadata, function arguments.

Elixir enables us to access this internal AST representation via quote.

iex(1)> quote do 2 + 2 - 1 end
{:-, [context: Elixir, import: Kernel],
 [{:+, [context: Elixir, import: Kernel], [2, 2]}, 1]}

We can modify these ASTs with macros, which are functions from AST to AST that are executed at compile-time.

You can use macros to generate boilerplate code, create new language features, or even build domain-specific languages (DSLs).

Actually, a lot of the language constructs that we regularly use in Elixir such as def, defmodule, if, and others are macros. Furthermore, many popular libraries like Phoenix, Ecto, and Absinthe use macros liberally to create convenient developer experiences.

Hereā€™s an example Ecto query from the documentation:

query = from u in "users",
          where: u.age > 18,
          select: u.name

Metaprogramming in Elixir is a powerful tool. It approaches LISP (the OG metaprogramming steam roller) in expressivity but keeps things one level above in abstraction, enabling you to delve into AST only when you need to. In other words, Elixir is basically LISP but readable. šŸ™ƒ

Getting started

So how do we channel this immense power? šŸ§™

While metaprogramming can be rather tricky, it is rather simple to start metaprogramming in Elixir. All you need to know are three things.

quote unquote defmacro
code -> AST outside quote -> inside quote AST -> AST

quote

quote converts Elixir code to its internal AST representation.

You can think of the difference between regular and quoted expressions to be the difference in two different requests.

  • Say your name, please. Here, the request is to reply with your name.
  • Say ā€œyour nameā€, please. Here, the request is to reply with the internal representation of the request in the language ā€“ ā€œyour nameā€.
iex(1)> 2 + 2
4
iex(2)> quote do 2 + 2 end
{:+, [context: Elixir, import: Kernel], [2, 2]}

quote makes it a breeze to write Elixir macros since we donā€™t have to generate or write the AST by hand.

unquote

But what if we want to have access to variables inside quote? The solution is unquote.

unquote functions like string interpolation, enabling you to pull variables into quoted blocks from the surrounding context.

Hereā€™s how it looks in Elixir:

iex(1)> two = 2
2
iex(2)> quote do 2 + 2 end
{:+, [context: Elixir, import: Kernel], [2, 2]}
iex(3)> quote do two + two end
{:+, [context: Elixir, import: Kernel],
 [{:two, [], Elixir}, {:two, [], Elixir}]}
iex(4)> quote do unquote(two) + unquote(two) end
{:+, [context: Elixir, import: Kernel], [2, 2]}

If we donā€™t unquote two, we will get Elixirā€™s internal representation of some unassigned variable called two. If we unquote it, we get access to the variable inside the quote block.

defmacro

Macros are functions from ASTs to ASTs.

For example, suppose we want to make a new type of expression that checks for the oddity of numbers.

We can make a macro for it in just a few lines with defmacro, quote, and unquote.

defmodule My do
  defmacro odd(number, do: do_clause, else: else_clause) do
    quote do
      if rem(unquote(number), 2) == 1, do: unquote(do_clause), else: unquote(else_clause)
    end
  end
end
iex(1)> require My
My
iex(2)> My.odd 5, do: "is odd", else: "is not odd"
"is odd"
iex(3)> My.odd 6, do: "is odd", else: "is not odd"
"is not odd"

When should you use metaprogramming?

"Rule 1: Donā€™t Write Macros" ā€“ Chris McCord, Metaprogramming in Elixir

While metaprogramming can be an awesome tool, it should be used with caution.

Macros can make debugging much harder and increase overall complexity. They should be turned to only when it is necessary ā€“ when you run into problems you canā€™t solve with regular functions or when there is a lot of plumbing behind the scenes that you need to hide.

When used correctly, they can be very rewarding, though. To see how they can improve developer life, let's look at some real-life examples from Phoenix, the main Elixir web framework.

How macros are used in Phoenix

In the following section, weā€™ll analyze the router submodule of a freshly made Phoenix project as an example of how macros are used in Elixir.

use

If you look at the top of basically any Phoenix file, you will most likely see a use macro. Our router submodule has one.

defmodule HelloWeb.Router do
  use HelloWeb, :router

What this one expands to is:

require HelloWeb
HelloWeb.__using__(:router)

require asks HelloWeb to compile its macros so that they can be used for the module. But whatā€™s using? It, as you might have guessed, is another macro!

  defmacro __using__(which) when is_atom(which) do
    apply(__MODULE__, which, [])
  end 

In our case, this macro invokes the router function from the HelloWeb module.

  def router do
    quote do
      use Phoenix.Router

      import Plug.Conn
      import Phoenix.Controller
    end
  end

router imports two modules and launches another __using__ macro.

As you can see, this hides a lot, which can be both a good and a bad thing. But it also gives us access to a magical use HelloWeb, :router to have everything ready for quick webdev action whenever we need.

pipeline

Now, look below use.

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

Yup, more macros.

pipeline and plug define pipelines of plugs, which are functions of functions that transform the connection data structure.

While the previous macro was used for convenient one-line imports, this one helps to write pipelines in a very clear and natural language.

scope

And, of course, the routing table is a macro as well.

  scope "/", HelloWeb do
    pipe_through :browser

    get "/", PageController, :index

  end

scope, pipe_through, get ā€“ all macros.

In fact, the whole module is macros and an if statement (which is a macro) that adds an import statement and executes a macro.

I hope that this helps you see how metaprogramming is at the heart of Elixir.

Now, letā€™s try to build our own macro.

Build your own Elixir macro

Elixir and currying donā€™t really vibe together. But with some effort, you can create a curried function in Elixir.

Hereā€™s a regular Elixir sum function:

def sum(a, b), do: a + b

Hereā€™s a curried sum function:

def sum() do
  fn x ->
    fn y ->
      x + y
    end
  end
end

Hereā€™s how they both behave:

iex(1)> Example.sum(1,2)
3
iex(2)> plustwo = Example.sum.(2)# Function<10.76762873/1 in Example.sum/0>
iex(3)> plustwo.(2)
4

Letā€™s say that we want to use curried functions in Elixir for some reason (for example, we want to create a monad library). Writing out every function in our code like that would be, to say the least, inconvenient.

But with the power of metaprogramming, we can introduce curried functions without a lot of boilerplate. Letā€™s define our own defc macro that will define curried functions for us.

First, we need to take a look at how a regular def looks as an AST:

iex(1)> quote do def sum(a, b), do: a + b end
{:def, [context: Elixir, import: Kernel],
 [
   {:sum, [context: Elixir], [{:a, [], Elixir}, {:b, [], Elixir}]},
   [
     do: {:+, [context: Elixir, import: Kernel],
      [{:a, [], Elixir}, {:b, [], Elixir}]}
   ]
 ]}

It is a macro with two arguments: the function definition (in this case, sum is being defined) and a do: expression.

Therefore, our defc (which should take the same data) will be a macro that takes two things:

  1. A function definition, which consists of the function name, context, and supplied arguments.
  2. A do: expression, which consists of everything that should be done with these arguments.
defmodule Curry do
  defmacro defc({name, ctx, arguments} = clause, do: expression) do
  end
end

We want the macro to define two functions:

  1. The function defined in defc.
  2. A 0-argument function that returns the 1st function, curried.
  defmacro defc({name, ctx, arguments} = clause, do: expression) do
    quote do
      def unquote(clause), do: unquote(expression)
      def unquote({name, ctx, []}), do: unquote(body)
    end
  end

Thatā€™s more or less the macro. Now, we need to generate the main part of it, the body.

To do that, we need to go through the whole argument list and, for each argument, wrap the expression in a lambda.

  defp create_fun([h | t], expression) do
    rest = create_fun(t, expression)

    quote do
      fn unquote(h) -> unquote(rest) end
    end
  end

  defp create_fun([], expression) do
    quote do
      unquote(expression)
    end
  end

Then, we assign the variable body in the macro to be the result of create_fun, applied to the arguments and the expression.

  defmacro defc({name, ctx, arguments} = clause, do: expression) do
    body = create_fun(arguments, expression)

    quote do
      def unquote(clause), do: unquote(expression)
      def unquote({name, ctx, []}), do: unquote(body)
    end
  end

Thatā€™s it! šŸ„³

To try it out, letā€™s define another module with a sum function in it.

defmodule Example do
  import Curry

  defc sum(a, b), do: a + b

end
iex(1)> Example.sum(2,2)
4
iex(2)> Example.sum.(2).(2)
4
iex(3)> plustwo = Example.sum.(2)# Function<5.100981091/1 in Example.sum/0>
iex(4)> plustwo.(2)
4

You can see the full code here.

In our example, the macro provides only the sum() and sum(a,b) functions. But from here, itā€™s easy to extend our macro to generate partial functions for all arities.

In the case of sum, we can make it so that the macro generates sum(), sum(a), and sum(a,b), modifying the function definition to account for the missing arguments.

Since it is an awesome exercise to try on your own, I will not spoil the answer. šŸ˜Š

Further learning

If you want to learn more about macros in Elixir, here are a few resources I suggest to check out:

Did you like this article?

Serokell

Serokell is a software development company focused on creating customized high-performance solutions with functional programming technologies.

See other articles by Serokell

Related jobs

See all

Title

The company

  • Remote

Title

The company

  • Remote

Title

The company

  • Remote

Title

The company

  • Remote

Related articles

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

ā€¢

12 Sep 2021

JavaScript Functional Style Made Simple

JavaScript Functional Style Made Simple

Daniel Boros

ā€¢

12 Sep 2021

WorksHub

CareersCompaniesSitemapFunctional WorksBlockchain WorksJavaScript WorksAI WorksGolang WorksJava WorksPython WorksRemote Works
hello@works-hub.com

Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ

108 E 16th Street, New York, NY 10003

Subscribe to our newsletter

Join over 111,000 others and get access to exclusive content, job opportunities and more!

Ā© 2024 WorksHub

Privacy PolicyDeveloped by WorksHub