✨ ARCANUM RUNE DIFFICULTY: Adept Creational

The Singleton of the Progenitor

Ensure a single, globally accessible Weave of magic using ML's module system.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Singleton
ARCANUM SCHOOL: Abjuration // Singular Binding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
signature WEAVE_STATE = sig
  val getEnergy : unit -> int
  val consumeEnergy : int -> unit
end

structure GlobalWeave : WEAVE_STATE = struct
  val energy = ref 1000
  
  fun getEnergy () = !energy
  
  fun consumeEnergy amount =
    if !energy >= amount then
      energy := !energy - amount
    else
      print "Insufficient arcane energy in the Weave!\n"
end

(* Usage *)
val current = GlobalWeave.getEnergy ()
val _ = GlobalWeave.consumeEnergy 50

To achieve a Singleton in the ML Progenitor, we turn to the module system and stateful references. By enclosing a ref within a structure and exposing only controlled functions via a signature, we create a singular, globally accessible entity. The SML compiler ensures that only one instance of GlobalWeave exists, making it a true Singleton to harbor the world’s arcane energy safely.

Runic Tags: #modules #references #global state