This file provides context for Gemini Code when working on this project.
This project uses Prolog to parse, analyze, and execute Clarion programs. Clarion is a 4GL (fourth-generation language) used primarily for database application development. It also includes formal verification of medical imaging algorithms (ALGT).
This project uses a DSC v3 configuration (algt-devenv.dsc.yaml) to define the required development environment. On every session start, run the check script to verify the environment is correct:
bash check_devenv.shReview the output for any resources not in desired state and inform the user of missing or misconfigured tools before proceeding with other work.
- Prolog: Primary implementation language (targeting SWI-Prolog)
- Logtalk: Object-oriented Prolog extension (domain models)
- Clarion 11.1: The language being analyzed (compiles to 32-bit Windows DLLs/EXEs)
- Python 3.11 (32-bit): ctypes interop with Clarion DLLs
Clarion programs consist of:
- Program files (.clw) - Main source files
- Include files (.inc) - Header/interface files
- Dictionary files (.dct) - Database schema definitions
Clarion uses a structured syntax with:
PROGRAM,MAP,CODEsectionsPROCEDUREdefinitions- Data declarations with types like
STRING,LONG,SHORT,DECIMAL - Control structures:
IF,LOOP,CASE - Embedded SQL for database access
The unified simulator represents Clarion code as Prolog facts and rules:
- Source code is parsed via DCG into an AST represented as Prolog terms
- An execution engine interprets the AST with pluggable storage and UI backends
- Execution traces can be compared against compiled Clarion DLL behavior
├── clarion_projects/ # Compiled Clarion projects
│ ├── hello-world/ # Simple PROGRAM exe
│ ├── python-dll/ # DLL with exported functions (Python ctypes)
│ ├── diagnosis-store/ # DOS flat-file CRUD DLL
│ ├── sensor-data/ # Sensor readings DLL, trace comparison
│ ├── stats-calc/ # Statistical calculations DLL
│ ├── odbc-store/ # ODBC DLL with SQL Server LocalDB
│ ├── clarion_examples/ # Reference .clw files
│ ├── form-demo/ # GUI form + FormLib DLL for CDB tracing
│ ├── form-cli/ # CLI form with EventReader, .evt format
│ └── treatment-offset/ # Treatment offset entry with sign-flip
├── simulators/clarion/ # Prolog Clarion simulator
│ └── unified/ # DCG parser + execution engine (104 tests)
│ ├── clarion.pl # Public API
│ ├── clarion_parser.pl # DCG parser
│ ├── ast_bridge.pl # AST transformation
│ ├── simulator.pl # Core execution engine
│ ├── simulator_builtins.pl # Built-in functions
│ ├── simulator_eval.pl # Expression evaluation
│ ├── simulator_control.pl # Control flow
│ ├── simulator_state.pl # State management
│ ├── simulator_classes.pl # Class support
│ ├── execution_tracer.pl # ML exports (PGM, PyMC, Stan, GNN-VAE)
│ ├── scenario_dsl.pl # Scenario DSL
│ ├── scenario_ahk.pl # AutoHotkey generation
│ ├── storage_backend.pl # Pluggable storage dispatch
│ ├── storage_memory.pl # In-memory storage
│ ├── storage_csv.pl # CSV file storage
│ ├── storage_odbc.pl # ODBC storage
│ ├── ui_backend.pl # UI backend abstraction
│ ├── ui_simulation.pl # UI simulation
│ ├── web_server.pl # Web server interface
│ └── test_unified.pl # Test suite
├── algt_tests/ # Algorithm verification test suite
├── domain_models/ # Logtalk domain models & workflows
│ ├── imaging_services/ # Image import manager, contracts
│ ├── subject_image_domain_model/
│ ├── treatment_image_domain_model/
│ └── appointment_domain_model/
├── model_checker/ # Concurrent operation verification
├── mcp_servers/ # MCP server implementations
│ ├── prolog/ # MCP server (Prolog)
│ ├── erlang/ # MCP server (Erlang)
│ └── elixir/ # MCP server (Elixir)
└── docs/
A single modular simulator (21 Prolog files, 104 tests) that combines parsing and execution:
Parses Clarion source files into an AST using DCG (Definite Clause Grammars).
Transforms parsed structures into a normalized AST for the execution engine.
Executes Clarion programs from their AST representation.
Supported features:
- Variables (local, global, prefixed file fields like
Cust:CustomerID) - Expressions (arithmetic, comparison, logical, string concatenation)
- Control flow:
IF/ELSIF/ELSE,LOOP(infinite, TO, WHILE, UNTIL),CASE/OF,BREAK,CYCLE - Procedures with parameters and local variables
- Routines (
DO/ROUTINEwithEXIT) - Class support
- File I/O with pluggable storage backends:
- In-memory (
storage_memory.pl) - CSV files (
storage_csv.pl) - ODBC/SQL (
storage_odbc.pl)
- In-memory (
- Built-in functions:
MESSAGE,CLIP,LEN,CHR,VAL,TODAY,CLOCK - UI simulation with pluggable backends
- Scenario-based testing with AutoHotkey generation
- Execution tracer with ML model exports (PGM, PyMC, Stan, GNN-VAE)
cd simulators/clarion/unified
swipl
?- use_module(clarion).
?- init_session(Source, Session), call_procedure(Session, 'MyProc', Result).# Unified simulator tests (104 tests)
cd simulators/clarion/unified
swipl -g "main,halt" -t "halt(1)" test_unified.pl
# ALGT verification tests
swipl -s algt_tests/ALGT_BEAM_VOLUME.pl- Use descriptive predicate names following Prolog conventions (lowercase, underscores)
- Document predicates with comments explaining their purpose
- Keep facts and rules modular for easy testing
- Use DCG (Definite Clause Grammars) for parsing when appropriate
.pl- Prolog source files.clw- Clarion source files (input for analysis).inc- Clarion include files.lgt- Logtalk source files
- Define the pattern to match as a Prolog predicate
- Add test cases in the test suite
- Document the rule's purpose and usage
- Extend the DCG grammar in
clarion_parser.pl - Add AST bridge transformations in
ast_bridge.pl - Add execution support in the appropriate simulator module
- Add tests in
test_unified.pl