Turning EMU Turing Complete

This article deals with EMU. To try it out follow this link and click “skip” at the login screen.


With EMU I wanted to merge what you can do in spreadsheet with what you can do in an ordinary text editor.

The embedded language in EMU is designed to have a familiar syntax (JavaScript/C-like) and be immediately useful when comes to basic calculations and calling external functions for task such as creating a graph.

Putting something in tuborgs { } in an EMU-document creates a “slot” and inside that you can put a piece of code that looks a lot like a basic piece of JavaScript. If the expression contains any calculation the output is printed right after the slot. Ie.: { 2 + 3 } = 5.

Simply putting JavaScript in the slot would have a number of problems. First the user would be able to break the page (calling alert(...) etc.), secondly JavaScript is a language with side-effects and state making it work quite differently from what one is used to from a spreadsheet. Writing a = a + 1 is perfectly valid and meaningful JavaScript whereas it would make no sense in a spreadsheet.

In the earlier versions of EMU the language allowed just simple mathematical expressions over numbers, list and maps. But in the later versions the language has evolved into a fully-fledged language.

“Fully-fledged” means a number of things. As features such as control structures and recursive functions are introduced the language becomes “Turing complete” meaning that it is able to encode any computational problem, it also may loop forever without any way of detecting that.

It also means that the language has to become “coherent” (I cannot think of a better word). In sense that you as programmer should be able to fully access the structures you are working with. Eg. when you have a map; it should be possible to get the keys of that map and it should access an element of that map with a non-hardcoded index.

It sounds obvious but as soon as you start fiddle about with things you realise that having a map like {a: 2} requires you to have a string type (as strings are the key in the map), a null value (default value of items not present in the structure) and support functions for taking the keys or the values out of the map as lists.

It also gives you a lot of understanding of the various design choices done in languages such as JavaScript; there are languages within the functional space that seem to have simpler and more consistent designs but they often give up programmer usuability and familiarity for a more abstract syntax.

The language in EMU as this time (june 2016) looks as follows:

JavaScript-like syntax with R-like algebra and utilities

You can define a map as you would in JavaScript and call a built-in function to sum its values:

{fruits = { banana: 5, apple: 3, lemon: 6 }}
{sum(fruits)} = 14

Define another containing the weights and mutiply that with fruits:

{weights = { banana: 100, apple: 200, lemon: 75}}
{weights * fruits} = { banana: 500, apple: 600, lemon: 450}}
{sum(_)} = 1,550

Where underscore is a language primitive that gives you the value of the previous slot.

Unlike its JavaScript and R counterparts EMU is strictly without side-effects and is lazy evaluated. Meaning you can write the about in any order.

The EMU editor will show the results of the evaluations (in green) as you type.

Functions, ranges and control structures

EMU has first-order functions defined via the arrow syntax, if/then/else via the JS-style question marks, ranges via … (as in Swift):

{factorial = n -> n > 1 ? factorial(n-1) * n : 1}
{map(factorial, 1...4)} = [1, 2, 6, 24]

Map along with filter and reduce are functions in the standard library.

New iOS Game: Fidus Fidus

My new iOS Game “Fidus Fidus” has been released on Apple’s App Store. It is made using Apple Swift and Apple SpriteKit.

The game is spin on the classic falling block action puzzles like Puyo Puyo and Tetris.

Like Puyo Puyo you control two beans falling from the top of the screen. The object is to match the beans up in groups of 4 or more while preventing the beans to grow to the top. Unlike Puyo Puyo you don’t play against an opponent and your reward for clearing beans in a cascade is that you get a speciale “happy fiduser” that will clear up even more beans.

Anyways. This description probably does not make much sense. So here is a video of the game action.

As you can see from the white strokes in the video the game is gesture controlled. With gestures for left, right and down plus circular gesture for rotating clockwise or counter-clockwise.

Try the game in the App Store. Free.

How to get the SQL schema generated by Hibernate/JPA in a Spring Boot Application

Spring Boot is set up with automatic schema creaation when you create a database-based application. This is very nice for prototyping but as your project develops you will want to take full control on how the database schema is setup and migrated. Unfornately it is not obvious how get a Spring Boot application to spit out the schema it is creating; here is what I ended up doing:

Put this in the test part of your main application. It will scan the components of your application and create a database schema using the defaults used by Spring Boot.

You will need to change the Environment.DIALECT (here org.hibernate.dialext.MySQL5Dialect), the output file (schema.sql) and the package “dk.christianhvid”. Leaving the package name to be an empty works but is fairly slow.

package dk.christianhvid.emu.utils;

import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class SchemaFileDumper {
    private List findClassesWithEntityAnnotation(String packageName) throws ClassNotFoundException {
        List result = new ArrayList<>();

        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);

        provider.addIncludeFilter(new AnnotationTypeFilter(Entity.class));

        Set beanDefinitions = provider.findCandidateComponents(packageName);

        for (BeanDefinition bd : beanDefinitions) {

        return result;

    public void dumpSchema() throws ClassNotFoundException {
        Configuration configuration = new Configuration();

        configuration.setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");

        for (Class klass : findClassesWithEntityAnnotation("dk.christianhvid")) {

        configuration.setNamingStrategy(new SpringNamingStrategy());

        SchemaExport schema = new SchemaExport(configuration);
        schema.create(false, false);

    public static void main(String[] args) throws ClassNotFoundException {
        new SchemaFileDumper().dumpSchema();

Basic hosting of a Java 8 / MySQL application on Digital Ocean

Here is how I set up Java 8 hosting on Digital Ocean. Digital Ocean is an American hosting company that offers solid virtual servers starting at the rock bottom price of 5 USD pr. month.

This setup is somewhat lacking security; in particular in that it is a single user setup (only root), does not use SSH keys and let’s Tomcat listen directly to port 80. Also it is running http rather than https so deployments with the manager application sends your password in cleartext.

So use this setup at your own peril.

0. Signup for Digital Ocean. Type in your credit card and use the coupon: DROPLET10.

1. Pick droplet Ubunto 14.04 for 5 USD (512 MB, one core). Get the password via email. Notice the IP-address.

2. Shell into the droplet and change the password to something secure.

$ ssh root@xxx.xxx.xxx.xxx

3. Add the repository for Oracle Java and upgrade the OS:

$ add-apt-repository ppa:webupd8team/java
$ apt update
$ apt upgrade

4. Setup a firewall allowing only port 80 for http and 22 for ssh:

$ apt-get install ufw
$ ufw default deny incoming
$ ufw default allow outgoing
$ ufw allow ssh
$ ufw allow http
$ ufw enable

5. Setup a swap-file. This is a minimum memory system so it needs a swap-file:

$ dd if=/dev/zero of=/swapfile bs=256M count=4
$ chmod 600 /swapfile
$ mkswap /swapfile
$ swapon /swapfile
$ swapon -s
$ echo "/swapfile   none    swap    sw    0   0" >> /etc/fstab

6. Install some software:

6.1. Emacs:

apt-get install emacs24

6.2. Configure emacs not to make those silly tilde-files:

$ echo \(setq\ backup-directory-alist\ \'\(\(\".\"\ .\ \"\~/.emacs.d/backup\"\)\)\) > /root/.emacs

6.3. Install MySQL – leave the root password blank – the firewall prevents access from outside this machine:

$ apt-get install mysql-server

6.4. Install zip and unzip:

$ apt-get install zip
$ apt-get install unzip

6.5. Install haveged – a random number generator – otherwise Tomcat startup will slow to a crawl:

$ apt-get install haveged

6.6. Install Java 8:

$ apt-get install oracle-java8-installer
$ apt-get install oracle-java8-set-default

6.7. Install Apache Tomcat 8:

$ curl "http://mirrors.rackhosting.com/apache/tomcat/tomcat-8/v8.0.32/bin/apache-tomcat-8.0.32.zip" > apache-tomcat-8.0.32.zip
$ unzip apache-tomcat-8.0.32.zip
$ mv apache-tomcat-8.0.32 /usr/local/

7. Configure Tomcat via files in /usr/local/apache-tomcat-8.0.32/conf:

7.1. In server.xml: Setup Tomcat to serve applications at port 80




7.2. In users.xml: Create a user for Tomcat Manager – change the password to something secure.



8. Make Tomcat run at startup:

$ cd /etc/init.d
$ ln -s /usr/local/apache-tomcat-8.0.32/bin/catalina.sh catalina
$ chmod 755 catalina
$ update-rc.d catalina defaults

9. Restart your machine:

$ shutdown -r now

10. Login again, tail your log and point your webbrowser to http://xxx.xxx.xxx.xxx:80 :

$ ssh root@xxx.xxx.xxx.xxx
$ tail -f /usr/local/apache-tomcat-8.0.32/logs/catalina.out

My Java Applet Games

Since 1996 I have made a series of Java applet that I put online on my old websites (vredungmand.dk and my university website). The games used to be very popular peeking at tens of thousands of daily users.

However Oracle has changed the applet security policies and lately announced a complete deprecation of the technology, so the games are no longer working and not available online.

Here is a video showing what the games looked like:

I have put the full source code of the 8 games up on GitHub:


Work in Progress – EMU

EMU – which is short for “extended markup” – is project I have been working on for a while. At its core is a what-you-see-is-what-you-get markup editor with a small functional language embedded that allows the user to do calculations that you normally would use a spreadsheet to do.

It built from “scratch” in the sense it does not use “content editable” or similar to do its editing. It runs as a web application with a Java 8 / Spring Boot server but most of the work is on the JavaScript client using React and a parser/lexer-library called Nearley.

This animation shows the WYSIWYG editing in effect:


The embedded language snippets can be used to call an external library containing various functions. I.e. a function for rendering a pie chart:

Screen Shot 2016-01-08 at 00.03.39

You can try the beta here: apelab.com/emu.

This version will store your documents in your web browser and thus without the backend server.

Apple has Thru-the-Roof Customer Service

I have a Macbook Pro retina as my main work machine.

It has been working fine until about 6 months ago when it started to become unstable. The problem seemed to be related to overheating and the discrete GPU. In the beginning it would just reset once in a while or refuse to turn on the screen after sleep.

This is about when the 2 year Danish consumer warranty expires and I was travelling SE Asia. So I tried various things such as software utilities for increasing fan speeds or for turning the discrete GPU off.

It worked in the beginning but the problems got worse to the point where sticking the computer into the aircon for 2 minutes no longer worked. And just after I crossed the border from Vietnam into China the computer was not able to run for more than 30 seconds before the screen went blank.

Just before this happened Apple had setup a program offering extended warranty for GPU-related problems for my model. Appearantly the problem stems from poor soldering of the GPU where it gradually becomes disconnected from the logic board and causes the whole machine to become unstable.

I decided to give it a go – so in Shanghai on a Saturday evening I went into the Apple Store at Hong Kong Plaza and explained my problem and handed in the machine.

Now this is in China where it is hard to find anyone speaking good English, even at an Apple Store.

Less than two days later I got an email asking me to contact the store. After a confusing 45 minutes on the phone I was transferred to someone in America telling me that the repair was cancelled and that was all he could see on the file.

The next day I show up in the store expecting to have my computer back unrepaired since I appearantly did not qualify for the extended warranty.

Well it turned out that the reason they had cancelled the repair was that they needed to reinstall the OS since the drive had been corrupted and wanted to offer me backing up my files before they did it. I backed up my computer to an external drive and about an hour later I walked out of the store with a computer runnning as good as new.

Needless to say I was very satisfied.

I know of no other company where you can buy a computer in Denmark and have it serviced in China 2 1/2 years later. This fast, with this level of care and without cost eventhough the company has contractual obligation to do so.

And as a unexpected bonus: Two weeks later I clicked “About This Mac” and noticed that the machine no longer had 8 GB but 16 GB RAM.

Update February 2016: Apple finally recognized “Stain Gate” aka the keyboard scratching the screen as a production fault and I just got my screen replaced. Making this 3.5 year old lady looking as fresh and young as ever.