r/ProgrammingLanguages • • 32m ago

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

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?

3 Upvotes

2 comments sorted by

1

u/Lauel 12m ago edited 9m ago

I am sorry I forgot to share dependency permissions and the idea of manifests. There are no build scripts, and third party dependencies only get the OS access you explicitly allow in `package.toml`, checked at compile time. Please note this is my idea, I have not yet fleshed even this too. I placed my dreams, visions and desires before even seeing whether something like this can be built, because even if it doesn't an attempt was just as beautiful to me.

[dependencies]
ini = { version = "0.1", allow = [] }
fetch = { version = "0.2", allow = ["net"] }

Cherry picking aside, the language has a lot of issues to tackle, which are to some extent much harder than I expected:

  1. I have not mentioned but C is the ceiling. This is because the MIR emits C, and Clang compiles it. As a single man, I do not hold the time and workforce to have my own backend, or start with LLVM. My experience with LLVM is not good either. And as you would guess it is harder to match or beat C because the language is paying a "cost", while, I am trying to minimize the pain of that "cost", there is limit on how much optimized it can be. Because considering ARC, and runtime checks, what if those can't be proven, due to a low quality Prover and checker, and this ultimately slows the program? I haven't reached the benchmarking and comparison point yet, but I feel this will be an issue.
  2. There is no LLVM directly in the compiler, yet. I am split on this one, and I was yesterday reading how Zig, Rust and Odin handle LLVM. Emitting C keeps the compiler small and portable which is what I desire and it also lets me use Clang, GCC, or TCC. But what do you lose for powers from others? Code gen and optimization.
  3. While on paper it is easy to claim soundness of a language without lifetimes, but it is way harder I understood. The borrow checker was dropped, this ofcourse reduced friction but added tracking views, `mut` aliasing, `ref` cycles and arena escapes. I just fixed a bug with arena escapes seg faulting!
  4. Prover reach on real code. Even with a fast pass for everyday builds and Z3 only for release or strict mode, it is the complex real world loops which are hard to prove cleanly, WITHOUT bringing the very same friction I intended to avoid.

2

u/alphaglosined 3m ago

Putting pre and post conditions inside the body indeed doesn't matter vs at the signature during that functions analysis. Of course there is no way to know what is a pre and post condition if you do this (which can be fine).

But that isn't the only scenario where you must analyze it, consider what happens to functions without bodies or when you're looking at the documentation.