Document not found (404)
+This URL is invalid, sorry. Please use the navigation bar or search to continue.
+ +From 22c4776f9fddef47a6ce3f309e4eafa2fbdc3a65 Mon Sep 17 00:00:00 2001 From: JJ Date: Sun, 28 Jan 2024 01:32:52 -0800 Subject: docs: host mdbook --- docs/book/.nojekyll | 1 + docs/book/404.html | 234 ++ docs/book/ASYNC.html | 285 +++ docs/book/ERRORS.html | 304 +++ docs/book/FontAwesome/css/font-awesome.css | 4 + docs/book/FontAwesome/fonts/FontAwesome.ttf | Bin 0 -> 165548 bytes .../book/FontAwesome/fonts/fontawesome-webfont.eot | Bin 0 -> 165742 bytes .../book/FontAwesome/fonts/fontawesome-webfont.svg | 2671 ++++++++++++++++++++ .../book/FontAwesome/fonts/fontawesome-webfont.ttf | Bin 0 -> 165548 bytes .../FontAwesome/fonts/fontawesome-webfont.woff | Bin 0 -> 98024 bytes .../FontAwesome/fonts/fontawesome-webfont.woff2 | Bin 0 -> 77160 bytes docs/book/INTEROP.html | 268 ++ docs/book/METAPROGRAMMING.html | 290 +++ docs/book/MODULES.html | 264 ++ docs/book/OVERVIEW.html | 405 +++ docs/book/SYNTAX.html | 412 +++ docs/book/TYPES.html | 601 +++++ docs/book/ayu-highlight.css | 78 + docs/book/book.js | 697 +++++ docs/book/clipboard.min.js | 7 + docs/book/css/chrome.css | 606 +++++ docs/book/css/general.css | 234 ++ docs/book/css/print.css | 50 + docs/book/css/variables.css | 279 ++ docs/book/elasticlunr.min.js | 10 + docs/book/favicon.png | Bin 0 -> 5679 bytes docs/book/favicon.svg | 22 + docs/book/fonts/OPEN-SANS-LICENSE.txt | 202 ++ docs/book/fonts/SOURCE-CODE-PRO-LICENSE.txt | 93 + docs/book/fonts/fonts.css | 100 + .../fonts/open-sans-v17-all-charsets-300.woff2 | Bin 0 -> 44352 bytes .../open-sans-v17-all-charsets-300italic.woff2 | Bin 0 -> 40656 bytes .../fonts/open-sans-v17-all-charsets-600.woff2 | Bin 0 -> 44936 bytes .../open-sans-v17-all-charsets-600italic.woff2 | Bin 0 -> 42120 bytes .../fonts/open-sans-v17-all-charsets-700.woff2 | Bin 0 -> 44988 bytes .../open-sans-v17-all-charsets-700italic.woff2 | Bin 0 -> 40800 bytes .../fonts/open-sans-v17-all-charsets-800.woff2 | Bin 0 -> 44536 bytes .../open-sans-v17-all-charsets-800italic.woff2 | Bin 0 -> 40812 bytes .../fonts/open-sans-v17-all-charsets-italic.woff2 | Bin 0 -> 41076 bytes .../fonts/open-sans-v17-all-charsets-regular.woff2 | Bin 0 -> 43236 bytes .../source-code-pro-v11-all-charsets-500.woff2 | Bin 0 -> 59140 bytes docs/book/highlight.css | 82 + docs/book/highlight.js | 53 + docs/book/index.html | 340 +++ docs/book/mark.min.js | 7 + docs/book/print.html | 1231 +++++++++ docs/book/searcher.js | 483 ++++ docs/book/searchindex.js | 1 + docs/book/searchindex.json | 1 + docs/book/tomorrow-night.css | 102 + docs/index.html | 340 +++ 51 files changed, 10757 insertions(+) create mode 100644 docs/book/.nojekyll create mode 100644 docs/book/404.html create mode 100644 docs/book/ASYNC.html create mode 100644 docs/book/ERRORS.html create mode 100644 docs/book/FontAwesome/css/font-awesome.css create mode 100644 docs/book/FontAwesome/fonts/FontAwesome.ttf create mode 100644 docs/book/FontAwesome/fonts/fontawesome-webfont.eot create mode 100644 docs/book/FontAwesome/fonts/fontawesome-webfont.svg create mode 100644 docs/book/FontAwesome/fonts/fontawesome-webfont.ttf create mode 100644 docs/book/FontAwesome/fonts/fontawesome-webfont.woff create mode 100644 docs/book/FontAwesome/fonts/fontawesome-webfont.woff2 create mode 100644 docs/book/INTEROP.html create mode 100644 docs/book/METAPROGRAMMING.html create mode 100644 docs/book/MODULES.html create mode 100644 docs/book/OVERVIEW.html create mode 100644 docs/book/SYNTAX.html create mode 100644 docs/book/TYPES.html create mode 100644 docs/book/ayu-highlight.css create mode 100644 docs/book/book.js create mode 100644 docs/book/clipboard.min.js create mode 100644 docs/book/css/chrome.css create mode 100644 docs/book/css/general.css create mode 100644 docs/book/css/print.css create mode 100644 docs/book/css/variables.css create mode 100644 docs/book/elasticlunr.min.js create mode 100644 docs/book/favicon.png create mode 100644 docs/book/favicon.svg create mode 100644 docs/book/fonts/OPEN-SANS-LICENSE.txt create mode 100644 docs/book/fonts/SOURCE-CODE-PRO-LICENSE.txt create mode 100644 docs/book/fonts/fonts.css create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-300.woff2 create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-300italic.woff2 create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-600.woff2 create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-600italic.woff2 create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-700.woff2 create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-700italic.woff2 create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-800.woff2 create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-800italic.woff2 create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-italic.woff2 create mode 100644 docs/book/fonts/open-sans-v17-all-charsets-regular.woff2 create mode 100644 docs/book/fonts/source-code-pro-v11-all-charsets-500.woff2 create mode 100644 docs/book/highlight.css create mode 100644 docs/book/highlight.js create mode 100644 docs/book/index.html create mode 100644 docs/book/mark.min.js create mode 100644 docs/book/print.html create mode 100644 docs/book/searcher.js create mode 100644 docs/book/searchindex.js create mode 100644 docs/book/searchindex.json create mode 100644 docs/book/tomorrow-night.css create mode 100644 docs/index.html diff --git a/docs/book/.nojekyll b/docs/book/.nojekyll new file mode 100644 index 0000000..f173110 --- /dev/null +++ b/docs/book/.nojekyll @@ -0,0 +1 @@ +This file makes sure that Github Pages doesn't process mdBook's output. diff --git a/docs/book/404.html b/docs/book/404.html new file mode 100644 index 0000000..ad6526d --- /dev/null +++ b/docs/book/404.html @@ -0,0 +1,234 @@ + + +
+ + +This URL is invalid, sorry. Please use the navigation bar or search to continue.
+ +++! This section is a draft. Many important details have yet to be ironed out.
+
Puck has colourless async/await, heavily inspired by Zig's implementation.
+pub func fetch(url: str): str = ...
+
+let a: Future[T] = async fetch_html()
+let b: T = a.await
+let c: T = await async fetch_html()
+
+Puck's async implementation relies heavily on its metaprogramming system.
+The async
macro will wrap a call returning T
in a Future[T]
and compute it asynchronously. The await
function takes in a Future[T]
and will block until it returns a value (or error). The Future[T]
type is opaque, containing internal information useful for the async
and await
routines.
pub macro async(self): Future[T] =
+ ... todo ...
+
+pub func await[T](self: Future[T]): T =
+ while not self.ready:
+ block
+ self.value! # apply callbacks?
+
+This implementation differs from standard async/await implementations quite a bit. +In particular, this means there is no concept of an "async function" - any block of computation that resolves to a value can be made asynchronous. This allows for "anonymous" async functions, among other things.
+This (packaging up blocks of code to suspend and resume arbitrarily) is hard, and requires particular portable intermediate structures out of the compiler. Luckily, Zig is doing all of the R&D here. Some design decisions to consider revolve around APIs. The Linux kernel interface (among other things) provides both synchronous and asynchronous versions of its API, and fast code will use one or the other, depending if it is in an async context. Zig works around this by way of a known global constant that low-level functions read at compile time to determine whether to operate on synchronous APIs or asynchronous APIs. This is... not great. But what's better?
+ +It should be noted that async is not the same as threading, nor is it solely useful in the presence of threads...
+How threads work deserves somewhat of a mention...
+References:
+Is async worth having separate from effect handlers? I think so...
+ +Puck's error handling is shamelessly stolen from Swift. It uses a combination of Option
/Result
types and try
/catch
statements, and leans somewhat on Puck's metaprogramming capabilities.
There are several ways to handle errors in Puck. If the error is encoded in the type, one can:
+match
on the errorif ... of
?
!
If an error is thrown, one must explicitly handle (or disregard) it with a try/catch
block or risk runtime failure. This method of error handling may feel more familiar to Java programmers.
Puck provides Option[T]
and a Result[T, E]
types, imported by default. These are union
types and so must be pattern matched upon to be useful: but the standard library provides a bevy of helper functions.
+Two in particular are of note. The ?
operator unwraps a Result or propagates its error up a function call (and may only be used in type-appropriate contexts). The !
operator unwraps an Option or Result directly or throws an exception in the case of None or Error.
pub macro `?`[T, E](self: Result[T, E]) =
+ quote:
+ match `self`
+ of Okay(x): x
+ of Error(e): return Error(e)
+
+pub func `!`[T](self: Option[T]): T =
+ match self
+ of Some(x): x
+ of None: raise EmptyValue
+
+pub func `!`[T, E](self: Result[T, E]): T =
+ of Okay(x): x
+ of Error(e): raise e
+
+The utility of the provided helpers in std.options
and std.results
should not be understated. While encoding errors into the type system may appear restrictive at first glance, some syntactic sugar goes a long way in writing compact and idiomatic code. Java programmers in particular are urged to give type-first errors a try, before falling back on unwraps and try
/catch
.
A notable helpful type is the aliasing of Result[T]
to Result[T, ref Err]
, for when the particular error does not matter. This breaks try
/catch
exhaustion (as ref Err
denotes a reference to any Error), but is particularly useful when used in conjunction with the propagation operator.
Errors raised by raise
/throw
(or subsequently the !
operator) must be explicitly caught and handled via a try
/catch
/finally
statement.
+If an exception is not handled within a function body, the function must be explicitly marked as a throwing function via the yeet
prefix (name to be determined). The compiler will statically determine which exceptions in particular are thrown from any given function, and enforce them to be explicitly handled or explicitly ignored.
Despite functioning here as exceptions: errors remain types. An error thrown from an unwrapped Result[T, E]
is of type E
. catch
statements, then, may pattern match upon possible errors, behaving similarly to of
branches.
try:
+ ...
+catch "Error":
+ ...
+finally:
+ ...
+
+This creates a distinction between two types of error handling, working in sync: functional error handling with Option and Result types, and object-oriented error handling with catchable exceptions. These styles may be swapped between with minimal syntactic overhead. Libraries, however, should universally use Option
/Result
, as this provides the best support for both styles.
Some functions do not return a value but can still fail: for example, setters.
+This can make it difficult to do monadic error handling elegantly: one could return a Result[void, E]
, but...
pub func set[T](self: list[T], i: uint, val: T) =
+ if i > self.length:
+ raise IndexOutOfBounds
+ self.data.raw_set(offset = i, val)
+
+There exist errors from which a program can not reasonably recover. These are the following:
+Assertation Failure
: a call to an assert
function has returned false at runtime.Out of Memory
: the executable is out of memory.Stack Overflow
: the executable has overflowed the stack.They are not recoverable, but the user should be aware of them as possible failure conditions.
+References: Error Handling in Swift
+ +++! This section is a draft. Many important details have yet to be ironed out.
+
A major goal of Puck is minimal-overhead language interoperability while maintaining type safety.
+There are three issues that complicate language interop:
+For the first, Puck uses what amounts to a combination of ownership and reference counting: and thus it is exchangeable in this regard with Nim (same system), Rust (ownership), Swift (reference counting), and many others. (It should be noted that ownership systems are broadly compatible with reference counting systems).
+For the second, Puck has a type system of similar capability to that of Rust, Nim, and Swift: and thus interop with those languages should be straightforward for the user. Its type system is strictly more powerful than that of Python or C, and so interop requires additional help. Its type system is equally as powerful as but somewhat orthogonal to Java's, and so interop is a little more difficult.
+For the third, Puck is being written at the same time as the crABI ABI spec is in development. crABI promises a C-ABI-compatible, cross-language ABI spec, which would dramatically simplify the task of linking to object files produced by other languages. It is being led by the Rust language team, and both the Nim and Swift teams have expressed interest in it, which bodes quite well for its future.
+Languages often focus on interop from purely technical details. This is very important: but typically no thought is given to usability (and often none can be, for necessity of compiler support), and so using foreign function interfaces very much feel like using foreign interfaces. Puck attempts to change that.
+...todo...
+Existing systems to learn from:
+callfunc
Puck has rich metaprogramming support, heavily inspired by Nim. Many features that would have to be at the compiler level in most languages (error propagation ?
, std.fmt.print
, async
/await
) are instead implemented as macros within the standard library.
Macros take in fragments of the AST within their scope, transform them with arbitrary compile-time code, and spit back out transformed AST fragments to be injected and checked for validity. This is similar to what Nim and the Lisp family of languages do. +By keeping an intentionally minimal AST, some things not possible to express in literal code may be expressible in the AST: in particular, bindings can be injected in many places they could not be injected in ordinarily. (A minimal AST also has the benefit of being quite predictable.)
+Macros may not change Puck's syntax: the syntax is flexible enough. Code is syntactically checked (parsed), but not semantically checked (typechecked) before being passed to macros. This may change in the future. Macros have the same scope as other routines, that is:
+function scope: takes the arguments within or following a function call
+macro print(params: varargs) =
+ for param in params:
+ result.add(quote(stdout.write(`params`.str)))
+
+print(1, 2, 3, 4)
+print "hello", " ", "world", "!"
+
+block scope: takes the expression following a colon as a single argument
+macro my_macro(body)
+
+my_macro:
+ 1
+ 2
+ 3
+ 4
+
+operator scope: takes one or two parameters either as a postfix (one parameter) or an infix (two parameters) operator
+macro +=(a, b) =
+ quote:
+ `a` = `a` + `b`
+
+a += b
+
+Macros typically take a list of parameters without types, but they optionally may be given a type to constrain the usage of a macro. Regardless: as macros operate at compile time, their parameters are not instances of a type, but rather an Expr
expression representing a portion of the abstract syntax tree.
+Similarly, macros always return an Expr
to be injected into the abstract syntax tree despite the usual absence of an explicit return type, but the return type may be specified to additionally typecheck the returned Expr
.
+As macros operate at compile time, they may not inspect the values that their parameters evaluate to. However, parameters may be marked with static[T]
: in which case they will be treated like parameters in functions: as values. (note static parameters may be written as static[T]
or static T
.) There are many restrictions on what might be static
parameters. Currently, it is constrained to literals i.e. 1
, "hello"
, etc, though this will hopefully be expanded to any function that may be evaluated statically in the future.
macro ?[T, E](self: Result[T, E]) =
+ quote:
+ match self
+ of Okay(x): x
+ of Error(e): return Error(e)
+
+func meow: Result[bool, ref Err] =
+ let a = stdin.get()?
+
+The quote
macro is special. It takes in literal code and returns that code as the AST. Within quoted data, backticks may be used to break out in order to evaluate and inject arbitrary code: though the code must evaluate to an expression of type Expr
.
+The Expr
type is available from std.ast
, as are many helpers, and combined they provide the construction of arbitrary syntax trees (indeed, quote
relies on and emits types of it). It is a union
type with its variants directly corresponding to the variants of the internal AST of Puck.
+Construction of macros can be difficult: and so several helpers are provided to ease debugging. The Debug
and Display
interfaces are implemented for abstract syntax trees: dbg
will print a representation of the passed syntax tree as an object, and print
will print a best-effort representation as literal code. Together with quote
and optionally with static
, these can be used to quickly get the representation of arbitrary code.
++! This section is incomplete. Proceed with caution.
+
Puck has a first-class module system, inspired by such expressive designs in the ML family.
+
+Modules package up code for use by others. Identifiers known at compile time may be part of a module signature: these being constants, functions, macros, types, and other modules themselves. They may be made accessible to external users by prefixing them with the pub
keyword. Files are modules, named with their filename. The mod
keyword followed by an identifier and an indented block of code explicitly defines a module, inside of the current module. Modules are first class: they may be bound to constants (having the type : mod
) and publicly exported, or bound to local variables and passed into functions for who knows what purpose.
The use
keyword lets you use other modules. The use
keyword imports public symbols from the specified module into the current scope unqualified. This runs contrary to expectations coming from most other languages: from Python to Standard ML, the standard notion of an "import" usually puts the imported symbols behind another symbol to avoid "polluting the namespace". As Puck is strongly typed and allows overloading, however, the author sees no reason for namespace pollution to be of concern. These unqualified imports have the added benefit of making uniform function call syntax more widely accessible. It is inevitable that identifier conflicts will exist on occasion, of course: when this happens, the compiler will force qualification (this then does restrict uniform function call syntax).
+Nonetheless, if qualification of imports is so desired, an alternative approach is available - binding a module to a constant. Both the standard library and external libraries are available behind identifiers without use of use
: std
and lib
, respectively. (FFI and local modules will likely use more identifiers, but this is not hammered out yet.) A submodule - for example, std.net
- may be bound in a constant as const net = std.net
, providing all of the modules' public identifiers for use, as fields of the constant net
. We will see this construction to be extraordinarily helpful in crafting high-level public APIs for libraries later on.
Multiple modules can be imported at once, i.e. use std.[logs, tests]
, use lib.crypto, lib.http
. The standard namespaces (std
, lib
) deserve more than a passing mention. There are several of these: std
for the standard library, lib
for all external libraries, crate
for the top-level namespace of a project (subject to change), this
for the current containing module (subject to change)... In addition: there are a suite of language namespaces, for FFI - rust
, nim
, and swift
preliminarily - that give access to libraries from other languages. Recall that imports are unqualified - so use std
will allow use of the standard library without the std
qualifier (not recommended: several modules have common names), and use lib
will dump every library it can find into the global namespace (even less recommended).
A major goal of Puck's module system is to allow the same level of expressiveness as the ML family, while cutting down on the extraneous syntax and boilerplate needed to do so. As such, access modifiers are written directly inline with their declaration, and the file system structure is reused to form an implicit module system for internal use. This - particularly the former - limits the structure a module can expose at first glance, but we will see later that interfaces recoup much of this lost specificity.
+We mentioned that the filesystem forms an implicit module structure. This begets a couple of design choices. Module names must be lowercase, for compatibility with case-insensitive filesystems. Both a file and a folder with the same name can exist. Files within the aforementioned folder are treated as submodules of the aforementioned file. This again restricts the sorts of module structures we can build, but we will again see later that this restriction can be bypassed.
+The this
and crate
modules are useful for this implicit structure...
...
+The filesystem provides an implicit module structure, but it may not be the one you want to expose to users.
+...
+ +Puck is an experimental, high-level, memory-safe, statically-typed, whitespace-sensitive, interface-oriented, imperative programming language with functional underpinnings.
+It attempts to explore designs in making functional programming paradigms comfortable to those familiar with imperative and object-oriented languages, as well as deal with some more technical problems along the way, such as integrated refinement types and typesafe interop.
+This is the language I keep in my head. It reflects the way I think and reason about code.
+I do hope others enjoy it.
+let ident: int = 413
+# type annotations are optional
+var phrase = "Hello, world!"
+const compile_time = when linux: "linux" else: "windows"
+
+Variables may be mutable (var
), immutable (let
), or compile-time evaluated and immutable (const
).
+Type annotations on variables and other bindings follow the name of the binding (with : Type
), and are typically optional.
+Variables are conventionally written in snake_case
. Types are conventionally written in PascalCase
.
+The type system is comprehensive, and complex enough to warrant delaying full coverage of until the end. Some basic types are of note, however:
int
, uint
: signed and unsigned integers
+i8
/i16
/i32
/i64
/i128
: their fixed-size counterpartsu8
/u16
/u32
/u64
/u128
: their fixed-size counterpartsfloat
, decimal
: floating-point numbers
+f32
/f64
/f128
: their fixed-size counterpartsdec64
/dec128
: their fixed-size counterpartsbyte
: an alias to u8
, representing one bytechr
: an alias to u32
, representing one Unicode characterbool
: defined as union[false, true]
array[T, S]
: primitive fixed-size (S
) arrayslist[T]
: dynamic listsstr
: mutable strings. internally a list[byte]
, externally a list[chr]
slice[T]
: borrowed "views" into the three types aboveComments are declared with #
and run until the end of the line.
+Documentation comments are declared with ##
and may be parsed by language servers and other tooling.
+Multi-line comments are declared with #[ ]#
and may be nested.
+Taking cues from the Lisp family of languages, any expression may be commented out with a preceding #;
.
+Functions are declared with the func
keyword. They take an (optional) list of generic parameters (in brackets), an (optional) list of parameters (in parentheses), and must be annotated with a return type if they return a type. Every function parameter must be annotated with a type. Their type may optionally be prefixed with either mut
or static
: denoting a mutable type (types are copied into functions and thus immutable by default), or a static type (known to the compiler at compile time, and usable in const
exprs). Generic parameters may each be optionally annotated with a type functioning as a constraint.
Whitespace is significant but flexible: functions may be declared entirely on one line if so desired. A new level of indentation after certain tokens (:
, =
) denotes a new level of scope. There are some places where arbitrary indentation and line breaks are allowed - as a general rule of thumb, after operators, commas, and opening parentheses. The particular rules governing indentation may be found in the syntax guide.
func inc(self: list[int], by: int): list[int] =
+ self.map(x => x + by)
+
+print inc([1, 2, 3], len("four")) # 5, 6, 7
+print [1, 2, 3].inc(1) # 2, 3, 4
+print [1].len # 1
+
+Puck supports uniform function call syntax: and so any function may be called using the typical syntax for method calls, that is, the first parameter of any function may be appended with a .
and moved to precede it, in the style of a typical method. (There are no methods in Puck. All functions are statically dispatched. This may change in the future.)
This allows for a number of syntactic cleanups. Arbitrary functions with compatible types may be chained with no need for a special pipe operator. Object field access, module member access, and function calls are unified, reducing the need for getters and setters. Given a first type, IDEs using dot-autocomplete can fill in all the functions defined for that type. Programmers from object-oriented languages may find the lack of classes more bearable. UFCS is implemented in shockingly few languages, and so Puck joins the tiny club that previously consisted of just D and Nim.
+
+Boolean logic and integer operations are standard and as one would expect out of a typed language: and
, or
, xor
, not
, shl
, shr
, +
, -
, *
, /
, <
, >
, <=
, >=
, div
, mod
, rem
. Notably:
and
/or
/not
/shl
/shr
are used instead of the symbolic &&
/||
/!
/<<
/>>
div
while floating point division uses /
%
is absent and replaced with distinct modulus and remainder operatorsThe above operations are performed with operators, special functions that take a prefixed first argument and (often) a suffixed second argument. Custom operators may be implemented, but they must consist of only a combination of the symbols =
+
-
*
/
<
>
@
$
~
&
%
|
!
?
^
\
for the purpose of keeping the grammar context-free. They are are declared identically to functions.
Term (in)equality is expressed with the ==
and !=
operators. Type equality is expressed with is
. Subtyping relations may be queried with of
, which has the additional property of introducing new bindings in the current scope (more on this in the types document).
let phrase: str = "I am a string! Wheeee! ✨"
+for c in phrase:
+ stdout.write(c) # I am a string! Wheeee! ✨
+for b in phrase.bytes():
+ stdout.write(b.chr) # Error: cannot convert between u8 and chr
+print phrase.last() # ✨
+
+String concatenation uses a distinct &
operator rather than overloading the +
operator (as the complement -
has no natural meaning for strings). Strings are unified, mutable, internally a byte array, externally a char array, and are stored as a pointer to heap data after their length and capacity (fat pointer). Chars are four bytes and represent a Unicode character in UTF-8 encoding. Slices of strings are stored as a length followed by a pointer to string data, and have non-trivial interactions with the memory management system. More details can be found in the type system overview.
+Basic conditional control flow uses standard if
/elif
/else
statements. The when
statement provides a compile-time if
. It also takes elif
and else
branches and is syntactic sugar for an if
statement within a static
block (more on those later).
All values in Puck must be handled, or explicitly discarded. This allows for conditional statements and many other control flow constructs to function as expressions, and evaluate to a value, when an unbound value is left at the end of each of their branches' scopes. This is particularly relevant for functions, where it is often idiomatic to omit an explicit return
statement. There is no attempt made to differentiate without context, and so expressions and statements often look identical in syntax.
+Exhaustive structural pattern matching is available with the match
/of
statement, and is particularly useful for the struct
and union
types. of
branches of a match
statement take a pattern, of which the unbound identifiers within will be injected into the branch's scope. Multiple patterns may be used for one branch provided they all bind the same identifiers of the same type. Branches may be guarded with the where
keyword, which takes a conditional, and will necessarily remove the branch from exhaustivity checks.
The of
statement also stands on its own as an operator for querying subtype equality. Used as a conditional in if
statements or while
loops, it retains the variable injection properties of its match
counterpart. This allows it to be used as a compact alternative to if let
statements in other languages.
func may_fail: Result[T, ref Err]
+
+Error handling is done via a fusion of imperative try
/catch
statements and functional Option
/Result
types, with much syntactic sugar. Functions may raise
errors, but should return Option[T]
or Result[T, E]
types instead by convention. The compiler will note functions that raise
errors, and force explicit qualification of them via try
/catch
statements.
A bevy of helper functions and macros are available for Option
/Result
types, and are documented and available in the std.options
and std.results
modules (included in the prelude by default). Two in particular are of note: the ?
macro accesses the inner value of a Result[T, E]
or propagates (returns in context) the Error(e)
, and the !
accesses the inner value of an Option[T]
/ Result[T, E]
or raises an error on None
/ the specific Error(e)
. Both operators take one parameter and so are postfix. (There is additionally another ?
postfix macro, taking in a type, as a shorthand for Option[T]
)
The utility of the ?
macro is readily apparent to anyone who has written code in Rust or Swift. The utility of the !
function is perhaps less so obvious. These errors raised by !
, however, are known to the compiler: and they may be comprehensively caught by a single or sequence of catch
statements. This allows for users used to a try
/catch
error handling style to do so with ease, with only the need to add one additional character to a function call.
More details may be found in error handling overview.
+loop:
+ print "This will never normally exit."
+ break
+
+for i in 0 .. 3: # exclusive
+ for j in 0 ..= 3: # inclusive
+ print "{} {}".fmt(i, j)
+
+Three types of loops are available: for
loops, while
loops, and infinite loops (loop
loops). For loops take a binding (which may be structural, see pattern matching) and an iterable object and will loop until the iterable object is spent. While loops take a condition that is executed upon the beginning of each iteration to determine whether to keep looping. Infinite loops are infinite are infinite are infinite are infinite are infinite are infinite and must be manually broken out of.
There is no special concept of iterators: iterable objects are any object that implements the Iter[T]
interface (more on those in the type system document), that is, provides a self.next()
function returning an Option[T]
. As such, iterators are first-class constructs. For loops can be thought of as while loops that unwrap the result of the next()
function and end iteration upon a None
value. While loops, in turn, can be thought of as infinite loops with an explicit conditional break.
The break
keyword immediately breaks out of the current loop, and the continue
keyword immediately jumps to the next iteration of the current loop. Loops may be used in conjunction with blocks for more fine-grained control flow manipulation.
block:
+ statement
+
+let x = block:
+ let y = read_input()
+ transform_input(y)
+
+block foo:
+ for i in 0 ..= 100:
+ block bar:
+ if i == 10: break foo
+ print i
+
+Blocks provide arbitrary scope manipulation. They may be labelled or unlabelled. The break
keyword additionally functions inside of blocks and without any parameters will jump out of the current enclosing block (or loop). It may also take a block label as a parameter for fine-grained scope control.
+Code is segmented into modules. Modules may be made explicit with the mod
keyword followed by a name, but there is also an implicit module structure in every codebase that follows the structure and naming of the local filesystem. For compatibility with filesystems, and for consistency, module names are exclusively lowercase (following the same rules as Windows).
A module can be imported into another module by use of the use
keyword, taking a path to a module or modules. Contrary to the majority of languages ex. Python, unqualified imports are encouraged - in fact, are idiomatic (and the default) - type-based disambiguation and official LSP support are intended to remove any ambiguity.
Within a module, functions, types, constants, and other modules may be exported for use by other modules with the pub
keyword. All such identifiers are private by default and only accessible module-locally without. Modules are first-class and may be bound, inspected, modified, and returned. As such, imported modules may be re-exported for use by other modules by binding them to a public constant, i.e. use my_module; pub const my_module = my_module
.
More details may be found in the modules document.
+
+Compile-time programming may be done via the previously-mentioned const
keyword and when
statements: or via const
blocks. All code within a const
block is evaluated at compile-time and all assignments and allocations made are propagated to the compiled binary as static data.
Further compile-time programming may be done via metaprogramming: compile-time manipulation of the abstract syntax tree. The macro system is complex, and a description may be found in the metaprogramming document.
+
+The async system is colourblind: the special async
macro will turn any function call returning a T
into an asynchronous call returning a Future[T]
. The special await
function will wait for any Future[T]
and return a T
(or an error). Async support is included in the standard library in std.async
in order to allow for competing implementations. More details may be found in the async document.
Threading support is complex and also regulated to external libraries. OS-provided primitives will likely provide a spawn
function, and there will be substantial restrictions for memory safety. I really haven't given much thought to this.
+Details on memory safety, references and pointers, and deep optimizations may be found in the memory management overview. +The memory model intertwines deeply with the type system.
+
+Finally, a few notes on the type system are in order.
+Types are declared with the type
keyword and are transparent aliases.
+That is, type Foo = Bar
means that any function defined for Bar
is defined for Foo
- that is, objects of type Foo
can be used any time an object of type Bar
is called for.
+If such behavior is not desired, the distinct
keyword forces explicit qualification and conversion of types. type Foo = distinct Baz
will force a type Foo
to be wrapped in a call to the constructor Baz()
before being passed to such functions.
Types, like functions, can be generic: declared with "holes" that may be filled in with other types upon usage. A type must have all its holes filled before it can be constructed. The syntax for generics in types much resembles the syntax for generics in functions, and constraints and the like also apply.
+type MyStruct = struct
+ a: str
+ b: str
+type MyTuple = tuple[str, b: str]
+
+let a: MyTuple = ("hello", "world")
+print a.1 # world
+print a.b # world
+
+Struct and tuple types are declared with struct[<fields>]
and tuple[<fields>]
, respectively. Their declarations make them look similar at a glance, but they differ fairly fundamentally. Structs are unordered, and every field must be named. They may be constructed with {}
brackets. Tuples are ordered and so field names are optional - names are just syntactic sugar for positional access. Tuples may be constructed with ()
parenthesis.
I am undecided whether to allow structural subtyping: that is, {a: Type, b: Type, c: Type}
being valid in a context expecting {a: Type, b: Type}
. This has benefits (multiple inheritance with no boilerplate) but also downsides (obvious).
It is worth noting that there is no concept of pub
at a field level on structs - a type is either fully transparent, or fully opaque. This is because such partial transparency breaks with structural initialization (how could one provide for hidden fields?). An idiomatic workaround is to model the desired field structure with a public-facing interface.
type Expr = union
+ Literal(int)
+ Variable(str)
+ Abstraction(param: str, body: ref Expr)
+ Application(body: ref Expr, arg: ref Expr)
+
+Union types are composed of a list of variants. Each variant has a tag and an inner type the union wraps over. Before the inner type can be accessed, the tag must be pattern matched upon, in order to handle all possible values. These are also known as sum types or tagged unions in other languages.
+Union types are the bread and butter of structural pattern matching. Composed with structs and tuples, unions provide for a very general programming construct commonly referred to as an algebraic data type. +This is often useful as an idiomatic and safer replacement for inheritance.
+pub type Iter[T] = interface
+ next(mut Self): T?
+
+pub type Peek[T] = interface
+ next(mut Self): T?
+ peek(mut Self): T?
+ peek_nth(mut Self, int): T?
+
+Interface types function much as type classes in Haskell or traits in Rust do. They are not concrete types, and cannot be constructed - instead, their utility is via indirection, as parameters or as ref
types, providing constraints that some concrete type must meet. They consist of a list of function signatures, implementations of which must exist for the given type in order to compile.
Their major difference, however, is that Puck's interfaces are implicit: there is no impl
block that implementations of their associated functions have to go under. If functions for a concrete type exist satisfying some interface, the type implements that interface. This does run the risk of accidentally implementing an interface one does not desire to, but the author believes such situations are few and far between, well worth the decreased syntactic and semantic complexity, and mitigatable with tactical usage of the distinct
keyword.
As the compiler makes no such distinction between fields and single-argument functions on a type when determining identifier conflicts, interfaces similarly make no such distinction. They do distinguish mutable and immutable parameters, those being part of the type signature.
+Interfaces are widely used throughout the standard library to provide general implementations of such conveniences like iteration, debug and display printing, generic error handling, and much more.
+ +++! This section is incomplete. Proceed with caution.
+
The following keywords are reserved:
+let
var
const
if
elif
else
match
of
loop
while
for
in
block
break
continue
return
func
mut
static
varargs
pub
mod
use
as
try
catch
finally
macro
quote
when
type
distinct
ref
struct
tuple
union
enum
interface
impl
object
class
concept
auto
empty
effect
case
suspend
resume
spawn
pool
thread
closure
cyclic
acyclic
sink
move
destroy
copy
trace
deepcopy
The following identifiers are in use by the standard prelude:
+not
and
or
xor
shl
shr
div
mod
rem
+
-
*
/
<
>
<=
>=
==
!=
is
async
await
int
uint
float
+i8
i16
i32
i64
i128
u8
u16
u32
u64
u128
f32
f64
f128
dec64
dec128
bool
byte
char
str
void
never
&
(string append)The following punctuation is taken:
+=
(assignment).
(chaining),
(params);
(statements):
(types)#
(comment)_
(unused bindings)|
(generics)\
(string/char escaping)()
(params, tuples){}
(scope, structs)[]
(generics, lists)""
(strings)''
(chars)``
(unquoting)~
@
$
%
We now shall take a look at a more formal description of Puck's syntax.
+Syntax rules are described in extended Backus–Naur form (EBNF): however, most rules surrounding whitespace, and scope, and line breaks, are modified to how they would appear after a lexing step.
+Ident ::= (Letter | '_') (Letter | Digit | '_')*
+Letter ::= 'A'..'Z' | 'a'..'z' | '\x80'..'\xff' # todo
+Digit ::= '0'..'9'
+
+Int ::= '-'? (DecLit | HexLit | OctLit | BinLit)
+Float ::= '-'? DecLit '.' DecLit
+BinLit ::= '0b' BinDigit ('_'? BinDigit)*
+OctLit ::= '0o' OctDigit ('_'? OctDigit)*
+HexLit ::= '0x' HexDigit ('_'? HexDigit)*
+DecLit ::= Digit ('_'? Digit)*
+BinDigit ::= '0'..'1'
+OctDigit ::= '0'..'7'
+HexDigit ::= Digit | 'A'..'F' | 'a'..'f'
+
+CHAR ::= '\'' (PRINT - '\'' | '\\\'')* '\''
+STRING ::= SINGLE_LINE_STRING | MULTI_LINE_STRING
+COMMENT ::= SINGLE_LINE_COMMENT | MULTI_LINE_COMMENT | EXPRESSION_COMMENT
+SINGLE_LINE_STRING ::= '"' (PRINT - '"' | '\\"')* '"'
+MULTI_LINE_STRING ::= '"""' (PRINT | '\n' | '\r')* '"""'
+SINGLE_LINE_COMMENT ::= '#' PRINT*
+MULTI_LINE_COMMENT ::= '#[' (PRINT | '\n' | '\r' | MULTI_LINE_COMMENT)* ']#'
+EXPRESSION_COMMENT ::= '#;' SINGLE_STMT
+PRINT ::= LETTER | DIGIT | OPR |
+ '"' | '#' | "'" | '(' | ')' | # notably the dual of OPR
+ ',' | ';' | '[' | ']' | '_' |
+ '`' | '{' | '}' | ' ' | '\t'
+
+Value ::= Int | Float | String | Char | Array | Tuple | Struct
+Array ::= '[' (Expr (',' Expr)*)? ']'
+Tuple ::= '(' (Ident ':')? Expr (',' (Ident ':')? Expr)* ')'
+Struct ::= '{' Ident ':' Expr (',' Ident ':' Expr)* '}'
+
+Decl ::= Let | Var | Const | Func | Type
+Let ::= 'let' Pattern Annotation? '=' Expr
+Var ::= 'var' Pattern Annotation? ('=' Expr)?
+Const ::= 'pub'? 'const' Pattern Annotation? '=' Expr
+Pattern ::= Char | String | Number | Float | Ident | '(' Pattern (',' Pattern)* ')'
+ Ident '(' Pattern (',' Pattern)* ')'
+
+Func ::= 'pub'? 'func' Ident Generics? Parameters? Annotation? '=' Body
+Macro ::= 'pub'? 'macro' Ident Generics? Parameters? Annotation? '=' Body
+Generics ::= '[' Ident Annotation? (',' Ident Annotation?)* ']'
+Parameters ::= '(' Ident Annotation? (',' Ident Annotation?)* ')'
+Annotation ::= ':' Type
+
+TypeDecl ::= 'pub'? 'type' Ident Generics? '=' Type
+Type ::= StructType | TupleType | EnumType | UnionType | Interface |
+ (('distinct' | 'ref' | 'ptr' | 'mut' | 'static') (Type | ('[' Type ']'))?)
+StructType ::= 'struct' ('[' Ident ':' Type (',' Ident ':' Type)* ']')?
+UnionType ::= 'union' ('[' Ident ':' Type (',' Ident ':' Type)* ']')?
+TupleType ::= 'tuple' ('[' (Ident ':')? Type (',' (Ident ':')? Type)* ']')?
+EnumType ::= 'enum' ('[' Ident ('=' Expr)? (',' Ident ('=' Expr)?)* ']')?
+Interface ::= 'interface' ('[' Signature (',' Signature)* ']')?
+Signature ::= Ident Generics? ('(' Type (',' Type)* ')')? Annotation?
+
+If ::= 'if' Expr ':' Body ('elif' Expr ':' Body)* ('else' ':' Body)?
+When ::= 'when' Expr ':' Body ('elif' Expr ':' Body)* ('else' ':' Body)?
+Try ::= 'try' ':' Body
+ ('except' Ident ('as' Ident)? (',' Ident ('as' Ident)?)*) ':' Body)*
+ ('finally' ':' Body)?
+Match ::= 'match' Expr ('of' Pattern (',' Pattern)* ('where' Expr)? ':' Body)+
+Block ::= 'block' Ident? ':' Body
+Block ::= 'static' ':' Body
+Loop ::= 'loop' ':' Body
+While ::= 'while' Expr ':' Body
+For ::= 'for' Pattern 'in' Expr Body
+
+Mod ::= 'pub'? 'mod' Ident ':' Body
+Use ::= 'use' Ident ('/' Ident)* ('/' ('[' Ident (',' Ident)* ']'))?
+
+Operator ::= 'and' | 'or' | 'not' | 'xor' | 'shl' | 'shr' |
+ 'div' | 'mod' | 'rem' | 'is' | 'in' |
+ Opr+
+Opr ::= '=' | '+' | '-' | '*' | '/' | '<' | '>' |
+ '@' | '$' | '~' | '&' | '%' | '|' |
+ '!' | '?' | '^' | '.' | ':' | '\\'
+
+Call ::= Ident ('[' Call (',' Call)* ']')? ('(' (Ident '=')? Call (',' (Ident '=')? Call)* ')')? |
+ Ident Call (',' Call)* |
+ Call Operator Call? |
+ Call ':' Body
+Expr ::= Let | Var | Const | Func | Type | Mod | Use | Block | Static |
+ For | While | Loop | If | When | Try | Match | Call
+Body ::= Expr | ('{' Expr (';' Expr)* '}')
+
+References:
+ + +++! This section needs a rewrite. Proceed with low standards.
+
Puck has a comprehensive static type system, inspired by the likes of Nim, Rust, and Swift.
+Basic types can be one-of:
+bool
: internally an enum.int
: integer number. x bits of precision by default.
+uint
: same as int
, but unsigned for more precision.i8
, i16
, i32
, i64
, i128
: specified integer sizeu8
, u16
, u32
, u64
, u128
: specified integer sizefloat
: floating-point number.
+f32
, f64
: specified float sizesdecimal
: precision decimal number.
+dec32
, dec64
, dec128
: specified decimal sizesbyte
: an alias to u8
.char
: a distinct alias to u32
. For working with Unicode. str
: a string type. mutable. internally a byte-array: externally a char-array.void
: an internal type designating the absence of a value. often elided. never
: a type that denotes functions that do not return. distinct from returning nothing. bool
and int
/uint
/float
and siblings (and subsequently byte
and char
) are all considered primitive types and are always copied (unless passed as mutable). More on when parameters are passed by value vs. passed by reference can be found in the memory management document.
Primitive types combine with str
, void
, and never
to form basic types. void
and never
will rarely be referenced by name: instead, the absence of a type typically implicitly denotes one or the other. Still, having a name is helpful in some situations.
todo
+Strings are:
+They are also quite complicated. Puck has full support for Unicode and wishes to be intuitive, performant, and safe, as all languages wish to be. Strings present a problem that much effort has been expended on in (primarily) Swift and Rust to solve.
+Abstract types, broadly speaking, are types described by their behavior rather than their implementation. They are more commonly know as abstract data types: which is confusingly similar to "algebraic data types", another term for the advanced types they are built out of under the hood. We refer to them here as "abstract types" to mitigate some confusion.
+Iterable types can be one-of:
+array[S, T]
: Fixed-size arrays. Can only contain one type T
. Of a fixed size S
and cannot grow/shrink, but can mutate. Initialized in-place with [a, b, c]
.list[T]
: Dynamic arrays. Can only contain one type T
. May grow/shrink dynamically. Initialized in-place with [a, b, c]
. (this is the same as arrays!) slice[T]
: Slices. Used to represent a "view" into some sequence of elements of type T
. Cannot be directly constructed: they are unsized. Cannot grow/shrink, but their elements may be accessed and mutated. As they are underlyingly a reference to an array or list, they must not outlive the data they reference: this is non-trivial, and so slices interact in complex ways with the memory management system. str
: Strings. Described above. They are alternatively treated as either list[byte]
or list[char]
, depending on who's asking. Initialized in-place with "abc"
.These iterable types are commonly used, and bits and pieces of compiler magic are used here and there (mostly around initialization, and ownership) to ease use. All of these types are some sort of sequence: and implement the Iter
interface, and so can be iterated (hence the name).
Unlike the iterable types above, these abstract types do not have a necessarily straightforward or best implementation, and so multiple implementations are provided in the standard library.
+These abstract data types can be one-of:
+BitSet[T]
: high-performance sets implemented as a bit array.
+HashSet[T]
instead.AssocTable[T, U]
: simple symbol tables implemented as an association list.
+HashTable[T, U]
instead.HashSet[T]
: standard hash sets.HashTable[T, U]
: standard hash tables.These abstract types do not have a natural ordering, unlike the iterable types above, and thus do not implement Iter
. Despite this: for utility an elems()
iterator based on a normalization of the elements is provided for set
and HashSet
, and keys()
, values()
, and pairs()
iterators are provided for table
and HashTable
(based on a normalization of the keys).
Some types are only valid when being passed to a function, or in similar contexts. +No variables may be assigned these types, nor may any function return them. +These are monomorphized into more specific functions at compile-time if needed.
+Parameter types can be one-of:
+func foo(a: mut str)
: Marks a parameter as mutable (parameters are immutable by default). Passed as a ref
if not one already.func foo(a: static str)
: Denotes a parameter whose value must be known at compile-time. Useful in macros, and with when
for writing generic code.func foo[T](a: list[T], b: T)
: The standard implementation of generics, where a parameter's exact type is not listed, and instead statically dispatched based on usage.func foo(a: str | int | float)
: A basic implementation of generics, where a parameter can be one-of several listed types. The only allowed operations on such parameters are those shared by each type. Makes for particularly straightforward monomorphization. func foo(a: (int, int) -> int)
: First-class functions. All functions are first class - function declarations implicitly have this type, and may be bound in variable declarations. However, the function type is only terribly useful as a parameter type.func foo(a: slice[...])
: Slices of existing lists, strings, and arrays. Generic over length. These are references under the hood, may be either immutable or mutable (with mut
), and interact non-trivially with Puck's ownership system.func foo(a: Stack[int])
: Implicit typeclasses. More in the interfaces section.
+type Stack[T] = interface[push(mut Self, T); pop(mut Self): T]
func foo(a: struct)
: Included, special interfaces for being generic over advanced types. These include struct
, tuple
, union
, enum
, interface
, and others.Several of these parameter types - specifically, slices, functions, and interfaces - share a common trait: they are not sized. The exact size of the type is not generally known until compilation - and in some cases, not even during compilation! As the size is not always rigorously known, problems arise when attempting to construct these parameter types or compose them with other types: and so this is disallowed. They may still be used with indirection, however - detailed in the section on reference types.
+Functions can take a generic type, that is, be defined for a number of types at once:
+func add[T](a: list[T], b: T) =
+ return a.add(b)
+
+func length[T](a: T) =
+ return a.len # monomorphizes based on usage.
+ # lots of things use .len, but only a few called by this do.
+ # throws a warning if exported for lack of specitivity.
+
+func length(a: str | list) =
+ return a.len
+
+The syntax for generics is func
, ident
, followed by the names of the generic parameters in brackets [T, U, V]
, followed by the function's parameters (which may then refer to the generic types).
+Generics are replaced with concrete types at compile time (monomorphization) based on their usage in function calls within the main function body.
Constrained generics have two syntaxes: the constraint can be defined directly on a parameter, leaving off the [T]
box, or it may be defined within the box as [T: int | float]
for easy reuse in the parameters.
Other constructions like modules and type declarations themselves may also be generic.
+Types are typically constructed by value on the stack. That is, without any level of indirection: and so type declarations that recursively refer to one another, or involve unsized types (notably including parameter types), would not be allowed. However, Puck provides two avenues for indirection.
+Reference types can be one-of:
+ref T
: An automatically-managed reference to type T
. This is a pointer of size uint
(native).ptr T
: A manually-managed pointer to type T
. (very) unsafe. The compiler will yell at you.type BinaryTree = ref struct
+ left: BinaryTree
+ right: BinaryTree
+
+type AbstractTree[T] = interface
+ func left(self: Self): Option[AbstractTree[T]]
+ func right(self: Self): Option[AbstractTree[T]]
+ func data(self: Self): T
+
+type AbstractRoot[T] = struct
+ left: ref AbstractTree[T]
+ right: ref AbstractTree[T]
+
+# allowed, but unsafe & strongly discouraged
+type UnsafeTree = struct
+ left: ptr UnsafeTree
+ right: ptr UnsafeTree
+
+The ref
prefix may be placed at the top level of type declarations, or inside on a field of a structural type. ref
types may often be more efficient when dealing with large data structures. They also provide for the usage of unsized types (functions, interfaces, slices) within type declarations.
The compiler abstracts over ref
types to provide optimization for reference counts: and so a distinction between Rc
/Arc
/Box
is not needed. Furthermore, access implicitly dereferences (with address access available via .addr
), and so a *
dereference operator is also not needed. Much care has been given to make references efficient and safe, and so ptr
should be avoided if at all possible. The compiler will yell at you if you use it (or any other unsafe features).
The implementation of ref
is delved into in further detail in the memory management document.
The type
keyword is used to declare aliases to custom data types. These types are algebraic: they function by composition. Algebraic data types can be one-of:
struct
: An unordered, named collection of types. May have default values.tuple
: An ordered collection of types. Optionally named.enum
: Ordinal labels, that may hold values. Their default values are their ordinality.union
: Powerful matchable tagged unions a la Rust. Sum types.interface
: Implicit typeclasses. User-defined duck typing.There also exist distinct
types: while type
declarations define an alias to an existing or new type, distinct
types define a type that must be explicitly converted to/from. This is useful for having some level of separation from the implicit interfaces that abound.
Structs are an unordered collection of named types.
+They are declared with struct[identifier: Type, ...]
and initialized with brackets: {field: "value", another: 500}
.
type LinkedNode[T] = struct
+ previous, next: Option[ref LinkedNode[T]]
+ data: T
+
+let node = {
+ previous: None, next: None
+ data: 413
+}
+
+func pretty_print(node: LinkedNode[int]) =
+ print node.data
+ if node.next of Some(node):
+ node.pretty_print()
+
+# structural typing!
+prints_data(node)
+
+Structs are structural and so structs composed entirely of fields with the same signature (identical in name and type) are considered equivalent. +This is part of a broader structural trend in the type system, and is discussed in detail in the section on subtyping.
+Tuples are an ordered collection of either named and/or unnamed types.
+They are declared with tuple[Type, identifier: Type, ...]
and initialized with parentheses: (413, "hello", value: 40000)
. Syntax sugar allows for them to be declared with ()
as well.
They are exclusively ordered - named types within tuples are just syntax sugar for positional access. Passing a fully unnamed tuple into a context that expects a tuple with a named parameter is allowed so long as the types line up in order.
+let grouping = (1, 2, 3)
+
+func foo: tuple[string, string] = ("hello", "world")
+
+Tuples are particularly useful for "on-the-fly" types. Creating type aliases to tuples is discouraged - structs are generally a better choice for custom type declarations.
+Enums are ordinal labels that may have associated values.
+They are declared with enum[Label, AnotherLabel = 4, ...]
and are never initialized (their values are known statically).
+Enums may be accessed directly by their label, and are ordinal and iterable regardless of their associated value. They are useful in collecting large numbers of "magic values", that would otherwise be constants.
type Keys = enum
+ Left, Right, Up, Down
+ A = "a"
+ B = "b"
+
+In the case of an identifier conflict (with other enum labels, or types, or...) they must be prefixed with the name of their associated type (separated by a dot). This is standard for identifier conflicts: and is discussed in more detail in the modules document.
+Unions are tagged type unions. They provide a high-level wrapper over an inner type that must be safely accessed via pattern matching.
+They are declared with union[Variant(Type), ...]
and initialized with the name of a variant followed by its inner type constructor in brackets: Square(side: 5)
. Tuples and structs are special-cased to eliminate extraneous parentheses.
type Value = u64
+type Ident = str
+type Expr = ref union
+ Literal(Value)
+ Variable(Ident)
+ Abstraction(param: Ident, body: Expr)
+ Application(body: Expr, arg: Expr)
+ Conditional(
+ condition: Expr
+ then_case: Expr
+ else_case: Expr
+ )
+
+They take up as much space in memory as the largest variant, plus the size of the tag (one byte).
+Unions abstract over differing types. In order to safely be used, their inner types must be accessed via pattern matching: leaving no room for type confusion. Pattern matching in Puck relies on two syntactic constructs: the match
statement, forcing qualification and handling of all possible types of a variable, and the of
statement, querying type equality while simultaneously binding new identifiers to underspecified portions of variables.
use std.tables
+
+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("Variable not in context")
+ of Application(body, arg):
+ if body of Abstraction(param, body as inner_body):
+ context.set(param, context.eval(arg)?) # from std.tables
+ context.eval(inner_body)
+ else:
+ Error("Expected Abstraction, found {}".fmt(body))
+ of Conditional(condition, then_case, else_case):
+ if context.eval(condition)? == "true":
+ context.eval(then_case)
+ else:
+ context.eval(else_case)
+ of expr:
+ Error("Invalid expression {}".fmt(expr))
+
+The match statement takes exclusively a list of of
sub-expressions, and checks for exhaustivity. The expr of Type(binding)
syntax can be reused as a conditional, in if
statements and elsewhere.
The of
operator is similar to the is
operator in that it queries type equality, returning a boolean. However, unbound identifiers within of
expressions are bound to appropriate values (if matched) and injected into the scope. This allows for succinct handling of union
types in situations where match
is overkill.
Each branch of a match expression can also have a guard: an arbitrary conditional that must be met in order for it to match. Guards are written as where cond
and immediately follow the last pattern in an of
branch, preceding the colon.
Interfaces can be thought of as analogous to Rust's traits, without explicit impl
blocks and without need for the derive
macro. Types that have functions fulfilling the interface requirements implicitly implement the associated interface.
The interface
type is composed of a list of function signatures that refer to the special type Self
that must exist for a type to be valid. The special type Self
is replaced with the concrete type at compile time in order to typecheck. They are declared with interface[signature, ...]
.
type Stack[T] = interface
+ push(self: mut Self, val: T)
+ pop(self: mut Self): T
+ peek(self: Self): T
+
+func takes_any_stack(stack: Stack[int]) =
+ # only stack.push, stack.pop, and stack.peek are available methods
+
+Differing from Rust, Haskell, and many others, there is no explicit impl
block. If there exist functions for a type that satisfy all of an interface's signatures, it is considered to match and the interface typechecks. This may seem strange and ambiguous - but again, static typing and uniform function call syntax help make this a more reasonable design. The purpose of explicit impl
blocks in ex. Rust is three-fold: to provide a limited form of uniform function call syntax; to explicitly group together associated code; and to disambiguate. UFCS provides for the first, the module system provides for the second, and the third is proposed to not matter.
Interfaces cannot be constructed because they are unsized. They serve purely as a list of valid operations on a type within a context: no information about their memory layout is relevant. The concrete type fulfilling an interface is known at compile time, however, and so there are no issues surrounding interfaces as parameters, just when attempted to be used as (part of) a concrete type. They can be used as part of a concrete type with indirection, however: type Foo = struct[a: int, b: ref interface[...]]
is perfectly valid.
Interfaces also cannot extend or rely upon other interfaces in any way. There is no concept of an interface extending an interface. There is no concept of a parameter satisfying two interfaces. In the author's experience, while such constructions are powerful, they are also an immense source of complexity, leading to less-than-useful interface hierarchies seen in languages like Java, and yes, Rust.
+Instead, if one wishes to form an interface that also satisfies another interface, they must include all of the other interface's associated functions within the new interface. Given that interfaces overwhelmingly only have a handful of associated functions, and if you're using more than one interface you really should be using a concrete type, the hope is that this will provide explicitness.
+ +Interfaces compose with modules to offer fine grained access control.
+ +Any type can be declared as an alias to a type simply by assigning it to such. All functions defined on the original type carry over, and functions expecting one type may receive the other with no issues.
+type Float = float
+
+It is no more than an alias. When explicit conversion between types is desired and functions carrying over is undesired, distinct
types may be used.
type MyFloat = distinct float
+let foo: MyFloat = MyFloat(192.68)
+
+Types then must be explicitly converted via constructors.
+Puck does not have any concept of null
: all values must be initialized.
+But always explicitly initializing types is syntactically verbose, and so most types have an associated "default value".
Default values:
+bool
: false
int
, uint
, etc: 0
float
, etc: 0.0
char
: '\0'
str
: ""
void
, never
: unconstructablearray[T]
, list[T]
: []
set[T]
, table[T, U]
: {}
tuple[T, U, ...]
: (default values of its fields)
struct[T, U, ...]
: {default values of its fields}
enum[One, Two, ...]
: <first label>
union[T, U, ...]
: disallowedslice[T]
, func
: disallowedref
, ptr
: disallowedFor unions, slices, references, and pointers, this is a bit trickier. They all have no reasonable "default" for these types aside from null. +Instead of giving in, the compiler instead disallows any non-initializations or other cases in which a default value would be inserted.
+todo: consider user-defined defaults (ex. structs)
+Puck supports overloading - that is, there may exist multiple functions, or multiple types, or multiple modules, so long as they have the same signature. +The signature of a function / type / module is important. Interfaces, among other constructs, depend on the user having some understanding of what the compiler considers to be a signature. +So, it is stated here explicitly:
+Result[T]
and Result[T, E]
are defined in std.results
Mention of subtyping has been on occasion in contexts surrounding structural type systems, particularly the section on distinct types, but no explicit description of what the subtyping rules are have been given.
+Subtyping is the implicit conversion of compatible types, usually in a one-way direction. The following types are implicitly convertible:
+uint
==> int
int
==> float
uint
==> float
string
==> list[char]
(the opposite no, use pack
)array[T; n]
==> list[T]
struct[a: T, b: U, ...]
==> struct[a: T, b: U]
union[A: T, B: U]
==> union[A: T, B: U, ...]
Puck is not an object-oriented language. Idiomatic design patterns in object-oriented languages are harder to accomplish and not idiomatic here.
+But, Puck has a number of features that somewhat support the object-oriented paradigm, including:
+type Building = struct
+ size: struct[length, width: uint]
+ color: enum[Red, Blue, Green]
+ location: tuple[longitude, latitude: float]
+
+type House = struct
+ size: struct[length, width: uint]
+ color: enum[Red, Blue, Green]
+ location: tuple[longitude, latitude: float]
+ occupant: str
+
+func init(_: type[House]): House =
+ { size: {length, width: 500}, color: Red
+ location: (0.0, 0.0), occupant: "Barry" }
+
+func address(building: Building): str =
+ let number = int(building.location.0 / building.location.1).abs
+ let street = "Logan Lane"
+ return number.str & " " & street
+
+# subtyping! methods!
+print House.init().address()
+
+func address(house: House): str =
+ let number = int(house.location.0 - house.location.1).abs
+ let street = "Logan Lane"
+ return number.str & " " & street
+
+# overriding! (will warn)
+print address(House.init())
+
+# abstract types! inheritance!
+type Addressable = interface for Building
+ func address(self: Self)
+
+These features may compose into code that closely resembles its object-oriented counterpart. But make no mistake! Puck is static first and functional somewhere in there: dynamic dispatch and the like are not accessible (currently).
+ +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.
+# 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 & "\""
+
+## 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))
+
+...
+
+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:
+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.
+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.