BQN "macros" with •Decompose 02/04/2023 Using •Decompose to create functions with access to the syntax tree of their arguments. I just learned about the BQN system function •Decompose, and it's really cool. As its name suggests, •Decompose breaks down a compound function, giving access to the parts of the function and specifying how they were combined. In other words, it grants us access to the syntax tree of a tacit function, sort of like a Lisp macro. This is a powerful ability! It allows us to do dark magic in opposition to BQN's "no magic" design philosophy. There are limitations, however. •Decompose only does something useful with tacit functions. It doesn't delay evaluation of its argument or anything like that. So if we call it on 2+2 it only sees 4. Also we can't pass it something with invalid syntax like 4+. In this blog post, I demonstrate two uses of •Decompose: alternate train semantics and symbolic differentiation. Using •Decompose •Decompose breaks down a function and returns a list. The first element of the list is a code indicating what kind of structure the function has. The rest of the elements are the components of the function. Here are a couple examples: •Decompose ⊑⟨+´÷≠⟩ ⟨ 3 +´ ÷ ≠ ⟩ 3 indicates that the original function was a 3-train. •Decompose ⊑⟨|∘-⟩ ⟨ 5 | ∘ - ⟩ 5 indicates that the original function was a 2-modifier applied to two functions. Note that •Decompose is a function and not a modifier, so we need to pass it a function with a subject role. Putting a function in a list and then extracting it from the list with ⊑ is one way to do that. Also note that •Decompose only breaks down one level of a function. For example, it did not further break down +´ in the first example. Here is the full list of codes and what they mean: Code Kind ---- ---- -1 Non-operation 0 Primitive 1 Other 2 2-train 3 3-train 4 1-modifier 5 2-modifier Codes -1, 0, and 1 correspond to things that are not compound functions. With those preliminaries out the way, le...
First seen: 2025-10-19 12:01
Last seen: 2025-10-19 15:01