O'Reilly logo

Erlang Programming by Francesco Cesarini, Simon Thompson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

TypEr: Success Types and Type Inference

The TypEr system, built by Tobias Lindahl and Kostis Sagonas,[45] is used to check the validity of –spec annotations, as well as to infer the types of functions in modules without type annotations.

You use TypEr from the command line. You can see the full range of options by typing:

typer --help

Taking the example of the mobile user database from Chapter 10, the following command:

typer --show usr.erl usr_db.erl

gives the following output (shortened for brevity):

Unknown functions: [{ets,safefixtable,2}]

%% File: "usr.erl"
%% ---------------
-spec start() -> 'ok' | {'error','starting'}.
-spec start(_) -> 'ok' | {'error','starting'}.
-spec stop() -> any().
-spec add_usr(_,_,_) -> any().
-spec delete_usr(_) -> any().
 ...

%% File: "usr_db.erl"
%% ------------------
-spec create_tables(_) -> any().
-spec close_tables() -> any().
-spec add_usr(#usr{}) -> 'ok'.
-spec update_usr([tuple()] | tuple()) -> 'ok'.
-spec delete_usr(_) -> 'ok' | {'error','instance'}.
 ...

In a statically typed language such as Haskell, the type of a function inferred by the type checker will provide a guarantee that the function will not fail if applied to arguments of the input type. Erlang is a dynamically typed language, and so the TypEr tool takes a different approach.

Note

TypEr infers success types, which encapsulate all the ways in which a function can be applied successfully. In general, this cannot be accurate, but it will always be an overapproximation, so using the function ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required