101 points · by jasim · 9y ago · 58 comments
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.
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)