Skip to content

Project Architecture Overview

Douglas R. Miles edited this page Oct 1, 2024 · 11 revisions

The Metta language processing system incorporates the Atomspace blackboard as its central knowledge representation and storage component. Atomspace, associated with OpenCog, is a powerful framework for storing symbolic structures—atoms—that represent concepts, code, or data. This integration allows for a flexible and semantic representation of knowledge, code, and logic within the system, making it more collaborative and decentralized.

MeTTa Language System Architecture

Below is a detailed description of the MeTTa language system architecture, organized into major components and subcomponents. Each file is linked for easy navigation.


1. Front-End

Purpose: Handles the initial processing of MeTTa source code, including parsing and syntactic analysis, converting code into internal representations for further processing.

  • Parser
    • Files:
      • metta_parser.pl: Parses MeTTa source code into abstract syntax trees (ASTs) or other internal representations.
      • metta_convert.pl: Converts parsed structures into formats suitable for evaluation or compilation.
    • Functionality:
      • Lexical analysis (if included within the parser).
      • Syntax checking and validation.
      • Generation of ASTs.

2. Core Libraries and Standard Definitions

Purpose: Provides foundational functions, definitions, and constructs essential for MeTTa programs and logical reasoning.


3. Interpreter and Compiler

Purpose: Translates MeTTa code into executable form and executes it, either by compiling or interpreting.

  • Compiler
    • Files:
    • Functionality:
      • Code optimization.
      • Bytecode or machine code generation (if applicable).
  • Interpreter
    • Files:
      • metta_interp.pl: Main interpreter integrating parsing and evaluation for executing MeTTa code.
      • metta_eval.pl: Evaluates the internal representations of MeTTa code.
    • Functionality:
      • Direct execution of MeTTa code.
      • Managing the execution context and environment.

4. Runtime Environment

Purpose: Manages the execution context, including variable scopes, data storage, and runtime utilities.

  • Execution Space
  • Substitution and Pattern Matching
    • metta_subst.pl: Handles variable substitution and pattern matching operations.
  • Type System
    • metta_types.pl: Implements the type system and type-checking mechanisms.
  • Persistence
  • Concurrency

5. REPL (Read-Eval-Print Loop)

Purpose: Facilitates interactive execution of MeTTa code, allowing users to input commands and receive immediate feedback.

  • REPL Implementation
    • metta_repl.pl: Implements the interactive shell environment in Prolog.
  • Default REPL Script

6. Debugging and Testing

Purpose: Provides tools for developers to debug and test MeTTa programs, ensuring reliability and correctness.

  • Debugging Tools
    • metta_debug.pl: Utilities for tracing execution, inspecting variables, and logging.
  • Testing Framework
    • metta_testing.pl: Contains test cases and frameworks for validating language features.

7. Ontology and Logic Programming Integration

Purpose: Enhances MeTTa with logic programming capabilities, allowing for advanced reasoning and knowledge representation.


8. Python Integration

Purpose: Enables interoperability between MeTTa and Python, allowing MeTTa code to utilize Python libraries and functions.


9. Server and Networking

Purpose: Allows MeTTa to operate as a server or service, handling network connections and client requests.

  • Server Implementation
    • metta_server.pl: Sets up the server environment for network interactions.

10. Utilities and Support Functions

Purpose: Provides general-purpose functions and support for the MeTTa system, ensuring smooth operation across different platforms.

  • Utility Functions
  • Output Formatting
    • metta_printer.pl: Manages how data structures and results are formatted and displayed.
  • SWI-Prolog Support
    • swi_support.pl: Ensures compatibility with SWI-Prolog-specific features.

11. Initialization and Configuration

Purpose: Handles the startup routines, configuration settings, and loading of necessary modules when MeTTa is launched.

  • Initialization Scripts
  • Loader

Interaction Flow

