Source code for snippets ⋅ Twitter discussion ⋅ Reddit discussion ⋅ Linkedin discussion ⋅ Facebook discussion
It's a follow up of ideas behind representing objects and their instantiation in tensors.
One-dimensional tensors are vectors, multidimensional tensors are functor composition of vectors:
Representing object for vectors is sum of Units, representing object for functor composition of vectors is product of sum of Units:
The principle is simple - we just use product as a representing object for functor compositions:
Let's say we want to tabulate i.e. co-represent a matrix with 2 rows and 3 columns:
So that we need to assign some value to each slot in such a representing object:
The problem is that with products we have to inspect each case manually which is tiresome:
I would not be happy if I have to read such a code - I should check coverage for all 6 cases... Well, at least I know that there should be exactly 6 of them - since we just multiply 3 and 2, right? What if I tell you that it's possible to perform this kindergarten arithmetics on type level? Consider Unit as 1:
Challenge: how could we define similar subtyping relations that involve Void?
Let's do the same with representing object for a matrix with 2 rows and 3 columns:
It's convenient to map Sum objects if they are in negative position i.e. contravariant:
Now we can attach st operator to ho to coerce representing object:
Challenge: initialise tensors with [4,3,2] and [4,3,2,1] dimensions using the same technique.
Next time we will explore how to co-represent recursive datastructures like lists and trees.