Learn MERCURY with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
1
Mercury Counter and Theme Toggle
:- module counter.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
main(!IO) :-
Count = 0,
IsDark = no,
updateUI(Count, IsDark, !IO),
Count1 = Count + 1,
updateUI(Count1, IsDark, !IO),
Count2 = Count1 + 1,
updateUI(Count2, IsDark, !IO),
IsDark1 = yes,
updateUI(Count2, IsDark1, !IO),
Count3 = Count2 - 1,
updateUI(Count3, IsDark1, !IO),
Count4 = 0,
updateUI(Count4, IsDark1, !IO).
:- pred updateUI(int::in, bool::in, io::di, io::uo) is det.
updateUI(Count, IsDark, !IO) :-
io.write_string("Counter: "), io.write_int(Count), io.write_string("\n", !IO),
(io.write_string("Theme: Dark\n", !IO) :- IsDark ; io.write_string("Theme: Light\n", !IO)).
Demonstrates a simple counter with theme toggling using Mercury predicates and declarative constructs.
2
Mercury Fibonacci Sequence
:- module fib.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- func fib(int) = int.
fib(N) = (if N =< 1 then N else fib(N-1) + fib(N-2)).
main(!IO) :-
for I = 0 to 9 do io.write_int(fib(I), !IO), io.write_string("\n", !IO).
Generates first 10 Fibonacci numbers using a pure predicate.
3
Mercury Factorial Calculator
:- module factorial.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- func fact(int) = int.
fact(0) = 1.
fact(N) = N * fact(N-1).
main(!IO) :- io.write_int(fact(5), !IO), io.write_string("\n", !IO).
Calculates factorial using a pure function.
4
Mercury Prime Checker
:- module prime.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- func is_prime(int) = bool.
is_prime(N) = (if N < 2 then no else not any([X :- X = 2..N-1, N mod X = 0]) ).
main(!IO) :- io.write_string(if is_prime(13) then "Prime\n" else "Not Prime\n", !IO).
Checks if a number is prime.
5
Mercury Sum of List
:- module sumlist.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- func sum(list(int)) = int.
sum([]) = 0.
sum([H|T]) = H + sum(T).
main(!IO) :- io.write_int(sum([1,2,3,4,5]), !IO), io.write_string("\n", !IO).
Calculates the sum of a list using a fold.
6
Mercury Reverse String
:- module revstring.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- func reverse(list(T)) = list(T).
reverse([]) = [].
reverse([H|T]) = reverse(T) ++ [H].
main(!IO) :-
L = ['H','E','L','L','O'],
R = reverse(L),
io.write_string(R, !IO), io.write_string("\n", !IO).
Reverses a string represented as a list of characters.
7
Mercury Multiplication Table
:- module multable.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
main(!IO) :-
N = 5,
for I = 1 to 10 do
io.write_int(N, !IO), io.write_string(" x ", !IO), io.write_int(I, !IO), io.write_string(" = ", !IO), io.write_int(N*I, !IO), io.write_string("\n", !IO).
Prints multiplication table for a number.
8
Mercury Celsius to Fahrenheit
:- module tempconv.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
main(!IO) :-
C = 25.0,
F = C * 9.0/5.0 + 32.0,
io.write_float(F, !IO), io.write_string("\n", !IO).
Converts Celsius to Fahrenheit.
9
Mercury Simple Alarm Simulation
:- module alarm.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
main(!IO) :-
Temp = 80,
Thresh = 75,
io.write_string(if Temp > Thresh then "Alarm: Temperature Too High!\n" else "Temperature Normal\n", !IO).
Simulates an alarm if threshold exceeded.
10
Mercury Random Walk Simulation
:- module randwalk.
:- interface.
:- import_module io, random.
:- pred main(io::di, io::uo) is det.
:- implementation.
main(!IO) :-
Steps = 10,
Pos = 0,
for I = 1 to Steps do (
Pos = Pos + (if random(0,1) = 0 then -1 else 1),
io.write_int(Pos, !IO), io.write_string("\n", !IO)
).
Simulates a 1D random walk.