Consider the following SML function:
fn x => x x
This produces the following error (Standard ML of New Jersey v110.72):
stdIn:1.9-1.12 Error: operator is not a function [circularity]
operator: 'Z
in expression:
x x
I can sort of see why this isn’t allowed — for one, I’m not really sure how to write down what its type would be — but it’s not completely nonsensical; for instance, I could pass the identity function to it and get it back.
Is there a name for this function? (Is there a way to express it in SML?)
There is no way to express this function in a language with an ML-like type system. Even with the identity function it wouldn’t work, because the first
xand the second inx xwould have to be different instances of that function, of type(_a -> _a) -> (_a -> _a)and_a -> _a, respectively, for some type_a.In fact, type systems are designed to forbid constructs like
in the untyped lambda calculus. In the dynamically typed language Scheme, you can write this function:
and get the expected result