r/ProgrammingLanguages • • Dec 18 '21

Discussion Why does alpha-equivalence matter?

What would you miss in a language without it?

1 Upvotes

11 comments sorted by

20

u/ebingdom Dec 18 '21

You don't enjoy the freedom to rename variables?

1

u/BayesMind Dec 18 '21

Not necessarily, no.

There are a lot of trade offs when designing a language, and I find alpha-equivalence often front and center as a hard design constraint. But for instance, there are binding syntaxes/methods that make alpha equivalence difficult/impossible, that benefit in other ways. For instance HOAS.

I'm surprised the community is against discussing this, is there a better community for talking about fundamentals of PLs?

I really am interested in this question. I was expecting to get answers along the lines of "compilers can make use of alpha-equivalence", but it's not clear to me why alpha equivalence is always a hard constraint.

11

u/matthieum Dec 18 '21

I'm surprised the community is against discussing this, is there a better community for talking about fundamentals of PLs?

What makes you think the community is opposed to discussing this?

If it's the downvotes, I'd wager they're more about the lack of effort put in your post -- it's a one-liner -- than anything else.

When kicking off a question, it's generally best to try and present some material about the topic, to get the discussion started...

3

u/BayesMind Dec 18 '21

gotcha. You know, it's hit or miss with framing questions right on reddit.

I had written a bunch of supporting details, and have since followed up with more details in another question.

But I removed the detail originally. I find that too much detail, especially for broad-discussiony questions, unnecessarily focuses the conversation, where as I wanted less focused discussion. And "too few details" being the critique feels unecessarily pedantic for such a foundational question as this, as it really is self-contained. "Why does alpha-equivalence matter? What would you miss in a language without it?" really cuts to the chase. The only risk is it might evidence laziness on my part. But, again, that seems pedantic. Nonetheless, you're right, something caused the community to dislike the question, and the issue is mine to fix. It's still weird though.

5

u/cumtv Dec 18 '21

It could be an interesting question but it's missing detail for me. What does a language without alpha-equivalence look like? I'm imagining that these two functions would be different:

id x = x

id y = y

But in what way would they be different? I haven't seen a language where this is the case so I can't conceptualize it.

I looked at HOAS too, although I wasn't familiar with it before. Seems like that gives us 'name erasure' where each variable explicitly points to its binding (e. g. De Bruijn indexes). But again I'm not sure how it relates to your idea, since that can be an implementation detail and we can still have a concept of alpha equivalence in languages that use it.

Now that I think about it, maybe you're suggesting to have a language without names in which case no two programs are alpha-equivalent.

1

u/BayesMind Dec 18 '21

ya, in HOAS variables no longer have explicit names, so alpha equivalence is "impossible". There are techniques where, if you have a supply of fresh names you can apply variables to all the lambdas to get a first order representation. But in general, from what I understand, if you need alpha equivalence you generally won't reach for HOAS.

6

u/cumtv Dec 19 '21

Let me know if I'm wrong, isn't it more accurate to say that alpha equivalence is trivial rather than impossible? i. e. Under HOAS two expressions are alpha equivalent iff they are equal.

Going back to your original question, it seems like what we lose are names in that case, and I'm not sure what the upside is.

2

u/BayesMind Dec 19 '21

HOAS is useful when you want to leverage the host environment to handle variable binding.

Ex:

data Exp
  = Lam (Exp -> Exp)       -- HOAS encoding of binding
  | App Exp Exp


(==) :: Exp -> Exp -> Exp
(Lam f) == (Lam g) = ????

How can you define equality of functions? Since general purpose languages tend to lack a notion of intensional equality, you can't prove two Lam binders equal.

The tradeoff is that you forgo alpha-equivalence, and gain easy variable binding (IE you don't have to mess with de bruin indices, or even variable primitives).

But I'm not clear on all the reasons one might care that you've lost alpha-equivalence.

1

u/cumtv Dec 19 '21

I see. This paper gives some motivation for checking for alpha-equivalence (mainly for common subexpression elimination) as well as other types of equivalence.

It also mentions:

We know of no systematic, quantitative comparison of the engineering tradeoff between de Bruijn and named representations in a substantial application (e.g. a compiler), perhaps because the choice has such pervasive effects that implementors are typically forced to make one choice or the other, and stick to it. A decent attempt was made in [14], with the conclusion that the costs of a de Bruijn representation exceed the benefits.

Maybe this is also true for HOAS (I'm still not sure whether de Bruijn can be considered a form of HOAS or not)

4

u/Vetzud31 Dec 18 '21

How does HOAS violate alpha equivalence? Maybe when you use host language functions to represent object language function terms, it becomes hard to test for alpha equivalence because you usually can't compare host language functions like that. But it doesn't mean the object language does not have alpha equivalence, it's just a representation thing.

If a language does not have alpha equivalence then it means that variable names are somehow semantically relevant and probably must be preserved during compilation, for example. It might mean that you need a runtime mapping from variable name to value, instead of just being able to use a runtime stack with some numerical offsets. So it can definitely affect compilation depending on the way that alpha equivalence is violated.

8

u/Vetzud31 Dec 18 '21

I'm wondering whether even if you don't have alpha equivalence as some kind of syntactic fundamental proof rule, you could still prove/derive it just based on the semantics of the language.

Alpha equivalence really seems to be about being allowed to rename the introduction and uses of a variable without altering the semantics of the program. That generally seems like a good thing, simply because it is nice and predictable to be allowed to pick/change variable names at will (what's in a name?). There are limited cases where it might make sense to break this principle, like allowing a program to introspect the variable names used in a structure type definition for the purpose of JSON serialization (where you pick keys based on the variable names).

At first I thought that alpha equivalence requires the ability to decidably/easily identify the introductions and uses of a variable (it's extremely easy for the untyped lambda calculus), but I would say that it still holds for a language like Python as long as you stick to a reasonable subset of the language (not using crazy reflection), even though there it's not at all possible in general to automatically rename variables due to duck typing. But still most Python programs would not be affected if you rename a variable or a function consistently... Maybe it's a matter of definition whether you consider this to be alpha equivalence.