A Jupyter kernel for executing MeTTa code using PeTTa.
- Execute MeTTa code in Jupyter notebooks
- Clean output formatting
- Graceful error handling with readable error messages
- State persistence across notebook cells
- Support for syntax errors, type errors, and runtime errors
- Working directory control via
%cdmagic (enables relativeimport!paths)
This kernel requires PeTTa to be installed separately. PeTTa provides the core MeTTa execution engine.
Install PeTTa first:
git clone https://github.com/patham9/PeTTa.git
cd PeTTa
# Follow PeTTa installation instructions- Python 3.8+
- SWI-Prolog >= 9.3.x
- PeTTa (installed separately - see Prerequisites above)
- janus-swi (Python package)
- ipykernel (Python package)
- jupyterlab or jupyter notebook
It's recommended to install the Jupyter kernel in a Python virtual environment. You can use any virtual environment tool (venv, virtualenv, conda, etc.).
Example using venv:
# Create a virtual environment
python3 -m venv ~/jupyter-env
# Activate it
source ~/jupyter-env/bin/activate # On Windows: ~/jupyter-env/Scripts/activateThe kernel needs to know where PeTTa is installed. Set the PETTA_PATH environment variable:
export PETTA_PATH=/path/to/PeTTaTo make this permanent, add it to your shell configuration file (~/.bashrc, ~/.zshrc, etc.):
echo 'export PETTA_PATH=/path/to/PeTTa' >> ~/.bashrc
source ~/.bashrccd /path/to/jupyter-petta-kernel
./install.shThe install script will:
- Check if you're in a virtual environment (recommended)
- Install the kernel package
- Check for JupyterLab and offer to install it if needed
- Register the kernel with Jupyter
# Install the package
export PETTA_PATH=/path/to/PeTTa
pip install -e /path/to/jupyter-petta-kernel
# Register the kernel
jupyter kernelspec install --user /path/to/jupyter-petta-kernel/resources --name pettaRun the verification script to check all requirements:
cd /path/to/jupyter-petta-kernel
python3 verify_kernel.pyThis will check:
- PETTA_PATH environment variable is set
- SWI-Prolog is installed and in PATH
- janus-swi Python package is installed
- petta_jupyter kernel can import successfully
- Kernel is registered with Jupyter
Alternatively, check manually:
jupyter kernelspec listYou should see petta in the list.
-
Launch JupyterLab or Jupyter Notebook:
jupyter lab # or jupyter notebook -
Create a new notebook and select "PeTTa (MeTTa)" as the kernel
-
Write MeTTa code in cells and execute them:
!(+ 1 2)
Output:
3 -
Define functions:
(= (factorial 0) 1) (= (factorial $n) (* $n (factorial (- $n 1))))
-
Use defined functions:
!(factorial 5)
Output:
120
The kernel supports a %cd magic command for controlling the working directory. This is particularly important when using import! with relative file paths.
%cd /path/to/metta-examples/aunt-kg
After this, relative imports resolve against the new directory:
!(import! &self toy.metta)
!(match &self (parent $x Bob) $x)Relative paths work too:
%cd ../collatz
%cd
Output: /path/to/current/directory
Note: The kernel's working directory defaults to wherever
jupyter labwas launched. If your notebook usesimport!with relative paths, either launch JupyterLab from the same directory as your.mettafiles, or use%cdat the top of the notebook to set it explicitly.
!(+ 1 2) # Returns: 3
!(* 3 4) # Returns: 12
!(- 10 5) # Returns: 5(= (double $x) (* $x 2))
!(double 5) # Returns: 10!(+ 1 # Error: Parse error: missing ')'
!(+ abc def) # Error: Type error: Expected evaluable, got def/0- kernel.py: Main kernel implementation, handles code execution
- output_formatter.py: Formats results and error messages
- resources/kernel.json: Kernel specification for Jupyter
- Uses temporary files to pass code to PeTTa (avoids Prolog string escaping issues)
- Integrates with PeTTa's Python API via janus_swi
- Extracts clean error messages from Prolog error terms
- Displays errors in red (stderr) and normal output in black (stdout)
Run the verification script to identify issues:
cd /path/to/jupyter-petta-kernel
python3 verify_kernel.pyMake sure the PETTA_PATH environment variable is set and persists across sessions:
export PETTA_PATH=/path/to/PeTTaAdd it to your shell configuration file (~/.bashrc, ~/.zshrc, etc.) to make it permanent, then restart your terminal and JupyterLab.
If you see errors about missing SWI-Prolog libraries, this means janus-swi was compiled against a different SWI-Prolog installation than the one currently in your PATH.
Fix by reinstalling janus-swi:
# Ensure SWI-Prolog is in your PATH first
which swipl # Should show the path to your SWI-Prolog installation
# Reinstall janus-swi
pip uninstall janus-swi -y
pip install janus-swi --no-cache-dirImportant: Make sure SWI-Prolog remains in your PATH when you start JupyterLab. Add the appropriate export to your shell configuration file.
Make sure janus-swi is installed:
pip install janus-swiRe-register the kernel:
jupyter kernelspec install --user /path/to/jupyter-petta-kernel/resources --replaceTo modify the kernel:
- Edit the source files in
python/petta_jupyter/ - Reinstall:
pip install -e . - Restart the kernel in your notebook
[Add appropriate license information]