r/functionalprogramming • u/Deonisos • Jul 19 '23
Question When does Variable Capture happen in substitution?
I want to program a compiler for uni and have to use the locally nameless representation. I just don't get the point of locally nameless. Is it just a functional programming problem or does variable capture happen in java or kotlin too?
4
Upvotes
2
u/Deonisos Jul 19 '23
Apart from avoiding variable capture, it's supposed to be faster? Or takes less ressources when having a lot of recursion? I just can't imagine a scenario in wich many lambda functions with the same parameter name would occur so that it would matter. Isn't the programmer supposed to pick a different name for most variables anyway? These are my sources so far:
youtube.com/watch?v=uhGqJ1A_PRE
chargueraud.org/research/2009/ln/main.pdf
boarders.github.io/posts/locally-nameless.html
3
u/wtfftw Jul 19 '23
I am assuming they just want to avoid making lexical closures over variables visible in the lexical scope that you are defining a function in.
2
u/Hoo_Lee_Sheetus Jul 20 '23
Variable declaration have your rules, depending of your language. C programmer must be attent to declare your functions or global variables. C++ add namespaces to lead with this. Java and other OO languages uses a class and/or packages. But inside a function, the rules for most of the languages is to shadow external ones. Some languages allows you do shadowing inside a function, like Rust. But, it's just a rule generally recursive that knows what variable exists inside a block. When this occurs? Well, this depends of the implementation and language you are talking about.
OBS: The expression "Variable capture in substitution" seems like the process of computation of expressions in prolog, or a resolution of a chain of functions in a functional language. An example can be helpful.