Skip to content

Stack Notation

molarmanful edited this page Nov 4, 2023 · 11 revisions

Dev Note: I am not too happy with the current notation, so this may be subject to change in the future. I am also thinking about backing the notation with a proper type-checker implementation.

In the commands reference, you may notice that each command has a Stack: entry. These entries informally describe how the command affects the stack. Take for example the following signature for swap:

a b -> b a

Here, b is the top of the stack and a is second from top of the stack. -> separates the "before" and "after" parts of the signature.

Another signature, this time for roll:

(a @ n) b* (n >NUM) -> b* a

Looks kinda gnarly right? Let's break it down.

  • n >NUM means that n is implicitly converted to NUM at the argument level.
  • a @ n means that a is n positions from the top of the stack (0-indexed). So n = 0 is the top of the stack, n = 1 is second from the top of the stack, n = 2 is third from the top of the stack, etc.
  • b* means that 0 or more items may exist at that location in the stack.

Other notation to know:

  • a' means that a vectorizes
  • SEQ[NUM*] means a SEQ containing 0 or more NUMs
  • MAP[(STR => NUM)*] means a MAP containing STR keys and NUM values
  • a.x means a value x that is an item in a
  • a | b means "a or b."
  • f: x -> _ means a value f that evaluates using the stack signature x -> _.