The issue is that you only define l in g when it is called with type B. In the let sk line however you call f with type C, which calls g with the same type. Thus, the when branch isn't part of the g proc for that case. So l is never defined.
Is that clear?