Flix (programming language)

Flix is a functional, imperative, and logic programming language developed at Aarhus University, with funding from the Independent Research Fund Denmark, and by a community of open source contributors. The Flix language supports algebraic data types, pattern matching, parametric polymorphism, currying, higher-order functions, extensible records, channel and process-based concurrency, and tail call elimination. Two notable features of Flix are its type and effect system and its support for first-class Datalog constraints.

The Flix type and effect system supports Hindley-Milner-style type inference. The system separates pure and impure code: if an expression is typed as pure then it cannot produce an effect at run-time. Higher-order functions can enforce that they are given pure (or impure) function arguments. The type and effect system supports effect polymorphism which means that the effect of a higher-order function may depend on the effect(s) of its argument(s).

Flix supports Datalog programs as first-class values. A Datalog program value, i.e. a collection of Datalog facts and rules, can be passed to and returned from functions, stored in data structures, and composed with other Datalog program values. The minimal model of a Datalog program value can be computed and is itself a Datalog program value. In this way, Flix can be viewed as a meta programming language for Datalog. Flix supports stratified negation and the Flix compiler ensures stratification at compile-time. Flix also supports an enriched form of Datalog constraints where predicates are given lattice semantics.

Overview
Flix is a programming language in the ML-family of languages. Its type and effect system is based on Hindley-Milner with several extensions, including row polymorphism and Boolean unification. The syntax of Flix is inspired by Scala and uses short keywords and curly braces. Flix supports uniform function call syntax which allows a function call  to be written as. The concurrency model of Flix is inspired by Go and based on channels and processes. A process is a light-weight thread that does not share (mutable) memory with another process. Processes communicate over channels which are bounded or unbounded queues of immutable messages.

While many programming languages support a mixture of functional and imperative programming, the Flix type and effect system tracks the purity of every expression making it possible to write parts of a Flix program in a purely functional style with purity enforced by the effect system.

Flix programs compile to JVM bytecode and are executable on the Java Virtual Machine (JVM). The Flix compiler performs whole program compilation, eliminates polymorphism via monomorphization, and uses tree shaking to remove unreachable code. Monomorphization avoids boxing of primitive values at the cost of longer compilation times and larger executable binaries. Flix has some support for interoperability with programs written in Java.

Flix supports tail call elimination which ensures that function calls in tail position never consume stack space and hence cannot cause the call stack to overflow. Since the JVM instruction set lacks explicit support for tail calls, such calls are emulated using a form of reusable stack frames. Support for tail call elimination is important since all iteration in Flix is expressed through recursion.

The Flix compiler disallows most forms of unused or redundant code, including: unused local variables, unused functions, unused formal parameters, unused type parameters, and unused type declarations, such unused constructs are reported as compiler errors. Variable shadowing is also disallowed. The stated rationale is that unused or redundant code is often correlated with erroneous code

A Visual Studio Code extension for Flix is available. The extension is based on the Language Server Protocol, a common interface between IDEs and compilers being developed by Microsoft.

Flix is open source software available under the Apache 2.0 License.

Hello world
The following program prints "Hello World!" when compiled and executed:

The type and effect signature of the  function specifies that it has no parameters, returns a value of type , and that the function is impure. The  function is impure because it invokes   which is impure.

Algebraic data types and pattern matching
The following program fragment declares an algebraic data type (ADT) named :

The ADT has three constructors:,  , and.

The following program fragment uses pattern matching to destruct a  value:

Higher-order functions
The following program fragment defines a higher-order function named  that when given a function   from   to   returns a function that applies   to its input two times:

We can use the function  as follows:

Here the call to  returns a function that will increment its argument two times. Thus the result of the whole expression is.

Parametric polymorphism
The following program fragment illustrates a polymorphic function that maps a function  over a list of elements of type   returning a list of elements of type  :

The  function recursively traverses the list   and applies   to each element constructing a new list.

Flix supports type parameter elision hence it is not required that the type parameters  and   are explicitly introduced.

Extensible records
The following program fragment shows how to construct a record with two fields  and  :

Flix uses row polymorphism to type records. The  function below takes a record that has   and   fields (and possibly other fields) and returns the sum of the two fields:

The following are all valid calls to the  function:

Polymorphic effects
The Flix type and effect system separates pure and impure expressions. A pure expression is guaranteed to be referentially transparent. A pure function always returns the same value when given the same argument(s) and cannot have any (observable) side-effects.

For example, the following expression is of type  and is  :

whereas the following expression is :

A higher-order function can specify that a function argument must be pure, impure, or that it is effect polymorphic.

For example, the definition of  requires that its function argument   is pure:

The requirement that  must be pure ensures that implementation details do not leak. For example, since  is pure it cannot be used to determine in what order the elements of the set are traversed. If  was impure such details could leak, e.g. by passing a function that also prints the current element, revealing the internal element order inside the set.

A higher-order function can also require that a function is impure.

For example, the definition of  requires that its function argument   is impure:

