✨ ARCANUM RUNE DIFFICULTY: Adept Behavioral

The Chain of Responsibility

Passing an arcane request through a linked sequence of Gallina Wards until one successfully binds it.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Divination // Flow
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
(* Gallina Ward: Chain of Responsibility *)
Require Import String List.
Import ListNotations.

(* A Handler processes a request, returning Some result if handled, or None to pass it on *)
Definition Handler := nat -> option string.

Definition handleLowLevel : Handler := fun req =>
  if Nat.leb req 10 then Some "Handled by Low-Level Ward" else None.
  
Definition handleMidLevel : Handler := fun req =>
  if Nat.leb req 100 then Some "Handled by Mid-Level Ward" else None.
  
Definition handleHighLevel : Handler := fun req =>
  Some "Handled by Archmage Ward".
  
Fixpoint processChain (chain : list Handler) (req : nat) : option string :=
  match chain with
  | [] => None
  | h :: t => match h req with
              | Some res => Some res
              | None => processChain t req
              end
  end.
  
Definition wardChain := [handleLowLevel; handleMidLevel; handleHighLevel].
Definition invoke (req : nat) := processChain wardChain req.
Runic Tags: #chain-of-responsibility #gallina #wards