Learn Mercury - 10 Code Examples & CST Typing Practice Test
Mercury is a purely declarative logic programming language with strong typing, determinism analysis, and a focus on reliability and performance. It is designed for building large-scale, maintainable, and efficient logic programs while avoiding common pitfalls of traditional Prolog systems.
View all 10 Mercury code examples →
Learn MERCURY with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Mercury
What is Mercury?
Mercury is a purely declarative logic programming language with strong typing, determinism analysis, and a focus on reliability and performance. It is designed for building large-scale, maintainable, and efficient logic programs while avoiding common pitfalls of traditional Prolog systems.
What are the primary use cases for Mercury?
Logic-based and symbolic programming. Constraint solving. Knowledge representation. Formal verification and theorem proving. Academic research and teaching
What are the strengths of Mercury?
Eliminates many runtime errors via type and mode checking. Predictable declarative behavior. Efficient execution through optimized compilation. Highly maintainable large logic programs. Facilitates reasoning about program correctness
What are the limitations of Mercury?
Smaller ecosystem and community. Steeper learning curve than Prolog for beginners. Limited libraries for modern software development. Not suitable for mainstream web or mobile apps. Requires strict adherence to modes and types
How can I practice Mercury typing speed?
CodeSpeedTest offers 10+ real Mercury code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.