UnifyWeaver

UnifyWeaver

A Prolog transpiler that turns logic programs into LINQ-style data pipelines.

One codebase β†’ Bash streams, C# queries, or PowerShell orchestration.

UnifyWeaver transforms declarative Prolog predicates into efficient code for multiple targets, with optimized handling of transitive closures, advanced recursion patterns, and relational query semantics.

πŸ“š Extended Documentation - Comprehensive tutorials, examples, and advanced usage πŸŽ“ Educational Materials - Learn UnifyWeaver with a 13-book educational series

Target Comparison

Target Approach Best For Status
Bash Stream/Procedural Unix pipelines, text processing Production
C# Query Fixed-Point (Query Engine) Complex recursion, .NET integration Production
C# Codegen Generator-Based Standalone .NET apps Production
Go Generator-Based High-performance services Experimental
Rust Generator-Based Systems programming Experimental
Python Generator-Based Data science, scripting Experimental
SQL Declarative Database queries Experimental
AWK Stream/Procedural Text processing, log analysis Experimental
PowerShell Orchestration Windows/.NET orchestration In Development
Prolog Direct Copy Fallback, debugging Ready

Features

Core Compilation

Advanced Recursion

C# Target Family (v0.1)

Data Source Plugin System (v0.0.2)

PowerShell Target Support (v0.0.2)

Control Plane

Installation

Requirements:

git clone https://github.com/s243a/UnifyWeaver.git
cd UnifyWeaver

Quick Start

Test Environment

UnifyWeaver includes a convenient test environment with auto-discovery:

# Linux/WSL
cd scripts/testing
./init_testing.sh

# Windows (PowerShell)
cd scripts\testing
.\Init-TestEnvironment.ps1

Then in the test environment:

?- test_all.           % Run all tests
?- test_stream.        % Test stream compilation
?- test_recursive.     % Test basic recursion
?- test_advanced.      % Test advanced recursion patterns
?- test_constraints.   % Test constraint system

Manual Usage

?- use_module(unifyweaver(core/recursive_compiler)).
?- test_recursive_compiler.

This generates bash scripts in the output/ directory:

cd output
bash test_recursive.sh

Usage

Basic Example

Define your Prolog predicates:

% Facts
parent(alice, bob).
parent(bob, charlie).

% Rules
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z).

Compile to bash:

?- compile_recursive(ancestor/2, [], BashCode).
?- write_bash_file('ancestor.sh', BashCode).

Use the generated script:

source parent.sh
source ancestor.sh

ancestor_all alice  # Find all descendants of alice
ancestor_check alice charlie && echo "Yes" || echo "No"  # Check specific relationship

See Extended Documentation for advanced recursion patterns, data sources, and complete examples.

Architecture

UnifyWeaver follows a modular compilation pipeline:

  1. Classification - Analyzes predicate to determine recursion pattern
  2. Constraint Analysis - Detects unique/ordering constraints
  3. Pattern Matching - Tries tail β†’ linear β†’ tree β†’ mutual β†’ basic recursion
  4. Optimization - Applies pattern-specific optimizations (BFS, loops, memoization)
  5. Target Selection - Chooses Bash, PowerShell, or C# backend based on requested target/1 preference
  6. Code Generation / Plan Emission - Renders shell templates, C# source, or query plans for the selected backend

See ARCHITECTURE.md and Extended Documentation for detailed architecture.

Examples

Family Tree Queries

ancestor(X, Y)    % Transitive closure of parent
descendant(X, Y)  % Reverse of ancestor
sibling(X, Y)     % Same parent, different children

Data Source Integration (v0.0.2)

% CSV/TSV data processing
:- source(csv, users, [csv_file('data/users.csv'), has_header(true)]).

% AWK text processing
:- source(awk, extract_fields, [
    awk_program('$3 > 100 { print $1, $2 }'),
    input_file('data.txt')
]).

% HTTP API integration with caching
:- source(http, github_repos, [
    url('https://api.github.com/users/octocat/repos'),
    cache_duration(3600)
]).

% Python with SQLite
:- source(python, get_users, [
    sqlite_query('SELECT name, age FROM users WHERE active = 1'),
    database('app.db')
]).

% JSON processing with jq
:- source(json, extract_names, [
    jq_filter('.users[] | {name, email} | @tsv'),
    json_file('data.json')
]).

Complete ETL Pipeline Demo

cd scripts/testing/test_env5
swipl -g main -t halt examples/pipeline_demo.pl

See Extended Documentation for complete examples including:

What’s Supported

βœ… Recursion Patterns

βœ… Data Sources (v0.0.2)

⚠️ Current Limitations

See Extended Documentation for complete details and troubleshooting.

Testing

cd scripts/testing
./init_testing.sh  # or Init-TestEnvironment.ps1 on Windows

In SWI-Prolog:

?- test_all.          % Run all tests
?- test_stream.       % Stream compilation
?- test_recursive.    % Basic recursion
?- test_advanced.     % Advanced patterns

See TESTING_GUIDE.md for adding tests.

Documentation

Contributing

Issues and pull requests welcome! See CONTRIBUTING.md for details.

Key areas:

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Acknowledgments

Developed as an exploration of compiling declarative logic to multiple target languages while preserving correctness and efficiency. UnifyWeaver makes Prolog’s power accessible across Unix shells, .NET, and beyond.

Contributors: