Klar Programming Language
No ambiguity. No surprises. The code explains itself.
Klar is an AI-native application programming language designed for AI code generation. Every construct is self-describing, parseable at a glance, and has defined semantics.
Quick Start
fn main() -> i32 {
let message: string = "Hello, Klar!"
println(message)
return 0
}Save as hello.kl and run:
klar run hello.klDocumentation
Philosophy
- Design Philosophy - The three principles behind every design decision
Getting Started
- Installation - Build the compiler from source
- Hello World - Your first Klar program
- CLI Reference - Command-line interface
- REPL Guide - Interactive development
- AI Agent's Guide - Guide for AI agents generating Klar code
Language Guide
- Basics - Variables, types, expressions
- Functions - Functions, closures, generics
- Control Flow - if, match, for, while, loop
- Structs - Struct definitions and methods
- Enums - Enum definitions and pattern matching
- Traits - Trait system
- Generics - Generic types and functions
- Error Handling - Optional, Result, and the
?operator - Modules - Import system and visibility
Type System
- Primitives - i32, f64, bool, char, string
- Arrays - Fixed-size arrays
[T; N] - Tuples - Tuple types
(T1, T2, ...) - Optional - The
?Toptional type - Result - The
Result#[T, E]type - Collections - List, Map, Set, Range
- Smart Pointers - Rc, Arc, Cell
- I/O Types - File, streams, readers/writers
Memory Management
- Ownership - Ownership model
- References -
refandinoutparameters - Reference Counting - Rc and Arc patterns
Guides
- WebAssembly - Compile to WebAssembly
- Self-Hosting - Bootstrap architecture and compiler frontend port
Advanced Topics
- Operators - Complete operator reference
- Comptime - Compile-time programming
- Builtin Traits - Eq, Ordered, Clone, Drop, etc.
- FFI - Foreign Function Interface for C interop
Design
- Meta Layer - Self-describing code with
metaannotations - DSPy Opportunities - DSPy-inspired language features
- MoonBit Semantic Sampler - MoonBit-inspired design
- Nanolang Inspiration - Nanolang-inspired features
Appendix
Design Philosophy
Parseable at a Glance
Every construct is self-describing without surrounding context:
[i32; 3] // Array type - self-contained, no lookahead needed
and, or, not // Keywords over cryptic symbols
let x: i32 = 5 // Explicit type annotationsNo Undefined Behavior
Every operation has defined semantics:
let a: i32 = 100
let b: i32 = a +% 200 // Wrapping addition (explicit overflow handling)
let c: i32 = a +| 200 // Saturating addition (clamps at max)No Implicit Conversions
Type changes are always explicit:
let x: i64 = 42.as#[i64] // Safe conversion
let y: ?i32 = "42".to#[i32] // Fallible conversion
let z: i8 = x.trunc#[i8] // Truncating conversionOwnership Without Complexity
Memory safety through ownership, but simpler than Rust:
let data: Rc#[Data] = Rc.new(Data { ... }) // Reference counted
let copy: Rc#[Data] = data.clone() // Explicit cloneC Interoperability (FFI)
Call C functions and use C types with explicit unsafe blocks:
extern { fn puts(s: CStr) -> i32 }
fn main() -> i32 {
unsafe { puts("Hello from Klar FFI!".as_cstr()) }
return 0
}Version
Current version: 0.4.0
License
See LICENSE for details.