kind2
kind2 copied to clipboard
`kind2 to-js` generates definitions in alphabetic order
Consider the following kind2 program:
// Unit/_.kind2
data Unit
| new
// Unit/new.kind2
new: Unit = ~λP λnew new
// Something/_.kind2
Something : * = Unit
// main.kind2
main: Something = Unit/new
Checking all the files results in no error.
After compiling the main file with kind2 to-js main, the result is the following:
const Something = Unit;
const Unit = null;
const Unit_new = (P_1) => (new_2) => new_2;
const main = Unit_new;
Because the identifiers are sorted alphabetically, const Something = Unit; comes before Unit is defined, which results in an error:
ReferenceError: Cannot access 'Unit' before initialization
Instead of sorting the identifier alphabetically, an alternative would to be sort them accordingly to the dependence tree, so that all dependencies are sorted before their dependents.