r/ProgrammingLanguages • • 2d 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 :)

68 Upvotes

27 comments sorted by

11

u/realguy2300000 2d 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

5

u/matthieum 1d ago

To format code as a code block, you need to indent it by 4 spaces.

8

u/AustinVelonaut Admiran 1d ago edited 1d 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?

4

u/realguy2300000 1d ago

it’s written slightly confusingly. here res is the namespace qualifier, as well as a val(separate). the compiler resolves them separately.

2

u/Haunting_Departure68 1d ago

Hi I wrote that snippet, the res here is how I named the result module. The beginning of the file has a "use <result.kav> as res" statement

1

u/AustinVelonaut Admiran 1d ago

Thanks -- that clears it up. I was thinking it might have something to do with result.kav, since it appears to be chaining a Result type (Ok | Err), but alternatively could also have something to do with the res val being linear, so it's referencing that.

8

u/kuviman 1d ago

Solved advent of code day 5 with this, I enjoyed trying it.

It seems there is not closures, only anonymous functions and no recursive lambdas either which made the code a but more verbose than necessary when trying to implement a bfs

Also I couldn't find a way to convert stralloc into cstr, I was trying to parse cmdline args to run code like ./main --part2 input

4

u/realguy2300000 1d ago

yeah, no closures yet. i need to think about how to implement when handling linear values. you also can’t convert a stralloc into a cstr yet but it’ll probably be added to the stdlib very soon.

i’m glad you enjoyed the language !! if you wait a little longer, it’ll be even better. also, i’d love to see your solution if you uploaded it anywhere.

4

u/kuviman 1d ago

Sure, here's the code: https://github.com/kuviman/advent-of-code-2026/blob/main/src/day05/main.kav

I was doing this on stream too if you want to check the vod

a couple more notes:

  • would be nice to omit val _ = for executing expression purely for side effects/freeing resources
  • logging requires io, so World needs to be present, bringing more verbosity. i just wanted some debug printing

i was implementing a queue using two stacks but didnt have time to properly test dependent stuff for having queue length encoded in its type, maybe i'll come back later to try doing smth like that

2

u/realguy2300000 21h ago

it was very informative to watch someone who isn’t me learn the language!! about the qbe issue you were having, nixos hasn’t packaged qbe 1.3 which added the extern keyword. on those two extra points, the first i’m planning on doing. the second one, i see the utility, but im hesitant to compromise the purity of the language just for some debug prints. i’ll have to think about it

8

u/titaniumalt 2d 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

7

u/realguy2300000 1d ago edited 1d 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/titaniumalt 1d ago

but like, giving the entire World to a function isn't optimal, and having an Fs type that readlines can use only to, well, read files without being able to do anything else is better, also allowing others to know what a function does by reading the function signature (so an Fs argument means said function can only do file system operations).

So, for example, you'd use readlines(w.fs, "hello.txt") and the signature for readlines would be something like Fs -> String -> Result [String].

and yes, i started wayland.fyi

so fucking cool, i was trying to get into neuswc but shko was too buggy and i dropped out, still considering tohu or making my own

3

u/particlemanwavegirl 2d ago

What's the memory management strategy? 

9

u/realguy2300000 2d 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.

4

u/Big-Astronaut-9510 1d ago

what about exceptions (like from mmap reads or something)? does it possibly leak then?

4

u/matthieum 1d ago

Does the language support references? (even second-class)

For example, I see list::sort(fn(n, m) { pair::snd(m) - pair::snd(n) }) which seems to hint that the lambda passed to sort doesn't consume the values it's passed.

3

u/Haunting_Departure68 1d ago

These are "borrows", the function can use the value as long as it doesn't free it. If I were to spell out the type of that lambda it is: fn(!(int, int), !(int, int)) -> int where the ! marks a borrow of the linear tuple. Compare it to the list::map below which does take a "consuming" lambda where I do the val (n, _) = p n, which does free the tuple

1

u/matthieum 9h ago

Can the function return a borrow? (ie fn(!int) -> !int)

9

u/Tasty_Replacement_29 Bau 2d 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).

3

u/Haunting_Departure68 1d ago

If anyone is using nix and wants to try it out I have a flake which provides the Kav compiler and stdlib with an overriden updated qbe!

https://github.com/ricardomaps/neu-nix

1

u/MadCervantes 1d ago

What's with the all lower caps? Is this an anti not detection technique?

1

u/dabrahams 20h ago

This sounds really cool, but I don’t quite understand how you can have a systems programming language with no mutation. I mean, a systems language needs to give you access to the machine at a low level, doesn’t it? Don’t you need to be able to write to a memory mapped register, or a shared memory buffer, to call it a systems programming language? Seems to me that merely being efficient isn’t sufficient.

3

u/Haunting_Departure68 16h ago

Purely functional doesn't mean no mutation. Haskell for instance is the prototypical "pure" language but it still has mutation, it's just hidden behind a monad. Kav does something similar with special values like the World shown in the snippet. It's a form of effect system

1

u/dabrahams 6h ago

Haskell gives you monads that let you write as though you have mutation when you “actually” (if that means anything in the face of the optimizer) don’t. That as-though-mutating monadic syntax is translated into pure Haskell that looks like any other, and then you can hope (somewhat absurdly) the optimizer will turn it back into the in-place mutation you wanted in the first place. That’s not a guarantee and more importantly for systems programming, it’s not the same as having direct access to the machine.

1

u/-snip- 4h ago

You totally misunderstood the point he was making

1

u/This_Background7442 1d ago

I really like the idea but the syntax looks cluttered to me. For me that style makes more sense for an imperative approach whereas something like:

solve : [n : Nat] -> List Int n -> Int
solve l =
  l
  |> List.map (\n -> (n, 1))
  |> Map.ofListWith Int.sub (_ n m -> n + m)
  |> Map.toList
  |> List.sort (\n m -> Pair.snd m - Pair.snd n)

Is much easier to read (to me) for functional style code.