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

Operations over Binary random access lists #4

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mmontin
Copy link

@mmontin mmontin commented Nov 9, 2022

This PR proposes an implementations for the primitives on binary random access lists.
It includes the following changes:

  • addition of lookup and update for trees
  • addition of an indexed version of bral to allow for recursive functions over the structure
  • implementation of the actual primitives
    A question: what is the semantics of the remaining primitives, namely snoc, rear and front. My guess:
  • snoc adds a tree of size k+1 at the end of the structure
  • rear returns the last tree of the structure
  • front returns the first tree of the structure

@@ -3,9 +3,12 @@
open import Data.Nat
open import Data.Unit
open import Data.Empty
open import Data.Product
open import Data.Product
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dangling whitespace.

Comment on lines +34 to +35
_O : ∀ {n} (bs : DBin-g A (suc n)) → DBin-g A n
_[_]I : ∀ {n} (bs : DBin-g A (suc n)) (t : T A n) → DBin-g A n
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistic change, postpone to another PR performing a global and uniform update.

Comment on lines +8 to +11
data Bin : Set where
ϵ : Bin
_O : Bin → Bin
_I : Bin → Bin
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Stylistic change, line noise)

Comment on lines +20 to +21
Bin⇒ℕ-g (bs O) k = {- 0 * 2 ^ k + -} Bin⇒ℕ-g bs (suc k)
Bin⇒ℕ-g (bs I) k = {- 1 * -} 2 ^ k + Bin⇒ℕ-g bs (suc k)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Stylistic change, line noise)


Bin⇒Nat : Bin → ℕ
Bin⇒Nat b = Bin⇒Nat-g b 0
Bin⇒ℕ-g : Bin → ℕ → ℕ
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Stylistic change, line noise)


private
-- Appends a new Tree in a structure of trees
consTree : ∀ {A n} → Tree A n → RAL-g A n → RAL-g A n
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a lemma relating the size of the underlying containers: something like

forget (consTree l t) = BintoNat (size l) +Bin forget t

(Catherine will explicit need this)

consTree l (t [ l₁ ]I) = consTree (Node l l₁) t O

-- Removes (and returns) the first tree of the list of trees if not empty
unconsTree : ∀ {A n} → RAL-g A n → Maybe $ Tree A n × RAL-g A n
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, lemma about underlying sizes / binary representations.

unconsTree (e [ t ]I) = just (t , e O)

-- Retrieves an element in a list of trees
lookup-trees : ∀ {A n} → RAL-g A n → ℕ → Maybe A
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nasty. Would it be easier if we were indexing by a Bin instead of ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the implementations from the document without putting too much though in whether they could be optimized or not. There's a section after that which tries to improve the runtime of the functions which might introduce changes like that but I still have not looked into that. Now that my understanding of the structure is better, I could make such attempts.

(no _) → lookup-trees s (i ∸ pow)}

-- Replaces an element inside the structure
update-trees : ∀ {A n} → RAL-g A n → ℕ → A → RAL-g A n
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also nasty.

-- To be improved, we are working with binary trees after all
-- It is sad to transform to vectors to lookup, but it's late
-- and I don't want to handle decidability cases with 2^k-1
lookup-tree : ∀ {A k} → Tree A k → Fin (2 ^ k) → A
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be easier if we were not using a Fin (2 ^ k) but an inductive family indexed by k : Nat that represent numbers between 0 and 2^k:

data Finᵇ : Nat → Set where
  here : ∀ {k} → Finᵇ k
  left : ∀ {k} → Finᵇ k → Finᵇ (suc k)
  right : ∀ {k} → Finᵇ k → Finᵇ (suc k)

Going left means + 0 * 2 ^ k while going right means + 1 * 2 ^ k.

WARNING: untested thought.

@mmontin
Copy link
Author

mmontin commented Nov 13, 2022

Thanks for these comments @pedagand
Do I have the right interpretation for the primitives I mentioned, namely front, rear and snoc ?

@pedagand
Copy link
Owner

I think so, yes.

@mmontin
Copy link
Author

mmontin commented Nov 23, 2022

Thanks, I will fill these blanks and come up with less nasty implementations.

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

Successfully merging this pull request may close these issues.

2 participants