refactor: is_trivial
check to be more succinct
#24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If we start with
let is_trivial := args.length == arity && args.toList.enum.all fun ⟨i, arg⟩ => arg.isFVar && isLiveVar vars arg.fvarId! && vars'.indexOf arg.fvarId! == i
Then we can first observe that
vars'.indexOf arg.fvarId! == i => isLiveVar vars arg.fvarId!
.this comes from indexOf returning the length of the array if the entry is not found.
Since 0 ≤ i < vars'.legnth we can never have i == vars'.length thereby the impl holds
so we can remove the isLive check
let is_trivial := args.length == arity && args.toList.enum.all fun ⟨i, arg⟩ => arg.isFVar && vars'.indexOf arg.fvarId! == i
Then we can re-arrange to take the transform for fvarIds out
let is_trivial := args.length == arity && (args.toList.mapM Expr.fvarId?).any (·.enum.all fun ⟨i, arg⟩ => vars'.indexOf arg == i)
Now observe how we are checking an index at each step along the way.
If the arrays have the same length (the first check) we then know if they are equal this holds.
So by doing the next transform we get:
let is_trivial := args.length == arity && (args.toList.mapM Expr.fvarId?).any (· == vars')
And finally if the arrays are equal it implies the first condition so we end up with:
let is_trivial := (args.toList.mapM Expr.fvarId?).any (· == vars')
Which happends to also correspond to what the documentation states