Lycan

A statically typed Lisp for systems programming.

Lycan has multiple dispatch, compile-time macros, and deterministic memory management.

Coming soon
lycan dev
lycan> (+ 20 22)
42

lycan> (fn square ((n i32)) -> i32
.....>   (* n n))
defined square

lycan> (square 6)
36

lycan> 

01 / Syntax

Statically typed Lisp

Lycan is a Lisp: parentheses group expressions, and one expression can use the result of another. Here, sum-of-squares calls square twice and adds the results.

Declare a function's parameter and return types, and the compiler checks them before the program runs. Local variables can infer their types from an initial value, and a function returns the value of its last expression.

Syntax and functions ↗
Functions and return values.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

02 / Object system

Multiple dispatch and inheritance

Lycan's object system takes inspiration from Common Lisp's CLOS. Types support multiple inheritance, and functions live outside type declarations so an operation can specialize on several arguments.

In this example, Wolf and Raven inherit from Animal, and voice follows the concrete type through an Animal reference. Method combination lets an overload call next-method or add behaviour :before, :after, or :around the primary call.

Functions and dispatch ↗
An open family.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"

03 / Values and lifetimes

Memory and ownership

Lycan uses scope-based lifetimes (RAII) to manage memory and other resources. When a value reaches the end of its scope, Lycan cleans it up automatically, freeing its memory and closing resources such as files. You can pass a value to another part of the program or share it when it needs to live longer, without having to free it manually.

Functions can read or update an existing object through a reference, while its owner remains responsible for cleanup.

Values and ownership ↗

Values and references

T
An ordinary value
&T
A read reference
!&T
A writable reference
(Own T)
Unique boxed ownership
(Shared T)
Shared boxed ownership

No garbage collector; cleanup follows ownership.

04 / Development

Interactive development

With lycan dev, you can explore your program in the REPL and reload code as you work. Save a source file and the running application picks up your changes, so you can adjust a scene and see the result without leaving it.

main.lyc.lyc ↓
(import
  (howl *)
  (howl/forge ()))

(const gravity 9.81)

(inputs platformer
        (axis1 move
               ((key (KeyA)) -1.0)
               ((key (KeyD)) 1.0))
        (button jump (key (KeySpace))))

(type Ball < DeformableModel
  ((grounded bool nil)
   (airborne f32 0.0)
   (charge f32 0.0)
   (stretch f32 0.0)
   (impact-speed f32 0.0)
   (rolling f32 0.0)))

(var landing-dust
     (EmitterConfig :texture (load "textures/landing-dust.png")
                    :atlas (Atlas :cols 6)
                    :max-particles 12
                    :shape (EmitDisc 0.65)
                    :lifetime (Interval 0.4 0.6)
                    :start-size (Interval 0.45 0.75)
                    :start-speed (Interval 0.9 1.6)
                    :start-rotation (Interval 0.0 360.0)
                    :angular-velocity (Interval -100.0 100.0)
                    :acceleration (Vec3 0.0 0.5 0.0)
                    :drag 5.5
                    :size-over-life (curve (CurveKey 0.0 0.7) (CurveKey 0.6 1.3))
                    :color-over-life (gradient (GradientKey 0.0 (Color 210 178 140 100))
                                               (GradientKey 0.15 (Color 210 178 140 205))
                                               (GradientKey 0.85 (Color 210 178 140 0)))))

(pub fn setup () -> void
  (scene:load "scenes/ball.scene")
  (spawn (Camera :position (Vec3 0.0 4.0 18.0)
                 :target (Vec3 0.0 1.6 0.0)
                 :fov-y 22.0))
  (spawn (DirectionalLight :direction (Vec3 0.35 -1.0 -0.25)
                           :color (Color 255 228 183)
                           :intensity 1.0
                           :ambient 0.3))
  (spawn (Ball :position (Vec3 -2.4 0.65 0.0)
               :mesh (sphere-mesh 0.55 32 20)
               :settings (PhysicsMeshSettings :surface-sim true
                                              :material (PhysicsMaterial :density 10.0
                                                                         :pressure 35.0
                                                                         :compliance 0.0004
                                                                         :bend-compliance (Some 0.02)
                                                                         :damping 0.08
                                                                         :iterations 16))
               :body-settings (SoftBodySettings :vertex-radius 0.025 :friction 0.65)
               :material (Material :base (Color 166 222 179)
                                   :albedo (load "textures/slime.jpg")
                                   :highlight-intensity 0.45
                                   :highlight-exponent 16.0))))

(fn jump ((velocity !&Vec3)) -> void
  (let ((height 0.6))
    (set [velocity y] (sqrt (* 2.0 gravity height)))))

(fn on-update ((ball !&Ball) (dt f32)) -> void
  (let ((bounds (bounds ball))
        (center (center bounds))
        (velocity (velocity ball))
        (ground (raycast (Vec3 [center x] (+ [bounds min y] 0.12) [center z])
                         (Vec3 0.0 -0.24 0.0)
                         (ignoring ball)))
        (on-ground (when-let ((hit ground))
                     (and (> [hit normal y] 0.6)
                          (< [velocity y] 0.8)
                          (< (- [bounds min y] [hit point y]) 0.045))))
        (direction (axis1 (input platformer move)))
        (wall (raycast center
                       (Vec3 (* direction (+ [(half bounds) x] 0.08)) 0.0 0.0)
                       (ignoring ball)))
        (target velocity))
    (when (and on-ground (not [ball grounded]) (> [ball airborne] 0.15))
      (when-let ((hit ground))
        (burst landing-dust (+ [hit point] (Vec3 0.0 0.04 0.0))
               (i32 (clamp (* [ball impact-speed] 2.0) 5.0 12.0)))))

    (set [ball impact-speed] (if on-ground 0.0 (max [ball impact-speed] (- 0.0 [velocity y])))
         [ball airborne] (if on-ground 0.0 (+ [ball airborne] dt))
         [ball grounded] on-ground
         [ball stretch] (max 0.0 (- [ball stretch] dt))
         [target x] (lerp [velocity x] (if (some? wall) 0.0 (* direction 3.6))
                          (- 1.0 (exp (* -12.0 dt))))
         [target z] (* [center z] -8.0))

    (when on-ground
      (set [ball rolling] (lerp [ball rolling] (/ (- 0.0 [target x]) 0.55)
                                (- 1.0 (exp (* -14.0 dt))))))

    (when (and [ball grounded] (= [ball charge] 0.0) (pressed? (input platformer jump)))
      (set [ball charge] 0.12))
    (when (> [ball charge] 0.0)
      (set [ball charge] (max 0.0 (- [ball charge] dt)))
      (when (= [ball charge] 0.0)
        (jump target)
        (set [ball grounded] nil [ball stretch] 0.09)))

    (deform ball center velocity target dt)))

(fn deform ((ball !&Ball) (center Vec3) (velocity Vec3) (target Vec3) (dt f32)) -> void
  (let ((strain (cond
                  ((> [ball charge] 0.0) -5.0)
                  ((> [ball stretch] 0.0) 4.0)
                  (else 0.0)))
        (vertices (edit-vertices ball)))
    (for (i (count vertices))
      (let ((offset (- (position vertices i) center))
            (relative (- (velocity vertices i) velocity))
            (spin (cross (Vec3 0.0 0.0 [ball rolling]) offset)))
        ;; Preserve rolling while damping deformation around it
        (set relative
             (if (/= strain 0.0)
               (lerp relative (+ spin (Vec3 (* [offset x] strain -0.5)
                                            (* [offset y] strain)
                                            (* [offset z] strain -0.5)))
                     (- 1.0 (exp (* -12.0 dt))))
               (+ spin (* (- relative spin) (exp (* -10.0 dt))))))
        (set-velocity vertices i (+ target relative))))))

(pub fn main () -> (Result void string)
  (run (App :title "Ball Platformer"
            :width 640
            :height 480)
       :setup setup))
lycan dev
REPL
lycan> 

Howl is a game engine written in Lycan.

05 / Metaprogramming

Macros

A macro takes source forms and produces new ones before type checking. You can use one to generate declarations or give a recurring task its own syntax, with compile-time reflection to inspect the types and functions already declared.

This site uses an HTML macro to turn element forms into ordinary values, which functions compose into pages. A Lycan program then renders those pages to HTML.

Macros and compile time ↗
HTML written in Lycan.lyc ↓
(import (std/html *))

(pub fn greeting ((name string)) -> Html
  (html
    (article :class "greeting"
      (h1 "Hello, " name ".")
      (p "This page is a Lycan value."))))

06 / Standard library

Batteries included

The standard library includes the tools for command-line programs, network services, and web applications alongside the usual collections and file I/O.

Collections and data
Vectors, maps, sets, Unicode text, JSON, and binary serialization with derived codecs.
System access
Files, paths, filesystem watching, environment variables, and subprocesses.
Networking
TCP and UDP, HTTP clients and servers, routing, and asynchronous requests.
Web interfaces
HTML composition, DOM updates, event handlers, and JavaScript interop.
Concurrency
Threads, channels, synchronization, and asynchronous tasks.
Developer tools
Command-line parsers, terminal interfaces, testing, profiling, and allocation tracking.

07 / Interop and tooling

A few more things

C interoperability
Call C functions directly and describe C-compatible layouts for data shared with existing libraries.
Concurrency
Threads, channels, and asynchronous tasks let programs do work in parallel or wait for I/O.
Calling the C standard library.lyc ↓
(extern c "string.h"
  (fn strlen ((text c-string)) -> usize))

(pub fn byte-length ((text c-string)) -> usize
  (strlen text))
result(byte-length "snow") → 4

Project status

Coming soon

Lycan is self-hosted and uses LLVM to compile to native code. It's being built for games, simulations, and other software where performance is important.

These examples and docs introduce the language as it stands while the first public release is in preparation. Syntax and APIs may change.

Join the Discord server or email jfaz@lycan.systems to try the alpha. Expect bugs.

Lycan draws inspiration from Common Lisp, Nim, Rust, and C++.