Dialyzer

Dialyzer is the static analysis tool that ships with Erlang/OTP. It reads the compiled beams of a project and works out what each function can return, a property called success typing. It does not need a single annotation to start: the code itself says that half/1 returns a float, and Dialyzer believes the code. A -spec is the place where the programmer states the same thing, and the comparison of the two is the check. The typespecs lesson writes the annotations; this one runs the tool.

Success typing

A success typing is the set of arguments for which a function can possibly return, and the set of values it can return. Dialyzer starts from the clauses and widens the set until it covers every path, so it reports only a contradiction that is certain: a call that can never succeed, a clause that can never match, or a spec that promises a type the body cannot produce. It does not report a value it merely finds unlikely, and it does not need the program to be run.

The PLT

Before the first check Dialyzer builds a PLT, a persistent lookup table of the types of the applications it will compare against. A course project uses erts, kernel and stdlib, which is the set this course caches. The build takes minutes once; every later run loads the file and takes seconds:

dialyzer --version

Dialyzer version v5.3.1.1

dialyzer --build_plt --apps erts kernel stdlib --output_plt ~/.cache/erlang-course/otp27-plt

The build prints the modules it analyzes and a final done line; the output is long and the times vary, so the page does not quote it. A project that adds crypto or ssl to the PLT must rebuild it, or every call into those applications is reported as an unknown function.

A clean run

A module with an honest spec passes. This one states the type its body really has, and the run ends with the success line:

ratio.erl

-module(ratio). -export([main/0, half/1]). -spec half(number()) -> float(). half(Number) -> Number / 2. main() -> io:format("~p~n", [half(5)]).

erlc ratio.erl

erl -noshell -eval 'ratio:main(), halt().'

2.5

dialyzer --no_check_plt --plt ~/.cache/erlang-course/otp27-plt ratio.beam

done (passed successfully)

The command is marked as not run on this page because it needs the PLT that the reader builds in the previous section and a beam in the folder of the reader; the line above is the last line of a real run. The run also prints Proceeding with analysis... and a time, which change with the machine.

The spec that disagrees

The warning worth recognising is the one where the code and the spec disagree. This is the body of a module whose spec promises an integer, while the division returns a float. The block is a fragment: the module header and its main/0 are left out so the wrong claim is the whole subject:

liar.erl

-spec half(integer()) -> integer(). half(Number) -> Number / 2.

dialyzer --no_check_plt --plt ~/.cache/erlang-course/otp27-plt liar.beam

liar.erl:4:2: Invalid type specification for function liar:half/1. The success typing is liar:half (number()) -> float() But the spec is liar:half (integer()) -> integer() The return types do not overlap

Read the two lines under The success typing is and But the spec is: the first is what the code does, the second is what the programmer wrote, and the last line names the contradiction. The fix is to correct the spec to number() -> float(), not to bend the code to a promise it never kept. The warning names the file, the line and the column of the attribute, which is why a CI job can point a reviewer at it.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is a success typing?

    Answer

    The arguments and results Dialyzer proves a function can have

    Dialyzer infers what a function can return from its code; the programmer writes no annotation for it.

  2. What is the PLT?

    Answer

    A persistent lookup table of the types of the applications

    The PLT holds the analysed types of erts, kernel, stdlib or another set of applications.

  3. Why does the first Dialyzer run take minutes?

    Answer

    It builds the PLT once

    The PLT build analyzes the OTP applications; later runs load the file and take seconds.

  4. A warning that names Invalid type specification means what?

    Answer

    The body cannot return the type the spec promises

    The success typing of the body and the spec do not overlap, so one of the two is wrong.

  5. What does this module print? -module(q1). -export([main/0]). -spec double(integer()) -> integer(). double(X) -> X * 2. main() -> io:format("~p~n", [double(21)]).

    Answer

    42

    The spec matches the body: an integer in, an integer out, and the run prints 42.

  6. What does this module print? -module(q2). -export([main/0]). -spec half(number()) -> float(). half(X) -> X / 2. main() -> io:format("~p~n", [half(5)]).

    Answer

    2.5

    / returns a float and the spec says float, so the run prints 2.5 and Dialyzer has nothing to report.

  7. What does this module print? -module(q3). -export([main/0]). -spec classify(integer()) -> atom(). classify(N) when N > 0 -> positive; classify(0) -> zero; classify(_N) -> negative. main() -> io:format("~p~n", [classify(-2)]).

    Answer

    negative

    The last clause catches the negative number and returns the atom negative, which the spec allows.

  8. What does this module print? -module(q4). -export([main/0]). -spec total([integer()]) -> integer(). total(Numbers) -> lists:sum(Numbers). main() -> io:format("~p~n", [total([1, 2, 3])]).

    Answer

    6

    The sum of the list is 6, and the spec and the body agree on integers.

  9. Which applications does the course PLT cover?

    Answer

    erts, kernel and stdlib

    The cached PLT holds the three applications every program needs; a call into another is reported as unknown.

  10. How should a spec that disagrees with the body be fixed?

    Answer

    Correct the spec to the type the body really has

    The body is the truth Dialyzer inferred; the spec is the claim to correct.

The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14) and Dialyzer 5.3.1.1 on Alpine Linux 3.24.3 (x86_64) in a Docker container; the two dialyzer lines were run there and are marked not run on this page because they need a PLT and a beam in the reader's folder.

Article author: Arthur Isaev