MicroHs dialectexperimental
Lennart Augustsson's Haskell compiler: an extended subset of Haskell 2010 translated to SKI combinators and reduced by a small C runtime, so a C compiler is the only build requirement and the whole system fits on a micro-controller.
Overview
MicroHs (also written MicroHaskell) is Lennart Augustsson's compiler for an extended subset of Haskell 2010, translated to combinator graphs rather than native code. It exists so that a functional Haskell toolchain needs only a C compiler to build, and fits on a micro-controller.
Code
-- literals construct; operators consume.
name = "MicroHs"
year = 2023 :: Int
greet = s"Hello ${name}, since ${year + 1}" -- MicroHs string interpolation
half = 3 / 4 :: Double
-- algebraic data types compose; record fields and patterns consume.
data Color = Red | Green | Blue
deriving (Eq, Show)
data Point = Point { px :: Double, py :: Double }
deriving (Show)
shift :: Point -> Point
shift p = p { px = p.px + 1 } -- overloaded record dot and update
-- a type class is the definable unit; an instance supplies its dictionary.
class Describe a where
describe :: a -> String
instance Describe Color where
describe Red = "red"
describe Green = "green"
describe Blue = "blue"
-- dictionary selection consumes the abstraction:
-- the method is an ordinary value, so the higher-order `map` takes it.
allColors :: String
allColors = unwords (map describe [Red, Green, Blue])
-- recursion over patterns is the idiom; the graph reduces on demand.
fac :: Integer -> Integer
fac 0 = 1
fac n = n * fac (n - 1)
evens :: [Int] -> [Int]
evens xs = [x | x <- xs, even x] -- list comprehension, produced lazily
main :: IO ()
main = print (map fac [1 .. 10], evens [1 .. 20])
S, K, and I, so fac reaches the runtime as a graph that the C
evaluator reduces — no lambda survives into the object code.Distinctive traits
- Combinators are the object code: lambdas become
S/K/I, so the runtime is a graph-reduction machine written in C. - Bootstrapping without GHC: the compiler is written in MicroHs itself, and
generated/mhs.clets a C compiler rebuild it from scratch. - The dialect diverges from GHC: string interpolation and overloaded record access and update are MicroHs syntax, not extensions you opt into.
- Heap and stack are fixed sizes chosen by runtime flags, which is what makes the micro-controller targets possible.
- Every supported extension is always on and
LANGUAGEpragmas are ignored, so one fixed dialect compiles every module.
Status
Released and maintained, though the package metadata still declares its
stability experimental. Hackage
carries 0.8 (November 2023) through 0.16 under Apache-2.0, shipping mhs,
cpphs, and mcabal; the repository is already at 0.17, with targets spanning
Unix, Windows, WebAssembly, and micro-controllers.
Sources & references
- Official sitegithub.com
- MicroHs — manual: language, compiler, combinator encoding, runtimegithub.com
- MicroHs — supported extensions and standard-library coverage (wiki)github.com
- The combinator file format — MicroHs runtime object codegithub.com
- MicroHs: A Small Compiler for Haskell — Augustsson, Haskell Symposium 2024dl.acm.org
- CloudMicroHaskell: Direct-Style Distributed Haskell via Runtime Graph Serialisation — uses the MicroHaskell spellingarxiv.org
- MicroHs on Hackage — release history (first upload 0.8, November 2023)hackage.haskell.org
- The Haskell 2010 Language Reporthaskell.org