The guts of the question is: can one use the class system to code up the reflexive, transitive closure
Computing the transitive closure of types is possible: http://www.haskell.org/pipermail/haskell-cafe/2003-October/005249.html http://www.haskell.org/pipermail/haskell-cafe/2003-November/005433.html
pair :: STRef ((s1, s2), s3) a -> STRef (s1, s2) b -> ST (((s1, s2), s3), s4) (STRefr1 (a, b))
which isn't as general as I would like, as it imposes a particular order on the nested scopes.
That problem could be solved by open sums, it seems. The HList paper showed how to achieve that, in current Haskell.
I've experimented with a few different attempts, but in the best case succeeded only in exhausting stack space while searching for satisfying instances.
Could it be possible to post the complete code for those attempts? I'd like to try a few tricks to avoid the divergence.