In this post, we implement the Monkey-Banana Problem in Prolog — a classic AI planning problem used to demonstrate goal-directed reasoning. The scenario: a monkey is in a room with a banana hanging from the ceiling. There is also a stick and a chair in the room. To reach the banana, the monkey must pick up the stick, move the chair under the banana, climb on the chair, and then hit the banana with the stick. The Prolog program models each required action as an interactive predicate and chains them together to achieve the final goal.
How Does the Prolog Solution Work?
Each step in the plan is modelled as a Prolog predicate (take, move, get_on, hit). Each predicate asks the user a yes/no question. The main goal go chains all four predicates together using Prolog’s conjunction operator (,). If every step is confirmed by the user, the goal succeeds and the monkey reaches the banana. If any step fails, Prolog backtracks to the second go clause and reports failure.
In this post, we implement Inverse Kinematics (IK) for a SCARA robot in Java. While Direct Kinematics computes the end-effector position from known joint angles, Inverse Kinematics does the opposite: given a desired end-effector position (W vector), it computes the joint angles required to reach that position. This is the fundamental calculation needed for robot path planning and motion control.
What is Inverse Kinematics?
For a SCARA robot with 4 joints, the IK solution uses geometric and trigonometric relationships derived from the robot’s DH parameters. The inputs are:
W — End-effector position vector: [wx, wy, wz, w4, w5, w6]
d — Link offsets: [d1, d2, d3, d4]
a — Link lengths: [a1, a2, a3, a4]
The output is the set of joint angles q1, q2, q3, q4 that position the end-effector at the specified W coordinates.
In this post, we implement the Homogeneous Coordinate Transformation Matrix (HCTM) in Java. HCTM is a fundamental concept in robotics and computer graphics for representing rotation and translation of a coordinate frame in 3D space. Using a 4×4 matrix allows both rotation and translation to be combined into a single matrix multiplication, making it efficient to apply multiple transformations in sequence.
What is HCTM?
A Homogeneous Coordinate Transformation Matrix extends a standard 3D rotation matrix to 4×4 by appending a translation vector and a homogeneous row [0 0 0 1]. This program implements three rotations (Yaw around X-axis, Pitch around Y-axis, Roll around Z-axis) and one translation. Given a rotation angle (in degrees) and a 4-element coordinate frame vector, the program computes the transformed frame using matrix–vector multiplication.
In this post, we implement a medical diagnosis expert system in Prolog. The program asks the user a series of yes/no questions about a patient’s symptoms and attempts to diagnose a disease based on the answers. This is a classic demonstration of how Prolog’s pattern matching and backtracking can model the reasoning process of a rule-based expert system.
How Does the Diagnosis Work?
The Prolog program defines a hypothesis predicate that maps a combination of symptoms to a disease. A symptom predicate interactively queries the user for each symptom. If all required symptoms are confirmed, Prolog unifies the disease variable and reports the diagnosis. If no hypothesis can be satisfied, a fallback clause prints a failure message.
In this example, the only disease defined is common cold, which requires the patient to have both a headache and sneezing. You can easily extend this to cover more diseases by adding more hypothesis and symptom clauses.
In this post, we implement Direct Kinematics (Forward Kinematics) for a SCARA robot in Java using the Denavit-Hartenberg (DH) convention. Direct kinematics determines the position and orientation of the robot’s end-effector (tool tip) given the joint angles and link parameters. This is the fundamental calculation in robot arm control and simulation.
What is Direct Kinematics?
For a multi-joint robot arm, each link between joints is described by four DH parameters:
θ (theta) — Joint angle (rotation about Z-axis)
d — Link offset (translation along Z-axis)
a — Link length (translation along X-axis)
α (alpha) — Link twist (rotation about X-axis)
For each link, a 4×4 General Link Coordinate Transformation Matrix (GLCTM) is computed from its DH parameters. The final end-effector pose is obtained by multiplying all the link matrices together: T04 = T01 × T12 × T23 × T34.
In this post, we implement Depth-First Search (DFS) in Java using an adjacency matrix and recursion. DFS is a fundamental graph traversal algorithm that starts at a source node and explores as far as possible along each branch before backtracking. It is used in cycle detection, topological sorting, maze solving, and AI game tree searches.
What is DFS?
DFS explores a graph by diving deep into one branch before exploring others. In this implementation, we use a recursive approach: starting from node 1, we immediately follow the first unvisited neighbour, then the first unvisited neighbour of that node, and so on. Only when we reach a dead end do we backtrack and try the next neighbour.
The graph is represented as an adjacency matrix. An array q[] is used both as the visited set and to record the traversal order. The variable qpos tracks how many nodes have been visited so far.
In this post, we implement a simple calculator in Prolog that performs basic arithmetic operations: addition, subtraction, multiplication, and division. Prolog is a logic programming language widely used in artificial intelligence and symbolic computing. Unlike imperative languages (Java, C), Prolog is declarative — you describe what relationships hold, not how to compute them step by step. The Prolog engine then uses unification and backtracking to derive answers.
What is a Prolog Predicate?
In Prolog, a predicate defines a relationship between its arguments. When you query a predicate, Prolog attempts to unify the query with the predicate’s head and then evaluates the body. For arithmetic, the is operator (or direct unification with =) binds a variable to the result of an expression. In our calculator, ADD(A, B, SUM) :- SUM = A + B. means: “SUM holds if SUM equals A plus B.”