My six-year-old son walked up to me yesterday. “What are you reading?”

At the time, I was reading part of Janis Voigtländer’s habilitation thesis. Unsure where to even start, I decided to just answer straightforwardly: “I’m reading a very long story about free theorems.”

He persisted. “What are free theorems?”

Never one to shrink from a pedagogical challenge, I thought for a moment, then began: “Do you know what a *function* is?” He didn’t. “A function is like a machine where you put something in one end and something comes out the other end. For example, maybe you put a number in, and the number that is one bigger comes out. So if you put in three, four comes out, or if you put in six, seven comes out.” This clearly made sense to him, so I continued, “The *type* of a function machine tells you what kinds of things you put in and what kinds of things come out. So maybe you put a number in and get a number out. Or maybe you put in a *list* of numbers and get a number out.” He interrupted excitedly, “Or maybe you could put *words* in??” “Yes, exactly! Maybe you can put words in and get words out. Or maybe there is a function machine where you put *other function machines* in and get function machines out!” He gasped in astonishment at the idea of putting function machines into function machines.

“So,” I concluded, “a free theorem is when you can say something that is always true about a function machine if you *only* know its type, but you *don’t* know anything about what it does on the inside.” This seemed a bit beyond him (and to be fair, free theorems are only interesting when polymorphism is involved which I definitely didn’t want to go into). But the whole conversation had given me a different idea.

“Hey, I have a good idea for a game,” I said. “It’s called the function machine game. I will think of a function machine. You tell me things to put into the function machine, and I will tell you what comes out. Then you have to guess what the function machine does.” He immediately liked this game and it has been a huge hit; he wants to play it all the time. We played it while driving to a party yesterday, and we played it this morning while I was in the shower. So far, he has correctly guessed:

I tried but that was a bit tough for him. I realized that in some cases he may understand intuitively what the function does but have trouble expressing it in words (this was also a problem with ), so we started using the obvious variant where once the guesser thinks they know what the function does, the players switch roles and the person who came up with function specifies some inputs in order to test whether the guesser is able to produce the correct outputs.

was also surprisingly difficult for him to guess (though he did get it right eventually). I think he was just stuck on the idea of the function doing something arithmetical to the input, and was having trouble coming up with some sort of arithmetic procedure which would result in no matter what you put in! It simply hadn’t occurred to him that the machine might *not care* about the input. (Interestingly, many students in my functional programming class this semester were also confused by constant functions when we were learning about the lambda calculus; they really wanted to substitute the input *somewhere* and were upset/confused by the fact that the bound variable did not occur in the body at all!)

After a few rounds of guessing my functions, he wanted to come up with his own functions for me to guess (as I knew he would). Sometimes his functions are great and sometimes they don’t make sense (usually because his idea of what the function does changes over time, which of course he, in all sincerity, denies), but it’s fun either way. And after he finally understood , he came up with his own function which was something like

inspired, I think, by his kindergarten class where they were learning about pairs of numbers that added up to .

Definitely one of my better parenting days.

Ooh. I’m going to do this with my 6-year-old. Thanks for sharing!

Sure, have fun!

Me too!

Your game reminds me of the game Zendo. Mathematically speaking, in Zendo one player chooses a non-trivial subset of a universal set, and the other players try to figure out the rule governing the subset by asking if specific elements are in the subset. The main difference to me seems to be that while Zendo essentially concerns itself with deducing functions of the type $a \to 2$, your variant concerns itself with functions of type $a \to b$.

How did you deal with incorrect or vague guesses? For instance, if your son said that $\lambdax.10 \lfloor x/10 \rfloor$ was “round to the nearest 10?” how would you deal with that? The Zendo method would be to ask questions to clarify the meaning, like “What is 15 rounded to the nearest 10? What is 18?” until it’s clear what the guessed rule is, then provide a counter-example “18 rounded to the nearest 10 is 20, but my function turns 18 into 10”.

I didn’t know about Zendo, that looks like fun. Yes, I think it is similar.

For incorrect guesses I just say it is incorrect, and often encourage him to ask about some other particular inputs which will serve as counterexamples. We haven’t really had issues with vague guesses yet. But yes, I think asking more questions to clarify the meaning and providing counterexamples is a great way to deal with such situations. This is definitely related to the variant I mentioned, where the guesser has to prove they understand how the function works by correctly specifying the output for inputs chosen by the other.