To better understand how these components interact within the MeTTa system, here's a high-level flow of operations:

  1. Startup and Initialization

  2. User Interaction via REPL or Script Execution

    • If the user engages with MeTTa interactively, metta_repl.pl and repl.default.metta handle inputs and outputs.
    • For script execution, the interpreter processes MeTTa files directly.
  3. Parsing Source Code

    • Source code is fed into metta_parser.pl, which parses it into internal representations.
    • metta_convert.pl may further process these representations for compatibility with the evaluator or compiler.
  4. Compilation or Interpretation

  5. Runtime Execution

  6. Utilizing Core Libraries and Extensions

  7. Output and Feedback

  8. Persistence and State Management

  9. Server Operations

    • If running in server mode, metta_server.pl manages client connections and request handling.

System Architecture Diagram

Below is a textual representation of the MeTTa system architecture, showing the relationships between components:

[ User Interface ]
       |
       V
[ REPL (metta_repl.pl) ] -- [ REPL Script (repl.default.metta) ]
       |
       V
[ Parser (metta_parser.pl) ] -- [ Converter (metta_convert.pl) ]
       |
       V
[ Interpreter (metta_interp.pl) ] -- [ Compiler (metta_compiler.pl) ]
       |                               |
       V                               V
[ Evaluator (metta_eval.pl) ]        [ Compilation Templates (metta_comp_templates.pl) ]
       |
       V
[ Runtime Environment ]
       |
       |-- [ Execution Space (metta_space.pl) ]
       |-- [ Substitution (metta_subst.pl) ]
       |-- [ Type System (metta_types.pl) ]
       |-- [ Concurrency (metta_threads.pl) ]
       |
[ Core Libraries ]
       |
       |-- [ corelib.metta ]
       |-- [ metta_corelib.pl ]
       |
[ Extensions and Integrations ]
       |
       |-- [ Logic Programming ]
       |     |-- [ metta_ontology.pfc.pl ]
       |     |-- [ metta_pfc_base.pl ]
       |     |-- [ metta_pfc_support.pl ]
       |
       |-- [ Python Integration ]
             |-- [ metta_python.pl ]
             |-- [ metta_python_override.py ]
             |-- [ metta_python_patcher.py ]
             |-- [ metta_python_proxy.py ]

Summary

The MeTTa language system is structured to facilitate ease of use, extensibility, and powerful features for logical reasoning and interoperability. Here's a brief recap:

  • Front-End: Parses and prepares code for execution.
  • Core Libraries: Provide essential functions and definitions.
  • Interpreter/Compiler: Execute or compile code for running programs.
  • Runtime Environment: Manages execution contexts and supports runtime features.
  • REPL: Offers an interactive environment for code execution.
  • Debugging/Testing: Tools for ensuring code correctness.
  • Extensions: Enhances MeTTa with logic programming and Python interoperability.
  • Server Support: Allows MeTTa to function as a networked service.
  • Utilities: Additional support functions and compatibility layers.
  • Initialization: Handles startup routines and environment setup.

Note: The descriptions and architecture are based on standard naming conventions and typical functionalities associated with similar files in programming language implementations. For precise implementation details, please refer to the actual source code and official documentation.


Other Directories and Files

The other directories and files in the project provide additional functionality and support. Each top-level directory is expandable, and the link is provided outside the spoiler.

src: Main Source Code of the Project

Click to expand the src directory contents.

src/ext: External Modules Interfacing with External Libraries

These modules provide integration with external libraries and tools, extending the capabilities of the Metta system.

  • bhv_binding.py: Implements behavior bindings, potentially for integrating AI behavior models.
  • dasgate.py: Gateway to the Distributed Annotation System (DAS), used for accessing and integrating biological data.
  • kwargsme.py: Experiments with PyTorch keyword arguments, potentially for machine learning applications.
  • metta_repl.py: Interactive REPL for Metta written in Python, providing an alternative interface.
  • neurospace.py: Defines neural spaces for simulations, aiding in neural network modeling.
  • numme.py: Numerical experiments using NumPy, supporting mathematical computations.
  • parse_torch_func_signatures.py: Parses PyTorch function signatures, facilitating integration with PyTorch.
  • parsing_exceptions.py: Handles parsing exceptions, improving error handling during code parsing.
  • r.py: Utility script for resolution tasks, possibly related to logic programming.
  • resolve.py: Implements resolution logic, essential for logical inference mechanisms.
  • sql_space.py: Provides SQL space integration, enabling database interactions.
  • tm_test.py: Test script for PyTorch models, useful for machine learning development.
  • torchme.py: Script interfacing with PyTorch, aiding in deep learning tasks.

