Algorithms

From a factorial to an expert system

Axioma is a Turing-complete general-purpose language — and a symbolic-AI one, where rules and inference are native. Here is the algorithm canon, from sorting and search to dynamic programming and a forward-chaining expert system. Every example runs right here in the page.

▶ Edit any example and press Run — or /Ctrl+Enter. Same WebAssembly interpreter as the playground; nothing leaves your browser. interpreter: loading…
FUNDAMENTALS

Fundamentals

The everyday canon — recursion, a sort, a search, primes, the functional triad, a little set theory. Edit and run any of them; this is the same WebAssembly interpreter as the playground.

Factorial

Recursion — base case plus recursive step.

factorial.axOpen in Playground ↗

Arbitrary precision

No int64 ceiling: integers promote to arbitrary precision automatically when a result overflows the machine word, so the discrete-math canon — factorials, binomials, powers — stays exact. 100! is 158 digits; 2^256 is the size of the 256-bit keyspace.

bignum.axOpen in Playground ↗

Euclid's GCD

The oldest algorithm still in daily use (c. 300 BCE).

Quicksort

Divide and conquer with set-builder partitions around a pivot.

quicksort.axOpen in Playground ↗

Binary search

Halve the interval each step — O(log n) over a sorted array.

bsearch.axOpen in Playground ↗

Primes

Trial division as a set comprehension — prime iff it has no divisor.

primes.axOpen in Playground ↗

Map / filter / reduce

The functional triad — sum the squares of the evens.

mapreduce.axOpen in Playground ↗

FizzBuzz

The classic warm-up — multiples of 3, 5 and 15.

fizzbuzz.axOpen in Playground ↗

Palindrome

Fold a string's characters in reverse, then compare.

palindrome.axOpen in Playground ↗

Powerset

Set theory is first-class — every subset of {1, 2, 3}.

powerset.axOpen in Playground ↗
SEARCH & GRAPH

Search & graph

Traversal and ordering over graphs — including the Datalog one-liner where a single recursive rule computes the entire transitive closure.

Reachability (Datalog)

A recursive Horn rule computes a graph's whole transitive closure.

reachability.axOpen in Playground ↗

Depth-first search

Recurse into each neighbour, tracking a visited set.

Breadth-first search

A hand-rolled queue (array + head index) explores level by level.

Dijkstra's shortest path

Weighted shortest paths from a source over a small graph.

dijkstra.axOpen in Playground ↗

Topological sort

DFS post-order, reversed — a valid dependency ordering.

toposort.axOpen in Playground ↗
RECURSION & BACKTRACKING

Recursion & backtracking

Problems solved by trying and undoing — the shape of classical search.

Towers of Hanoi

Relocate a stack of disks, three pegs, recursively.

hanoi.axOpen in Playground ↗

N-Queens

Backtracking: count non-attacking placements on a 6×6 board.

nqueens.axOpen in Playground ↗

Permutations

Every ordering of a list, built recursively.

perms.axOpen in Playground ↗
DYNAMIC PROGRAMMING

Dynamic programming

Overlapping subproblems solved once and cached — by array tabulation, or a dict memo keyed by the subproblem's state.

Fibonacci (memoized)

Top-down memoization — a dict caches each result, so every value is computed once.

Edit distance

Top-down, with a dict memo keyed by a compound "i,j" state — the idiom a flat array can't express directly.

editdist.axOpen in Playground ↗

Longest common subsequence

The longest order-preserving shared subsequence (length).

0/1 Knapsack

Maximum value within a weight budget, each item at most once.

knapsack.axOpen in Playground ↗

Coin change

Fewest coins that make an amount, from given denominations.

coinchange.axOpen in Playground ↗
SYMBOLIC AI

Symbolic AI

Axioma's home ground: facts, rules, inference and defeasible reasoning — the classic GOFAI toolkit expressed directly. (Axioma is Datalog-flat, so these are forward-chaining and query-driven, not backtracking search.)

Forward-chaining expert system

Facts plus strict rules chain to a classification; the result is a theorem.

expert.axOpen in Playground ↗

Family-tree inference

Parent facts; recursive ancestor, grandparent and sibling rules.

family.axOpen in Playground ↗

Unification by join

A shared variable bound across two relations does what unification does.

Defeasible reasoning

A strict law (theorem) survives; a default (conjecture) can be cancelled.

defeasible.axOpen in Playground ↗

Rule-based NLP

Ordered morphological rules turn a noun into its English plural.

pluralize.axOpen in Playground ↗

State-space reachability

States and moves as facts; a recursive rule finds every reachable state.

planner.axOpen in Playground ↗
PROBLEM SOLVING

Problem solving

The expert systems above are forward-chaining — they push facts to conclusions. Axioma also ships three solver blocks that work the other way: state a problem, and the engine produces the answer it was never given, then checks its own work. Each one narrates its reasoning in the four phases of George Pólya's How to Solve It.

A problem to find

Pólya's worked example: a rectangular box with edges 3, 4, 12 — how long is its diagonal? Give the unknown a domain and a condition; the engine searches, finds d = 13, and re-verifies it. The answer appears nowhere in the source.

polya_find.axOpen in Playground ↗

A problem to prove

Switch the directive to prove and the engine derives the goal from rules, places it on the grounding ladder, and reports the proof chain. A strict derivation reaches theorem (proved); a defeasible one would only reach conjecture (plausible).

polya_prove.axOpen in Playground ↗

Means-ends planning (Newell & Simon's GPS)

The 1957 General Problem Solver, made executable. State the world, the goal, and operators with pre / add / del lists; solver/gps reduces the difference by recursively achieving preconditions — backward, goal-directed search. The classic monkey-and-bananas puzzle, solved in four steps.

gps_monkey.axOpen in Playground ↗

The solver as a function

The same engine is also an ordinary builtin — polya_solve(spec) — so a problem can be assembled from runtime data and composed into your own code. Here, a reusable box-diagonal solver built at runtime.

polya_solve.axOpen in Playground ↗

Turing-complete, and then some.

Recursion, loops, search, dynamic programming, a full inference engine and a Pólya/GPS problem solver — and every block above is editable and one click from the playground. The philosophical side lives next door in Symbolization.