Moggi documentation

Polymorphism and inference

A polymorphic function works at more than one type. Moggi infers that generality from the function’s body: you write id x = x and the compiler gives id the type a -> a, where a stands for any type. Nothing about id changes between uses — there is one compiled function, and it is called with different values.

id' :: a -> a
id' x = x

const' :: a -> b -> a
const' x _ = x

twice :: (a -> a) -> a -> a
twice f x = f (f x)

main :: IO ()
main = do
  putStrLn (show (twice (\n -> n + 1) (1 :: Int)))
  putStrLn (twice (\s -> s <> "!") "hi")
  putStrLn (show (const' (1 :: Int) "unused"))
3
hi!!
1

The type variables in a signature are implicitly quantified: a -> a means “for every type a”. A type argument is never written at a call site either; the compiler instantiates the signature from the context.

Because a can be anything, the body of id' cannot inspect it. The only operations available on a value of type a are the ones the signature’s constraints name. That restriction is what makes a polymorphic function behave identically at every type.

Signatures are optional and checked

A definition without a signature is inferred. With a signature, the body is checked against it — a mismatch is an error, never a silent reinterpretation.

Write signatures for exported functions, for recursive functions, and for anything whose type you had to think about. They document the interface, they catch a change that would otherwise generalise quietly, and they make error messages point at the signature instead of at a distant use.

Inference works from use sites

The compiler unifies the types of a definition’s body with the types its uses demand. Two consequences are worth knowing:

A use that cannot be satisfied is reported at the definition that introduced the constraint:

n = 1

b :: String
b = n
Main.mog:1:1: type error: unresolved type class constraint: `Num String`

  1 | n = 1
    | ^

Local bindings are polymorphic

A let or where binding with parameters is generalised like a top-level function, so one helper can be used at several types:

pair = let f x = x in (f 1, f True)

main :: IO ()
main = putStrLn (show (fst pair))
1

f is used at Int and at Bool; it is one definition, instantiated twice.

Advanced

The rest of this page is about the corners: how a value binding differs from a function, what defaulting picks when nothing else does, and how to pin a type. On a first reading, take away that signatures are optional but checked, and that a function without one is generalised from its body.

Value bindings and the monomorphism restriction

A binding with no parameters and no signature — count = 1 + 1, idAlias = \x -> x, empty = [] — is a pattern binding; only a binding with parameters is a function. Moggi applies a monomorphism restriction to pattern bindings: unlike a constrained function, such a binding is not given the dictionary parameters of its constraints, so give it a signature when its type matters.

count = 1 + 1

asInt :: Int
asInt = count

main :: IO ()
main = putStrLn (show asInt)
2

What you can observe:

Do not use a pattern binding to share one polymorphic value across several types: write a function (idAlias x = x) or put a signature on the value.

Defaulting

A numeric literal is polymorphic until a use pins it. When nothing does, the compiler picks a default: Int first, then Double for a fractional operation.

main :: IO ()
main = do
  putStrLn (show (1 + 2))        -- Int
  putStrLn (show (1 / 2))        -- Double
3
0.5

show renders each expression at its defaulted type, and putStrLn adds the newline. Adding a signature removes the default: putStrLn (show (1 :: Integer)) renders one as an arbitrary-precision value. (print, from System.IO, is Show a => a -> IO () and writes without a newline.)

Ascribing a type

(expression :: Type) fixes an expression’s type at that point:

main :: IO ()
main = putStrLn (show ((1 :: Int) + 2))
3

Ascription is how you settle a literal, a mempty, or a value read from a polymorphic function. It is also the tool for an ambiguous constraint: the error says which class has no instance, and the fix is usually a signature that names the type.

Explicit forall

A signature may name the variables it quantifies. forall a. t binds a in t, and binders may be listed together:

idPoly :: forall a. a -> a
idPoly x = x

constFirst :: forall a b. a -> b -> a
constFirst x _ = x

At the top level the binder changes nothing: a -> a and forall a. a -> a are the same type and print the same way, and variables the signature leaves free are still generalised. What a binder buys is rank. Under an arrow, a forall is a promise the caller has to keep:

applyPoly :: (forall a. a -> a) -> Int -> Int
applyPoly f n = f n

applyPoly needs an argument that works for every a — idPoly or a lambda does, a monomorphic Int -> Int does not. Rank 3 nests the same way, ((forall a. a -> a) -> Int) -> Int. Inference stays rank 1: a binder you do not write is not invented, so a higher-rank argument needs its signature.

A binder is in scope in the definition’s body as well as its signature, so an ascription may name it: f :: forall a. a -> a with f x = (x :: a) is accepted, and a body that ascribes the wrong binder is rejected rather than generalised. Nothing escapes into other definitions.

Superclasses may be quantified, which is how Data.Bifunctor states that a bifunctor is a functor in each parameter:

class (forall a. Functor (p a)) => Bifunctor (p :: Type -> Type -> Type) where

Bifunctor p then discharges Functor (p a) for any a. Type itself comes from Data.Kind.

Limits

Where next


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.