flatten nested closures in shape inference