open import Algebra.Quasigroup
open import Algebra.Semigroup
open import Algebra.Group.Ab
open import Algebra.Monoid
open import Algebra.Group

open import Cat.Prelude hiding (_/_; _+_; _-_)

import Algebra.Monoid.Reasoning
module Algebra.Quasigroup.Instances.Group where

Groups and quasigroupsπŸ”—

private variable
  β„“ : Level
  A : Type β„“

Every group is a quasigroup, as multiplication in a group is an equivalence.

is-group→is-left-quasigroup
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-group _⋆_
  β†’ is-left-quasigroup _⋆_
is-groupβ†’is-left-quasigroup {_⋆_ = _⋆_} ⋆-group =
  ⋆-equivlβ†’is-left-quasigroup (hlevel 2) ⋆-equivl
  where open is-group ⋆-group

is-group→is-right-quasigroup
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-group _⋆_
  β†’ is-right-quasigroup _⋆_
is-groupβ†’is-right-quasigroup {_⋆_ = _⋆_} ⋆-group =
  ⋆-equivrβ†’is-right-quasigroup (hlevel 2) ⋆-equivr
  where open is-group ⋆-group

is-group→is-quasigroup
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ is-group _⋆_
  β†’ is-quasigroup _⋆_
is-groupβ†’is-quasigroup {_⋆_ = _⋆_} ⋆-group .is-quasigroup.has-is-left-quasigroup =
  is-groupβ†’is-left-quasigroup ⋆-group
is-groupβ†’is-quasigroup {_⋆_ = _⋆_} ⋆-group .is-quasigroup.has-is-right-quasigroup =
  is-groupβ†’is-right-quasigroup ⋆-group

Associative quasigroups as groupsπŸ”—

Conversely, if a quasigroup is merely inhabited and associative, then it is a group.

associative+is-quasigroup→is-group
  : βˆ€ {_⋆_ : A β†’ A β†’ A}
  β†’ βˆ₯ A βˆ₯
  β†’ (βˆ€ x y z β†’ x ⋆ (y ⋆ z) ≑ (x ⋆ y) ⋆ z)
  β†’ is-quasigroup _⋆_
  β†’ is-group _⋆_

We start by observing that is-group is an h-prop, so it suffices to consider the case where is inhabited.

associative+is-quasigroupβ†’is-group {A = A} {_⋆_ = _⋆_} βˆ₯aβˆ₯ ⋆-assoc ⋆-quasi =
  rec! (Ξ» a β†’ to-is-group (⋆-group a)) βˆ₯aβˆ₯
  where
    open is-quasigroup ⋆-quasi renaming (has-is-magma to ⋆-magma)
    open make-group

Next, a useful technical lemma: if then

    ⧡-/-comm
      : βˆ€ {a b x y}
      β†’ x ⋆ b ≑ a ⋆ y
      β†’ x ⧡ a ≑ b / y

The proof is an elegant bit of algebra. Recall that in a quasigroup, is left and right cancellative, so it suffices to show that:

Next, and so it remains to show that Crucially, is associative, so:

which completes the proof.

    ⧡-/-comm {a} {b} {x} {y} comm =
      ⋆-cancell x $ ⋆-cancelr y $
       (x ⋆ (x ⧡ a)) ⋆ y β‰‘βŸ¨ ap (_⋆ y) (⧡-invr x a) ⟩
       a ⋆ y             β‰‘Λ˜βŸ¨ comm ⟩
       x ⋆ b             β‰‘Λ˜βŸ¨ ap (x ⋆_) (/-invl b y) ⟩
       x ⋆ ((b / y) ⋆ y) β‰‘βŸ¨ ⋆-assoc x (b / y) y ⟩
       (x ⋆ (b / y)) ⋆ y ∎

With that out of the way, we shall show that

forms a group for any

    ⋆-group : A β†’ make-group A
    ⋆-group a .group-is-set = hlevel 2
    ⋆-group a .unit = a ⧡ a
    ⋆-group a .mul x y = x ⋆ y
    ⋆-group a .inv x = (a ⧡ a) / x

Associativity is one of our hypotheses, so all that remains is left inverses and left identities. Next, note that

is just one of the quasigroup axioms. Finally, our lemma gives us so we have:

which completes the proof.

    ⋆-group a .assoc x y z =
      ⋆-assoc x y z
    ⋆-group a .invl x =
      /-invl (a ⧡ a) x
    ⋆-group a .idl x =
      (a ⧡ a) ⋆ x β‰‘βŸ¨ ap (_⋆ x) (⧡-/-comm refl) ⟩
      (x / x) ⋆ x β‰‘βŸ¨ /-invl x x ⟩
      x ∎

Taking a step back, this result demonstrates that associative quasigroups behave like potentially empty groups.