The Visitor
Separating a complex arcane algorithm from the crystalline structure of the Gallina Wards it traverses.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Visitor
ARCANUM SCHOOL: Divination // Surveying
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
(* Gallina Ward: Visitor *)
Require Import String.
Inductive Element :=
| NodeCore : nat -> Element
| NodeEdge : string -> Element.
(* The Visitor defines behaviors for each type of Element *)
Record Visitor (T : Type) := {
visitCore : nat -> T;
visitEdge : string -> T
}.
Definition accept {T : Type} (e : Element) (v : Visitor T) : T :=
match e with
| NodeCore n => v.(visitCore) n
| NodeEdge s => v.(visitEdge) s
end.
(* Concrete Visitor: Extracts string representations *)
Definition stringifyVisitor : Visitor string := {|
visitCore := fun n => "Core Resonance";
visitEdge := fun s => "Edge Signature: " ++ s
|}.
Definition testCore := NodeCore 42.
Definition result := accept testCore stringifyVisitor.
Runic Tags:
#visitor
#gallina
#wards