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

refactor: is_trivial check to be more succinct #24

Merged
merged 2 commits into from
Jun 28, 2024

Conversation

Equilibris
Copy link
Collaborator

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

@Equilibris Equilibris requested a review from alexkeizer as a code owner June 26, 2024 13:12
@Equilibris Equilibris enabled auto-merge June 26, 2024 13:40
@alexkeizer alexkeizer disabled auto-merge June 26, 2024 15:57
@Equilibris Equilibris changed the title refactor macro comp 3 Rewrite is_trivial check to be more succinct Jun 27, 2024
@Equilibris Equilibris changed the base branch from master to refactor-macro-comp-2 June 27, 2024 11:35
@Equilibris Equilibris changed the base branch from refactor-macro-comp-2 to master June 28, 2024 15:51
Copy link
Owner

@alexkeizer alexkeizer left a comment

Choose a reason for hiding this comment

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

LGTM, feel free to merge after #22 goes through

@alexkeizer alexkeizer changed the title Rewrite is_trivial check to be more succinct refactor: is_trivial check to be more succinct Jun 28, 2024
@Equilibris Equilibris enabled auto-merge June 28, 2024 15:52
@Equilibris Equilibris merged commit 3fcaf9b into master Jun 28, 2024
2 checks passed
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.

2 participants