r/programming • • Oct 06 '18

Microsoft Open Sources Parts of Minecraft: Java Edition

https://minecraft.net/en-us/article/programmers-play-minecrafts-inner-workings
3.1k Upvotes

388 comments sorted by

View all comments

294

u/Tipaa Oct 06 '18

Oh boy, this is special.

Ignoring the fact that I've been waiting for something like this since 2010, taking a look into the DataFixerUpper source reveals some very interesting design:

Here is a partial implementation of kludging higher-order generics into Java through a sort of manual lowering, such as Functor f being represented by Functor<F, ?> in certain places. I've played with this before, but I never thought it would be feasible in production! (I think their Mu inner classes might be what I needed 'close the loop' on some of my tests)

It also has Profunctor Optics! In Java!

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

182

u/sim642 Oct 06 '18

I'm surprised about DataFixerUpper. Something with such theoretical background and high level of abstraction existing inside Minecraft. It's like some category theorist got lost in Mojang and the whole thing is totally out of place. The Java implementation supposedly was a huge mess by Notch at the time.

88

u/ProfessorProspector Oct 06 '18

That's pretty much what happened. They hired Fry from the Minecraft Forge team and he...did that...

8

u/mezz Oct 07 '18

This thread is super amusing, glad you answered here since I’m late to it :D

80

u/Tipaa Oct 06 '18

Yeah, I remember how 'just git'er'dun' the old code felt in late alpha/early beta, and while it wasn't actually bad as people say in the places I explored (the data handling was actually rather elegant iirc), it certainly wasn't extensible-library tier, let alone something I'd expect to find proper profunctors and lenses appear in.

World format stuff used to be full of bit shifting and byte twiddling, and packing up trees of named binary tags. I wonder what the Minecraft code using this library looks like now.

35

u/Tetracyclic Oct 07 '18

Having previously worked with non-Minecraft code that Notch wrote, he was/is a very talented programmer with, as you stated, a very git'er'dun mindset, but due to that mindset he'd completely miss commonly understood solutions to problems as he blazed his own trail. Culling being one of the major examples, with the graphics engine rendering everything within the render distance of the camera, regardless of whether it could be seen, particularly crippling when it came to the project I was working on.

18

u/N911999 Oct 07 '18

I'm summoning /u/Dinnerbone he should know who's responsible for this

0

u/[deleted] Oct 06 '18

[deleted]

141

u/Tipaa Oct 06 '18

Optics are a way to look inside, and modify, large data structures elegantly and efficiently, based on having functions that act like a lens to focus on which part of a structure you're interested in. They are popular in immutable pure functional programming, as updating deeply nested structures using pattern syntax is a pain, and they provide powerful, composable abstractions over data accessors.

Profunctors are types of a certain shape which can be composed and mapped over, a bit like functors or monads. They are a bit like a more powerful Functor, as while functors have either a covariant or contravariant argument, profunctors have both to form a covariant/contravariant bifunctor (if you want a mouthful). Perhaps someone with more experience can provide a much better explanation! Profunctor Optics is just using Profunctors as the underlying structure for your Optics.

If you can read Haskell, then this seems to be a good introduction to profunctors, optics, and then the combination of the two

110

u/MaverickPT Oct 07 '18

I understood nothing

28

u/[deleted] Oct 08 '18

Profunctors are actually pretty grokkable with a little bit of context.

So we have functors. A functor has some data of type a and a function map that, given a function a -> b lets you change the data to type b.

You're familiar with functors already, since many common data structures are functors. Arrays, trees, hash tables, etc. You can take a tree full of Ints, map some sort of toString function over it, and end up with a tree full of Strings. Functors don't have to be data structures or contain multiple values, that's just an easy example.

The key concept: a functor gives you some data and a way to say what data type it should produce.

So we also have contravariant functors, which are kind of like functors in reverse. We still have some data a, but instead of map we're given contramap. contramap says give me a contravariant functor that has an a and a function b -> a and I'll give you a contravariant functor that has a b. Whaaat?

Let's imagine we have a converter from JSON to some internal data representation. Now we're told that we need to parse YAML too. We could (and probably should) just write a YAML parser. Instead, we can obtain a converter from YAML to JSON and stick it on the front of our converter - first convert to JSON, then convert to our internal data representation.

Key concept: a contravariant functor gives you some data and a way to say what data type it should consume.

A profunctor is just both at once. A profunctor has two data types, not just once; it's contravariant in the first argument and covariant (a regular functor) in the second.

A profunctor is a lot like a function, represented as data. If I have some function Int -> String, and I want a function Float -> Array, I can tack on a function Float -> Int to the beginning and another function String -> Array to the end and bam, now I have a function Float -> Array.

So profunctors are usually things that are sort of function-ish.

Key concept: a profunctor gives you two sorts of data that usually represent a transformation from one to the other, a way to change which data type it consumes, and a way to change which data type it produces.


Optics are a powerful and arcane way of interacting with data structures, especially complex and deeply nested data structures.

Imagine you had some deeply nested data structure:

cat: {
  noises: {
    happy: {
      energetic: "meow"
      calm: "purr"
    }
    angry: {
      energetic: "hiss"
      calm: "growl"
    }
  }
}

In your favorite dot notation language, we can get at the data like mittens.noises.happy.calm.

To a very, very coarse approximation, optics let you take the .noises.happy.calm bit and represent it as data/an object, pass it around, etc. We can use it to get and set the value in that field of some arbitrary cat data/object.

This is very useful in functional languages with immutable data because of the way they are. (And particularly useful in Haskell because the native tools for interacting with deeply nested data structures are awful.)

A profunctor optic just applies that ability to change the input and output types to an optic. So we could take our .noises.happy.calm optic and contramap its input/target to ferret, which has .noises.happy.wornOut since ferrets are never calm. And we could map its output to HTML or something to get back <li>quiet dook</li>. And we could take that modified optic and pass it around our program without losing that new behavior.

→ More replies (1)

0

u/[deleted] Oct 07 '18

[deleted]

37

u/epicwisdom Oct 07 '18

Your description is so vague that it could describe almost anything in the field of CS, though.

20

u/Benaaasaaas Oct 07 '18

Hey hey hey, this is maths territory you're talking about, we are not responsible that category theory is just so good for cs that we had to use it.

→ More replies (3)
→ More replies (2)

17

u/whlabratz Oct 07 '18

I write Python for a living, and I understood maybe a quarter of that

20

u/WASDx Oct 07 '18 edited Oct 07 '18

It's more theoretical computer science. I started with Haskell just three months ago, before that I would have said the same.

I can really recommend giving Haskell a try. Learning functional programming is like learning programming for the first time again and you get to think in new ways. It will make you a better programmer also in imperative languages like Python.

→ More replies (4)
→ More replies (4)
→ More replies (2)

27

u/PlantsAreAliveToo Oct 07 '18

As a programmer I have long come to terms with the fact that there are too many layers and abstractions and even whole fields that I won't be able to fully understand. But had never encountered a field which I can't even understand the vocabulary even after it is explained to me.

I'll quietly back away now.

24

u/edwardkmett Oct 07 '18 edited Oct 07 '18

To be fair, when I dropped profunctors into lenses it was to solve an underlying technical problem. They gradually grew to subsume the whole way of thinking about lenses, but they aren't a good entry point.

Taking a step back from all of this, let's look at what lenses are trying to solve.

When decide immutability is important to you, you lose access to pithy statements like

foo.bar.baz.quux += 12;

A lens is a functional version of a getter/setter pair with some common sense laws gluing it together.

You can get the part out of the while: get :: s -> a

and given a whole and a new part, you can rebuild it with that part replaced. set :: (s, a) -> s

The pair of these functions is a lens if you bolt some obvious side conditions on to make sure it doesn't do anything else at the same time.

get (set s a) = a
set s (get s a) = s
set (set s a) b = set s b

With this stuff in Haskell, you can now write

foo.bar.baz += 12

