Skip to content

Instantly share code, notes, and snippets.

@jdegoes
Last active January 9, 2019 11:23
Show Gist options
  • Save jdegoes/6842d471e7b8849f90d5bb5644ecb3b2 to your computer and use it in GitHub Desktop.
Save jdegoes/6842d471e7b8849f90d5bb5644ecb3b2 to your computer and use it in GitHub Desktop.
Modeling higher-kinded types in a language without them.
class Option<A> {
protected Option() { }
}
interface App<F, A> {
F proof();
}
class OptionF {
private OptionF() {}
private static class AppOption<A> implements App<OptionF, A> {
public final Option<A> value;
AppOption(Option<A> value) {
this.value = value;
}
public OptionF proof() {
return new OptionF();
}
}
public static <A> App<OptionF, A> fromOption(Option<A> v) {
return new AppOption(v);
}
public static <A> Option<A> toOption(App<OptionF, A> v) {
return (((AppOption<A>)v).value);
}
}
interface Function<A, B> {
B apply(A a);
}
interface Functor<F> {
<A, B> App<F, B> map(Function<A, B> f, App<F, A> fa);
}
@jbgi
Copy link

jbgi commented Jul 9, 2016

@TomasMikula even if it is obscure, if that does not impact client code and code can be generated it could have been a good solution.

@jdegoes
Copy link
Author

jdegoes commented Jul 9, 2016

A simple modification renders the original "safe up to null", again:

class Option<A> {
  protected Option() { }
}
abstract class App<F, A> {
  protected F proof();
}
class OptionF  {
  private OptionF() {}

  private static class AppOption<A> extends App<OptionF, A> {
    public final Option<A> value;

    AppOption(Option<A> value) {
      this.value = value;
    }

    protected OptionF proof() {
      return new OptionF();
    }
  }

  public static <A> App<OptionF, A> fromOption(Option<A> v) {
    return new AppOption(v);
  }

  public static <A> Option<A> toOption(App<OptionF, A> v) {
    return (((AppOption<A>)v).value);
  }
}
interface Function<A, B> {
  B apply(A a);
}
interface Functor<F> {
  <A, B> App<F, B> map(Function<A, B> f, App<F, A> fa);
}

@jbgi
Copy link

jbgi commented Jul 9, 2016

@jdegoes, I don't think so. My original counter-example still produce a ClassCastException:
https://gist.github.com/jdegoes/6842d471e7b8849f90d5bb5644ecb3b2#gistcomment-1818237

@jdegoes
Copy link
Author

jdegoes commented Jul 9, 2016

Damn access methods. If only Java had protected[this]! Or an abstract private method that could be implemented and seen only by subclasses...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment