module Algebra.Quasigroup where

QuasigroupsπŸ”—

Traditionally, groups are defined as monoids where every element has a (necessarily unique) inverse. However, there is another path to the theory of groups that emphasizes division/subtraction over inverses. This perspective is especially interesting when generalizing groups to non-associative settings; axioms of the form are rather ill-behaved without associativity, as inverses are no longer necessarily unique!

Right quasigroupsπŸ”—

For the sake of maximum generality, we will build up to the definition of quasigroups in stages, starting with right quasigroups.

Let be a binary operator. is a right quasigroup if is a magma, and there is some binary operator subject to the following axioms:

  • For all
  • For all
record is-right-quasigroup {β„“} {A : Type β„“} (_⋆_ : A β†’ A β†’ A) : Type β„“ where
  no-eta-equality
  field
    _/_ : A β†’ A β†’ A
    /-invl : βˆ€ x y β†’ (x / y) ⋆ y ≑ x
    /-invr : βˆ€ x y β†’ (x ⋆ y) / y ≑ x
    has-is-magma : is-magma _⋆_

  open is-magma has-is-magma public

Intuitively, the operation acts like a sort of right-biased division. This is further cemented by noting that the existence of such an operation implies that every the action is an equivalence.

  ⋆-equivr : βˆ€ x β†’ is-equiv (_⋆ x)
  ⋆-equivr x .is-eqv y .centre = y / x , /-invl y x
  ⋆-equivr x .is-eqv y .paths (l , lx=y) =
    Ξ£-prop-path (Ξ» l β†’ has-is-set (l ⋆ x) y) $
      y / x       β‰‘Λ˜βŸ¨ ap (_/ x) lx=y βŸ©β‰‘Λ˜
      (l ⋆ x) / x β‰‘βŸ¨ /-invr l x βŸ©β‰‘
      l           ∎

This in turn implies that is an equivalence.

  /-equivr : βˆ€ x β†’ is-equiv (_/ x)
  /-equivr x = inverse-is-equiv (⋆-equivr x)

As easy corollaries, we get that multiplication and division in are right-cancellative.

  opaque
    ⋆-cancelr : βˆ€ {x y} (z : A) β†’ x ⋆ z ≑ y ⋆ z β†’ x ≑ y
    ⋆-cancelr z = Equiv.injective (_⋆ z , ⋆-equivr z)

    /-cancelr : βˆ€ {x y} (z : A) β†’ x / z ≑ y / z β†’ x ≑ y
    /-cancelr z = Equiv.injective (_/ z , /-equivr z)

It turns out that being an equivalence is a sufficient condition for a to be a right quasigroup, provided that is a set.

⋆-equivrβ†’is-right-quasigroup
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-set A
  β†’ (βˆ€ x β†’ is-equiv (_⋆ x))
  β†’ is-right-quasigroup _⋆_

The proof is an exercise in unrolling definitions. If is an equivalence, then serves as a perfectly valid definition of both axioms follow directly from the unit and counit of the equivalence.

⋆-equivrβ†’is-right-quasigroup {_⋆_ = _⋆_} A-set ⋆-eqv =
  ⋆-right-quasi
  where
    open is-right-quasigroup

    module ⋆-eqv x = Equiv (_⋆ x , ⋆-eqv x)

    ⋆-right-quasi : is-right-quasigroup _⋆_
    ⋆-right-quasi ._/_ x y = ⋆-eqv.from y x
    ⋆-right-quasi ./-invl x y = ⋆-eqv.Ξ΅ y x
    ⋆-right-quasi ./-invr x y = ⋆-eqv.Ξ· y x
    ⋆-right-quasi .has-is-magma .is-magma.has-is-set = A-set

We can upgrade this correspondence to an equivalence, as we definitionally get back the same division operation we started with after round-tripping.

is-right-quasigroup≃⋆-equivr
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-right-quasigroup _⋆_ ≃ (is-set A Γ— (βˆ€ x β†’ is-equiv (_⋆ x)))
is-right-quasigroup≃⋆-equivr {_⋆_ = _⋆_} =
  Iso→Equiv $
    ⟨ has-is-set , ⋆-equivr ⟩ ,
    iso (uncurry ⋆-equivrβ†’is-right-quasigroup)
      (Ξ» _ β†’ prop!)
     (Ξ» ⋆-right-quasi β†’
      let open is-right-quasigroup ⋆-right-quasi in
      Iso.injective eqv (refl ,β‚š prop!))
  where
    open is-right-quasigroup
    unquoteDecl eqv = declare-record-iso eqv (quote is-right-quasigroup)

Crucially, this means that the division operation is actually a property rather than structure, as both is-equiv and is-set are propositions.

is-right-quasigroup-is-prop
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-prop (is-right-quasigroup _⋆_)
is-right-quasigroup-is-prop {A = A} =
  Equivβ†’is-hlevel 1 is-right-quasigroup≃⋆-equivr (hlevel 1)

Right quasigroup homomorphismsπŸ”—

In the previous section, we proved that the existence of a right division operation was actually a property of a magma, rather than structure. We shall now see that right division is automatically preserved by magma homomorphisms.

We start by defining the corresponding notion of structure for right quasigroups.

A right quasigroup structure on a type is a binary operation such that is a right quasigroup.

record Right-quasigroup-on {β„“} (A : Type β„“) : Type β„“ where
  no-eta-equality
  field
    _⋆_ : A β†’ A β†’ A
    has-is-right-quasigroup : is-right-quasigroup _⋆_

  open is-right-quasigroup has-is-right-quasigroup public

A right quasigroup homomorphism between two right quasigroups is a function such that

record is-right-quasigroup-hom
  {β„“ β„“'} {A : Type β„“} {B : Type β„“'}
  (f : A β†’ B)
  (S : Right-quasigroup-on A) (T : Right-quasigroup-on B)
  : Type (β„“ βŠ” β„“')
  where
  no-eta-equality
  private
    module A = Right-quasigroup-on S
    module B = Right-quasigroup-on T
  field
    pres-⋆ : βˆ€ x y β†’ f (x A.⋆ y) ≑ f x B.⋆ f y

Preservation of right division follows almost immediately from right-cancellativity.

  pres-/ : βˆ€ x y β†’ f (x A./ y) ≑ f x B./ f y
  pres-/ x y =
    B.⋆-cancelr (f y) $
      f (x A./ y) B.⋆ f y   β‰‘Λ˜βŸ¨ pres-⋆ (x A./ y) y βŸ©β‰‘Λ˜
      f ((x A./ y) A.⋆ y)   β‰‘βŸ¨ ap f (A./-invl x y) βŸ©β‰‘
      f x                   β‰‘Λ˜βŸ¨ B./-invl (f x) (f y) βŸ©β‰‘Λ˜
      (f x B./ f y) B.⋆ f y ∎

Right quasigroups are algebraic structures, so they form a thin structure over

  Right-quasigroup-structure : βˆ€ β„“ β†’ Thin-structure β„“ Right-quasigroup-on
  Right-quasigroup-structure β„“ .is-hom f A B .∣_∣ =
    is-right-quasigroup-hom f A B
  Right-quasigroup-structure β„“ .is-hom f A B .is-tr =
    is-right-quasigroup-hom-is-prop
  Right-quasigroup-structure β„“ .id-is-hom .pres-⋆ x y =
    refl
  Right-quasigroup-structure β„“ .∘-is-hom f g f-hom g-hom .pres-⋆ x y =
    ap f (g-hom .pres-⋆ x y) βˆ™ f-hom .pres-⋆ (g x) (g y)
  Right-quasigroup-structure β„“ .id-hom-unique {A} {S} {T} p q =
    Right-quasigroup-on-pathp (ext (p .pres-⋆))

This observation lets us neatly organize right quasigroups into a category.

Right-quasigroups : βˆ€ β„“ β†’ Precategory (lsuc β„“) β„“
Right-quasigroups β„“ = Structured-objects (Right-quasigroup-structure β„“)

module Right-quasigroups {β„“} = Cat.Reasoning (Right-quasigroups β„“)

Right-quasigroup : (β„“ : Level) β†’ Type (lsuc β„“)
Right-quasigroup β„“ = Right-quasigroups.Ob {β„“}

Left quasigroupsπŸ”—

We can dualise the definition of right quasigroups to arrive at the notion of a left quasigroup.

Let be a binary operator. is a left quasigroup if is a magma, and there is some binary operator subject to the following axioms:

  • For all
  • For all
record is-left-quasigroup {β„“} {A : Type β„“} (_⋆_ : A β†’ A β†’ A) : Type β„“ where
  no-eta-equality
  field
    _⧡_ : A β†’ A β†’ A
    ⧡-invr : βˆ€ x y β†’ x ⋆ (x ⧡ y) ≑ y
    ⧡-invl : βˆ€ x y β†’ x ⧡ (x ⋆ y) ≑ y
    has-is-magma : is-magma _⋆_

  open is-magma has-is-magma public

Intuitively, we should think of as β€œ divided by ” or β€œ over ”. This is reinforced by the fact that for every the action is an equivalence.

  ⋆-equivl : βˆ€ x β†’ is-equiv (x ⋆_)
  ⋆-equivl x .is-eqv y .centre = x ⧡ y , ⧡-invr x y
  ⋆-equivl x .is-eqv y .paths (r , xr=y) =
    Ξ£-prop-path (Ξ» r β†’ has-is-set (x ⋆ r) y) $
      x ⧡ y       β‰‘Λ˜βŸ¨ ap (x ⧡_) xr=y βŸ©β‰‘Λ˜
      x ⧡ (x ⋆ r) β‰‘βŸ¨ ⧡-invl x r βŸ©β‰‘
      r ∎

  ⧡-equivl : βˆ€ x β†’ is-equiv (x ⧡_)
  ⧡-equivl x = inverse-is-equiv (⋆-equivl x)

This implies that that multiplication and division in is left-cancellative.

  opaque
    ⋆-cancell : βˆ€ (x : A) {y z} β†’ x ⋆ y ≑ x ⋆ z β†’ y ≑ z
    ⋆-cancell x = Equiv.injective (x ⋆_ , ⋆-equivl x)

    ⧡-cancell : βˆ€ (x : A) {y z} β†’ x ⧡ y ≑ x ⧡ z β†’ y ≑ z
    ⧡-cancell x = Equiv.injective (x ⧡_ , ⧡-equivl x)

Next, we shall show that left quasigroups are formally dual to right quasigroups.

is-left-quasigroup≃op-is-right-quasigroup
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-left-quasigroup _⋆_ ≃ is-right-quasigroup (Ξ» x y β†’ y ⋆ x)
The proof of this fact is rather tedious, so we will omit the details.
is-left-quasigroup→op-is-right-quasigroup
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-left-quasigroup _⋆_ β†’ is-right-quasigroup (Ξ» x y β†’ y ⋆ x)

is-right-quasigroup→op-is-left-quasigroup
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-right-quasigroup _⋆_ β†’ is-left-quasigroup (Ξ» x y β†’ y ⋆ x)

is-left-quasigroupβ†’op-is-right-quasigroup {_⋆_ = _⋆_} A-quasi = A-op-quasi
  where
    open is-left-quasigroup A-quasi
    open is-right-quasigroup

    A-op-quasi : is-right-quasigroup (Ξ» x y β†’ y ⋆ x)
    A-op-quasi ._/_ x y = y ⧡ x
    A-op-quasi ./-invr = flip ⧡-invl
    A-op-quasi ./-invl = flip ⧡-invr
    A-op-quasi .has-is-magma .is-magma.has-is-set = hlevel 2

is-right-quasigroupβ†’op-is-left-quasigroup {_⋆_ = _⋆_} A-quasi = A-op-quasi
  where
    open is-right-quasigroup A-quasi
    open is-left-quasigroup

    A-op-quasi : is-left-quasigroup (Ξ» x y β†’ y ⋆ x)
    A-op-quasi ._⧡_ x y = y / x
    A-op-quasi .⧡-invr = flip /-invl
    A-op-quasi .⧡-invl = flip /-invr
    A-op-quasi .has-is-magma .is-magma.has-is-set = hlevel 2

is-left-quasigroup≃op-is-right-quasigroup =
  Iso→Equiv $
    is-left-quasigroup→op-is-right-quasigroup ,
    iso is-right-quasigroup→op-is-left-quasigroup
      (Ξ» _ β†’ prop!)
      (Ξ» ⋆-left-quasi β†’
        let open is-left-quasigroup ⋆-left-quasi in
        Iso.injective eqv (refl ,β‚š prop!))
    where
      private unquoteDecl eqv = declare-record-iso eqv (quote is-left-quasigroup)

This in turn means that being a left quasigroup is a property of a binary operation.

is-left-quasigroup-is-prop
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-prop (is-left-quasigroup _⋆_)
is-left-quasigroup-is-prop {A = A} =
  Equivβ†’is-hlevel 1 (is-left-quasigroup≃op-is-right-quasigroup) (hlevel 1)

Left quasigroup homomorphismsπŸ”—

We can continue dualizing to define a notion of homomorphism for left quasigroups, though we shall be much more terse in our development. Following the pattern of right quasigroups, we begin by defining left quasigroup structures.

A left quasigroup structure on a type is a binary operation such that is a left quasigroup.

record Left-quasigroup-on {β„“} (A : Type β„“) : Type β„“ where
  no-eta-equality
  field
    _⋆_ : A β†’ A β†’ A
    has-is-left-quasigroup : is-left-quasigroup _⋆_

  open is-left-quasigroup has-is-left-quasigroup public

A left quasigroup homomorphism between two left quasigroups is a function such that

record is-left-quasigroup-hom
  {β„“ β„“'} {A : Type β„“} {B : Type β„“'}
  (f : A β†’ B)
  (S : Left-quasigroup-on A) (T : Left-quasigroup-on B)
  : Type (β„“ βŠ” β„“')
  where
  no-eta-equality
  private
    module A = Left-quasigroup-on S
    module B = Left-quasigroup-on T
  field
    pres-⋆ : βˆ€ x y β†’ f (x A.⋆ y) ≑ f x B.⋆ f y

Dual to right quasigroups, preservation of left division follows from left-cancellativity.

  pres-⧡ : βˆ€ x y β†’ f (x A.⧡ y) ≑ f x B.⧡ f y
  pres-⧡ x y =
    B.⋆-cancell (f x) $
      f x B.⋆ f (x A.⧡ y)     β‰‘Λ˜βŸ¨ pres-⋆ x (x A.⧡ y) βŸ©β‰‘Λ˜
      f (x A.⋆ (x A.⧡ y))     β‰‘βŸ¨ ap f (A.⧡-invr x y) βŸ©β‰‘
      f y                     β‰‘Λ˜βŸ¨ B.⧡-invr (f x) (f y) βŸ©β‰‘Λ˜
      f x B.⋆ (f x B.⧡ f y) ∎

Left quasigroups are algebraic structures, so they form a thin structure over

  Left-quasigroup-structure : βˆ€ β„“ β†’ Thin-structure β„“ Left-quasigroup-on
  Left-quasigroup-structure β„“ .is-hom f A B .∣_∣ =
    is-left-quasigroup-hom f A B
  Left-quasigroup-structure β„“ .is-hom f A B .is-tr =
    is-left-quasigroup-hom-is-prop
  Left-quasigroup-structure β„“ .id-is-hom .pres-⋆ x y =
    refl
  Left-quasigroup-structure β„“ .∘-is-hom f g f-hom g-hom .pres-⋆ x y =
    ap f (g-hom .pres-⋆ x y) βˆ™ f-hom .pres-⋆ (g x) (g y)
  Left-quasigroup-structure β„“ .id-hom-unique p q =
    Left-quasigroup-on-pathp (ext (p .pres-⋆))

This observation lets us assemble left quasigroups into a category.

Left-quasigroups : βˆ€ β„“ β†’ Precategory (lsuc β„“) β„“
Left-quasigroups β„“ = Structured-objects (Left-quasigroup-structure β„“)

module Left-quasigroups {β„“} = Cat.Reasoning (Left-quasigroups β„“)

Left-quasigroup : (β„“ : Level) β†’ Type (lsuc β„“)
Left-quasigroup β„“ = Left-quasigroups.Ob {β„“}

QuasigroupsπŸ”—

A type equipped with a binary operation is a quasigroup if it is a left quasigroup and a right quasigroup.

record is-quasigroup {β„“} {A : Type β„“} (_⋆_ : A β†’ A β†’ A) : Type β„“ where
  no-eta-equality
  field
    has-is-left-quasigroup : is-left-quasigroup _⋆_
    has-is-right-quasigroup : is-right-quasigroup _⋆_

Quasigroups obey the latin square property: for every there exists a unique pair such that and

  ⋆-latin : βˆ€ x y β†’ is-contr (Ξ£[ l ∈ A ] Ξ£[ r ∈ A ] (l ⋆ x ≑ y Γ— x ⋆ r ≑ y))

The proof is an exercise in equivalence yoga: by definition, a quasigroup is both a left and right quasigroup, so multiplication on the left and right is an equivalence, so the types and are both contractible. Moreover, is equivalent to so the latter must also be contractible.

  ⋆-latin x y =
    Equiv→is-hlevel 0
      latin-eqv
      (Γ—-is-hlevel 0 (⋆-equivr x .is-eqv y) (⋆-equivl x .is-eqv y))
    where
      latin-eqv
        : (Ξ£[ l ∈ A ] Ξ£[ r ∈ A ] (l ⋆ x ≑ y Γ— x ⋆ r ≑ y))
        ≃ ((Ξ£[ l ∈ A ] l ⋆ x ≑ y) Γ— (Ξ£[ r ∈ A ] x ⋆ r ≑ y))
      latin-eqv =
        Ξ£[ l ∈ A ] Ξ£[ r ∈ A ] (l ⋆ x ≑ y Γ— x ⋆ r ≑ y)     β‰ƒβŸ¨ Ξ£-ap id≃ (Ξ» l β†’ Ξ£-swapβ‚‚) βŸ©β‰ƒ
        Ξ£[ l ∈ A ] (l ⋆ x ≑ y Γ— (Ξ£[ r ∈ A ] (x ⋆ r) ≑ y)) β‰ƒβŸ¨ Ξ£-assoc βŸ©β‰ƒ
        (Ξ£[ l ∈ A ] l ⋆ x ≑ y) Γ— (Ξ£[ r ∈ A ] x ⋆ r ≑ y)   β‰ƒβˆŽ

We also have the following pair of useful identities that relate left and right division.

  /-⧡-cancelr : βˆ€ x y β†’ x / (y ⧡ x) ≑ y
  /-⧡-cancelr x y =
    x / (y ⧡ x)             β‰‘Λ˜βŸ¨ ap (_/ (y ⧡ x)) (⧡-invr y x) βŸ©β‰‘Λ˜
    (y ⋆ (y ⧡ x)) / (y ⧡ x) β‰‘βŸ¨ /-invr y (y ⧡ x) βŸ©β‰‘
    y                       ∎

  /-⧡-cancell : βˆ€ x y β†’ (x / y) ⧡ x ≑ y
  /-⧡-cancell x y =
    (x / y) ⧡ x             β‰‘Λ˜βŸ¨ ap ((x / y) ⧡_) (/-invl x y) βŸ©β‰‘Λ˜
    (x / y) ⧡ ((x / y) ⋆ y) β‰‘βŸ¨ ⧡-invl (x / y) y βŸ©β‰‘
    y                       ∎

Quasigroup homomorphismsπŸ”—

A quasigroup structure on a type is a binary operation such that is a quasigroup.

record Quasigroup-on {β„“} (A : Type β„“) : Type β„“ where
  no-eta-equality
  field
    _⋆_ : A β†’ A β†’ A
    has-is-quasigroup : is-quasigroup _⋆_

  open is-quasigroup has-is-quasigroup public

A quasigroup homomorphism between two quasigroups is a function such that

record is-quasigroup-hom
  {β„“ β„“'} {A : Type β„“} {B : Type β„“'}
  (f : A β†’ B)
  (S : Quasigroup-on A) (T : Quasigroup-on B)
  : Type (β„“ βŠ” β„“')
  where
  no-eta-equality
  private
    module A = Quasigroup-on S
    module B = Quasigroup-on T
  field
    pres-⋆ : βˆ€ x y β†’ f (x A.⋆ y) ≑ f x B.⋆ f y

Quasigroups and quasigroup homomorphisms form a thin structure.

  Quasigroup-structure : βˆ€ β„“ β†’ Thin-structure β„“ Quasigroup-on
  Quasigroup-structure β„“ .is-hom f A B .∣_∣ =
    is-quasigroup-hom f A B
  Quasigroup-structure β„“ .is-hom f A B .is-tr =
    is-quasigroup-hom-is-prop
  Quasigroup-structure β„“ .id-is-hom .pres-⋆ x y =
    refl
  Quasigroup-structure β„“ .∘-is-hom f g f-hom g-hom .pres-⋆ x y =
    ap f (g-hom .pres-⋆ x y) βˆ™ f-hom .pres-⋆ (g x) (g y)
  Quasigroup-structure β„“ .id-hom-unique p q =
    Quasigroup-on-pathp (ext (p .pres-⋆))

This gives us a tidy way to construct the category of quasigroups.

Quasigroups : βˆ€ β„“ β†’ Precategory (lsuc β„“) β„“
Quasigroups β„“ = Structured-objects (Quasigroup-structure β„“)

module Quasigroups {β„“} = Cat.Reasoning (Quasigroups β„“)

Quasigroup : (β„“ : Level) β†’ Type (lsuc β„“)
Quasigroup β„“ = Quasigroups.Ob {β„“}

Constructing quasigroupsπŸ”—

The interfaces for is-quasigroup and Quasigroup-on are deeply nested and contain some duplication, so we introduce some helper functions for constructing them.

record make-quasigroup {β„“} (A : Type β„“) : Type β„“ where
  field
    quasigroup-is-set : is-set A
    _⋆_ : A β†’ A β†’ A
    _⧡_ : A β†’ A β†’ A
    _/_ : A β†’ A β†’ A

    /-invl : βˆ€ x y β†’ (x / y) ⋆ y ≑ x
    /-invr : βˆ€ x y β†’ (x ⋆ y) / y ≑ x
    ⧡-invr : βˆ€ x y β†’ x ⋆ (x ⧡ y) ≑ y
    ⧡-invl : βˆ€ x y β†’ x ⧡ (x ⋆ y) ≑ y