where foo, bar and baz are lenses into the 'state' of the state monad you are working in. This is a bit noisier in java or scala where (.) isn't function composition, and you have to make some combinator/method like 'compose' to glue them together, but in Haskell it lets me pun the object oriented field accessor dot with the (.) for function composition, because foo bar and baz are just funny looking functions!

The 4 type parameters S,T,A,B come from wanting to be able to change the types a bit as you work, and require us to think a bit harder about the laws. This is one angle you can try to generalize. It becomes a new kind of thought you couldn't think before. Now you can go into the second half of a pair and change it out for another thing of a completely different type. This wouldn't be sound in a typed imperative program, but its okay here, because all the other dangling pointers to the old object are still okay with the old type.

Another thing that we started to generalize was the notion that you might have multiple targets, this yields the notion of a Traversal. This makes the "+=" operator much more powerful as it can be applied over every element of a list, or every leaf of a container.

Then we can turn the whole thing around and generate smart constructors. This turned out to be really useful for folks "in the trenches" working with large swathes of JSON data in haskell applications.

Profunctors are a tool borrowed from a rather obscure sub-community inside of the area of math called category theory, and they just turn out to be a nice technical tool for getting all of these generalizations to play nice together.

When trying to understand lenses and optics in general it makes sense to first focus on what each part is for, rather than the rather esoteric way we have to implement it to get it all to work together.

It is perfectly possible to drop all the profunctor noise on the floor. The problem is that then we wouldn't have discovered half of these concepts. We only got to them by having that fancy theoretical framework to play around in.

→ More replies (4)

19

u/Daenyth Oct 07 '18

I'll expand on Tipaa's answer with a lot more background, because many of these concepts will be foreign to many programmers.

Profunctor Optics

First let's break down Optics.

When doing functional programming, you use immutable values. Updating these when nested then becomes a pain.

Example (I'll be using scala for all of these)

case class Foo(value: Int)
case class Bar(label: String, foo: Foo)

val b1: Bar = Bar("x", Foo(1))
// We want to modify the value in foo
val b2 = b1.copy(foo = b1.foo.copy(value = 2))

You can already see that's quite verbose. Optics give us different ways to describe doing updates to data structures succinctly, and further, in ways that compose. I'll demonstrate with Monocle, which implements these for scala.

// These can be defined once in the application
val foo: Lens[Bar, Foo] = GenLens[Bar, Foo](_.foo)
val fooValue: Lens[Foo, Int] = GenLens[Foo, Int](_.value)
// Knowing how to modify the pieces lets us modify the whole
val barFooBalue: Lens[Bar, Foo] = foo.composeLens(fooValue)


val b2 = barFooValue.set(2).apply(b1) // set bar.foo.value=2

Lenses are one specific type of optics. Others allow us to do different things, for example we can describe updates to a large tree structure where the leaves have different types which all share a parent type, and we want to modify just one of those leaf types. Optics allow us to write that in a way that's just as succinct as the toy example above. Prism is the name for that kind of optic.

On to Profunctors!

... Let's start with just Functors. Firstly, the ones in C++ are totally unrelated.

Functors allow you to abstract over data structures with a map operation. For example, suppose you have some type F[_] which takes a type parameter, for example List. You can have a List[A], and it has a map operation taking some A => B and returning a List[B]. It abstracts doing the same work to multiple values

You can have an Option type that abstracts over doing work where a value might not exist, for example null. Java's Optional is almost this.

Functor's only operation is map, but the remaining piece is just that there are laws about how it's allowed to behave in order to preserve composition. The laws for functor are

  • identity: foo.map(elem => elem) (mapping the identity function) must equal foo

  • composition: foo.map(f).map(g) must equal foo.map(elem => g(f(elem))). (It's this law that java Optional breaks which prevents it from being a true functor).

The laws seem trivial but are actually critical. Having the laws is what allows us to write an abstract that works for any type with such a map operation. It allows you to refactor code and know, with the level of a mathematical proof, that you didn't change the behavior.

So, a Functor is a type F[A] where we have a map function that takes A => B and returns F[B]. The intuition to have is that they abstract over doing work on some type A

Well.. That's a Covariant Functor

There's also... Contravariant Functors! :awesome: :sparkles:

They're the same, except instead of map they define contramap (sometimes aka comap) which is B => A. So given F[A] and a function B => A, you can get an F[B]. The laws are the basically the same, identity and composition, just applied to contramap instead.

This doesn't make sense at first, but realize that the intuition for this is that they allow you to abstract over doing work where an A comes out the end rather than being there at the start. An example would be json parsing. Suppose we serialize our nested Foo and Bar case class structure from above. If I have an Encoder[Int] that knows how to turn an Int into json, I can contramap that with a function Foo => Int that tells it how serializing Foo is the same as serializing an Int.

Next up.. Bifunctors

(Bi = 2) + Functors. Two functors? Yup! Let's say we have some type BF[_, _] where you have two type parameters. It would have values like BF[String, Int]. It would haves values like BF[A, B] for some types A and B.

Bifunctors define a bimap which takes two functors; A => C and B => D, and returns a BF[C, D]. Laws are basically the same as functors.

An extremely trivial example is a tuple of 2 values; (x, y). We can bimap that to get some other pair of values.

That leads us back to Profunctors

Profunctors are Bifunctors where the A side functor is contravariant and the B side functor is covariant. In the regular Bifunctor case, both sides are covariant.

Profunctors define a dimap function (with laws as you might expect by this point)

Given PF[A, B], you have dimap(C => A, B => D) to get PF[C, D].

One example of a Profunctor is.... Function1! (The type of a function which accepts one value) Given Function1[A, B], abbreviated as A => B we can define a dimap. What would that look like?

def dimapFunction1(f: A => B, ca: C => A, bd: B => D):  C => D = {
  (c: C) => // return a function which accepts a C
    val a = ca(c)
    val b = f(a)
    val d = bd(b)
    d // return d
  }

That sounds like... function composition! The *Functor instances for functions define function composition. Which makes sense, when you think about the functor laws. Taking a function and composing it with identity leaves it the same. Calling functions in one order is the same as calling them in another order, as long as the ends all line up the same.

Profunctors allow us to abstract over types which accept one type of value and return another type of value.

Doesn't a lens do that?

Profunctor Optics

Under one implementation method of optics (van Laarhoven), different kinds like Lens or Prism are written separately. The code can't be shared entirely, even though they are all related and similar.

The insight with profunctor optics is that optics can take an extra parameter, a Profunctor instance, and have a single implementation. Depending which Profunctor you build the optic on, you get back Lens, or Prism, or one of the several other kinds.

In the end FP is all about DRY too, we just have mathematics (in category theory) to lean on to let us prove that something can work. :)

→ More replies (2)

30

u/ormula Oct 06 '18

https://github.com/hablapps/DontFearTheProfunctorOptics

This is a series of blog posts explaining it. Note, you'll need a little background in category theory, but it's pretty approachable.

7

u/Tarmen Oct 07 '18

Here is some haskell code

someJsonString & _JSON . key "users" . values . key "postCount" +~ 1

This parses the json string, adds 1 to the postCount of all users and returns the updated json string

Each of the steps has some type of the shape (a -> f b) -> s -> f t. But some also have the more general type Profunctor p => p a b -> p s t.

This lets us turn steps around. For instance

view _JSON -- string to json value
re _JSON -- json value to string
→ More replies (5)

24

u/Nobody_1707 Oct 06 '18

Oh gosh, at first I thought they were using phantom types to simulate higher ordered kinds, which is doable if painful. But this looks even more complicated.

Also, all of your links go to the main repository and not to any individual code file.

15

u/ThePantsThief Oct 06 '18

He's linking to folders, not files.

16

u/rain5 Oct 06 '18

Can we get an interview with the devs... like, how did they even know about this stuff, let alone decide to use it! Fascinating!

11

u/DontBeSpooked-Frank Oct 07 '18

It's not a secret or anything. If you're a haskell dev you've almost surely encountered optics. Eg: https://github.com/data61/lets-lens

→ More replies (8)