aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 19283ace0e44b58d8de77702bf0581ffc22a255f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# 🧚 puck - an experimental 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 consistent and succinct while performant: inspired by the syntax and metaprogramming of [Nim](https://nim-lang.org/), the error handling of [Swift](https://www.swift.org/), the memory management of [Rust](https://www.rust-lang.org/) and [Koka](https://koka-lang.github.io/), the async/await and comptime of [Zig](https://ziglang.org/), and the module system of [OCaml](https://ocaml.org/).

<details>
<summary><b>Example: Type Classes</b></summary>

```nim
# 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 class. Useful for dynamically dispatching errors.
pub type Err = class
  str(Self): str
  dbg(Self): str

## A Result type that uses dynamically dispatched errors.
## The Error may be any type implementing the Err class.
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 & "\""
```

</details>

<details>
<summary><b>Example: Metaprogramming</b></summary>

```nim
# Note: These declarations are adapted from the standard prelude.

## Syntactic sugar for dynamic result type declarations.
pub macro !(T: type) =
  quote Result[`T`]

## Indirect access. Propagates `Error`.
pub macro ?[T, E](self: Result[T, E]) =
  quote
    match `self`
    of Okay(x) then x
    of Error(e) then return Error(e)
```

</details>

<details open>
<summary><b>Example: Pattern Matching</b></summary>

```nim
## Opens the std.tables module for unqualified use.
use std.tables

pub type Value = str
pub type Ident = str
pub type Expr = ref union # tagged, algebraic unions
  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 Table[Ident, Value], expr: lent Expr): Value! =
  match expr # structural pattern matching and guards are supported but not shown
  of Literal(value) then
    Okay(value.clone) # ownership necessitates we explicitly clone
  of Variable(ident) then
    context.get(ident) # whitespace is significant but flexible
      .err("Could not find variable {} in context!"
      .fmt(ident)) # uniform function call syntax allows arbitrary piping/chaining
  of Application(body, arg) then
    if body of Abstraction(param, body as inner_body) then # compact matching with if
      context.set(param, context.clone.eval(arg)?)
      context.eval(inner_body) # all values must be handled: returns are implicit
    else
      Error("Expected Abstraction, found body {} and arg {}".fmt(body.clone, arg.clone))
  of Conditional(condition, then_branch, else_branch) then
    if context.clone.eval(condition)? == "true" then
      context.eval(then_case)
    else
      context.eval(else_case)
  of _ then Error("Invalid expression {}".fmt(expr))
```

</details>

<details>
<summary><b>Example: Modules</b></summary>

```nim
# The top-level module declaration can be elided if the file shares the same name.
pub mod tables =
  ## The Table class. Any sort of table - no matter the underlying
  ## representation - must implement these methods.
  pub type Table[K, V] = class
    get(lent Self, lent K): lent V?
    get(mut Self, lent K): mut V?
    set(mut Self, lent K, V): V?
    pop(mut Self, lent K): V?
    clear(mut Self)
    size(lent Self): uint
    init(varargs (K, V)): Self

  ...

  pub mod hashtable =
    use std.hashes

    pub type HashTable[K, V] = struct
      ...
```

</details>

## 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*, significant whitespace, and consistent scoping rules
- The **type system**, being modern and powerful with a strong emphasis on safety, algebraic data types, optional and result types, first-class functions, generics, interfaces, and modules
- The **memory management system**, implementing a model of strict ownership with an optimized reference counting escape hatch
- 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 syntax/semantics from a bevy of other 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](docs/BASIC.md) document lays out the fundamental semantics of Puck.
- The [syntax](docs/SYNTAX.md) document provides a deeper and formal look into the grammar of Puck.
- The [type system](docs/TYPES.md) document gives an in-depth analysis of Puck's extensive type system.
- The [modules](docs/MODULES.md) document provides a more detailed look at the first-class module system.
- The [error handling](docs/ERRORS.md) document gives a look at the various kinds of error handling available.
- The [memory management](docs/MEMORY_MANAGEMENT.md) document gives an overview of Puck's memory model.
- The [metaprogramming](docs/METAPROGRAMMING.md) document explains how using metaprogramming to extend the language works.
- The [asynchronous](docs/ASYNC.md) document gives an overview of Puck's colourless asynchronous support.
- The [interop](docs/INTEROP.md) document gives an overview of how the first-class language interop system works.
- The [standard library](docs/STDLIB.md) document provides an overview and examples of usage of the standard library.
- The [roadmap](docs/ROADMAP.md) 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](docs/ROADMAP.md) 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 base (un)safety tracking, exception handling, and async/await on a future effects system. It plans to integrate refinement types in the future as a basis for `range[]` types, and to explore safety and optimizations surrounding integer overflow.

## Primary References
- [The Rust I wanted had no future](https://graydon2.dreamwidth.org/307291.html)
- [Notes on a smaller Rust](https://boats.gitlab.io/blog/post/notes-on-a-smaller-rust/)
- [Notes on the next Rust compiler](https://matklad.github.io/2023/01/25/next-rust-compiler.html)