Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inverse to Data.Fin.quotRem and properties #1422

Closed
Taneb opened this issue Feb 15, 2021 · 8 comments · Fixed by #1423
Closed

Add inverse to Data.Fin.quotRem and properties #1422

Taneb opened this issue Feb 15, 2021 · 8 comments · Fixed by #1423
Labels

Comments

@Taneb
Copy link
Member

Taneb commented Feb 15, 2021

It would be nice to be able to demonstrate that Fin (x * y) is isomorphic to Fin x × Fin y. This would be useful for proving, for example, in agda-categories that FinSetoids is cartesian, or in something that I'm working on, that you can combine two finite state machines in various way

The inverse function can be implemented as:

combine :  {n k}  Fin k  Fin n  Fin (n ℕ.* k)
combine x zero = inject+ _ x
combine x (suc y) = raise _ (combine x y)

With one direction of inverse proof as:

 :  {n k} x (y : Fin n)  quotRem k (combine x y) ≡ (x , y)
quotRem-combine {suc n} {k} x 0F rewrite splitAt-inject+ k (n ℕ.* k) x = refl
quotRem-combine {suc n} {k} x (suc y) rewrite splitAt-raise k (n ℕ.* k) (combine x y) = cong (Data.Product.map₂ suc) (quotRem-combine x y)

But I haven't been able to prove the other direction

@MatthewDaggitt
Copy link
Contributor

Without looking at much, @uzytkownik opened a recent PR (#1402) with some results about quotRem in. They might be relevant?

@MatthewDaggitt
Copy link
Contributor

In fact I think they implemented exactly the results you're looking for!

@Taneb
Copy link
Member Author

Taneb commented Feb 15, 2021

Ah! I hadn't seen that. The types match roughly what I had in mind, but the proofs seem very complicated... I'm going to hunt around for a simper proof

@Taneb
Copy link
Member Author

Taneb commented Feb 15, 2021

Finding a simpler proof turned out to be way easier than expected. I'll make a PR this evening

@gallais
Copy link
Member

gallais commented Feb 15, 2021

Can we please make combine take its arguments in the same order as _*_?
So combine : Fin n → Fin k → Fin (n ℕ.* k) rather than Fin k → Fin n → ...

@Taneb
Copy link
Member Author

Taneb commented Feb 15, 2021

Can we please make combine take its arguments in the same order as _*_?
So combine : Fin n → Fin k → Fin (n ℕ.* k) rather than Fin k → Fin n → ...

I have no objections to this. I was going with consistency with quotRem, which reverses them. Maybe that should also change?

@JacquesCarette
Copy link
Contributor

I have a horrible proof in pi-dual that I meant to clean up and add too. A nice proof is better!

I would suggest adding remQuot (and combine') instead of changing the ones that are there. quotRem outputs things in the 'right' order given its name!

@Taneb
Copy link
Member Author

Taneb commented Feb 15, 2021

Before doing anything with reversing multiplications, here's my proof of the other side of the inverse:

combine-quotRem :  {n} k (i : Fin (n ℕ.* k))  uncurry combine (quotRem {n} k i) ≡ i
combine-quotRem {suc n} k i with splitAt k i | P.inspect (splitAt k) i
... | inj₁ j | P.[ eq ] = begin
  join k (n ℕ.* k) (inj₁ j)      ≡˘⟨ cong (join k (n ℕ.* k)) eq ⟩
  join k (n ℕ.* k) (splitAt k i) ≡⟨ join-splitAt k (n ℕ.* k) i ⟩
  i                              ∎
  where open ≡-Reasoning
... | inj₂ j | P.[ eq ] = begin
  raise {n ℕ.* k} k (uncurry combine (quotRem {n} k j)) ≡⟨ cong (raise k) (combine-quotRem {n} k j) ⟩
  join k (n ℕ.* k) (inj₂ j)                             ≡˘⟨ cong (join k (n ℕ.* k)) eq ⟩
  join k (n ℕ.* k) (splitAt k i)                        ≡⟨ join-splitAt k (n ℕ.* k) i ⟩
  i                                                     ∎
  where open ≡-Reasoning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants