Idris combines a number of features from relatively mainstream functional programming languages with features borrowed from proof assistants.
Functional programming
The syntax of Idris shows many similarities with that of Haskell. A hello world program in Idris might look like this: module Main main : IO main = putStrLn "Hello, World!"
Idris supports inductively-defined data types and parametric polymorphism. Such types can be defined both in traditional "Haskell98"-like syntax: data Tree a = Node | Leaf a
or in the more general GADT-like syntax: data Tree : Type -> Type where Node : Tree a -> Tree a -> Tree a Leaf : a -> Tree a
With dependent types, it is possible for values to appear in the types; in effect, any value-level computation can be performed during typechecking. The following defines a type of lists whose lengths are known before the program runs, traditionally called vectors: data Vect : Nat -> Type -> Type where Nil : Vect 0 a : -> -> Vect a
This type can be used as follows: total append : Vect n a -> Vect m a -> Vect a append Nil ys = ys append ys = x :: append xs ys
The functions append a vector of m elements of type a to a vector of n elements of type a. Since the precise types of the input vectors depend on a value, it is possible to be certain at compile-time that the resulting vector will have exactly elements of type a. The word "total" invokes the totality checker which will report an error if the function doesn't cover all possible cases or cannot be proven not to enter an infinite loop. Another common example is pairwise addition of two vectors that are parameterized over their length: total pairAdd : Num a => Vect n a -> Vect n a -> Vect n a pairAdd Nil Nil = Nil pairAdd = x + y :: pairAdd xs ys
Num a signifies that the type a belongs to the type class Num. Note that this function still typechecks successfully as total, even though there is no case matching Nil in one vector and a number in the other. Since both vectors are ensured by the type system to have exactly the same length, we can be sure at compile time that this case will not occur. Hence it does not need to be mentioned for the function to be total.
Dependent types are powerful enough to encode most properties of programs, and an Idris program can prove invariants at compile-time. This makes Idris into a proof assistant. There are two standard ways of interacting with proof assistants: by writing a series of tactic invocations, or by interactively elaborating a proof term. Idris supports both modes of interaction, although the set of available tactics is not yet as useful as that of Coq.
Code generation
Because Idris contains a proof assistant, Idris programs can be written to pass proofs around. If treated naïvely, such proofs remain around at runtime. Idris aims to avoid this pitfall by aggressively erasing unused terms. By default, Idris generates native code through C. The other officially supported backend generates JavaScript.