this post was submitted on 30 Sep 2023
61 points (85.1% liked)

Programming

17497 readers
35 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 1 year ago

Alright as someone who likes Haskell and has dabbled in unison before, I believe I can answer all these questions for you:

  • Why is helloWorld there twice?

It is common in languages like haskell and ocaml to first mention the type of a function, so in this case:

  • the type of helloWorld is '{IO, Exception} (). That is it's type signature (important for later)
  • the implementation of helloWorld is \_ -> println "Hello, World!"
  • What's the ' for?
  • What are the () for?

Here is where I have to get into the nitty gritty of how unison actually works. Unison has what programming language researchers call an effect system. The type signature of helloWorld indicates that it can perform the IO and Exception types of side effects, and these need to be handled. (in this case, they are handled by the compiler, but other types of side effects can be handled by the programmer themselves)
However, for reasons Unison does not like dealing with eagerly evaluated non-function values with side effects. For this reason, there is '. Essentially, what it does is turn a value into a function that accepts () as it's argument. We could therefore say that the type signature of helloWorld is also () -> {IO, Exception} (). The last () indicates that, next to it's IO and Exception side effects, it also returns () as a value. This is because, in functional programming languages, all functions need to return values (or run infinitely, but that is for another topic)

Now I've been used to functional programming for quite a while now, so things that seem natural to me can be absolutely woozy for anyone not used to this paradigm. So if anything still feels vague to you feel free to comment