I am implementing a kind system for a new functional programming language and I am currently writing the function to unify two kinds. There are four cases two consider:
+---------+---------+-------------------------------------------------------+
| k1 | k2 | action |
+=========+=========+=======================================================+
| var | var | k1 := k2 ^ k2 := k1 |
+---------+---------+-------------------------------------------------------+
| var | non var | if (!occurs(k1, k2)) k1 := k2 |
+---------+---------+-------------------------------------------------------+
| non var | var | if (!occurs(k2, k1)) k2 := k1 |
+---------+---------+-------------------------------------------------------+
| non var | non var | ensure same name and arity, and unify respective args |
+---------+---------+-------------------------------------------------------+
- When both
k1
andk2
are variables then they are instantiated to each other. - When only
k1
is a variable then it is instantiated tok2
iffk1
doesn't occur ink2
. - When only
k2
is a variable then it is instantiated tok1
iffk2
doesn't occur ink1
. - Otherwise we check whether
k1
andk2
have the same name and arity, and unify their respective arguments.
For the second and third cases we need to implement the occurs check so that we don't get stuck in an infinite loop. However, I doubt that a programmer will be able to construct an infinite kind at all.
In Haskell, it's easy to construct an infinite type:
let f x = f
However, I haven't been able to construct an infinite kind no matter how hard I tried. Note, that I didn't make use of any language extensions.
The reason I am asking this is because if it's not possible to construct an infinite kind at all then I won't even bother implementing the occurs check for kinds in my kind system.