AnandA777
AnandA777

Reputation: 33

Type of recursively-typed functions in Ceylon

Is there a way to achieve some kind of recursively-typed functions in Ceylon? For example, I can define combinatory logic in Ceylon in a type-safe way like so:

class Fi(shared Fi(Fi) o) { }

Fi veritas =
    Fi((Fi t) =>
        Fi((Fi f) =>
            t));
Fi perfidas =
    Fi((Fi t) =>
        Fi((Fi f) =>
            f));
Fi si =
    Fi((Fi l) =>
        Fi((Fi m) =>
            Fi((Fi n) =>
                l.o(m).o(n))));

print(si.o(veritas).o(veritas).o(perfidas) == veritas);
print(si.o(perfidas).o(veritas).o(perfidas) == perfidas);
print(si.o(veritas).o(perfidas).o(veritas) == perfidas);
print(si.o(perfidas).o(perfidas).o(veritas) == veritas);

This code works as intended. However, for clarity, brevity, and applicability to other problems, I would like to be able to do implement this behavior using only functions. Take something like the following (non-working) example:

alias Fi => Fi(Fi);

Fi veritas(Fi t)(Fi f) => t;
Fi perfidas(Fi t)(Fi f) => f;
Fi si(Fi l)(Fi m)(Fi n) => l(m)(n);

print(si(veritas)(veritas)(perfidas) == veritas);
print(si(perfidas)(veritas)(perfidas) == perfidas);
print(si(veritas)(perfidas)(veritas) == perfidas);
print(si(perfidas)(perfidas)(veritas) == veritas);

In the function alias version, the Fi type represents functions whose operands and return values can be composed indefinitely. Note that due to their recursive nature, the types Fi, Fi(Fi), and Fi(Fi)(Fi) could be considered functionally equivalent; all that the consumer of any of them knows is that if they have a function that, if called on a Fi, will given them another Fi.

Here is my understanding of what Ceylon currently supports:

Can the desired behavior be implemented in the current version of Ceylon? Or would it definitely require one or both of the aforementioned backlog features?

Upvotes: 2

Views: 148

Answers (0)

Related Questions