Flix (programming language)
≈ 12 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
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.…
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. def main(): Unit \ IO = println("Hello World!") The type and effect signature of the main function specifies that it has no parameters, returns a value of type Unit , and that the function has the IO effect, i.e. is impure. The main function is impure because it invokes printLine which is impure. enum Shape { case Circle(Int32), // has circle radius case Square(Int32), // has side length case Rectangle(Int32, Int32) // has height and width } The ADT has three constructors: Circle , Square , and Rectangle . The following program fragment uses pattern matching to destruct a Shape value: def area(s: Shape): Int32 = match s { case Circle(r) => 3 * (r * r) case Square(w) => w * w case Rectangle(h, w) => h * w } def twice(f: Int32 -> Int32): Int32 -> Int32 = x -> f(f(x)) We can use the function twice as follows: twice(x -> x + 1)(0) Here the call to twice(x -> x + 1) returns a function that will increment its argument two times. Thus the result of the whole expression is 0 + 1 + 1 = 2 . def map(f: a -> b, l: List[a]): List[b] = match l { case Nil => Nil case x :: xs => f(x) :: map(f, xs) } The map function recursively traverses the list l and applies f to each element constructing a new list. Flix supports type parameter elision hence it is not required that the type parameters a and b are explicitly introduced. def point2d(): {x: Int32, y: Int32} = {x = 1, y = 2} Flix uses row polymorphism to type records. The sum function below takes a record that has x and y fields (and possibly other fields) and returns the sum of the two fields: def sum(r: {x: Int32, y: Int32 | rest}): Int = r.x + r.y The following are all valid calls to the sum function: sum({x = 1, y = 2}) sum({y = 2, x = 1}) sum({x = 1, y = 2, z = 3}) 1 + 2 : Int32 \ {} whereas the following expression has the IO effect, i.e. is impure: println("Hello World") : Unit \ IO 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 Set.exists requires that its function argument f is pure: // The syntax a -> Bool is short-hand for a -> Bool \ {} def exists(f: a -> Bool, xs: Set[a]): Bool = ... The requirement that f must be pure ensures that implementation details do not leak. For example, since f is pure it cannot be used to determine in what order the elements of the set are traversed. If f 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 List.foreach requires that its function argument f is impure: def foreach(f: a -> Unit \ IO, xs: List[a]): Unit \ IO The requirement that f must be impure ensures that the code makes sense: It would be meaningless to call List.foreach with a pure function since it always returns Unit . 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: if (1 == 2) println("Hello World!") else () 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 List.map is effect polymorphic: def map(f: a -> b \ e, xs: List[a]): List[b] \ e The List.map function takes a function f from elements of type a to b with effect e . The effect of the map function is itself e . Consequently, if List.map 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: def >>(f: a -> b \ e1, g: b -> c \ e2): a -> c \ e1 + e2 = x -> g(f(x)) The type and effect signature can be understood as follows: The >> function takes two function arguments: f with effect e1 and g with effect e2 . The effect of >> is effect polymorphic in the conjunction of e1 and e2 . If both are pure then the overall expression is pure. The type and effect system allows arbitrary set expressions to control the purity of function arguments. For example, it is possible to express a higher-order function h that accepts two function arguments f and g where the effects of f are disjoint from those of g : def h(f: a -> b \ e1, g: b -> c \ e2 - e1): Unit If h is called with a function argument f which has the IO effect then g cannot have the IO effect. 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: def main(): Unit \ IO = List.map(x -> 2 * x, 1 :: 2 :: Nil); println("Hello World") causes a compiler error: -- Redundancy Error -------------------------------------------------- >> Useless expression: It has no side-effect(s) and its result is discarded. 2 | List.map(x -> 2 * x, 1 :: 2 :: Nil); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ useless expression. because it is non-sensical to evaluate the pure expression List.map(x -> 2 * x, 1 :: 2 :: Nil) 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. Edge(1, 2). Edge(2, 3). Edge(3, 4). The following Datalog rules compute the transitive closure of the edge relation: Path(x, y) :- Edge(x, y). Path(x, z) :- Path(x, y), Edge(y, z). The minimal model of the facts and rules is: Edge(1, 2). Edge(2, 3). Edge(3, 4). Path(1, 2). Path(2, 3). Path(3, 4). Path(1, 3). Path(1, 4). Path(2, 4). In Flix, Datalog programs are values. The above program can be embedded in Flix as follows: def main(): #{Edge(Int32, Int32), Path(Int32, Int32)} = let f = #{ Edge(1, 2). Edge(2, 3). Edge(3, 4). }; let p = #{ Path(x, y) :- Edge(x, y). Path(x, z) :- Path(x, y), Edge(y, z). }; solve f p The local variable f holds a Datalog program value that consists of the edge facts. Similarly, the local variable p is a Datalog program value that consists of the two rules. The f p expression computes the composition (i.e. union) of the two Datalog programs f and p . The solve 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: def edges(): #{Edge(Int32, Int32), Path(Int32, Int32)} = #{ Edge(1, 2). Edge(2, 3). Edge(3, 4). } def closure(): #{Edge(Int32, Int32), Path(Int32, Int32)} = #{ Path(x, y) :- Edge(x, y). Path(x, z) :- Path(x, y), Edge(y, z). } def run(): #{Edge(Int32, Int32), Path(Int32, Int32)} = solve edges() closure() The un-directed closure of the graph can be computed by adding the rule: Path(x, y) :- Path(y, x). We can modify the closure function to take a Boolean argument that determines whether we want to compute the directed or un-directed closure: def closure(directed: Bool): #{Edge(Int32, Int32), Path(Int32, Int32)} = let p1 = #{ Path(x, y) :- Edge(x, y). Path(x, z) :- Path(x, y), Edge(y, z). }; let p2 = #{ Path(y, x) :- Path(x, y). }; if (directed) p1 else (p1 p2) let p1 = Edge(123, 456).; let p2 = Edge("a", "b").; p1 p2; because in p1 the type of the Edge predicate is Edge(Int32, Int32) whereas in p2 it has type Edge(String, String) . The Flix compiler rejects such programs as ill-typed. def main(): #{Male(String), Husband(String), Bachelor(String)} = let p1 = Husband(x) :- Male(x), not Bachelor(x).; let p2 = Bachelor(x) :- Male(x), not Husband(x).; p1 p2 // illegal, not stratified. because the last expression constructs a Datalog program value whose precedence graph contains a negative cycle: the Bachelor predicate negatively depends on the Husband predicate which in turn (positively) depends on the Bachelor 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: def main(): #{A(Int32), B(Int32)} = if (true) A(x) :- A(x), not B(x). else B(x) :- B(x), not A(x). 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 A and B predicates. Thus the program is rejected. This is despite the fact that at run-time the main function always returns a stratified Datalog program value.
Text derived from https://en.wikipedia.org/w/index.php?oldid=1351602194 under CC BY-SA 4.0; modifications have been made.