UnifyWeaver

The AWK Target Book

A practical guide to compiling Prolog predicates to self-contained AWK scripts using UnifyWeaver’s AWK target.

What You’ll Learn

This book teaches you how to use UnifyWeaver to generate portable AWK scripts from Prolog predicates. AWK is a powerful text-processing language available on virtually every Unix-like system, making it ideal for data pipelines and log analysis.

Prerequisites

Chapter Overview

Chapter 1: Getting Started

Introduction to the AWK target, installation verification, and your first compiled script.

Chapter 2: Facts and Filtering

Learn to compile Prolog facts to AWK associative arrays and filter data with constraints.

Chapter 3: Rules and Constraints

Compile single and multiple rules with arithmetic and logical constraints.

Chapter 4: Aggregations

Use built-in aggregation operations: sum, count, max, min, and avg.

Chapter 5: Tail Recursion

Compile tail-recursive predicates to efficient AWK while loops.

Chapter 6: Regex Pattern Matching

Use the match/2, match/3, and match/4 predicates for regex operations.

Chapter 7: Practical Applications

Real-world examples: log analysis, data transformation, and pipeline integration.

Key Features of the AWK Target

Feature Description
Facts Compiled to AWK associative arrays
Single Rules Direct translation with constraint evaluation
Multiple Rules Combined with union-style logic
Aggregations sum, count, max, min, avg operations
Tail Recursion Compiled to efficient while loops
Regex Matching Full support via match/2, match/3, match/4
Input Formats TSV, CSV, JSONL record formats

Input Format Options

The AWK target supports multiple input formats:

% Tab-separated values (default)
compile_predicate_to_awk(pred/2, [record_format(tsv)], AWK)

% Comma-separated values
compile_predicate_to_awk(pred/2, [record_format(csv)], AWK)

% JSON Lines (one JSON object per line)
compile_predicate_to_awk(pred/2, [record_format(jsonl)], AWK)

% Custom field separator
compile_predicate_to_awk(pred/2, [field_separator(':')], AWK)

Quick Example

Prolog predicate:

high_salary(Name, Salary) :-
    employee(Name, _, Salary),
    Salary > 50000.

Compile to AWK:

?- compile_predicate_to_awk(high_salary/2, [], AWK),
   write_awk_script('high_salary.awk', AWK).

Run the script:

awk -f high_salary.awk employees.tsv

Limitations

The AWK target is designed for stream processing and has some constraints:

These limitations are inherent to AWK’s streaming model and are not bugs.

Why AWK?

Getting Help

License

This educational content is dual-licensed under MIT (code) and CC-BY-4.0 (documentation).