Skip to content

Instantly share code, notes, and snippets.

@eMdOS
Last active May 11, 2020 18:04
Show Gist options
  • Save eMdOS/3861a37de2762bcede0f045fdb83dbd8 to your computer and use it in GitHub Desktop.
Save eMdOS/3861a37de2762bcede0f045fdb83dbd8 to your computer and use it in GitHub Desktop.
///
/// Associative Law
///
/// **Associativity** is the condition that the 2 ways to use a "binary composition" of morphisms to compose a sequence of 3 morphisms "are equal".
///
/// - Associative through **Addition**: `a + (b + c) = (a + b) + c`
/// - Associative through **Multiplication**: `a * (b * c) = (a * b) * c`
///
public enum AssociativeLaw<Element: Equatable> {}
public extension AssociativeLaw {
///
/// It verifies if the set of elements are **associative**.
///
/// Given a set of 3 elements, and a binary operation, the associativity "verification" happens.
///
/// - Parameters:
/// - a: The first element
/// - b: The second element
/// - c: The third element
/// - operation: The associative binary operation to evaluate on
///
static func verify(a: Element, b: Element, c: Element, operation: (Element, Element) -> Element) -> Bool {
// a <> (b <> c) = (a <> b) <> c
operation(a, operation(b, c)) == operation(operation(a, b), c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment