Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop type unification #172

Closed
Tracked by #72
ScottCarda-MS opened this issue Apr 17, 2023 · 3 comments
Closed
Tracked by #72

Loop type unification #172

ScottCarda-MS opened this issue Apr 17, 2023 · 3 comments

Comments

@ScottCarda-MS
Copy link
Contributor

ScottCarda-MS commented Apr 17, 2023

We need a pass to convert the different types of loops in Q# into one consolidated type of loop or loop-representation in the HIR.

@ScottCarda-MS
Copy link
Contributor Author

I think we might need to support RangeAsIntArray so that we can convert Range objects into something whose length is determined.

@ScottCarda-MS
Copy link
Contributor Author

I think while loops are a good candidate for a unified loop.

@ScottCarda-MS
Copy link
Contributor Author

ScottCarda-MS commented Apr 19, 2023

Loops will be converted to while loops:

for <pat> in <expr> { <stmts> } will be handled differently depending on the type of <expr>.
If <expr> is an Array typed expression:

let __ary__ = <expr>;
let __len__ = __ary__::Length;
mutable __index__ = 0;
while __index__ < __len__ {
    let <pat> = __ary__[__index__];
    <stmts>
    set __index__ += 1;
}

If <expr> is a Range typed expression:

let __range__ = <expr>;
mutable __index__ = __range__::Start;
let __step__ = __range__::Step;
let __end__ = __range__::End;
while (__step__ > 0 and __index__ <= __end__) or (__step__ < 0 and __index__ >= __end__) {
    let <pat> = __index__;
    <stmts>
    set __index__ += __step__;
}

Other types for <expr> are invalid and will be caught in type-checking.

repeat { <repeat stmts> } until <cond> fixup { <fixup stmts> }
becomes

mutable __continue_cond__ = true;
while __continue_cond__ {	
    <repeat stmts>
    set __continue_cond__ = !<cond>;
    if __continue_cond__ {
        <fixup stmts>
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant