highlights
Jul 28, 2023

🌞 Summer 2023 Unison ecosystem highlights

Rebecca Mark

The Unison ecosystem has been a hive of activity lately! After the launch of Unison projects, we've seen a number of new libraries materialize. Naturally, we took this as an opportunity to make fun things! Check out the demos and code snippets below. We hope this ecosystem synopsis inspires you to do the same.

Hotswapping Unison code

Inspiration struck Unison contributor @dfreeman in the form of an offhand feature request in Unison's community slack. The nerd-sniping worked and now we have a library for live-updating Unison code as it's running. 🀯 It might have been a speculative idea at the time, but the result is Hotswap: a library that enables you to record "placeholder" values in your program and then update them live during the program's runtime.

πŸ”₯

A demo is worth a thousand words:

We loved this library so much we created a cloneable project with steps for instrumenting hotswapping so you can try running it yourself!

It's adapted from the excellent video demo in the project's README shown below:

An animated gif demonstrating a hotswap server that responds as it's sent code updates.
Hot-swapping in action from the project's README.

The docs for this library are fantastic 🀌 so you can follow along with how the library was implemented, understand the caveats for using hot-swapping, and dream up new use cases.

Json Schema

Thanks to community member @chuwy, Unison has a libray that can model, derive, and validate Json Schemas. For folks who have never used Json Schemas, they're a way to describe the shape of a Json document and whew... the schema specification is vast πŸ˜…! We're very grateful to have @chuwy's expertise at work in authoring this library. πŸ“š

Routes

Unison has a new abilities-based library for defining server routes. As you're writing Unison web apps, you can use the Routes library to define how your application should respond if a given route is matched.

Here's what defining routes looks like:

_hiRoute : '{Route} ()
_hiRoute : '{Route} ()
_hiRoute = do
  use Parser /
  use Text ++
  name = route GET (s "hello" / Parser.text)
  ok.text ("πŸ‘‹ hello " ++ name)
_byeRoute : '{Route} ()
_byeRoute = do
  use Parser /
  use Text ++
  name = route GET (s "bye" / Parser.text)
  ok.text ("πŸ‘‹ bye " ++ name)

At the edge of your program, you can combine all of your routes together and serve them on a port.

_main : '{IO, Exception} ()
_main : '{IO, Exception} ()
_main = do
  use Route <|>
  stop = serveSimple (_hiRoute <|> _byeRoute) 8080
  printLine "Server running. Press <enter> to stop."
  _ = readLine
  stop()

Midi Sequencer

Take a look at the ever expanding universe that is Alvaro's project list, and you'll find a new library called Sequencer for interacting with the MIDI protocol. Sequencer enables you to write Unison programs for playing music on Midi devices.

You know what that means...

🎹

It's time for a demo! πŸŽ‰

If you would like to join the Unison command line band, pull the project here or write something brand new using the Sequencer library.

Terminus

Runar's terminal library is going to radically increase the amount of time you spend playing command line games while your non-Unison code compiles.

Terminus provides a set of functions for interacting with the terminal, including the ability to write text to the screen, read input from the user, and store, move, or reset the cursor at different screen locations.

🐍
The terminus library ships with an example snake game. Just pull the project and enter run examples.snake.main in the UCM to start playing.

TLS simple

TLS primitives have existed in Unison's standard lib for a while, but @runarorama recently added a library that makes it easier to send encrypted messages over a secure channel in Unison. The TLS library is build atop a new TCP server library, also authored by Runar. Rather than managing TCP sockets yourself, you can use more ergonomic functions like Tls.serve and Tls.connect.

Unison tree-sitter grammar

Tree-sitter is an open-source parsing library that can be used to build syntax highlighting, code folding, and other code editing features. If you've ever wished that your IDE could be "smarter" or more syntactically aware about its suggestions, tree-sitter exists to help languages support those features. Many programming languages publish tree-sitter grammars

Unison's tree-sitter grammar, written by community member Kyle Goetz is an impressive feat. Unison's grammar is complex and Kyle dove into the Haskell code, combed through our language guide and documentation, and we can only imagine spent many hours working to support all of Unison's unique language features.

We can't wait to incorporate this grammar into our IDE's. Thanks Kyle!

Web Sockets

Unison contributor @danfreeman has released a library for working with web sockets in Unison. Web sockets enable both the client and server to send and receive information in a two-way data stream. If you've always wondered how web-sockets work, check out the websockets.serve function to see how http requests are upgraded to web-socket connections.

The ecosystem is filling out with fundamental protocol libraries like this one. They're easy to take for granted if you've worked in older languages, so it's nice to be able to see their foundations in Unison.

XML Soup

For your web-scraping needs, @runarorama has added the Soup library for parsing and querying XML documents. It's nestled inside of a larger Unison XML library, but Soup is a user friendly interface for traversing through XML so its worth callling out separately.

Fun fact: The Unison blog itself publishes an Atom XML feed: https://www.unison-lang.org/feed.xml. You can use the Soup library to parse and query the feed. Maybe it's time for a Unison RSS reader project? πŸ€”

_getPosts : Soup ->{Throw XMLError} [(Text, Text)]
_getPosts input =
  use Soup text
  blogXML = input // "feed"
  Each.toList do
    entry = blogXML |> descendants |> Soup.named "entry"
    title = entry // "title" |> text
    date = entry // "updated" |> text
    (title, date)

Plotter library

Unison's tooling for making beautiful charts and graphs is getting off the ground thanks to Alvaro's plotter library.

πŸ“ˆ

Now you can move from using this...

A Texas Instruments 83 graphing calculator

To using this...

An image of equations graphed on a 2d plane using the plotter library
Ahhh yes... math πŸ’€

πŸ˜„ Thank you to our contributors and community

We're constantly wowed by the the ways that folks are using Unison's unique features to build new tools and libraries. Give our library author's a round of applause, or better yet, use their contributions to make something awesome. Happy coding!

An animated gif of running a Unison program that prints the Unison logo to the terminal