3 ms·
Indeed. Definition 2 is broadly in line with what "reference" means in general usage, while definition 1 is a term of art in the vocabulary of certain languages
by mannykannot 10y ago
Indeed. Definition 2 is broadly in line with what "reference" means in general usage, while definition 1 is a term of art in the vocabulary of certain languages.
Before languages had reference types or pointers, the distinction between pass-by-reference and pass-by-value (or also pass-by-name in Lisp and Algol) told you important things about the semantics of the language, such as whether a function you called could modify the objects you gave as arguments, or incorporate them by reference into some other object, and how arguments that are expressions are handled. Some of those distinctions become blurred when pointers or references are passed by value; the distinction on how you pass them now becomes a distinction on what you pass. In practice, is there any language that passes pointers or references by reference?
- kazinator 10y agoDon't know of any mainstream Lisp with "pass by name". Which dialects have or had that?
- lvh 10y agoNot the author, but they may be referring to NLAMBDA/FEXPR. (That hasn't been in Lisp since the eighties, because, well, Lisp had better tools. NLMABDA/FEXPR basically made AOT compiling Lisp impossible, and all of the use cases were much more neatly handled by a macro system.)
- mannykannot 10y agoIt has been a while since I used any form of Lisp, but nlambda seems to be what I was remembering as pass-by-name (and hand-annotated as deprecated, here:) https://www.cl.cam.ac.uk/teaching/1213/ConceptsPL/l4.pdf https://www.cl.cam.ac.uk/teaching/1213/ConceptsPL/l4.pdf
- kazinator 10y agohttps://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_name https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_na... Call by name is actually a closure based mechanism under the hood. It's like lazy evaluation, but with side effects allowed: (let ((0 42)) (a #(1 2 3)) (foo (aref a (incf i)))) If foo is call by name, then it receives a thunk (basically a lexical closure) for the argument. The aref expression is then evaluated inside foo whenever foo refers to the corresponding argument. (Unfortunately, repeating the incf side effect). We could simulate this with: (foo (lambda () (aref a (incf i))) with foo using (funcall arg) to get the argument's value. Call by need, described below that, seems exactly like the insertion of delay and force. The call-by-need argument expression is basically wrapped in an implicit delay, and callee refers to the argument value using implicit force. delay and force are just additional wrapping around a closure to add a flag to squash multiple evaluations.
- kazinator 10y agoAh! I just remembered I implemented some macros to simulate call by name in the TXR solution to the "Man or boy test" task on Rosetta Code: https://rosettacode.org/wiki/Man_or_boy_test#TXR https://rosettacode.org/wiki/Man_or_boy_test#TXR With these macros, Knuth's test is expressed exactly in the original form (modulo the Algol being cast into S-exp syntax). Call by name isn't just lambda with funcall (as might be wrongly inferred from my comments in the other reply), because it supports assignment. If we pass a variable A with call by name, the caller can assign to it. This all works with the given macros, and it probably more or less corresponds to the Algol mechanisms. (The task description is "Imitate Knuth's example in Algol 60 in another language, as far as possible." so the solutions which don't actually implement a call-by-name syntax and semantics don't go as far as this solution.)
- lvh 10y agoIt may be what reference means, but just because a language has references doesn't mean that it is passing-by-reference. There are plenty of languages that allow actual pass-by-ref, like C++. The usual litmus test is writing a function that takes two ints (not int refs!) and they're swapped at the end. In C++, you can tell because of "int&" in the signature of the function you're calling; as opposed to plain int. In languages with macros, you get to cheat a little, and write something that looks like the above without _really_ having those semantics. However, in languages like Lisps where your macro language is extremely powerful, you might not really miss it so much. There is a lot of convenience in _not_ having pass-by-ref! Sometimes I feel like the ardent opposition just doesn't want to admit their favorite language is missing a feature ;-) If I call a function f(a, b), I know that a, b are still pointing at the same object in e.g. Python. That is not the case in C++ (pass-by-ref) or Lisp (arbitrary macros). Predictable language semantics make code easier to reason about. That's a good thing, and it's quite plausible that the added complexity and confusion of supporting pass-by-ref isn't worth the expressiveness. I appreciate that the term is confusing, but it does mean something. I submit that making it mean two things would be even more confusing. If it's confusing, just say "passing (an argument) _as a reference_" which is unambiguously about what you're passing, and not how you're passing it.
- mannykannot 10y agoWhenever a word with a general meaning is adopted, and specialized, as a term of art, a double meaning is created. I get the impression that philosophy prefers to invent new words (unless it is reaching into the darkest corners of the vocabulary.) What has happened here is that the double meaning of 'reference' has caused some confusion over the term pass-by-reference, which is purely a term of art. So long as a person understands what gets passed and, especially, its implications, I am not particularly bothered by the mistaken usage.
- deleted 10y ago[deleted]
- Someone 10y ago"If I call a function f(a, b), I know that a, b are still pointing at the same object in e.g. Python." def f(x): global a a = "No, you don't" a = "I know" f(a) print a This counterexample may be far-sought, but it is a minimal version of what can easily happen in larger programs that have shared mutable state. But yes, call by reference can be confusing. It also can be very useful, though. That's why many modern languages have call by reference, but also require callers to specify it at each call site. I think that gives you the best of both worlds.
- TimJYoung 10y agoObject Pascal (Delphi, Free Pascal) allows this. You can use a var (variable) parameter to pass any type by reference to a function/procedure/method. This is used quite a bit in the Win32 API interface units in the run-time library because it simplifies the usage of the API by removing the requirement for double indirection when dealing with pointer parameters that need to be updated by the callee, as well as pointers in general for other type/structure parameters that need to be updated by the callee. As the caller, instead of passing a pointer to a structure as a parameter, you simply pass the structure. There are also out parameters that work similarly, but are, obviously, output-only. In general, you can use pointers in Object Pascal, but the language offers a lot of options for avoiding them, if you want to, and you get type-checked references in the process. Records (structs) are stack-based and pass-by-value, by default, and class references/class instances (instances are always heap-allocated) are pass-by-reference, by default. So, for the majority of applications, you can use parameters in a myriad number of ways in order to improve performance or provide the proper calling semantics, without any of the hassle of address-of or de-referencing operators.