NAME

Sub::Genius::Example - Practical and exploratory examples of Sub::Genius plans

INTRODUCTION

This document presents a curated collection of Sub::Genius plans intended to demonstrate how declarative concurrency can be expressed clearly, correctly, and usefully in plain Perl.

The examples range from immediately practical to deliberately non-intuitive. Some mirror common real-world concurrency patterns; others are included to expose edge cases, equivalence properties, and deeper semantics of Parallel Regular Expressions (PREs).

All examples are executable under Sub::Genius, and all plans describe sequentially consistent execution in Perl’s uniprocess runtime.

READING GUIDE

You can read this document in several ways:

  • As a cookbook of ready-made concurrency patterns

  • As a reference for designing correct execution plans

  • As an exploration of equivalence and transformation of plans

  • As a bridge between theory and real systems (AI, graphics, pipelines)

BASIC CONCURRENCY PATTERNS

Total Ordering (Baseline)

my $plan = q{ A B C };

Meaning: A must run before B, which must run before C.

This is equivalent to a traditional, linear call chain and serves as a baseline for comparison.

Full Concurrency (Shuffle)

my $plan = q{ A & B & C };

All three subroutines must run, but in any order. There are 3! valid execution orders.

This expresses logical concurrency without threads or synchronization.

Mixed Ordering

my $plan = q{ A ( B C ) & ( D E ) F };

Constraints:

  • A always runs first

  • B before C

  • D before E

  • The two chains may interleave

  • F always runs last

This models two independent pipelines with internal ordering constraints.

BARRIER AND FAN-IN PATTERNS

Initialization Barrier

my $plan = q{
  init
    (
      load_model
      &
      load_tokenizer
      &
      load_config
    )
  serve
};

All initialization steps may run in any order, but serve runs only after all have completed.

This pattern appears frequently in services, daemons, and inference servers.

Fan-Out / Fan-In

my $plan = q{
  start
    (
      preprocess
      &
      analyze
      &
      validate
    )
  merge
  finish
};

Independent work proceeds concurrently and then rejoins.

AI / ML PIPELINE EXAMPLES

Multimodal Feature Extraction

my $plan = q{
  ingest
    (
      extract_text_features
      &
      extract_image_features
      &
      extract_metadata_features
    )
  merge_features
  predict
};

Each extractor operates independently; all results are required before prediction.

Optional Model Path (Union)

my $plan = q{
  load_model
    (
      use_fast_model
      |
      use_accurate_model
    )
  infer
};

Exactly one model path is selected. Downstream logic remains unchanged.

Training with Asynchronous Logging

my $plan = q{
  init
    (
      load_data
      &
      load_model
      &
      setup_logger
    )
  (
    train_step
    &
    log_metrics
  )
  checkpoint
};

Logging never blocks training, but checkpointing waits for both.

GRAPHICS AND RENDERING PIPELINES

GPU Preparation Barrier

my $plan = q{
  startup
    (
      load_meshes
      &
      load_textures
      &
      compile_shaders
    )
  upload_to_gpu
  render_frame
};

Prevents rendering before all GPU assets are ready.

Deferred Rendering Passes

my $plan = q{
  begin_frame
    (
      geometry_pass
      &
      shadow_pass
    )
  lighting_pass
    (
      postprocess_hdr
      &
      postprocess_bloom
    )
  composite
  end_frame
};

Expresses a modern deferred rendering pipeline without nested control flow.

NON-INTUITIVE BUT VALID PLANS

Nested Shuffle with Choice

my $plan = q{
  begin
    (
      ( A | B )
      &
      ( C D )
    )
  end
};

Either A or B runs. C always precedes D. The two branches may interleave.

This plan remains finite, deterministic, and sequentially consistent.

PLAN EQUIVALENCE

Two plans are equivalent if they accept the same set of valid execution strings after normalization.

Equivalent Plans via Parentheses

my $p1 = q{ A ( B C ) D };
my $p2 = q{ (A B) (C D) };

Both enforce the same total order.

Equivalent Plans via Shuffle Commutativity

my $p1 = q{ (A B) & (C D) };
my $p2 = q{ (C D) & (A B) };

Both permit the same interleavings.

Equivalent Plans via Distribution

my $p1 = q{ start (fast | safe) end };
my $p2 = q{ (start fast end) | (start safe end) };

Choice may be factored inward or outward without changing meaning.

EXECUTION GRAPHS (DOT)

The following GraphViz DOT graph represents ordering constraints for (A B) & (C D):

digraph Plan {
  rankdir=LR;
  node [shape=box];

  A -> B;
  C -> D;

  subgraph cluster1 { label="L1"; A; B; }
  subgraph cluster2 { label="L2"; C; D; }
}

This graph shows happens-before constraints, not the full DFA.

Such graphs are useful for:

  • Documentation

  • Design review

  • Debugging concurrency intent

PATHOLOGICAL BUT REALISTIC CASES

Large Shuffle (Stress Test)

my $plan = join q{&}, qw( a b c d e f g h i j );

This admits 10! valid execution orders. If your code is correct under this plan, it is truly order-independent.

Caching is essential for plans of this nature.

DESIGN INSIGHT

Sub::Genius does not provide concurrency by executing code in parallel. Instead, it provides a rigorous way to:

  • Declare concurrency intent

  • Guarantee ordering constraints

  • Explore valid serializations

  • Preserve correctness in a uniprocess runtime

The result is code that is easier to reason about, audit, and extend.

SEE ALSO

Sub::Genius, Sub::Genius::Util, FLAT, Graph::PetriNet

AUTHOR

OODLER 577 <oodler@cpan.org>

LICENSE

Same terms as Perl itself.