Early documentation

The tour and docs will likely be rewritten for Lycan's first public release.

An introduction

Tour

A few small programs to introduce the syntax, the data model, and the way functions fit together.

01

Forms and functions

Lycan is a Lisp, so you'll see the same parenthesized forms throughout a program. For example, (+ 2 3) adds two numbers. To use that result in another calculation, put the call inside it: (* (+ 2 3) 4) produces 20.

Let's define a function to square a number, then use it to add two squares. The parameter and return types tell the compiler what values to expect, and the last expression supplies the result.

Two ordinary functions.lyc ↓
(pub fn square ((n i32)) -> i32
  (* n n))

(pub fn sum-of-squares ((a i32) (b i32)) -> i32
  (+ (square a) (square b)))
result(sum-of-squares 3 4) → 25

i32 is a signed 32-bit integer. Other common types include f32, bool, and string. A function that has no meaningful result returns void.

pub makes a declaration available to other modules, each of which is a source file. Imports specify which declarations the current module can use.

02

Products and fields

A product groups named fields, which can have defaults and can be initialized by name in a constructor. Use brackets to read a field and parentheses to call a function.

A point and an operation on points.lyc ↓
(pub type Point
  ((x f32 0.0)
   (y f32 0.0)))

(pub fn + ((a &Point) (b &Point)) -> Point
  (Point :x (+ [a x] [b x])
         :y (+ [a y] [b y])))

The constructor (Point :x 3.0 :y 4.0) creates a point, and [point x] reads its x coordinate. The + overload adds two points using ordinary function dispatch.

The &Point parameters borrow their arguments for reading, and the function returns a new Point value.

03

Sums and matching

A sum describes a closed set of possibilities, each of which may carry a value. Matching a variant selects an arm and binds its payload to a local name.

Data with alternatives.lyc ↓
(pub type Weather
  (Clear)
  (Snowing i32))

(pub fn describe ((weather Weather)) -> string
  (match weather
    ((Clear) "clear skies")
    ((Snowing depth)
     (+ (to #string depth) " cm of snow"))))
result(describe (Snowing 12)) → "12 cm of snow"

An arm's final expression becomes the result of the match. if and let are expressions as well.

04

Bindings and collections

let introduces local bindings whose types can be inferred from their initializers. Bindings are sequential, so a later initializer can use an earlier one.

A small vector.lyc ↓
(pub fn total () -> i32
  (let ((readings (vec 3 5 8))
        (sum 0))
    (push readings 13)
    (for (reading readings)
      (inc sum reading))
    sum))
result(total) → 29

A Vec stores elements contiguously, with push to append an element, for to visit the elements, and set to assign to storage.

Use (at readings 0) for checked element access, whether you need to read an element, borrow it, or assign a new value to it.

05

Errors as values

(Result T E) holds either an Ok value or an Err value. (Option T) represents a value that may be absent, using Some and None.

Returning and propagating a failure.lyc ↓
(pub fn divide ((a i32) (b i32)) -> (Result i32 string)
  (if (= b 0)
    (Err "division by zero")
    (Ok (/ a b))))

(pub fn half-plus-one ((n i32)) -> (Result i32 string)
  (let ((half (? (divide n 2))))
    (Ok (+ half 1))))
result(half-plus-one 10) → (Ok 6)

? extracts a successful result or returns a failure from the surrounding function, which must itself return a compatible Result or Option. To handle both cases explicitly, use match.

To work with the payload of an Option or Result, match it, propagate it, or use when-let to handle the successful case. Neither type can be used directly as a condition.

06

Open functions

Types describe data, with functions declared separately to operate on it. Functions with the same name can form an open family whose overloads handle different types.

Choosing an overload.lyc ↓
(pub type Animal ())
(pub type Wolf < Animal ())
(pub type Raven < Animal ())

(pub fn voice ((animal &Animal)) -> string
  "...")

(pub fn voice ((wolf &Wolf)) -> string
  "howl")

(pub fn voice ((raven &Raven)) -> string
  "croak")

(pub fn listen ((animal &Animal)) -> string
  (voice animal))
result(listen (Wolf)) → "howl"

listen accepts an Animal reference, but dispatch still reaches the Wolf overload when the value is a wolf. A family can specialize on more than one parameter.

A protocol names a set of operations that a type satisfies through compatible visible overloads.

A little further

The docs take a closer look at extensible operations, ownership, and compile-time source transformations.

Continue readingFunctions and dispatchFollow the dataValues and ownershipWork with source formsMacros and compile time