🧚 Puck - A Programming Language
A place where I can make some bad decisions.
Puck is an experimental, memory safe, structurally typed, interface-first, imperative programming language. It aims to be clean and succinct while performant: inspired by the syntax and metaprogramming of Nim, the error handling of Swift, the performance and safety guarantees of Rust, the async/await and comptime of Zig, and the module system of OCaml.
Example: Interfaces
# Note: These declarations are adapted from the standard prelude.
## The Result type. Represents either success or failure.
pub type Result[T, E] = union
Okay(T)
Error(E)
## The Err interface. Useful for dynamically dispatching errors.
pub type Err = interface
str(Self): str
dbg(Self): str
## A Result type that uses dynamically dispatched errors.
## The Error may be any type implementing Err.
pub type Result[T] = Result[T, ref Err]
## Implements the dbg function for strings.
## As the str function is already defined for strings,
## this in turn means strings now implicitly implement Err.
pub func dbg(self: str) = "\"" & self & "\""
Example: Pattern Matching
## Opens the std.tables module for unqualified use.
use std.tables
pub type Value = string
pub type Ident = string
pub type Expr = ref union
Literal(Value)
Variable(Ident)
Abstraction(param: Ident, body: Expr)
Application(body: Expr, arg: Expr)
Conditional(condition: Expr,
then_branch: Expr, else_branch: Expr)
## Evaluate an Expr down to a Value, or return an Error.
pub func eval(context: mut HashTable[Ident, Value], expr: Expr): Result[Value]
match expr
of Literal(value): Okay(value)
of Variable(ident):
context.get(ident)
.err("Could not find variable {} in context!".fmt(ident))
of Application(body, arg):
if body of Abstraction(param, body as inner_body):
context.set(param, context.clone.eval(arg)?)
context.eval(inner_body)
else:
Error("Expected Abstraction, found body {} and argument {}".fmt(body, arg))
of Conditional(condition, then_branch, else_branch):
if context.clone.eval(condition)? == "true":
context.eval(then_case)
else:
context.eval(else_case)
of _: Error("Invalid expression {}".fmt(expr))
Example: Modules
...
Why Puck?
Puck is primarily a testing ground and should not be used in any important capacity. Don't use it. Everything is unimplemented and it will break underneath your feet.
That said: in the future, once somewhat stabilized, reasons why you would use it would be for:
- The syntax, aiming to be flexible, predictable, and succinct, through the use of uniform function call syntax and significant whitespace
- The type system, being modern and powerful with a strong emphasis on safety, optional and result types, algebraic data types, interfaces, and modules
- The memory management system, implementing a model of strict ownership while allowing individual fallbacks to reference counts if so desired
- The metaprogramming, providing integrated macros capable of rewriting the abstract syntax tree before or after typechecking
- The interop system, allowing foreign functions to be usable with native semantics from a bevy of languages
This is the language I keep in my head. It sprung from a series of unstructured notes I kept on language design, that finally became something more comprehensive in early 2023. The overarching goal is to provide a language capable of elegantly expressing any problem, and explore ownership and interop along the way.
How do I learn more?
- The basic usage document lays out the fundamental semantics of Puck.
- The syntax document provides a deeper and formal look into the grammar of Puck.
- The type system document gives an in-depth analysis of Puck's extensive type system.
- The modules document provides a more detailed look at the first-class module system.
- The memory management document gives an overview of Puck's memory model.
- The metaprogramming document explains how using metaprogramming to extend the language works.
- The asynchronous document gives an overview of Puck's colourless asynchronous support.
- The interop document gives an overview of how the first-class language interop system works.
- The standard library document provides an overview and examples of usage of the standard library.
- The roadmap provides a clear view of the current state and future plans of the language's development.
These are best read in order.
Note that all of these documents (and parts of this README) are written as if everything already exists. Nothing already exists! You can see the roadmap for an actual sense as to the state of the language. I simply found writing in the present tense to be an easier way to collect my thoughts.
This language does not currently integrate ideas from the following areas of active research: effects systems, refinement types, and dependent types. It plans to integrate refinement types in the future as a basis for range[]
types, and to explore safety and optimizations surrounding integer overflow.