👋 Welcome fellow Unison programmers!

If you want to write Unison applications and libraries, you are in the right place. The Unison language and ecosystem was thoughtfully designed to offer a delightful way to write code.

If you want a sense of what Unison code looks like, below are a few bread-and-butter language fundamentals with links to more comprehensive docs:

👀 Unison at a glance

This document contains code snippets with a minimum of exposition. Links are provided throughout to more comprehensive docs sections. If you haven't downloaded the UCM, you might want to do that first. 😎

Hello World

Write your Unison code in any .u suffixed "scratch" file.

The classic helloWorld program performs console interactions via the IO ability. Read about how abilities model effects in Unison.

usingAbilitiesPt1.helloWorld : '{IO, Exception} ()
usingAbilitiesPt1.helloWorld _ = printLine "Hello World!"

Execute the entry point to your program with the run UCM command.

.> project.create hello-world
hello-world/main> run helloWorld

Or add your helloWorld program to the codebase and run it by packaging it up in a binary executable!

hello-world/main> add
hello-world/main> compile helloWorld helloFile

This will produce an executable file called helloFile.uc in the same directory as the codebase. You can then start your program in your terminal.

$ ucm run.compiled helloFile.uc

📚Learn more about executing Unison programs

Basic functions

The following introduces a function double with one parameter. Unison conventions for defining functions are detailed here

glance.double : Nat -> Nat
glance.double x =
  use Nat *
  x * 2
> double 4

The > in a scratch file runs the double function in a watch expression.

📚The Unison tour walks through watch expressions and other workflow features

Delayed computation syntax

The ' symbol is syntactic sugar for introducing a thunk and the ! symbol is syntactic sugar for calling the thunk.

atAGlance.greet : '{IO, Exception} ()
atAGlance.greet _ =
  use Text ++
  printLine "Enter your name:"
  name = !readLine
  printLine ("Hello " ++ name)
delayGreet : 'Text
delayGreet = do greet "Joy"
"Hi Joy"

A more detailed look at delayed computations

List literals

Square brackets introduce a Unison list.

The List.++ is our operator for list concatenation.

use List +: :+ head = 1 +: [2, 3, 4] head :+ 5

A variety of list patterns are available for lists.

📚Learn more about common collection types in Unison

List transformations

The |> operator is a "pipe" which passes the result of executing the expression on the left as an argument to function on right.

The parenthesized x -> x Nat.* 100 argument to lib.base.data.List.map is an example of lambdas in Unison.

📚Learn more about operators like |>

if/else and pattern matching

The expression below is written with both if then and else syntax and with pattern matching syntax

use Nat mod
isEven1 num =
  if mod num 2 === 0 then true else false
isEven2 num = match num with
  n | mod n 2 === 0 -> true
  _ -> false

Unison's pattern matching features include variable binding, pattern guards (separated by |), and as-patterns (indicated with an @).

match Some 12 with None -> "none" Some n| Nat.isEven n -> "n is a variable and | is a pattern guard" opt@(Some n) -> "opt binds to the entire optional value"
"n is a variable and | is a pattern guard"

The cases syntax can take the place of a full match ... with expression.

foo = cases 0 -> "zero" _ -> "not zero" foo 0
foo = cases 0 -> "zero" _ -> "not zero" foo 0

Type declarations

A unison data type with uniqueness determined by its name:

A recursive Tree data type with a single type parameter:

structural type Tree a
structural type Tree a
  = glance.Tree.Empty
  | glance.Tree.Node a (Tree a) (Tree a)

The structural keyword means that types defined with the same structure are identical.

More on data types and the difference between structural and unique.

Record types allow you to name the fields of your type.

unique type Pet = {
   age : Nat,
   species : Text,
   foodPreferences : [Text]

Creating a record type generates a series of helper methods to access and update the fields of the data type.

myProject/main> add Pet

 ⍟ I've added these definitions:

     unique type Pet
     Pet.age                    : Pet -> Nat
     Pet.age.modify             : (Nat ->{g} Nat) -> Pet ->{g} Pet
     Pet.age.set                : Nat -> Pet -> Pet

Record type syntax in depth

Exception handling

nonZero : Nat ->{Exception} Nat
nonZero = cases
    | n Nat.== 0  ->
      Exception.raise (Generic.failure "Zero was found" n)
    | otherwise   -> n
catch do nonZero 0
Left (Failure (typeLink Generic) "Zero was found" (Any 0))

An exception is "raised" with the Exception ability and "caught" with a handler.

Our error handling with abilities doc describes this pattern and more error types in detail.

Using abilities

Abilities are used for effect management in Unison.

getRandomElem : [a] ->{Abort, Random} a
getRandomElem list =
  index = base_2_12_0.abilities.Random.natIn 0 (List.size list)
  List.at! index list

This plucks a random element from the list by its index with base.abilities.Random.natIn, a function using the Random ability. If the index is not present in the list, it uses the Abort ability to halt execution.

splitmix and toOptional! are examples of ability handlers.

Distributed computations

Distributed computations can be expressed in the Unison language itself through the Remote ability. Read about the Remote ability and its features

distributed : Seq k Nat ->{Remote} Nat
distributed dseq =
  use Nat + ==
    |> Seq.map (x -> x + 1)
    |> Seq.filter (x -> Nat.mod x 7 == 0)
    |> Seq.reduce 0 (+)

This simple map/reduce code can operate over a distributed sequence, where the data may live in many different nodes in a cluster. This distributed computation use case has been fleshed out in an article.

Issuing an http request

Pull the library from Unison Share. with the pull command.

myProject/main> pull @unison/httpclient/releases/latest lib.httpclient
exampleGet : '{IO, Exception} HttpResponse
exampleGet _ =
  uri =
    match URI.parse
      "https://share.unison-lang.org/@unison/httpclient" with
      Some uri -> uri
      None ->
          (typeLink IOError) "unable to parse URI" ()
  req = do Http.get uri
  Http.run req

The first part of this code uses data constructors from the http library to create a full uri out of an authority and path. The request is handled by passing it to the Http handler.

The http library has great docs!

Basic file operations

Our standard library has a number of helpful File operations built in. They're located under the FilePath and Handle namespaces.

Concurrency primitives

Concurrency primitives like MVar, TVar, and STM are built into the base library. TVar and STM make it easy to write lock-free concurrent mutable data structures. For instance, here’s a simple lock-free queue implementation and a few helper functions:

type TQueue a
enqueue : a -> TQueue a ->{STM} ()
enqueue : a -> TQueue a ->{STM} ()
enqueue a = cases
  TQueue elems _ -> TVar.modify elems (es -> a List.+: es)
dequeue : TQueue a ->{STM} a
dequeue : TQueue a ->{STM} a
dequeue tq = match tryDequeue tq with
  None   -> !STM.retry
  Some a -> a

The block introduced by STM.atomically below ensures that no one can access state of the queue until after the actions in the block have taken place.

queueExample : '{IO, Exception} ()
queueExample _ =
  runQueue : '{STM} Nat
  runQueue _ =
    queue = TQueue.fromList [1, 2, 3, 4, 5]
    enqueue 6 queue
    dequeue queue
    dequeue queue
  result = STM.atomically runQueue
  printLine (Nat.toText result)