The requirement that  must be impure ensures that the code makes sense: It would be meaningless to call   with a pure function since it always returns.

The type and effect is sound, but not complete. That is, if a function is pure then it cannot cause an effect, whereas if a function is impure then it may, but not necessarily, cause an effect. For example, the following expression is impure even though it cannot produce an effect at run-time:

A higher-order function can also be effect polymorphic: its effect(s) can depend on its argument(s).

For example, the standard library definition of  is effect polymorphic:

The  function takes a function   from elements of type   to   with effect. The effect of the map function is itself. Consequently, if  is invoked with a pure function then the entire expression is pure whereas if it is invoked with an impure function then the entire expression is impure. It is effect polymorphic.

A higher-order function that takes multiple function arguments may combine their effects.

For example, the standard library definition of forward function composition  is pure if both its function arguments are pure:

The type and effect signature can be understood as follows: The  function takes two function arguments:   with effect   and   with effect. The effect of  is effect polymorphic in the conjunction of   and. If both are pure (their effect is true) then the overall expression is pure (true). Otherwise it is impure.

The type and effect system allows arbitrary boolean expressions to control the purity of function arguments.

For example, it is possible to express a higher-order function  that accepts two function arguments   and   of which at most one is impure:

If  is called with a function argument   which is impure (false) then the second argument must be pure (true). Conversely, if  is pure (true) then   may be pure (true) or impure (false). It is a compile-time error to call  with two impure functions.

The type and effect system can be used to ensure that statement expressions are useful, i.e. that if an expression or function is evaluated and its result is discarded then it must have a side-effect. For example, compiling the program fragment below:

causes a compiler error:

because it is non-sensical to evaluate the pure expression  and then to discard its result. Most likely the programmer wanted to use the result (or alternatively the expression is redundant and could be deleted). Consequently, Flix rejects such programs.

First-class datalog constraints
Flix supports Datalog programs as first-class values. A Datalog program is a logic program that consists of a collection of unordered facts and rules. Together, the facts and rules imply a minimal model, a unique solution to any Datalog program. In Flix, Datalog program values can be passed to and returned from functions, stored in data structures, composed with other Datalog program values, and solved. The solution to a Datalog program (the minimal model) is itself a Datalog program. Thus, it is possible to construct pipelines of Datalog programs where the solution, i.e. "output", of one Datalog program becomes the "input" to another Datalog program.

The following edge facts define a graph:

The following Datalog rules compute the transitive closure of the edge relation:

The minimal model of the facts and rules is:

In Flix, Datalog programs are values. The above program can be embedded in Flix as follows:

The local variable  holds a Datalog program value that consists of the edge facts. Similarly, the local variable  is a Datalog program value that consists of the two rules. The  expression computes the composition (i.e. union) of the two Datalog programs   and. The  expression computes the minimal model of the combined Datalog program, returning the edge and path facts shown above.

Since Datalog programs are first-class values, we can refactor the above program into several functions. For example:

The un-directed closure of the graph can be computed by adding the rule:

We can modify the  function to take a Boolean argument that determines whether we want to compute the directed or un-directed closure:

Type-safe composition
The Flix type system ensures that Datalog program values are well-typed.

For example, the following program fragment does not type check:

because in  the type of the   predicate is   whereas in   it has type. The Flix compiler rejects such programs as ill-typed.

Stratified negation
The Flix compiler ensures that every Datalog program value constructed at run-time is stratified. Stratification is important because it guarantees the existence of a unique minimal model in the presence of negation. Intuitively, a Datalog program is stratified if there is no recursion through negation, i.e. a predicate cannot depend negatively on itself. Given a Datalog program, a cycle detection algorithm can be used to determine if it is stratified.

For example, the following Flix program contains an expression that cannot be stratified:

because the last expression constructs a Datalog program value whose precedence graph contains a negative cycle: the  predicate negatively depends on the   predicate which in turn (positively) depends on the   predicate.

The Flix compiler computes the precedence graph for every Datalog program valued expression and determines its stratification at compile-time. If an expression is not stratified, the program is rejected by the compiler.

The stratification is sound, but conservative. For example, the following program is unfairly rejected:

The type system conservatively assumes that both branches of the if expression can be taken and consequently infers that there may be a negative cycle between the  and   predicates. Thus the program is rejected. This is despite the fact that at run-time the  function always returns a stratified Datalog program value.

Design philosophy
Flix is designed around a collection of stated principles:


 * Everything is an expression. Most Flix constructs, except for top-level declarations, are expressions that evaluate to values.
 * Closed-world assumption. The Flix compiler assumes that the source code of the entire program is available at compile-time.
 * Pure and impure code is separated. The type and effect system precisely captures whether an expression may produce an effect
 * The language has no compile-time warnings, only errors.

The principles also list several programming language features that have been deliberately omitted. In particular, Flix lacks support for:


 * Null values. Instead the use of the Option data type is recommended.
 * Implicit coercions. Instead type conversions must be performed explicitly by the programmer.
 * Reflection. The programmer cannot reflect over the structure of the program at run-time.