Skip to content

Generic GCD proposal #1305

@mechvel

Description

@mechvel

gcdProposal.lagda.zip
The proposal of a generic GCD notion for standard library


  1. Introduction
    ===========

Agda is nice, and generally, its library is nice.
But it is evident that the desgn for divisibility and GCD needs improvement.

A generic GCD notion can easily be defined on the base of lib-1.4
for any CancellativeSemiring
-- a CommutativeSemiring with the law of cancellation by a nonzero.
It provides an uniform definition and many property proofs for GCD for Nat, Integer, and many other domains.
The implementation is simple and will not increase the library code, nor the type check cost.

It is as follows.
....

module Algebra.Bundles ... where
...
record CancellativeSemiring α α≈ :  Set _
  where                                                                             
  field commutativeSemiring : CommutativeSemiring α α≈                           
  -----                                                  
  open CommutativeSemiring commutativeSemiring public                             
  open OfMonoid *-monoid using (_∣_)           
  open OfSemiring semiring using (LeftNZCancellative; ...)

  field cancelNZˡ : LeftNZCancellative  
  -----                                                        kind of integral semiring

...
module Algebera.Properties.CancellativeSemiring {α α≈}
                           (R : CancellativeSemiring α α≈) where          

open CancellativeSemiring R public using ...   renaming (Carrier to C)
...
open OfMonoid *-monoid using (_∣_; _∤_; ...)           

IsIrreducible :  Pred C (α ⊔ α≈)        
IsIrreducible p =  p ∤ 1#  ×  (∀ {x y} → (p ≈ x * y) → x ∣ 1# ⊎ y ∣ 1#) 

Coprime : Rel C (α ⊔ α≈)       
Coprime a b =  ∀ {c} → c ∣ a → c ∣ b → c ∣ 1#  

record GCD (a b : C) :  Set (α ⊔ α≈)    -- a result of gcd       
  where                      
  constructor gcdᶜ                                                           
                                                                                     
  field proper   : C                 -- proper gcd        
        divides₁ : proper ∣ a                                        
        divides₂ : proper ∣ b        
        greatest : ∀ {d} → (d ∣ a) → (d ∣ b) → (d ∣ proper)    

...

record GCDSemiring α α≈ :  Set (suc (α ⊔ α≈)) 
  where                                                                                        
  field cancellativeSemiring : CancellativeSemiring α α≈         
  -----                                 
  ...                                             
  field  gcd : (a b : Carrier) → GCD a b         

Several properties of GCD are highly usable, and their proofs can be easily implemented.
(I can provide a simple implementation).
...

  1. Avoid special notions of IsIrreducible, Coprime, GCD declared for ℕ, ℕᵇ, ℤ, ℤᵇ
    ==============================================================
    Instead they will import such from
    Algebra.Properties.CancellativeSemiring and Algebra.Bundles.
    Also the property proofs for these items for ℕ, ℕᵇ, ℤ, ℤᵇ to be imported as general.
    A large corresponding part in Data.Nat.GCD, Data.Integer.GCD and such
    will be replaced with import of the generic items.
    ...

Why CancellativeSemiring ?
Textbooks usually define the GCD notion for IntegralDomain. ℤ is of IntegralDomain. But ℕ is not. So the textbooks
define the GCD for ℕ individually. Also the relation between ℤ and ℕ is so evident, that textbooks do not even consider
the difference between these two GCD notions.

In Agda, proofs are fully formal, so these two notions need to be agreed in some nice way. The proposal does this by
introducing CancellativeSemiring and by providing all the necessary proofs. The condition of cancellation by a nonzero
is needed to provide the property of GCD uniqueness (in a certain sense), which actually makes the GCD notion sensible.

The full proposal text (233 lines) is attached.
gcdProposal.lagda.zip

Please, read it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions