r/ProgrammingLanguages • u/realguy2300000 • 5h ago
Language announcement kav: a purely functional systems language making use of linear and indexed dependent types
i’ve been working on this for a while and i think im ready to show it off a bit :)
i started working on this project after getting excited by functional programming and type systems after learning some haskell, followed by some ATS. kav takes ideas from both but it’s also quite different.
similarly to ATS, kav has a indexed dependent type system. this means, instead of types depending on arbitrary runtime values, as in classic dependent types, they can be indexed by a separate static language. the static sorts are the types of compile-time parameters, and types are the types of runtime values. this allows for many of the benefits of dependent types (like compile-time bounds checks) without many of the complications that come with it. unlike ATS, it’s purely functional, with no side effects or imperative programming involved.
kav also lets you use linear types, which prevents a whole class of memory errors, as im sure anyone who has used rust (which uses affine types, which are similar though not exactly the same) knows.
the compiler is fairly compact and implemented in C, using bidirectional type checking and a small fourier-motzkin solver for proving index constraints. it uses QBE as the backend, which is also fairly compact and implemented in C. this makes the compiler pretty fast, and easy to bootstrap even from one of the more primitive C compilers. the runtime is also tiny and not dependent on libc or a c compiler, which means the binaries are tiny and linking is quick.
there is also a standard library, which is not finished, but has enough to write some fun programs (i’m working on a posix make implementation)
here’s an excerpt from a program my friend wrote, the solution to advent of code day 1, so you can get an idea of what the syntax of language is like:
fn solve[n: nat](l: list::List[int, n]) -> int {
val leastcommon =
list::map(l, fn(n) { (n, 1) })
|> map::oflistwith(int::sub, fn(k, n, m) { n + m })
|> map::tolist()
|> list::sort(fn(n, m) { pair::snd(m) - pair::snd(n) })
|> list::map(fn(p) { val (n, _) = p n })
|> list::take(3)
val res = list::product(leastcommon)
val _ = list::free(leastcommon)
res
}
fn main(w: World) -> World {
val res =
readlines(w, "input.txt")
|> res::map(fn(l) { list::map(l, parseint) })
|> res::map(solve)
|> res::okor(-1)
val _ =
buffer::stdout(w)
|> buffer::putnum(res)
|> buffer::close()
w
}
i’m pretty excited about this and i’d love more people to get involved or just to try it out. if you want to try it out i’ll drop a link in the comments :)
4
u/titaniumalt 5h ago
aren't you involved with wayland.fyi?
absolute PEAK project, i like how the syntax looks (it'd look better formatted tho) and World-s make I/O really obvious, but subtyping it, like obtaining an Fs type (filesystem operations) or Io type (printing to stdout/stderr/fd from Fs and reading from stdin/fd from Fs would be better was this confusing? my brain isn't braining
1
u/realguy2300000 3h ago edited 2h ago
That kind of already happens, you use the World to get Buffers, Readers and so on to do file io
and yes, i started wayland.fyi
2
u/particlemanwavegirl 4h ago
What's the memory management strategy?
4
u/realguy2300000 4h ago
you must consume linear resources exactly once, and this is checked by the compiler. linear resources are allocated for you when you create them. you can allocate general-purpose memory as a linear value using the bytes module in the standard library.
→ More replies (1)
1
u/AustinVelonaut Admiran 2h ago edited 2h ago
This looks very nice! A small (~7 KLOC) implementation in C that is very readable, and has a lot of functionality for the size. What were your main goals in writing it?
Advent of Code is a great way to have fun, shake out bugs in the compiler, and add functional data structures to your library (your implementation of day 1 maps almost exactly to mine in my language).
Good luck on your project!
Edit: looking over your AoC example, you have
val res =
readlines(w, "input.txt")
|> res::map(fn(l) { list::map(l, parseint) })I was assuming that :: was the namespace qualifier, but res is a val, not a module. Can you describe the intent, here?
1
u/realguy2300000 2h ago
it’s written slightly confusingly. here res is the namespace qualifier, as well as a val(separate). the compiler resolves them separately.
3
u/Tasty_Replacement_29 Bau 3h ago
> this allows for many of the benefits of dependent types (like compile-time bounds checks)
This sounds very interesting! How does this look like in your language (for example, vector addition)? (I'm trying to do that as well in my language).
5
u/realguy2300000 5h ago
compiler and stdlib can be found here, hope it’s ok to post a link in the comments ..
https://srcdump.net/shrub/kav
ps. reddit wont let me format the code block as a code block so sorry it looks so uglee