src/main: Contains the Last Known Good Version of the Source Code

This directory holds a stable version of the source code, ensuring that a working copy is available for reference or rollback.

src/metta_jupyter_kernel.py: Implements a Jupyter Kernel for the Metta Language

Allows Metta code to be executed within Jupyter notebooks, facilitating interactive development and data analysis.

src/mettalog: Python Metta Logic Library for Logical Reasoning

This library provides logical reasoning capabilities within the Metta environment.

  • logic.py: Core logic functions that implement fundamental logical operations.
  • reasoning.py: Reasoning algorithms that enable inference and deduction.
  • utils.py: Utility functions supporting the logic library.

docs: Contains Project Documentation, Including User Guides and API References

Contents
  • user_guide.md: Comprehensive guide for users, covering installation, usage, and best practices.
  • api_reference.md: Detailed API documentation, outlining functions, classes, and modules.
  • developer_guide.md: Guidelines for developers contributing to the project, including coding standards and development workflows.

setup.py: Build Script Specifying How to Package and Install Project Components

This script ensures that the project components are correctly packaged and installed, managing dependencies and configurations.


notebooks: Contains Jupyter Notebooks for Experimentation and Data Analysis

Contents
  • experiment1.ipynb: Analysis of dataset X, showcasing data processing capabilities.
  • visualization.ipynb: Data visualization examples, illustrating how to create visual representations of data.
  • from_das: Demonstrations of data retrieval from the Distributed Annotation System (DAS).
    • das_example.ipynb: Using DAS data, demonstrating how to access and utilize biological data.
  • images: Visual assets used in notebooks, such as charts and diagrams.

scripts: Utility Scripts for Data Processing, Testing, and Environment Setup

Files

library: Reusable Code Modules and Libraries

Click to expand the library directory contents.

graphml: Scripts Related to the GraphML Format

  • ext_loader_graphml.pl: Parses GraphML files, enabling the import of graph data.
  • tests: Test cases for the GraphML loader, ensuring correct functionality.

genome: Prolog Scripts for Genomic Data Processing

Modules focused on processing genomic data, integrating with bioinformatics databases.


tests: Various Test Suites to Ensure Correctness and Stability

Test Suites
  • baseline_compat: Tests for baseline compatibility, ensuring fundamental functionalities work as expected.
  • compiler_baseline: Tests for compiler baseline functionality.
  • direct_comp: Tests for direct compilation processes without intermediate steps.
  • extended_compat: Tests for compatibility with extended features and modules.
  • features: Tests focused on specific features of the Metta language and system.
  • flybase: Tests related to the processing of FlyBase data.
  • more-anti-regression: Additional regression tests to catch unintended changes.
  • nars_interp: Tests for the NARS (Non-Axiomatic Reasoning System) interpreter integration.
  • nars_w_comp: Tests for NARS with compiler integrations, ensuring reasoning capabilities.
  • performance: Performance measurement tests to benchmark system efficiency.
  • python_compat: Tests for Python compatibility, verifying the integration between Metta and Python.
  • timing: Scripts and tests focused on measuring execution times and performance metrics.

Conclusion

This detailed overview provides an in-depth look into the architecture and workflow of the Metta language processing system. Starting from the REPL interface, which allows users to interact with the interpreter and load files, the system efficiently manages code addition, parsing, forward chaining for logical inference, compilation, and execution. The inclusion of utilities and support modules enhances the system's capabilities, offering integration with external languages like Python and providing tools for debugging and testing.

Understanding each component's role and how they interact enhances appreciation of the system's workflow and capabilities. This comprehensive guide serves as a valuable resource for both users and developers navigating and utilizing the Metta language processing system.

For further clarification, refer to the docs directory or contact the development team.