Skip to content

Instantly share code, notes, and snippets.

@MawKKe
Last active February 5, 2024 21:28
Show Gist options
  • Save MawKKe/ae8bfd393e15383b89b659b8dc6e10c7 to your computer and use it in GitHub Desktop.
Save MawKKe/ae8bfd393e15383b89b659b8dc6e10c7 to your computer and use it in GitHub Desktop.
Atoms? It was already like that when I found it...

Background

Most programming languages have basic types such as booleans, bytes, shorts, integers, floating point numbers, strings, lists, etc. If you squint your eyes a bit, you'll notice all of these are just a bunch of bytes with varying sizes and shapes.

Strings (arrays) are (possibly unbounded) sequence of bytes. Integers and floating point types are often represented as fixed number of bytes or bits. Booleans semantically means "one bit", but often are implemented as a single byte (for performance reasons).

Now, think how many different values or states can each of these represent. Well, obviously the answer if 2**(number of bits in the type), duh!

  • Strings (arrays) can encode billions, trillions, gazillions ... of different states.
  • Integers can represent quite many, but considerably less (~4 billion for 32 bits)
  • Short integers? Just about 16k.
  • Byte? 8.
  • Booleans can represent only 2.

You might notice a trend downwards in the number of states. But, can we go even lower?

What about a type that can represent or encode just a single state?

Could such thing be possible? What would it even look like?

What would it even mean?

It's not like you can split a bit in half, can you??? I thought a single bit was the lowest amount of information possible??

But maybe there is: atoms.

Atoms

Atoms, the weird thing you'be maybe came across while studying Lisp but didn't really appreaciate at the time. I claim that the idea of atoms represent a type or category of types that can encode a single state.

As in: it can only exist.

Atoms are often thrown around with such a rapid pace that they seem to emulate booleans with extra steps. Yet, I claim they are something else: atoms can only exist. Atom is not a boolean or anything else. An atom is and atom, and atom can only exists. Nothing else.

You cannot set an atom to any value, as it is not something that can store information. However, you can create them really easily. All you need to do is think and come up with a new atom and BLOOP. Now it exist! Congratulations on fathering a healthy atom-baby!

You can of course check if a variable has been set to specific atom instance. However, don't be fooled, an atom instance cannot not exist: for you to be able to check if it exists, or whether a variable is set to a specific atom instance requires you to first bring the atom into existence. Only then can you compare if something equals to it. The atom can only exist. And also checking a variable implies that somehow, somebody, somewhere already could have set the variable to the atom. As in, it already existed earlier than you thought.

A somewhat paradoxical situation. But its fine. It's fine. Shhh.. go back to sleep little baby.

I also cannot tell you to not think about an atom, because doing so also ensures both of us are thinking about it.

As long as there are references to an atom ensures it exists. Only if all mentions or memories of it have perished, maayybe the atom will cease to exist. Just maybe. But as long as we are talking here about it, it will never go away.

So, in the name of all that is Good and Logical, STOP THINKING!

Thank you for listening to my TED Talk.


anyways...

Atoms in programming

There aren't many (popular) languages that make use of atoms. Only Lisp(s), Erlang and Elixir come to mind. Most languages have enums but those require you to design your program and plan ahead like some kind of egghead, ugh. Can't I just throw some apostrophes around and be done with it? Thanks. And often if you make an enum you need to jump through all kinds of extra hoops to make them useful, like make them printable (fuck you C++). Recently I've been learning Rust, and its such a relief to be able to just chuck #[derive(Debug)] into the mix and move on with my life.

So, if you are designing a Next Big Language, do the needful and consider implementing support for atoms. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment