Running Unison Programs

Running a main functions

Unison programs can be executed within theUCMwith theruncommand. This command is used to execute the "main" entry point to your Unison code. Theruncommand expects adelayed computationreturning any type. Theruncommand provides a handler for theIOandExceptionabilities, so programs which perform IO and bubble up top-level failures can be executed by the UCM.

🧠
It's common for yourmainfunction to have a a type signature ofmyMain : '{IO, Exception}().

Arguments to main functions

To accept arguments to the entry point of your Unison program, you can use thegetArgsfunction:getArgs : '{IO, Exception} [Text].

getArgsreturns a list ofTextrepresenting the arguments of the program in a thunk or delayed computation. Remember to force the thunk with the!when you actually want the arguments to work with.

✨
It might be tempting to represent the fact that your main function accepts a list of arguments in its type signature, butgetArgseffectfullyreads the command line arguments, which is why it's expressed in IO. Your main function should still have the signaturemyMain : '{IO, Exception} ()so that it can be executed by the UCM handler for running programs.

The following function will print out the given main function arguments:

myMain : '{IO, Exception} ()
myMain _ =
  use Text ++
  args : [Text]
  args = !getArgs
  printLine ("Hello " ++ lib.base.Text.join " " args)

In the UCM, you can call it withrunlike

project/main> run myMain a b c

And it will return the following to the console:

project/main> run myMain a b c
Hello a b c

Stand-alone binaries

The UCM can produce standalone binary executables for when you want to run a Unison program without entering into the CLI! These binary executables contain all the Unison code for the program in questionANDits essential dependencies in a single, lightweight bytecode file.

Toproducethe binary file run thecompilecommand from within the UCM:

myProject/main> compile myMain myRadExecutable

The first argument is the entry point to your Unison program (the "main" method described above), and the second argument is the name of the executable file that the UCM should produce.

Unison executable files have the suffix.ucso the above command will write a file calledmyRadExecutable.ucin the folder where the codebase lives.

Torunthe binary executable, you can then issue the following command from the terminal of your choice:

$ ucm run.compiled myRadExecutable.uc

The arguments supplied to this are

🌻 Your Unison program is now up and running!