Flix (programming language)

This is an old revision of this page, as edited by JorKadeen (talk | contribs) at 08:30, 30 August 2020 (Hello World). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Flix is a functional, imperative, and logic programming language developed at Aarhus University with funding from the Independent Research Fund Denmark[2] and by a community of open source contributors. Flix supports algebraic data types, pattern matching, parametric polymorphism, currying, higher-order functions, extensible records[3], channel and process-based concurrency, and tail call elimination.

Flix
ParadigmMulti-paradigm: functional, imperative, logic
DeveloperAarhus University, open-source contributors
First appeared10 August 2015; 10 years ago (2015-08-10)
Typing disciplineinferred, static, strong
PlatformJVM
LicenseApache License 2.0[1]
Filename extensions.flix
Websiteflix.dev
Influenced by
F#, Go, Haskell, OCaml, Scala

Flix supports Datalog constraints as first-class values that can be passed around, compose with other constraints, and solved.

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. Higher-order functions can enforce that they are given pure function arguments or alternatively they can be effect polymorphic in the effect of their arguments. The effect inference algorithm is based on Boolean unification.

Language & Compiler

Flix programs compile to JVM bytecode. The Flix compiler is a whole-program compiler that performs monomorphization to eliminate polymorphism while avoiding boxing and tree shaking to remove unused code.

Examples

Hello World

The following program prints Hello World when compiled and executed:

def main(): Unit & Impure = 
    Console.printLine("Hello World")

The type and effect signature of the main function specifies that it takes no parameters, returns a value of type Unit, and that the function is impure. The main is impure because it invokes the printLine which causes an effect.

Algebraic Data Types and Pattern Matching

/// An algebraic data type for shapes.
enum Shape {
    case Circle(Int),        // circle radius
    case Square(Int),        // side length
    case Rectangle(Int, Int) // height and width
}


/// Computes the area of the given shape using pattern matching and basic arithmetic.
def area(s: Shape): Int = match s {
    case Circle(r)       => 3 * (r * r)
    case Square(w)       => w * w
    case Rectangle(h, w) => h * w
}

Higher-Order Functions

/// Returns the sum of x and y.
def add(x: Int, y: Int): Int = x + y

/// Returns x plus one.
def inc(x: Int): Int = add(x, 1)

/// Returns a function that applies f twice.
def twice(f: Int -> Int): Int -> Int = x -> f(f(x))

/// Returns x plus two.
def two(x: Int): Int = twice(inc)(x)

/// Returns 123 plus 4 = 127.
def main(): Int = twice(two)(123)

Extensible Records

...

Notable Features

Polymorphic Effects

...

First-class Datalog Constraints

...

Principles

The Flix language design includes a collection of stated principles that shape the language[4]. These include:

  • Everything is an expression
  • Separation of pure and impure code
  • Closed-world assumption

The principles also list features that have been deliberately omitted due to their perceived flaws:

  • No null values
  • No implicit coercions
  • No reflection
  • No compiler warnings, only compiler errors
  • No dead or unreachable code
  • No variable shadowing

References

  1. ^ "Apache License 2.0" – via GitHub.
  2. ^ "Forskningsprojekter". Danmarks Frie Forskningsfond (in Danish).
  3. ^ Leijen, Daan. "Extensible records with scoped labels". Trends in Functional Programming.
  4. ^ "The Flix Programming Language - Principles". flix.dev. Retrieved 28 August 2020.

Category:Functional languages