implement schema and types as discussed in #48

The attempt made in #48 yelded interesting results but the code got very messy and diverged a bit from master (which went ahead in the meanwhile).

This is a new attempt, I hope to write cleaner code and get closer to something usable.

For the entire discussion see #48.