Skip to content

Conversation

@DCupello1
Copy link

@DCupello1 DCupello1 commented Dec 15, 2025

After writing many passes, I think it makes sense to attempt to refactor them to avoid duplicate logic with the traversals. Inspired by AL's IL_walk.ml, it has been extended to be more general and have the following traversals:

  • Transfromation: IL -> IL
  • Collect: IL -> a' List
  • Exists: IL -> bool
  • Forall: IL -> bool

Each traversal has a record type that dictates the custom functions that can be modified to make the specific traversal you need. This can be extended to however many custom functions are needed. This can also be extended to have the custom functions return a bool to indicate whether to stop traversal at the specific branch of the AST.

Currently a draft to first see if the idea is worth it.

(Also, would probably want to merge collect, exists, and forall into a more generic traversal)

@DCupello1
Copy link
Author

@nomeata what do you think? This should help with the creation of the passes I think, especially since the traversal functions can be called at any point of the AST.

@nomeata
Copy link

nomeata commented Dec 15, 2025

I’m all for it. I wonder how general these will have to be – in particular when a traversal needs to pass a locally updated env around. But even something that helps in 80% of passes is useful. Or maybe your vision is to re-start a new traversal locally when updating some local environment?

in
f typ @ traverse_list

and collect_exp c e =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The collect functions could use an identity transform with a local mutable ref to collect the list, to avoid some duplication here. Not sure if it is worth it.

Copy link
Author

@DCupello1 DCupello1 Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at that point you could just use iter.ml couldn't you? edit: Ah I see what you mean, but either way it would functionally the same to iter.ml, which I think we wouldn't want.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly most of these traversals (except transform) could be done by iter.ml, but at least it provides more flexibility on whether we want to do a more functional approach to the traversals.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. I do think we should have these traversals, just saying that they could be implemented themselves using iter maybe? No big deal.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough! I will probably do that as it will reduce the duplication by a good amount.

@DCupello1
Copy link
Author

I’m all for it. I wonder how general these will have to be – in particular when a traversal needs to pass a locally updated env around. But even something that helps in 80% of passes is useful. Or maybe your vision is to re-start a new traversal locally when updating some local environment?

I would say my vision was more to re-start the traversal locally, as I think a general traversal with local updates would start to get very messy.

@DCupello1
Copy link
Author

Ended up merging the exists, forall, and collect into 1 generic traversal where you have to supply the default value and compose function.

Simplified Sub, SubExpansion, Totalize, aliasDemut, and Uncaseremoval with this new approach

@DCupello1
Copy link
Author

Tests won't pass due to me forgetting to traverse the new ITE construct in the #191 branch.

This is fixed though in #198 so will need that to be merged first.

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

Successfully merging this pull request may close these issues.

3 participants