What is “Open Recursion”?
journal.stuffwithstuff.com
journal.stuffwithstuff.com
If it's because doing so requires forward-declarations/hoisting/reassignment, here[1] is an implementation in JavaScript that has only a single `let` statement, for the counter itself.
What you need is the ability to implement recursive functions. That's not 'built in' to the lambda calculus, because there's no real concept of variable bindings, just functions that take arguments. In other words, you can't say "function x() { x(); }". In the untyped lambda calculus, that's no problem, because you can simulate it, e.g. by saying
let x = (_x) => _x(_x);
x(x);
Your implementation of open recursion uses a similar construct in 'mutualRecursion', as well as in the implementation of 'inc' (which calls 'set' passing 'set' itself as an argument).But in the simply typed lambda calculus, that doesn't work, because 'x' would have an infinite type. For example, how would you write the type of x in TypeScript? Something like this:
let x: (???) => whatever = (_x) => _x(_x);
where '???' is the type of x itself, so... let x: ((???) => whatever) => whatever = (_x) => _x(_x);
let x: (((???) => whatever) => whatever) => whatever = (_x) => _x(_x);
...that doesn't work.You could use a record/class type:
class X {
f: (X) => whatever;
constructor(x) { this.x = x; }
}
let x = new X((_x) => _x.f(_x));
x.f(x);
...but the desugaring of records into lambda calculus doesn't allow for that. (The use of X within its own type definition is recursive.)You could use a generic function type:
let x: <T>(arg: (T) => T) => whatever = (_x) => _x(_x);
but the simply typed lambda calculus has no generics.But there's a more modest extension that can enable recursion, which is taking a fixed-point combinator (which in the untyped lambda calculus is just a function you can implement, e.g. as the Y combinator), and baking it into the typed lambda calculus as a primitive. Which is one of the things Pierce talks about in the book.
Yes, exactly. I've created a fork [1] where I replace the multiple-parameter Y combinator with a single-parameter kind that operates on what is effectively a vtable.
Which has nothing to do with recursion.
A lambda and a closure are completely different things. A lambda is just an anonymous function. A closure is a function instance which carries with it some context from an outer scope. You can have a lambda which is not a closure, and a closure which is not a lambda. Javascript often has closures which are not lambdas - that's the usual form of a callback function.
For languages that work much like LISP, with dynamic function creation, nested functions, and garbage collection, you get most of the heavy machinery needed for closures by default. If you've got that, you can kludge objects into existence. This was originally called "flavors" in LISP. Javascript does this, and suffers from having too many ways to create OOP-like objects. The LISP and Javascript experiences indicate that implementing OOP via closures creates a mess in source code.
Lambdas are just syntactic sugar for nested functions.
The recursion in open recursion refers to the ability of an object to refer to itself (the 'this' or 'self' value) and the open bit refers to late binding (the value to which 'this' or 'self' is bound is open to change).
Agreed on the duality between closures and objects. Closures are a poor man's objects and objects are a poor man's closures, as the joke goes.
Had you read the article you might have realized this term is overloaded.
https://en.m.wikipedia.org/wiki/This_(computer_programming)#...
Can you clarify this further? To me a callback function is a lambda. It may or may not be a closure.
function foo(x) { return x + 1; }
function bar(f, y) { return f(y); }
// `foo` is a callback but neither a lambda nor a closure
bar(foo, 2);
edit: adding the missing the "b" in lambda foo = function (x) { return x + 1; }
is identical to function foo (x) { return x + 1; }
They are also closures. Since they don't refer to any outer variables, they are closures containing zero context variables. If foo were to refer to some other variable, the closure would contain one context variable.The only reason to distinguish between lambdas and closures is because some languages do not support closures (e.g. early versions of Emacs Lisp). Attempting to refer to other variables would always refer to a global variable with that name.
I'll concede that for JavaScript (and a few other languages); some languages don't treat all functions as just a lambda with a binding, though.
> They are also closures. Since they don't refer to any outer variables, they are closures containing zero context variables. If foo were to refer to some other variable, the closure would contain one context variable.
I'm not very keen on the definition of "closure" including functions that close over zero variables, because then "closure" becomes indistinguishable from "function", and the word is no longer useful. I might be wrong about the technical definition, but I feel like having a word for closures isn't very useful if it doesn't mean "closing over one or more variables".
> (lambda () (print "hi"))
(closure (t) nil (print "hi"))
> (let ((a 1) (b 2)) (lambda () (print "hi")))
(closure ((b . 2) (a . 1) t) nil (print "hi"))
> (let ((a 1)) (defun foo (x) (+ x 1)))
foo
> (symbol-function 'foo)
(closure ((a . 1) t) (x) (+ x 1))
> (defun bar (x) (+ x 1))
bar
> (symbol-function 'bar)
(closure (t) (x) (+ x 1))
So yes, every function is a closure.Every function isn't a closure. In particular, elisp lambdas aren't closures because elisp is dynamically bound - the variables are open, they are not closed over. Or with another perspective, the variables are bound in the global context. Either way, elisp closures aren't.
From emacs scratch buffer:
(setq f (let ((x 10))
(lambda ()
(progn
(setq x (+ x 1))
x))))
(funcall f)
11
(funcall f)
12
(setq x 20)
(funcall f)
21
This is because elisp is dynamically bound - it only has one environment (in this example), the global environment (leave aside buffer-local etc. for the moment).It doesn't matter if you try to create a new environment using a lambda, the same problem still occurs:
(setq g
(lambda (x)
(lambda ()
(progn
(setq x (+ x 1))
x))))
(setq f (funcall g 1))
(funcall f)
22 ;; or whatever you last assigned to x, + 1If you pass your examples to `(eval ... t)`, you'll see what I mean. Or run `(setq-local lexical-binding t)` before evaluating them in the scratch buffer.
> (setq g (lambda (x)
(lambda ()
(progn
(setq x (+ x 1))
x))))
(setq f (funcall g 1))
(funcall f)
2
> f
(closure ((x . 2) t) nil (progn (setq x (+ x 1)) x))
> (setq x 20)
20
> (funcall f)
3[0] https://developer.mozilla.org/en/docs/Web/JavaScript/Closure...
You can see this in Ruby where methods don't close over the environment, but blocks do, clearly Ruby methods aren't closures, not even when reïfied into a Method, whereas blocks are closures.
It's like code expecting rectangles wouldn't mind to get squares, but a code expecting squares will definitely do mind getting rectangles.
It is actually not that obvious since in OOP neither a square or a rectangle type is properly a subtype of the other. [0]
I understand how implementation-wise it could be convenient to treat a function as a closure. Although, I would prefer some other type, eg:
functoid := closure | function
[0] - https://stackoverflow.com/questions/1030521/is-deriving-squa...You might define the interface of a closure as `closure(f: Function, env: Mapping[str, Any])`, or in other words a closure is a function and a mapping of variable names to values. That mapping can be empty, but that's different than the different interface `not_a_closure(f: Function)`, which is incapable of closing over anything, including the empty set.
Pay attention to page 17 and subsequent pages, talking about variable binding in a closed or open context - this is the closing that closure refers to. Page 31 talks about closure in the context of a lambda.
-- only if lexical-binding is non-nil :)
What a surprise...
You're free to be as prejudiced as you want, but I gave an example of an actual implementation.
Regardless, though, I don't think what any one language calls something is going to go very far in convincing me. As for the Rust ad hominem, I've been specifically arguing against the terminology that Rust uses (i.e. all lambdas are called closures, and nothing is called lambdas) that is much more consistent to your viewpoint than mine, so I don't really see what point you're trying to make there, other than that you're as prejudiced against Rust as I am against Elisp. But that's fine though; people are free to like whichever languages they like, but I don't really think it's worth taking it personally when others disagree.
Most commonly a lambda is regarded as an anonymous function. [0] If you name it, it is no longer anonymous.
[0] - https://softwareengineering.stackexchange.com/questions/1307...
Read the article and ignore the comments.
I have a pretty good idea of what a scheme implementation is, but i'll leave the details as an exercise for the reader.
In [1]: def foo(f, a):
...: return f(foo, a)
...:
In [2]: foo(lambda f, a: f(lambda f, a: a, a-1) if a > 0 else a, 10)
Out[2]: 9
Obviously that's a completely useless and convoluted example, but I'm fairly sure the concept of a lambda calling the function it was passed to just works in most languages.so something vaguely like
(define local-state)
(super-y
(lambda (inc get set v)
(set inc get set (+ 1(get inc get set v))))) ;this one is inc
(lambda (inc get set v) local-state) ;this one is get
(lambda (inc get set v) (set! local-state v)) ; this one is set
you don't really even need local state, just pass around an accumulator.Once you have lambdas and a system that allows infinite types, mutual visibility is pretty easy to come by, mutual anonymous recursion is something i think is super nifty.
you can save yourself some pain by sticking the functions in a map, of course and passing that around instead.
Huh? What is this, the 80s?
Honest question: what does TCO have to do with use-before-declaration? (Maybe you mean that self-calls—not the only possible kind of tail call!—aren't natively allowed? I don't use Clojure, so I don't know if that's the case.)
Mutual recursion, tails calls to other functions. If you have two functions that each one calls each other you get a compiler error cause one of the two has not seen the definition of the other.
Since selfs-calls are easy to implement they are supported explicitly in Clojure via the recur keyword, they won't blow up the stack, but tails calls to other functions are not posible(without using some weird trickery) in Clojure because of the limitations in the JVM.
1> (mlet ((z (+ x y))
(y (succ x))
(x 42))
(list x y z))
(42 43 85)
2> (mlet ((z (+ x y))
(y (succ x))
(x (* z 2)))
(list x y z))
** (expr-2:1) force: recursion forcing delayed form (* z 2) (expr-2:3)
3> (flip *print-circle*)
t
4> (defstruct node () next prev)
#<struct-type node>
5> (mlet ((n (lnew node next n prev n))) n)
#1=#S(node next #1# prev #1#)
6> (mlet ((c (lcons 1 c))) c)
#1=(1 . #1#)The article makes the case that the "simpler language" is the one where a function can only call another function if it has been declared before. Then, the ability to call any function regardless of declaration would be seen as an addition to that.
But couldn't one make an equally valid case that the "simpler language" is the one where function declaration order is not significant, that unordered is "simpler" than ordered? Then, the availability of a "F was declared before G" relation would be seen as the addition.
After all, the only reason we intuitively feel there is an order to function declarations is because the computer program's representation in source code is linear text. But I'd argue that the more fundamental representation of a computer program is that of a graph. Or at least equally fundamental :) It's just that in practice, computer memory is linear, and so any representation of a graph structure will have an implicit order that we need to tell the computer to ignore. But mathematically this ordering is just a side-effect of computer memory, irrelevant just like whether '1' bits weigh more than '0' bits or not.
set(value) {
count = 0
while count != value {
increment()
}
}
(~~may not be~~ probably isn't valid Dart)