... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2012-02-17

.NET Framework Version in Mathematica

Out of the box, NETLink in Mathematica 8.0 will use version 2.0 of the .NET Framework. To use a newer version, you must edit the .NET configuration files InstallableNET.exe.config and InstallableNET32.exe.config. These files can be found at the location returned by this Mathematica expression:

FileNameJoin[{$InstallationDirectory, "SystemFiles", "Links", "NETLink"}]

Add a new supportedRuntime element for the desired runtime version (4.0 in this example):

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <startup>
    <!-- The supportedRuntime lines control which version of the .NET Framework will
         be used when .NET/Link is launched with InstallNET[].
         .NET/Link requires at least .NET 2.0. If you have .NET 3.0 installed,
         it will be used (note that the 3.0 version is really just the 2.0
         version with some extra assemblies).
      -->
      <supportedRuntime version="v4.0" />
      <supportedRuntime version="v2.0.50727" />
  </startup>
</configuration>

Note that supportedRuntime elements should be listed in order of preference.

2012-02-11

Java vs. Y-Combinator

Tonight, on You Did What?...

The gang decides to do penance for inflicting their software upon the world. Someone suggests implementing the Y-combinator in Java. Hilarity ensues.

Rated PG for violence to a coarse language.

public final class YCombinator {

    private interface Function<IN, OUT> {
        OUT apply(IN x);
    }

    private static final Function<Function<Function<Long, Long>, Function<Long, Long>>, Function<Long, Long>> Y = new Function<Function<Function<Long, Long>, Function<Long, Long>>, Function<Long, Long>>() {
        public Function<Long, Long> apply(final Function<Function<Long, Long>, Function<Long, Long>> f) {
            Function g = new Function<Function<Function, Function>, Function<Long, Long>>() {
                public Function<Long, Long> apply(final Function<Function, Function> h) {
                    return new Function<Long, Long>() {
                        @Override
                        public Long apply(Long n) {
                            return f.apply(h.apply(h)).apply(n);
                        }
                    };
                }
            };
            return (Function) g.apply(g);
        }
    };

    public static void main(String[] arguments) {
        Function<Function<Long, Long>, Function<Long, Long>> fac = new Function<Function<Long, Long>, Function<Long, Long>>() {
            public Function<Long, Long> apply(final Function<Long, Long> r) {
                return new Function<Long, Long>() {
                    @Override
                    public Long apply(Long n) {
                        return n < 2 ? 1 : n * r.apply(n - 1);
                    }
                };
            }
        };

        System.out.println(Y.apply(fac).apply(10L));
    }
}

Is this an argument for late-binding? Not necessarily: Haskell seems to cope, and you don't have to subvert the type system (much):

newtype Hold a = Hold (Hold a -> (a -> a))

y f = fg (Hold fg)
  where fg hg@(Hold g) = f (g hg)

Y-Combinator in Mathematica

Adding to the growing collection of posts about the Y-combinator...

Inspired by a question on the Mathematica StackExchange site, I came up with this implementation of the Y-combinator in Mathematica:

Y[f_] := #[#]&[Function[n, f[#[#]][n]]&]

... along with a test case:

fac[r_] := If[# < 2, 1, # * r[# - 1]]&

Y[fac][10]

What I really wanted to do was emulate the expression in that StackExchange question: (#[#] &)[#[#][#] &]. Note that it uses nothing but the special slot input form #, the postfix function operator &, the matchfix application operator [...] and parentheses. Expressing the Y-combinator in a form like this has eluded me so far.

Not all is lost, though. The Mathematica definition suggested some "simplifications" (obfuscations?) for the versions that I have used in other languages.

Javascript:

function Y(f) {
   return (function(g) { return g(g) })(
     function(h) { return function(n) { return f(h(h))(n) }} )
}

Scheme:

(define Y
   (lambda (f)
     ((lambda (g) (g g))
      (lambda (h) (lambda (n) ((f (h h)) n))) )))

Another Mathematica version, using the rarely seen \[Function] operator (entered as ESC f n ESC):

Y = f ↦ (g ↦ g[g])[h ↦ n ↦ f[h[h]][n]]

(*Y = f \[Function] (g \[Function] g[g])[h \[Function] n \[Function] f[h[h]][n]]*)

Blog Archive