Skip to content
HN On Hacker News ↗

GitHub - stass/type-c: C11 with the types C is missing: variants, options, generics and exhaustive matching

▲ 28 points • 4 comments • by stass • 1d ago • HN discussion ↗

Pangram verdict · v3.3

We believe that this text is a mix of AI and human-written content.

80 %

AI likelihood · overall

AI
20% human-written 80% AI-generated
SEGMENTS · HUMAN 1 of 22
SEGMENTS · AI 21 of 22
WORD COUNT 1,400
PEAK AI % 100% · §1
Analyzed
Nov 23
backend: pangram/v3.3
Segments scanned
22 windows
avg 64 words each
Distribution
20 / 80%
human / AI fraction
Verdict
AI
Pangram v3.3

Article text · 1,400 words · 22 segments analyzed

Human AI-generated
§1 AI · 100%

Type-C is C11 with the types C is missing: variants that match checks, distinct types for ids and units, generic types, and errors as values with try and defer. Its compiler, typec, takes the command line of cc, turns Type-C into C that you can read, and hands that C to your C compiler. A C11 file is already a Type-C file, so a program adopts it one file at a time. Type-C is early: every example on this page works today, and the rest of the language is designed but not built yet (Status).

§2 Human · 0%

Installing With Homebrew, on macOS or Linux: brew install stass/tap/typec typec --version man typec Homebrew builds typec from the release source with OCaml and dune.

§3 AI · 100%

typec hands its C to cc unless told otherwise (Choosing the backend). To build it from a checkout instead, see Try it.

§4 AI · 100%

A first look An evaluator of arithmetic expression trees, which reports a division by zero or an overflow as a value instead of crashing. Like every C and Type-C example in this README, it is checked by the tests.

§5 AI · 100%

#include <limits.h> #include <stdio.h> type expr = | Num(int value) | Add(const expr *l, const expr *r) | Mul(const expr *l, const expr *r) | Div(const expr *l, const expr *r); type eval_error = | DivByZero | Overflow; /* With a 32-bit int, long long holds every sum, product and quotient. */ static result(int, eval_error) fit(long long n) { if (n < INT_MIN || n > INT_MAX) return (Err(Overflow)); return (Ok((int)n)); } static result(int, eval_error) eval(const expr *e) { int d; match (*e) { case Num(value): return (Ok(value)); case Add(l, r): return (fit((long long)try eval(l) + try eval(r))); case Mul(l, r): return (fit((long long)try eval(l) * try eval(r))); case Div(l, r): d = try eval(r); if (d == 0) return (Err(DivByZero)); return (fit((long long)try eval(l) / d)); } } static void show(const expr *e) { match (eval(e)) { case Ok(value): printf("%d\n", value); break; case Err(DivByZero): printf("division by zero\n"); break; case Err(Overflow): printf("overflow\n"); break; } } int main(void) { expr two = Num(2), three = Num(3), four = Num(4), zero = Num(0); expr sum = Add(&three, &four), ratio = Div(&sum, &zero); expr good = Mul(&two, &sum), bad = Mul(&two, &ratio); show(&good); /* 2 * (3 + 4) */ show(&bad); /* 2 * ((3 + 4) / 0) */ } It builds with the options of cc and prints: $ typec -std=c11 -Wall -Wextra -o eval eval.tc $ ./eval 14 division by zero What it shows: Variants. expr and eval_error are closed sums: a value is exactly one of the cases, and holds the fields of that case only. Code reaches the fields by naming them in a case, so Type-C code cannot read an Add as a Num.

§6 AI · 100%

An exhaustive match.

§7 AI · 100%

Each match here covers every case, with no default. Add a Sub case to expr, and the build stops at the match in eval until it handles Sub (Variants and match). In show, Err(DivByZero) nests one pattern in another, so one match tells the errors apart.

§8 AI · 100%

result and try.

§9 AI · 100%

result(int, eval_error) is a variant that every Type-C file has: Ok of an int, or Err of an eval_error. try eval(l) gives the value of an Ok, or returns that Err from the function at once: the division by zero inside bad comes up through the Mul above it to show (result and try). try stands inside an expression, as in try eval(l) + try eval(r): the operands of the tries run first, left to right, and the first error returns.

§10 AI · 100%

No runtime.

§11 AI · 100%

typec writes C: a variant becomes a structure of a tag and a union of the fields of its cases, a match a switch on the tag, and try an early return (What the C looks like). The rest is C: the headers, the pointers, printf, the arithmetic in long long, and the C compiler that builds it. What Type-C adds to C explains each new piece, and shows the mistakes each one rules out. The tutorial grows a C program into Type-C one feature at a time, and the language reference states the rules of each construct.

§12 AI · 100%

Why Type-C It stays C.

§13 AI · 100%

A C11 file keeps its meaning (principle 2). Type-C keeps C's declarations, statements, pointers, preprocessor and headers, and adds only what C has no way to say: closed sums, distinct types, type parameters, errors as values and cleanups. It does not redo what C already does. No module system, build tool or package manager of its own: a program stays translation units, headers and the build it has, and Type-C interfaces are headers that #include reads.

§14 AI · 100%

No runtime.

§15 AI · 100%

No hidden allocation, garbage collector, exceptions or runtime library, and nothing added to the link; principle 4 lists the few places where typec adds code that the source does not spell out.

§16 AI · 100%

One toolchain.

§17 AI · 100%

typec writes C and hands it to your GCC or Clang: the same headers, the same compiler, the same options but a few, the same ABI, and the same debugger, which steps through the lines of the .tc file (with gaps in the debug information still). Adopt it one file at a time. Rename one .c file to .tc, build with CC=typec and a build rule for .tc files, and use the new types where they help. The other files do not change (Adopting one file). Not another language at the boundary. Rust, Zig and OCaml call C through bindings or stubs, and C code gains their checks only when it is rewritten in them; C++ compiles only some C. Type-C's checks start in the one file that you rename. Safe Rust and OCaml are memory safe, and Type-C is not (Why not just...). Costs measured against hand-written C. On a real library, the Type-C rewrite parses as fast as a hand-written C twin, within the noise of the measurement, and its object code is 2.8% (Clang) to 11.9% (GCC) larger (Measured on a real library).

§18 AI · 100%

Claims only what is checked.

§19 AI · 100%

Each guarantee says what it depends on (principle 5), and the tests check each C and Type-C example on this page, on GCC and Clang. What Type-C adds to C Each feature below rules out a kind of mistake that C compiles without a word: as an error that the section shows, or by making the mistake impossible to write. Where a C warning catches a form of it, the section says which. The messages are those that typec prints, with its exit status in brackets (What typec reports). The cases of a variant are its constructors, as the messages call them. Variants and match A tagged union in C, with two bugs: enum shape_kind { CIRCLE, RECT, TRIANGLE }; struct shape { enum shape_kind kind; union { struct { double r; } circle; struct { double w, h; } rect; struct { double b, h; } triangle; } u; }; double area(struct shape s) { switch (s.kind) { case CIRCLE: return (3.14159 * s.u.circle.r * s.u.circle.r); case RECT: return (s.u.rect.w * s.u.circle.r); /* wrong member */ default: /* TRIANGLE forgotten */ return (0); } } $ cc -std=c11 -Wall -Wextra -c shape.c The same in Type-C. Each field belongs to its case, so the wrong-member read cannot be written, and the forgotten case stops the build: type shape = | Circle(double r) | Rect(double w, double h) | Triangle(double b, double h); double area(shape s) { match (s) { case Circle(r): return (3.14159 * r * r); case Rect(w, h): return (w * h); } } $ typec -std=c11 -Wall -Wextra -c area.tc area.tc:9: error: the match does not cover the constructor 'Triangle' of the variant 'shape' [match-coverage] 9 | match (s) { [80] C's -Wall warns of a forgotten enumerator only in a switch with no default, and only when the kind has an enumeration type; a default written for a kind that is none of the three, or an int kind, hides it (-Wswitch-enum warns with the default too). No flag that test/run/comparison.t tries warns of the wrong-member read. A match needs no default: it stops the program on a value whose tag is none of the cases, and a case added later is an error at every match that misses it.

§20 AI · 100%

Asking about one case: if (case ...) if (case ...)

§21 AI · 100%

tests for one case and binds its fields, and while (case ...)

§22 AI · 100%

loops while it holds.