⚙️ ISABELLE RUNE DIFFICULTY: Adept Behavioral

The Chain of Responsibility

Avoid coupling the sender of a logical request to its receiver by giving multiple handlers a chance to process it.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Abjuration // Delegation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// ISABELLE PATTERN //
theory ChainOfResponsibility
  imports Main
begin

datatype request = Heal nat | Damage nat

type_synonym handler = "request \<Rightarrow> request option"

definition heal_handler :: handler where
  "heal_handler r = (case r of Heal n \<Rightarrow> None | _ \<Rightarrow> Some r)"

definition chain_handlers :: "handler list \<Rightarrow> request \<Rightarrow> request option" where
  "chain_handlers hs req = foldl (\<lambda>r h. case r of None \<Rightarrow> None | Some req' \<Rightarrow> h req') (Some req) hs"

end
Runic Tags: #isabelle #hol #abjuration #design-patterns