Moggi documentation

The type system

Moggi is statically typed. Every expression has a type, and the compiler checks the whole program before any of it runs: a program that compiles has no unmatched case, no missing instance, no arity mistake and no kind mistake left to discover at run time. The check is also what makes the language’s guarantees usable — you can change a type and let the compiler point at every place that still assumes the old one.

The type system is small enough to learn in a sitting, but it is layered. The pages below follow the layers, each one using only what the pages before it introduced.

Page Question it answers You meet it when
Data types and pattern matching How do I describe a shape of data, and how do I take it apart? Your first data declaration
Polymorphism and inference When do I write a type, and what does the compiler work out? Your first function used at two types
Type classes How does one function work for many types, and how are constraints checked? Your first class/instance
Deriving How do I get show, ==, map and friends without writing them? Your first deriving clause
Kinds What is the “type of a type”? A type error about Type -> Type
Higher-kinded types How can a class range over containers rather than elements? Writing a class over f a
Type-level data (DataKinds) How can a value’s type record a number or a tag? A phantom parameter such as Proxy 'Red
Generic programming How do I write one function over the shape of any data type? Deriving Generic

What the check covers

What it deliberately does not do

There is no run-time type test, no reflection over a value’s type, and no way to branch on a type. A function polymorphic in a cannot ask what a is; it can only use the operations its constraints name. That is a guarantee, not a gap: it is why a polymorphic function behaves the same at every type.

What is not implemented

A few things a reader may look for are absent, not hidden. A type constructor may not compute over its indices: a GADT refines one, but nothing evaluates on it (data types). Families are only class-associated (classes), and kinds are not polymorphic (kinds). A signature cannot require a polymorphic argument, and instance selection has no overlap pragmas, only specificity (polymorphism, classes).

Conventions used in these pages


Moggi compiles to PHP, the JVM and .NET. This site is generated from docs/ in the moggi-lang/moggi repository; the same pages are readable there as Markdown.