Generating Church-Numbers with other Languages

Today I have written a Java-Class that implements Church Numbers in the Java-Way. Just a few days ago I wrote about Church-Numbers in JavaScript. Java has no Functions but therefore uses Callable’s and Runnable’s. Unfortunately, both dont have the possibility to pass arguments, but still, it should be the most „natural“ thing in Java to use Interfaces to gain something similar to Lambda-Forms.

I would like to know how Church Numbers would be implemented in other languages. It should be comparably boring in Lisp, Haskell, SML, etc., but maybe in Languages like C++, Fortran, Basic, etc., it could be interesting, how a „natural“ implementation of church numerals could look like.

I mean a „real“ implementation, for example in Java, it would also have been possible to just implement a for-loop which executes a given function n times, but that is not really what a „real“ church number is (for example it already needs a notion of „number“).

On the other hand, in any language you could just implement the lambda-calculus. But thats not „natural“, it should be as basic as possible. At the moment I am thinking about how this could be done in C. Basic function shouldnt be enough. But maybe something thats not far above from that.

If you have any idea of how this could be done or time and fun for implementing church numbers in some other language, please do it, and send it to me either via comment, or via my e-mail-address you can find here. There should be at least some churchZero and some churchSucc (successor-function).

Here is my Java-Sourcecode:

public class ChurchNumbers {

    public interface CallableWithArg {
        public Object call (Object arg);
    }

    public static Object callCallable (CallableWithArg cwa, Object arg) {
        return cwa.call(arg);
    }

    public static CallableWithArg identityFunction () {
        return new CallableWithArg () {
            public Object call (Object arg) {
                return arg;
            }
        };
    }

    public static CallableWithArg churchZero () {
        return new CallableWithArg () {
            public Object call (Object arg) {
                return ChurchNumbers.identityFunction();
            }
        };
    }

    public static CallableWithArg churchSucc (final CallableWithArg n) {
        return new CallableWithArg () {
            public Object call (final Object f) {
                return new CallableWithArg () {
                    public Object call (Object x) {
                        return ChurchNumbers.callCallable
                            ((CallableWithArg) f,
                             ChurchNumbers.callCallable
                             ((CallableWithArg) ChurchNumbers.callCallable (n, f), x));
                    }
                };
            }
        };
    }

    public static CallableWithArg getChurchNumber (int n) {
        CallableWithArg ret = churchZero ();
        for (int i = 0; i < n; i++) {
            ret = churchSucc(ret);
        }
        return ret;
    }

    public static void main (String[] args) {
        int i = Integer.parseInt(args[0]);
        CallableWithArg doTest = getChurchNumber (i);
        System.out.println (
        callCallable((CallableWithArg) callCallable (doTest,
                                   new CallableWithArg () {
                                       public Object call (Object n) {
                                           return new Integer(((Integer) n) + 1);
                                       }
                                   }),
                     0));}

}

Eine Antwort zu Generating Church-Numbers with other Languages

  1. m^3 sagt:

    In OCaml :

    type Church = {C : ‚a.(‚a -> ‚a) -> (‚a -> ‚a)};;

    let zero = {C = fun f x -> x};;
    let succ n = {C = fun f x -> f (n.C f x)};;

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

%d Bloggern gefällt das: