ID: uiua-intro
2024-08-17 01:47

Notes from Uiua programming language ↯◰⍥

This week I've tried Uiua, a very strange programming language. How strange? Well I'm glad you've asked. It is

  • functional (i.e. mutations are not allowed)
  • array based
  • heavily relied on combinators
  • dynamicly typed
  • stack based [advocates tacit programming, meaning your arguments do not have name, only order matters]

you can say it is successor to APL, a very strange programming language that looks anything but A Programming Language [, hence the name]. see Array languages comparisons for more info.

if I want to present a little about its weirdness, just take a look at Uiua solution for 100 doors problem on Rosetta Code :

2/+=0⊞◿.+1100

Let's write it line by line for better illustration the cool thing about the interpreter is if you put empty double comment ##, after running your script, it will be replaced with the latest value on the stack ( actually you dont need to manually run it, you just hit uiua command and it will start to watch your .ua files ). . this is line separated version of above code. (I've replaced 100 with 10 for simplicity)

10
#- creates a range from 0 to 10-1
## [0 1 2 3 4 5 6 7 8 9]

+1
#- add 1 to all of them
## [1 2 3 4 5 6 7 8 9 10]

.
#- makes a copy of this array on the stack [actually not a copy, just a reference]
## [1 2 3 4 5 6 7 8 9 10]

⊞◿
#- creates a 2D array with each row modulated by column. ◿ is modulo function.
## ╭─
##   0 0 0 0 0 0 0 0 0 0
##   1 0 1 0 1 0 1 0 1 0
##   1 2 0 1 2 0 1 2 0 1
##   1 2 3 0 1 2 3 0 1 2
##   1 2 3 4 0 1 2 3 4 0
##   1 2 3 4 5 0 1 2 3 4
##   1 2 3 4 5 6 0 1 2 3
##   1 2 3 4 5 6 7 0 1 2
##   1 2 3 4 5 6 7 8 0 1
##   1 2 3 4 5 6 7 8 9 0
##                             ╯

=0
#- check which of them are 0
## ╭─
##   1 1 1 1 1 1 1 1 1 1
##   0 1 0 1 0 1 0 1 0 1
##   0 0 1 0 0 1 0 0 1 0
##   0 0 0 1 0 0 0 1 0 0
##   0 0 0 0 1 0 0 0 0 1
##   0 0 0 0 0 1 0 0 0 0
##   0 0 0 0 0 0 1 0 0 0
##   0 0 0 0 0 0 0 1 0 0
##   0 0 0 0 0 0 0 0 1 0
##   0 0 0 0 0 0 0 0 0 1
##                       ╯

/+
#- sums by column
## [1 2 2 3 2 4 2 4 3 4]2
#- take modulo 2 from every element from array
## [1 0 0 1 0 0 0 0 1 0]

The resulting array indicates which doors are open(1) and which ones are closed(0).

My motivation to learn Uiua was mostly Curiosity, I really wanted to know what is the secret behind this cryptic texts! The curiosity is activated after reading exploring Uiua, big thank to its author.

The Shiny Side
  1. Looks - the code seems pretty even though unreadable, its online editor and VS Code extension colors functions and combinators based on their arity (number of arguemtns) e.g. functions with 1 argument are green, functions with 2 arguments are blue, ...)
  2. Syntax - sorry this can be counted as looks, but isn't it amazing to write a function name and get its glyph by the formatter? you just have to remember its name! It reminds me Pinyin for Chinese the language that I love but can't find time to learn it
  3. Documentation - in some sections after explaining the idea behind concept, it had exercises with ability to run directly in the browser
The Ugly Side

Life is about trade-off.

  1. I found the tacit programming style actually a very good technique when your function is small and does something simple; But as purpose of the function gets more complex, you may feel drowning in your code. Although I'd say the author has done a remarkable job to reduce this complexity by ideas like Planet Notation ⋅⊙⋅∘ (for stack manipulation) or modifiers like Both (for applying 1 function to 2 sets of values) and Fork (for applying multiple functions on same set of values).
  2. The choice of using Box as a wrapper for array cells with dynamic typing behaviour of the language really hurts.
  3. Conditional primitives are not mature yet and using them is not convenient. I can't imagine how on Earth is possible to write a program without conditionals.
  4. Loop primitives is also confusing.

Side Notes
  • I really liked their defense of design; it made me think about basic things.
  • the choice of primitives help the language to do a novel sets of optimization.
  • I really liked this quote from Uiua website:
    If this code seems weird and unreadable, that's okay! It's important to remember that foreign ≠ confusing.
    It reminds me of this quote from Bernard Shaw
    Those who cannot change their minds cannot change anything
Take Aways
  • as I mentioned earlier, it gets exponentialy more complex when your problem gets more complex and your edge cases arise.
  • as result, even simple Advent of Code problems require you to spend hours and hours thinking array-based + stack based + functional; believe me It's too much.

Read more about Uiua here: