Upcoming Hy: Mangling changes

Upcoming version of Hy will have some changes to how mangling works. This blog post looks at the current state and explains it in high level.

Pull request #1517 introduces some changes in how mangling works. In short, mangling is Hy’s way of translating names that are valid in Hy, but not in Python, into valid Python names. This change mostly affects on Python code that interfaces with Hy code.

*foo* used to be mangled into FOO and foo! into foo_bang. This is not case anymore. If you were relying on this mangling on your Python code, you need to adapt it when taking next Hy release into use. On the other hand transformation of foo-bar to foo_bar and foo? to is_foo both remain.

Punycode transformation also has been removed. Earlier used to become hy_w7h when running Hy in Python 2.x environment.

But we still need a way to represent names that aren’t valid in Python. For that a new scheme is introduced where they’re are prepended with hyx_x and each illegal character is replaced with name that starts and ends with Δ. In between two Δs characters name or code point (prepended with U is inserted. Since my explanation isn’t the clearest, lets see couple examples:

  • *foo* becomes hyx_ΔasteriskΔfooΔasteriskΔ
  • ⚘ becomes hyx_ΔflowerΔ
  • ⊯ becomes hyx_Δnegated_double_vertical_bar_double_right_turnstileΔ (that’s a mouthful)

Finally, keyword arguments are also handled according to the same rules.

All this enables us to write things like (I’m not saying this necessarily is a good idea, but it’s possible):

=> (defn Δ [x₀ x₁]
...  (- x₁ x₀))

=> (Δ :x₀ 5 :x₁ 2)

which behind the scenes is equivalent to:

=> def Δ(hyx_xΔsubscript_zeroΔ, hyx_xΔsubscript_oneΔ):
...    return hyx_xΔsubscript_oneΔ - hyx_xΔsubscript_zeroΔ

=> Δ(hyx_xΔsubscript_zeroΔ=5, hyx_xΔsubscript_oneΔ=2)

The biggest advantage for me is that I can finally use subscripts and other unicode characters in parameter names. I have been using unicode names for local variables for a long time and been mildly annoying that it wasn’t possible to use all of them in parameter names.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s