Great Idea for a game! Thanks for sharing!

(BTW: You’re trending #1 on YC Hacker News 😊)

https://news.ycombinator.com/

Thanks, glad you like it! And I was aware of HN… I don’t read it myself but I could see that my blog stats were blowing up (more visits in the past 36 hours than I usually get in a year!).

I shared this on hacker news. I have a kid and thought this was utterly adorable. its turned out to be the 33rd most popular post of all time. http://www.hntoplinks.com/all/2, Great Post!

“usually because his idea of what the function does changes over time, which of course he, in all sincerity, denies”

He’s clearly going to be a project manager.

Excellent – Grinning from ear to ear :)

Haha! =)

If he’s engaged by this kind of thing, I can highly recommend Zoombinis, an educational game I loved as a child and recently got a remake.

It’s a game that does a really great job of teaching quite advanced logic puzzles while being charming and engaging. I can still play and enjoy it today as an adult (in fact, my mother still plays it from time to time after playing with us as children!). It introduces the concepts with easier versions and ramps up as you succeed. At the beginning you can just work from instinct, but as you go on you have to start constructing a mental model and adapting it as the puzzles get harder.

The remake is available for Win/Mac and Android/IOS and it’s cheap. https://external-wiki.terc.edu/display/ZOOM/The+Game

Just to give a bit more detail, you have a collection of Zoombinis, each of which has different traits (hair, eyes, noses, feet). The puzzles tend to be functions over these traits.

E.g: In the second level there are four caves (2×2 grid), and you must send Zoombinis to the right caves. So it might be say, red noses go left, others right, and glasses go top, others bottom.

In the “Fleens” level later in the game, you come across a group of creatures with different traits. Each Zoombini maps to a Fleen, and the aim is to work out the mapping function to select some Zoombinis matching target Fleens. To begin with it’s just red noses become purple noses, say, but as the difficulty ramps up you might need to understand that noses map to feet and hair is random.

This is all done in a simple way that’s really fun. without actually explaining anything like I’ve done here, it’s all just taught by letting the player experiment and deduce by generating hypothesis, testing them and rejecting or accepting them. Portal did a similar thing much later, but other than that it stands out amongst educational games for being both a ton of fun and truly teaching you something.

This sounds great, thanks for the link! I had not heard of Zoombinis before.

Several years ago I heard Matthias Felleisen say something about a strong correlation between kids that understand functions at a young age and skill in mathematics later in high school and college. I started experimenting with my kids doing things similar to your game. Typically I’d draw a picture of some crazy contraption with an input and an output and then let them suggest different inputs to see what I’d say the output would be. They would then try to figure out what the machine did.

I also found that having them discover (and write down) their own truth tables for simple logic circuits in Snap Circuits was a helpful way to get them to think through the idea of figuring out what something does by trying different inputs.

I hadn’t considered trying to see if I could explain higher-order functions until I read your post, but I’m going to have to try that now.

Yes, I was also thinking about higher-order functions… I think it would work just fine to play the function machine game with higher-order functions (“OK, you tell me a function machine from numbers to numbers, and I’ll tell you what number comes out…”), but it is definitely one or two notches up the ladder of abstraction and I don’t know if my son is quite there yet. If you try I’d love to hear about it!

Hey, I just finished some animations to explain HOF. Maybe are helpful for kids that are visual learners https://lambdabricks.github.io/animating-hofs/

Next up: The object inheritance game!

What? How do you play that?

As sure as the sun will shine tomorrow, parents will completely fabricate intelligent conversations with their children

While this may be true, I assure you I did not make up this conversation, though obviously I have no way to convince you of that.

Pingback: Link of the Day: May 7 2018 – skef.blog

Excellent game idea! I might play it with my friends

Have fun!

I think your son would become a great famous person in the field, one who pushes forward the boundaries of knowledge. Mark my words!

(Whatever catches the interest of a very young kid tends to influence them for the rest of their lives).

Perhaps! I don’t really care if he becomes famous; I will be happy as long as he always keeps a love for learning!

That reminds me of https://en.wikipedia.org/wiki/Eleusis_(card_game)#Eleusis_Express which is somewhat the same, even with switching side to test hypothesis

This is great, another similar game I hadn’t heard of! Thanks for the link.

This also reminds a lot of Elusis, a card game where one player makes a rule (and is “God”) and the other players must guess it (and are called “scientists”). It’s played with several decks of ordinary playing cards, and the rules must be based on those. If a player thinks they know the rule can become “prophet”, and must then judge the other players’ tries. https://en.wikipedia.org/wiki/Eleusis_(card_game)

This is great, another similar game I hadn’t heard of! Thanks for the link.

My 8 years old is new to computers but is not so good in Maths, but I have to try this as I really like this way to make kids understand. Thanks!!

You’re welcome! Have fun!

Pingback: Interesting Links for 08-05-2018 | Made from Truth and Lies

Awesome post! I have a 6 year old too and love teaching her concepts and maths. I’ll definitely use this.

Cool, have fun!

Very inspiring! Thanks for sharing. That will be a good game to play on road trips.

You’re welcome! I think you’re right that it is a great road trip game.

duuude – that is what great parenting is all about! wd!

Thanks!

Wow, teachers could learn from you so much! Great teaching approach :)

Thanks!

Awesome! :-) I really enjoyed reading this story. I have a 4 year old and I can’t wait :-)

Glad you enjoyed it! Yeah, 4 is probably too young for this game, but there are other fun math games you can do at that age (e.g. playing with different numbers of dots arranged in shapes like squares, rectangles, triangles…)

Sounds like your kid would enjoy Funville Adventures! https://www.amazon.com/Funville-Adventures-Fradkin/dp/1945899026/

Looks fun, thanks for the link!

Great post and it definitely sounds like he is enjoying the game. Can’t wait to play this with my nephew soon :)

Thanks — have fun! =)

WoW This is so cool. Someone actually told me long back that he has taught Functional programming to kids with great success. Excellent Post made me think how i would have perceived a concept such as this for the first time, “A Function machine inside a function machine”. Quite a crazy thought for a 6 yr old.

Yes! If you want to teach FP to kids I highly recommend https://code.world/ .

Your son might like my Function Game website where you can play the game with the computer:

http://www.functiongame.com/

Thanks for the link! This is great work. I think it is too hard for my son yet, but I will save the link for later.

Thanks. And I forgot to say how I really enjoyed your article!

Thanks, glad you enjoyed it!

Well done sir! I’ll definitely try this with my 9 and 6 year old. I also got a smirk when I read about the mystery of functions that don’t pay attention to their inputs… I was looking at the 16 binary functions that take two inputs and produce one output recently, and it was the first time I realized that only 10 of them pay attention to both inputs, 4 of them only pay attention to one input, and 2 of them don’t pay attention to either input.

Hah, good point, you’re right!

This post is awesome. My 10-year-old son was learning python and I was helping him. I tried to explain what function is. I told him function is like a factory(physical) – you deliver raw material and something comes out. Afterwhile – he insisted that it is a machine. We refused to see it my way and at the end I won. The joke in my family is ‘what is function?’ It is factory – starts with ‘f’. Now when you said a machine takes another machine and produces yet another machine. I think my son is right – it is machine. :) But I won’t tell my son.

Hah! I’m curious, what were you thinking of the difference between a machine and a factory? Why did you insist on “factory” and not “machine”?

Thanks for the inspiration, we tried it with our kids on a car journey and it was a lot of fun. They grasped the concept surprisingly quickly.

Cool, I’m glad it worked well!

Brent, i really like the idea. i have a 6y old daughter, and 4y old son. The oldest is enthousiastic on the game. So, i tried making a browser verison for it. Its simple, none-screen-responsive and looks like a christmastree, but it works: https://github.com/ronstarcool/machine

plz check is out to see if you like it. If you have node installed, its very easy to run.

Very cool, thanks for sharing!

Pingback: Newsy programistyczne 2018-05-13 – DevNation

This is nice, i like this article and your idea.

Truly wonderful the mind of a child is. – Yoda

This is so great.

Pingback: Carnival of Mathematics 158 | The Aperiodical

What about a logic programming language for children?

https://github.com/ToonTalk/ToonTalk

http://www.toontalk.com/English/papers.htm

Cool, thanks for the links!

Pingback: Resumen de lecturas compartidas durante mayo de 2018 | Vestigium

This is so adorable! I love the sound of teaching little kids concepts of programming like this. I teach after school programs that use a website called Scratch but I think making games like this would introduce the younger generation to the coding world a lot easier 😮