r/ProgrammingLanguages • • 20h ago

Language announcement kav: a purely functional systems language making use of linear and indexed dependent types

56 Upvotes

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 :)


r/ProgrammingLanguages • • 19h ago

Discussion Compiling to C

10 Upvotes

Ive found a lot of languages on this sub compile to c code instead of assembly or a backend. I'm curious what the advantages / disadvantages are to this. I know c is pretty optimized so is this an easy way to make a performant language or does it just make it simpler. I dont know c but I know assembly and some QBE so what are the pros and cons to directly compiling to assembly ( is it just more flexibility? etc...)


r/ProgrammingLanguages • • 19h ago

Type Theories with Phase Distinctions

Thumbnail topos.institute
9 Upvotes

r/ProgrammingLanguages • • 8h ago

Discussion I am working on a C-targeted systems programming language, with prover and checker at the heart

5 Upvotes

Note: English isn't my native language. Please pardon mistakes.

I love C, Rust, Zig and Go, and I love what they bring to the table. But majority of languages give you a choice: eat the cake or have it. I started thinking from a new end, what if we can eat the cake and have it too?

I will be sharing some ideas with some code from my language.

First, post conditions exist without introducing a separate spec language:

fn broken(mut x: int) {
    defer assert(x > 0)
    x = 0
}

In such cases compiler throws an error: 'broken' breaks its promised return/exit rule.

Second, range rules can work along with types:

type Percent = int in 0..=100

fn clamp(x: int, lo: int, hi: int >= lo) -> int in lo..=hi {
    return min(max(x, lo), hi)
}

With this compiler gets the ability to optimize even further.

Third, there are no lifetimes and graphs can't leak because the compiler statically rejects strong ref cycles, so you break them with weak:

// Example code only
struct Node {
    val: int
    parent: Lock[Option[weak Node]]
    kids: [..]ref Node
}

fn no_parent() -> Option[weak Node] {
     return None 
}

fn main() {
    kid := ref Node{
        val: 2, 
        parent: Lock.new(no_parent()), 
        kids: [..]ref Node{}
    }
    mut ks := [..]ref Node{}
    ks.push(kid.clone())
    root := ref Node{
        val: 1, 
        parent: Lock.new(no_parent()),
        kids: ks
    }
    kid.parent.set(Some(weak root))
    print("Test OK!")
}

Fourth, you can keep the allocator local to a block where YOU care about it. Many code issues can be avoided this way, since compiler stops dangling pointers automatically. Anything allocated inside the with Alloc(arena) block can't escape it, so the compiler explicitly rejects returning or assigning directly to an outer variable, unless you explicitly copy:

use std.mem

fn sum_only(xs: []int) -> int {
    with Alloc.none {
        mut s := 0
        for x in xs {
            s += x
        }
        return s
    }
}

fn main() {
    arena := mem.Arena.new(4096)
    mut kept := [..]int{}
    with Alloc(arena) {
        mut xs := [..]int{}
        xs.push(10)
        xs.push(32)
        kept = xs.clone_in(mem.heap)
    }
    print("sum={sum_only(kept)}")
}

Fifth, inferred error for private functions, explicitness and transparency for public functions. ? does the heavy job of lifting and wraps automatically:

enum E1 { Bad }
enum E2 { Missing }
enum AppErr {
    One(err: E1)
    Two(err: E2)
}

fn inner(x: int) -> Result[int, _] {
    if x == 0 { return Err(E1.Bad) }
    if x < 0 { return Err(E2.Missing) }
    return Ok(x)
}

pub fn wrap(x: int) -> Result[int, AppErr] { return Ok(inner(x)?) }

Adding to this are a heavy prover which is a Z3 searching for proofs and a tiny checker which verifies them before any runtime check gets removed, which are still being fleshed out. To my realization they are the toughest, and the most important piece of work here.

Please share your opinion on the work so far, and what questions or criticisms do you have in this regard?