Skip to content

Nested loops fail #340

Closed
Closed

Description

@samwaseda

The following code fails:

import flowrep as fr

def arange(min_value, max_value):
    result = range(min_value, max_value)
    return result

def sub(a, b):
    result = a - b
    return result

def mean(values):
    result = sum(values) / len(values)
    return result

@fr.workflow
def get_mean_value(min_value=0, max_value=3, x=2):
    r = arange(min_value=min_value, max_value=max_value)
    outer_result = []
    for v in r:
        inner_result = []
        for w in r:
            b = sub(w, x)
            inner_result.append(b)
        inner_mean_value = mean(inner_result)
        a = sub(v, inner_mean_value)
        outer_result.append(a)
    mean_value = mean(outer_result)
    return mean_value

Activity

  1. added
    bugCategory: Something does not work
    on Oct 22, 2026
  2. liamhuber commented on Oct 22, 2026

    @liamhuber
    Member

    @liamhuber Maybe we should talk whether this is a valid way of using nested for loops, because we could also argue that arange should be written as a list of lists instead of a simple list.

    I'm copying your comment here, because #341 itself is A-OK from a technical perspective and I don't want to clutter that.

    I did a quick check of my gut hypothesis, and this really is fundamentally only a label tracking issue; if we reassign a fresh symbol to the list before reusing it, the code parses and runs completely fine:

    @fr.workflow
    def get_mean_value(min_value=0, max_value=3, x=2):
        r = arange(min_value=min_value, max_value=max_value)
        s = r
        outer_result = []
        for v in r:
            inner_result = []
            for w in s:
                b = sub(w, x)
                inner_result.append(b)
            inner_mean_value = mean(inner_result)
            a = sub(v, inner_mean_value)
            outer_result.append(a)
        mean_value = mean(outer_result)
        return mean_value

    So if it would run with a trivial relabel, I definitely consider it a bug that it won't run without one.

    To directly address your concern, in this case writing arange as a list of lists would be memory wasteful, since each sub-list would be identical. We can make the inner list depend on the outer list by replacing b = sub(w, x) with b = sub(w, v), at which point it becomes meaningful and interesting to have made a nested iteration over the same source data twice.

    At the end of the day, I'm coming down solidly on the side that this is a real parsing bug and we might as well patch it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugCategory: Something does not work

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions