def vars_in_env(type_env, keys=False):
    """Where `type_env` is a mapping to types, return all type varaibles found
    in the mapping. If `keys` is true, collect all keys."""
    unsafe = set()
    for k in type_env:
        if keys:
            unsafe |= {k}
        unsafe |= type_env[k].bound_type_vars()
    return unsafe