Skip to content
This repository has been archived by the owner on Sep 7, 2018. It is now read-only.

Add safeIndexPow2 to safely index Vecs with 2^n elements #18

Closed
wants to merge 0 commits into from

Conversation

Ericson2314
Copy link
Contributor

My only concern is whether it is better to make the index be Unsigned or something to avoid negative out-of-bounds.

Fixes #17

@christiaanb
Copy link
Member

I think I prefer your suggestion of using Unsigned over the current pull-request. If we are going to have a safe indexing function, we might as well go all the way. Also, don't you think that maybe the Pow2 suffix is too much?

@Ericson2314
Copy link
Contributor Author

Ok, the only complication there is that Unsigned is not currently imported, but I hope it won't introduce a cycle. I figured I'd throw in the -Pow2 to differentiate from at which I hadn't heard of before, and which also is a "safe index", but I'm happy to chuck it.

A third idea that just occurred to me now is to make finite sets a la Agda, and rewrite at with it. Then the index given to at can be "dynamic", and we can make an Unsigned n -> Fin (n ^ 2) conversion.

@christiaanb
Copy link
Member

Yeah, probably best to update at to work on Fin, and make the suggested conversion from Unsigned to Fin. I suggest putting Fin in it's own module: CLaSH.Sized.Fin.

@Ericson2314
Copy link
Contributor Author

That all sounds good.

@Ericson2314
Copy link
Contributor Author

One last thing, now that we have arbitrary sized naturals in base, we could use them to implement BitVector, Unsigned, and Fin. Also, index_int could be index_word, and the negative check could be moved to (!!). Then at could skip the < 0 check.

Also, if we made a class like https://hackage.haskell.org/package/nats-0.1/docs/Numeric-Natural-Internal.html#t:Whole (that didn't make it to base-4.8, unfortunately), there there would be some use for a power of 2 safe index again.

@christiaanb
Copy link
Member

What module are these arbitrary size natural ins?

@Ericson2314
Copy link
Contributor Author

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

Successfully merging this pull request may close these issues.

Add Safe vector indexing
2 participants