Moggi documentation

Differences to Haskell base

Moggi looks like Haskell and ports the API of Haskell’s base library, but it is a strict language that compiles to three hosts (PHP, the JVM, .NET) — not to native code through the C ABI. This page tells you, as a Haskell programmer, what will surprise you. Everything not mentioned here is meant to behave like base; if you find a divergence that is not listed, that is a bug — please report it.

Two of them show up in the first five minutes of any port, and both are below: evaluation is strict, and String is the host’s own string type rather than [Char]. Most of what follows is a consequence of the first.

New to Moggi? Start with the tour instead; this page assumes you already know Haskell. The language itself is described in the language reference, and host calls in the FFI guide.

Evaluation is strict

This is the one big difference, and most of the rest follows from it. Arguments are evaluated before a call; nothing is deferred to first use — not function arguments, not record fields, not constructor fields.

Strings and characters

String is a host-native string (UTF-8/UTF-16 depending on the backend), not [Char]. Char is a Unicode scalar value. Consequences:

Names that differ

A few names are deliberately different, because Moggi is not a Haskell source-compatibility target and keeps one spelling per operation:

Records

Field access uses projection: person.name, which binds tighter than function application, as . does in the host languages. A record declaration generates no selector function, so two records in one module can freely share field names — the receiver’s type selects the field, and there is no name person spelling. This is what DuplicateRecordFields and OverloadedRecordDot give you there, always on here.

Record update (p { age = 31 }, nested form included) works on record types with a single constructor and is spelled as it is there, without OverloadedRecordUpdate or RebindableSyntax. A multi-constructor type cannot be updated, because which fields the value has is only known at run time; matching on the constructor explicitly is the alternative (see language.md § Records).

Numbers

The Prelude is smaller

The Prelude re-exports the pure core plus the System.IO essentials (IO, FilePath, putChar, putStr, putStrLn, print, getChar, getLine, getContents, interact, readFile, writeFile, appendFile, readIO, readLn, IOError, ioError, userError). Beyond those, handles come from import System.IO, paths from import System.FilePath, and directories from import System.Directory.

Exceptions

The synchronous base-4.22 API is available, including typed handlers, ExceptionContext, annotations, and the masking names. The differences that matter are:

Concurrency

Control.Concurrent and the MVar, Chan, QSem, and QSemN modules are available. forkIO creates an OS thread, not a green thread; forked threads are daemons and do not keep the process alive after main. A blocking foreign call blocks only the thread that makes it.

Calling the host

base reaches the host through the C FFI; Moggi declares host members directly with foreign declarations (see the FFI guide):

Where things live

Haskell’s base splits its definitions across a ghc-internal package behind re-export shims; Moggi does not mirror that tree. Implementations live in the module you import: numbers in Data.Int/Data.Word/Data.Integer/ Data.Double/Data.Num/Data.Real, Show and Read directly in Text.Show/Text.Read. Data.Tuple additionally carries the tuple types base puts in its internal tuple module (Solo, Tuple0–Tuple64, Unit), so there is no separate internal layer to learn.

Because there is one file per module and no build file, each library lives where you import it — there is no separate .cabal package tree to learn.

What is missing

No public module is a placeholder: a module either exists with base’s API or is absent, and the standard library page lists what is absent, so a name you cannot bring into scope is simply not there. A few operations whose host machinery does not exist are still present with their base signatures and report the missing capability when called, as described under Concurrency and Exceptions. What shapes a port is not a missing function but the language: Float has no counterpart (Numbers), IO does not nest, so MonadIO has only its IO instance (see the purity boundary), and the lazy shapes listed above — ZipList, unsafeInterleaveIO, iterate, mfix on lists — cannot be expressed at all. Debug.Trace’s eventlog and marker functions are no-ops, and traceStack prints host frames where base would print profiling annotations.

No language extensions to enable

There are no {-# LANGUAGE … #-} opt-ins for features. Everything the compiler implements — multi-parameter classes and their associated types, higher-kinded types, type-level data, generic programming — is available in every module; no feature is switched on per module or per file. The only LANGUAGE pragma is {-# LANGUAGE NoImplicitPrelude #-}, which drops the implicit Prelude import. A module can also carry a {-# BACKEND … #-} pragma above its header (language.md § Pragmas).

So a program ported here does not need its pragmas translated, and there is no way to disable a feature to get different behaviour: a feature either exists, with one behaviour, or it does not exist at all.

Extensions that are simply on

The list is what has a counterpart, not what exists; each row names the pragma you would write there and the syntax that works here with no pragma at all.

Extension What is available in every module
MultiParamTypeClasses class Convert a b where convert :: a -> b
FlexibleInstances instance C (Pair Int b) — an instance head may be a partially applied type
FlexibleContexts f :: Eq [a] => [a] -> Bool
ConstrainedClassMethods class C a where c :: Eq a => a -> a -> Bool
TypeFamilies associated types (class Collection c where type Element c) and closed families declared on their own (type family Not (x :: Bool) where)
KindSignatures data Tagged (c :: Color) = Tagged
RoleAnnotations type role T phantom representational
DataKinds promoted constructors ('Red), Nat, Symbol
DuplicateRecordFields two records may both declare name
OverloadedRecordDot person.name, chained
OverloadedRecordUpdate p { age = 31 }, nested included
DeriveFunctor, DeriveFoldable, DeriveTraversable deriving (Functor, Foldable, Traversable)
DeriveGeneric deriving (Generic) with import Moggi.Generics
GeneralizedNewtypeDeriving deriving newtype (Num)
DeriveAnyClass deriving anyclass (C)
DerivingVia deriving via (V) C
DerivingStrategies deriving stock (C), deriving newtype (C), deriving anyclass (C), deriving via (V) C
StandaloneDeriving deriving instance (Eq a) => Eq (T a)
GADTs data Expr a where Lit :: Int -> Expr Int; with it, existential constructor variables (MkBox :: a -> Box) and refinement from a constructor’s written result type
BlockArguments when True do …, with no $

The reverse has no analogue: an extension with nothing behind it has no pragma either. GADTs is the one readers look for first, and it is part of the language: data Expr a where … parses, refines and runs like it does in Haskell (data types).

What is not different

These look like deviations when diffing against base, and are not:


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.