Language
Forma is a typed Lisp for executable programs and domain languages. Types, schemas, and form contracts use the same syntax. The reader keeps source identity and spans for diagnostics, formatting, and structural editing.
Values and expressions
The primitive types are Unit, Bool, Int, Float, String, Keyword, and Symbol. Integers can be used where a Float is expected. Strings, keywords, and quoted symbols are distinct values: "ready", :ready, and 'ready do not compare equal. nil has type Unit.
1 is Int; 1.0 and 1e0 are Float even when their value is integral. Number and Num are rejected. Use Int or Float explicitly. +, -, *, min, max, and abs return Int when all operands are Int, otherwise Float. / always returns Float; mod requires Int operands and a nonzero divisor. floor, ceil, and round return Int (round sends ties toward positive infinity). Assignment from Int to Float is permitted without retagging the value. Equality compares numeric values, so (= 2 2.0) is true; numeric literal patterns distinguish their kinds.
Int uses JavaScript's safe range, −9007199254740991 through 9007199254740991. Out-of-range literals and arithmetic results fail instead of wrapping. Float uses IEEE 754, including NaN, infinities, and signed zero; division by zero follows IEEE rules. NaN does not equal itself. Rounding nonfinite or out-of-range values fails. Values print as 2 and 2.0; str spells an integral Float as 2.. Tagged host values retain their kinds; plain JSON erases them and encodes nonfinite values as null. See the numeric design decision for engine and generated TypeScript boundaries.
Parentheses call a function or introduce a special form. Braces always contain records or dictionaries. Square brackets contain lists; their elements share a common type. Semicolons begin comments.
(define greeting "Hello")
(define numbers [1 2 3])
(define person {:name "Ada" :age 36})
(+ person.age 1)Dot access follows lexical bindings: person.age reads the :age field of person. Service members such as Clock.now resolve through their service. get accepts a computed key. Keyword keys and string keys retain their identity.
Definitions and signatures
Use define for both values and functions. Parameters are patterns; signatures are written separately with : and may precede or follow a definition in the same module.
(: increment (-> Int Int))
(define increment [value] (+ value 1))
(define distance [point]
(+ point.x point.y))
(: distance (-> {:x Float :y Float} Float))fn makes an anonymous function. let binds values in lexical scope. if requires a Bool condition, and its branches must have a common type. do evaluates a sequence and returns its final expression. Quote returns syntax without evaluating it; quasiquote supports ~ and ~@ for substitution and splicing.
A keyword literal keeps its exact type: :open has type :open. Branches and collections of different keywords infer their union. Keyword accepts any keyword, including ones constructed at runtime. When a literal constrains an unresolved function parameter or a value compared with it, inference uses the literal's base type (Keyword for keywords). This lets a lambda compare a Keyword record field with :open without requiring that every record passed to it have exactly :open in that field. Explicit literal and enum types still restrict the accepted values.
Types are schemas
type names a shape. A record type has the same appearance as its values. Fields are required by default. (Option T) means an absent value, represented by None, or a present value, represented by (Some value).
(type OrderId (Brand String))
(type Status (Union :pending :paid :shipped))
(type Order
{:id OrderId :status Status :total-cents Int :coupon (Option String)})
(: example Order)
(define example
{:id (OrderId "order:1") :status :pending :total-cents 1200})In a record checked against an expected type, an omitted optional field becomes None, and a plain supplied value becomes Some. An existing Option stays an Option. Schema and artifact encodings omit absent optional fields.
(Brand T) takes its nominal name from the declaration. Two brands of the same underlying type remain distinct. (List T) describes a homogeneous list. (Map K V) describes a dictionary: keys may be strings, string brands, or a finite union of keyword or string literals. Dictionary lookup returns (Option V).
(: scores (Map String Int))
(define scores {"Ada" 10 "Grace" 12})
(match (get scores "Ada")
(Some score) score
None 0)An open record includes a row variable after &, as in {:name String & row}. Generic aliases name their parameters: (type (Box a) {:value a}).
Ordinary type aliases and signatures can compute closed record shapes:
(type Person {:name String :age Int})
(type Name (Pick Person [:name]))
(type PublicPerson (Omit Person [:age]))
(type Identified (Merge {:id String} PublicPerson))Pick keeps the listed fields; Omit removes them. Their second argument must be a literal vector of distinct keywords, and every listed field must exist. Empty vectors are allowed. Merge combines records with disjoint fields; overlapping names are errors even when their types agree. Aliases and nested operations are supported, and retained field types may be polymorphic. The checker requires known, closed outer record shapes: operations on whole-record variables or open rows report an error. These are type operations; they do not project or merge values or generate Effect schemas. Open-row disjointness constraints are not implemented. For computed record annotations, supply optional fields explicitly as Some or None; implicit wrapping and filling currently require explicit record type syntax.
Intersection types
(& A B ...) (or (Intersect A B ...)) combines two or more types:
(type Timestamped {:created-at String :updated-at String})
(type Named {:name String})
(type Auditable (& Timestamped Named))For records, an intersection merges fields across all members (resolving aliases) and reports a typecheck/intersection-conflict diagnostic if two members declare the same field with incompatible types. A value of type (& A B) is assignable to both A and B, and a record satisfying both A and B is assignable to (& A B). Record intersections also compile to merged Effect Schema.Struct codecs, TypeScript intersection types, and JSON Schema objects. Open-row record syntax ({:a Int & r}) remains distinct from list-form intersections (& A B).
Function intersections express overloaded signatures:
(: id-overload (& (-> Int Int) (-> String String)))Calling an overloaded function selects the matching overload branch from the argument types (including literal refinements such as :pattern).
Unions and tagged values
Union accepts types or literal values and has no discriminator. Tagged declares constructors. Capitalized constructor names distinguish them from lowercase binding patterns.
(type Shape (Tagged (Circle {:radius Float}) (Square {:side Float}) Point))
(: area (-> Shape Float))
(define area [shape]
(match shape
(Circle {:radius radius}) (* 3.14159 radius radius)
(Square {:side side}) (* side side)
Point 0))Constructors belong to their type: Shape.Circle is always explicit. Bare constructors resolve from the expected type when needed. The discriminator is :_tag by default; (Tagged :tag kind ...) selects another field. Record payloads merge into the tagged record; scalar payloads occupy :value. Inline record payloads must be closed and must not declare the discriminator field. A conflicting field or an open payload tail reports a located error.
class and error declare named record types with the same field syntax. Errors have a discriminator derived from their name and may appear in Effect error sets.
(class Customer {:name String})
(error OrderNotFound {:id OrderId})
(error Timeout)Indexed constructors (GADTs) and existentials
Tagged constructors may specify an explicit return type with :returns to index a parameterized type: (Ctor Field1 ... :returns ReturnType) (including nullary constructors such as (Empty :returns (Vec Zero a))).
(type Draft (Tagged Draft))
(type Paid (Tagged Paid))
(type (Order s)
(Tagged
(DraftOrder {:id Int :total Int} :returns (Order Draft))
(PaidOrder {:id Int :total Int :receipt String} :returns (Order Paid))))
(type (Action from to)
(Tagged
(Pay String :returns (Action Draft Paid))))
(: pay (-> (Order Draft) String (Order Paid)))
(define pay [order receipt]
(match order
(DraftOrder d) (PaidOrder {:id d.id :total d.total :receipt receipt})))
(: apply (-> (Order from) (Action from to) (Order to)))
(define apply [order action]
(match action
(Pay receipt) (pay order receipt)))Matching an indexed constructor refines the scrutinee's type parameters locally within that match arm without leaking equalities to sibling arms or outside the match. Constructors whose return type cannot unify with the scrutinee's type are pruned from exhaustiveness checks (so pay only matches DraftOrder), while polymorphic functions over (Order s) still must cover every constructor.
A lowercase type variable that appears in a constructor's payload fields but not in its ReturnType is existentially quantified:
(type SomeOrder
(Tagged
(PackOrder (Order s) :returns SomeOrder)))Unpacking PackOrder in a match introduces a fresh rigid type variable for s scoped to that arm; values of type (Order s) can be passed to polymorphic functions such as order-id, and returning the unpacked existential directly is rejected as skolem escape.
In Effect programs (generateEffectProgram and :effect / :run), indexed Tagged types elaborate to an erased Effect 4 Schema.Union runtime codec alongside an indexed TypeScript union type (Order<S = unknown>) that narrows concrete states (Order<"paid">) directly in strict TypeScript.
Open polymorphic variants
Complementing open records ({:name String & r}), Forma supports structural open and closed polymorphic variants without requiring a prior type or error declaration:
;; Open variant type (row tail `e`) and closed variant type
(: find-user (-> String (Variant (NotFound {:id String}) (Ok {:name String}) & e)))
(define find-user [id]
(if (= id "0")
(variant :NotFound {:id id})
(variant :Ok {:name "Ada"})))
;; Composes with another variant-producing function automatically via row unification
(define fetch-user [id]
(if (= id "slow")
(variant :Timeout "upstream timed out")
(find-user id)))(variant :Tag payload) (or nullary (variant :Ok)) infers an open variant row (Variant (Tag PayloadType) & r) that unifies with any larger variant type containing Tag. match narrows payload types per arm, enforces exhaustiveness against closed (Variant ...) types, and requires a fallback _ arm for open variants. Both the evaluator and bytecode VM support structural variants. Effect 4 TypeScript / Schema lowering for structural Variant types remains planned (see RFC 0030).
Lightweight dependent functions (Pi types)
Function signatures may name finite-domain parameters with [name Type] and compute the return type (or Effect success type) by matching on that parameter:
(type (Order s)
(Tagged
(Draft {:id String :total Int} :returns (Order :draft))
(Paid {:id String :total Int :receipt String} :returns (Order :paid))
(Cancelled {:id String} :returns (Order :cancelled))))
(: transition (-> (Order :draft) [action (Union :pay :cancel)]
(match action
:pay (Order :paid)
:cancel (Order :cancelled))))
(define transition [order action]
(match action
:pay (match order (Draft {:id id :total total}) (Paid {:id id :total total :receipt "rcpt"}))
:cancel (match order (Draft {:id id}) (Cancelled {:id id}))))Type-level match must reference an in-scope dependent parameter and cover its finite literal domain (or include a _ fallback arm). Checking a function body against a Pi signature refines the expected return type in each match arm, while calling a Pi function with a concrete literal (such as (transition order :pay)) reduces the return type to that branch ((Order :paid)). Calling with a union value reduces to the Union of matching branches (or preserves (match ...) when delegating from an enclosing Pi function).
In Effect programs, Pi functions and operations elaborate to generic TypeScript functions with conditional return types (<Action extends "pay" | "cancel">(..., action: Action): Action extends "pay" ? Order<"paid"> : Order<"cancelled">).
Patterns
fn, define, let, match, and catch share pattern conventions. Lowercase names bind, _ ignores a value, literals compare values, and capitalized names match constructors. Records and vectors destructure their members. & captures remaining members or elements, and :as retains the whole matched value. Record patterns, including {:keys [name]}, require their named keys to exist; missing keys do not bind to nil. Use (get record :name) when absence should return nil during evaluation. A statically known record must contain the key for typed access; a dictionary lookup has type (Option V).
A match on a closed tagged type must cover every constructor; missing constructors are errors in both the kernel and Effect checkers. Use a final _ arm to explicitly handle the remaining cases.
(define first-name [{:name name}] name)
(let [[first & rest] [1 2 3]] first)
(match (Some 7) (Some value) value None 0)Effects
An Effect type is (Effect Success [Errors] [Requirements]); omitted sets are empty. Definitions with Effect signatures use ordinary define. A zero-argument Effect is a value.
Inside an Effect operation, you can write ordinary direct-style Forma (let, sequential bodies, if, match, and nested calls) without do! or pure:
(error NotFound {:id String})
(service Users (: find (-> String (Effect {:name String :email String} [NotFound]))))
(service Mailer (: send (-> String String (Effect Unit))))
(: checkout (-> String (Effect String [NotFound] [Users.find Mailer.send])))
(define checkout [id]
(let [user (Users.find id)]
(Mailer.send user.email "Receipt")
user.name))The compiler lifts only effectful calls into Effect.gen + yield*, keeping pure subexpressions as plain JavaScript with zero fiber overhead. A pure final expression returns cleanly without (pure ...).
The unified handle form discharges both local service requirements (tail-resumptive via (resume val) or direct return) and typed failures ((fail Err [e] recovery)) in one expression:
(error NotFound {:id String})
(service Users (: find (-> String (Effect {:name String :email String} [NotFound]))))
(service Mailer (: send (-> String String (Effect Unit))))
(: checkout-safe (-> String (Effect String)))
(define checkout-safe [id]
(handle
(let [user (Users.find id)]
(Mailer.send user.email "Receipt")
user.name)
(Users.find [uid] (resume {:name "Ada" :email "ada@example.com"}))
(Mailer.send [_to _body] (resume nil))
(fail NotFound [e] (str "missing: " e.id))))Service clauses lower to Effect.provideService and fail clauses lower to Effect.catchTag in the Effect 4 TypeScript backend (and execute in-process in runEffectProgram). Explicit do!, :let, pure, catch, provide, and top-level layer forms remain fully supported for backwards compatibility. Effect 4 TypeScript is the first compilation backend; future zero-overhead evidence-passing JavaScript and Perceus-FBIP native/Wasm backends are planned (see RFC 0003). See the Effect reference for resources, concurrency, recovery, and generated TypeScript.
Typed forms
A form is a function from syntax to a declared IR type. Its pattern specifies positional arguments, trailing keyword options, and repeated child forms. Its hole types specify how those arguments parse and check.
(type GreetingIR {:kind "Greeting" :name Symbol :message String :doc (Option String)})
(form (greeting name message {:keys [doc]})
"Declare a greeting."
:types {:name (Declares Greeting) :message String :doc (Option String)}
:ir GreetingIR
{:kind "Greeting" :name name :message message :doc doc})
(greeting welcome "Hello" :doc "Shown on arrival")| Hole type | Meaning |
|---|---|
(Declares T) | Introduces a declaration name of type T |
(Refers T) | Resolves a name to a declaration of type T |
String, Int, Float, Symbol, Keyword | Parses a literal |
Type | Parses a type expression |
(Expr T) | Checks an expression at T |
(Record T) | Supplies record members with type T |
(List child) | Parses child forms and supplies their IR values |
(Option H) | Allows an absent hole |
A pattern ending in child ... accepts zero or more children. A :scope function provides bindings while checking a chosen hole. :check returns diagnostics; :type supplies a result type or computes one from the holes. Projection bodies are ordinary Forma expressions. The declared IR type checks their output and provides the schema used at the artifact boundary.
An optional positional (Option String) hole can hold a docstring; omitting it leaves the next non-string argument for the following hole. Scope signatures quantify their type, row, and effect variables independently at each use. Checks can attach diagnostics to nested authored syntax using :at, and form hooks can inspect declarations and aliases with declarations-of and type-definition. See Writing a form for diagnostic paths, reflection result shapes, and examples.
Domain declarations
Entities use short field names and derive attribute namespaces. References use (Id Entity); an optional entity attribute uses (Option T).
(entity Department {:name String})
(entity Employee
{:name String :department (Id Department) :active (Option Bool)})
(seed Employee "employee:ada"
{:name "Ada" :department "department:engineering"})
(query people :from Employee :where (= name "Ada") :select [name])Inside a query, fields from the :from entity enter the predicate scope. Predicates must have type Bool. Seed and link records check their field names, values, and required fields against the referenced entity or relation.
Structural tools
Forma's formatter and editor share the reader's lossless syntax tree. Diagnostics refer to author spans, including expressions produced through macros.