def METHOD_NAME(struct, path, new_deep_field, new_struct=None):
    import hail.ir.ir as ir
    refs = [struct]
    for i in range(len(path)):
        refs.append(ir.Ref(Env.get_uid(), refs[i].typ[path[i]]))
    acc = new_deep_field(refs[-1])
    for parent_struct, field_name in zip(refs[-2::-1], path[::-1]):
        acc = ir.InsertFields(parent_struct, [(field_name, acc)], None)
    if new_struct is not None:
        acc = new_struct(acc, refs[-1])
    for struct_ref, field_ref, field_name in zip(refs[-2::-1], refs[:0:-1], path[::-1]):
        acc = ir.Let(field_ref.name, ir.GetField(struct_ref, field_name), acc)
    return acc