Skip to content

Disastrous expansion of operations in Data.Rational.Base #1753

Closed

Description

@MatthewDaggitt

The operations such as addition and multiplication in Data.Rational.Base are defined in terms of the numerator and denominator projection operations, e.g.

_+_ : ℚ → ℚ → ℚ
p + q = (↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)

This is beautiful and elegant, and arguably the right way to define them. However, it does mean that Agda unfolds these definitions automatically. By the time you have an expression x + y + z + a + b + c the unfolded expression is over 200 lines long.

This makes the rational numbers almost impossible to work with in large proofs. In recent proofs I've resorted to redefining everything that's needed under the abstract keyword.

Activity

  1. added this to the v2.0 milestone on Apr 15, 2022
  2. MatthewDaggitt commented on Apr 15, 2022

    @MatthewDaggitt
    CollaboratorAuthor

    See also the recent mailing list issue which was also caused by this problem.

  3. MatthewDaggitt commented on May 3, 2022

    @MatthewDaggitt
    CollaboratorAuthor

    Suggestion from @andreasabel: investigate using --no-eta-equality in record

  4. andreasabel commented on May 3, 2022

    @andreasabel
    Member

    Turn off eta-equality but still allow pattern matching:

    module Data.Rational.Base where
    ...
    record ℚ : Set where
      no-eta-equality; pattern
      constructor mkℚ
      ...

    As @gallais said, you need to match on some arguments to enable reductions:

    >-nonZero : ∀ {p} → p > 0ℚ → NonZero p
    >-nonZero {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.>-nonZero {toℚᵘ p} (ℚᵘ.*<* p<q)
    
    <-nonZero : ∀ {p} → p < 0ℚ → NonZero p
    <-nonZero {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.<-nonZero {toℚᵘ p} (ℚᵘ.*<* p<q)
    
    positive : ∀ {p} → p > 0ℚ → Positive p
    positive {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.positive {toℚᵘ p} (ℚᵘ.*<* p<q)
    
    negative : ∀ {p} → p < 0ℚ → Negative p
    negative {p@(mkℚ _ _ _)} (*<* p<q) = ℚᵘ.negative {toℚᵘ p} (ℚᵘ.*<* p<q)
    
    nonPositive : ∀ {p} → p ≤ 0ℚ → NonPositive p
    nonPositive {p@(mkℚ _ _ _)} (*≤* p≤q) = ℚᵘ.nonPositive {toℚᵘ p} (ℚᵘ.*≤* p≤q)
    
    nonNegative : ∀ {p} → p ≥ 0ℚ → NonNegative p
    nonNegative {p@(mkℚ _ _ _)} (*≤* p≤q) = ℚᵘ.nonNegative {toℚᵘ p} (ℚᵘ.*≤* p≤q)
    ...

    You can force matching with p@(mkQ _ _ _) to make things reduce more lazily:

    -- addition
    _+_ : ℚ → ℚ → ℚ
    p@(mkℚ _ _ _) + q@(mkℚ _ _ _) = (↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)
    
    -- multiplication
    _*_ : ℚ → ℚ → ℚ
    p@(mkℚ _ _ _) * q@(mkℚ _ _ _) = (↥ p ℤ.* ↥ q) / (↧ₙ p ℕ.* ↧ₙ q)
    ...
  5. andreasabel commented on May 5, 2022

    @andreasabel
    Member

    Alternative way to force matching: x@record{} (closer to Haskell, don't need to mention constructor name):

    _+_ : ℚ → ℚ → ℚ
    p@record{} + q@record{} = (↥ p ℤ.* ↧ q ℤ.+ ↥ q ℤ.* ↧ p) / (↧ₙ p ℕ.* ↧ₙ q)
  6. MatthewDaggitt commented on May 5, 2022

    @MatthewDaggitt
    CollaboratorAuthor

    Ooh that's even nicer. Yup, I'm happy with that solution.

  7. MatthewDaggitt commented on May 5, 2022

    @MatthewDaggitt
    CollaboratorAuthor

    Going to have a hack at this now.

  8. MatthewDaggitt commented on May 5, 2022

    @MatthewDaggitt
    CollaboratorAuthor

    Hmm, we have the same issue with multiplication in Data.Integer but that's appears harder to fix as there's no one constructor to pattern match on....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions