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

First class byte array support #378

Open
Riateche opened this issue May 15, 2020 · 3 comments
Open

First class byte array support #378

Riateche opened this issue May 15, 2020 · 3 comments

Comments

@Riateche
Copy link

For example, if I want to prove that a function returns a well-typed output on any file. What should the input type be? Data.Buffer has IO interface and can't be pattern matched on. String can't handle arbitrary bytes and doesn't allow to pattern match on individual bytes. I could use List, but it seems that there is no type for a byte either. If I use List (Fin 256), will the performance be reasonable?

Without a good way to work with bytes, I struggle to find any practical application for Idris. Almost any program has to work with bytes because a program would be useless without interacting with files or network. Working with compressed data (images, video), any binary file formats (like protobuf) is impossible.

Are there any workarounds I'm not aware of? I found the following:

@clayrat
Copy link
Contributor

clayrat commented May 15, 2020

A few years ago, I've helped making a port of Coq's formalized binary arithmetic here: https://github.com/sbp/idris-bi. This includes both unbounded and bounded inductively defined binaries. The original Coq library is used in CompCert verified C compiler.

One standing problem with that library is that there's no way to implement an analogue of a "nat hack", i.e. a compiler optimization mapping these transparently to machine words. I think after adding a "bin hack", idris-bi can serve as a reasonable foundation for proofs on bytes.

@Riateche
Copy link
Author

idris-bi seems powerful, but I'm not sure it provides the right semantics for byte manipulation. It's more convenient to view byte arrays as lists of bytes, not an arbitrary precision number. For example, limiting max value of the number representing a buffer usually doesn't make sense, while limiting max number of bytes is often needed. Matching on a few first bytes of the buffer also doesn't seem straightforward with idris-bi. So, really, a primitive byte type combined with List would probably suffice as the foundation, but a compiler optimization is indeed needed for this case.

@clayrat
Copy link
Contributor

clayrat commented May 16, 2020

Yes, you can make a List (BizMod2 8) for a byte array. If you want a primitive byte type, there's https://github.com/idris-lang/Idris-dev/blob/master/libs/base/Data/Bits.idr, but you won't be able to prove anything about it.

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

No branches or pull requests

2 participants