The Visitor
An external entity traverses the abstract syntax tree of a ward, extracting its magical properties without modifying its pure structure.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Visitor
ARCANUM SCHOOL: Divination // Analysis
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// LEAN PATTERN //
namespace MathematicalWards
inductive GeometricNode where
| circle (radius : Nat)
| square (side : Nat)
| union (a b : GeometricNode)
class GeometricVisitor (V : Type) where
visitCircle : V → Nat → V
visitSquare : V → Nat → V
def accept {V : Type} [GeometricVisitor V] (node : GeometricNode) (visitor : V) : V :=
match node with
| GeometricNode.circle r => GeometricVisitor.visitCircle visitor r
| GeometricNode.square s => GeometricVisitor.visitSquare visitor s
| GeometricNode.union a b =>
let v' := accept a visitor
accept b v'
structure AreaCalculator where
total : Nat
instance : GeometricVisitor AreaCalculator where
visitCircle v r := { total := v.total + (3 * r * r) } -- Approximation for integer math
visitSquare v s := { total := v.total + (s * s) }
end MathematicalWards
Using typeclasses to emulate double-dispatch, the Visitor traverses complex inductive data types to compute properties externally.
Runic Tags:
#behavioral
#lean4
#visitor
#wards