hax icon indicating copy to clipboard operation
hax copied to clipboard

Support patterns on arrays and opaque types.

Open maximebuyse opened this issue 1 year ago • 4 comments

Closes #464 , #804

We add a phase to rewrite array patterns and usize/isize/i128/u128 patterns.

The phase rewrites them with if let guards.

For integer types the idea is to rewrite:

match x {
  1 => ...
}

as

match x {
  x if let true = (x == 1) => ...
}

For array/slice patterns, the idea is to rewrite:

match x {
  [a, b] => ...
}

as

match x {
 t if let (a, b) = (t[0], t[1]) => ...
}

In the case of slice patterns we also test the length of the slice in the condition:

match x {
  [a,.., b] => ...
}

becomes

match x {
 t if let Some(a, b) = if t.len() >= 2 then Some(t[0], t[t.len()-1]) else None => ...
}

In case the subpatterns of an array also need a guard to be rewritten, we rewrite using a pattern matching.

In case there is a disjunctive pattern with guards in the subpatterns, we try to take a boolean disjunction of the guards. When it is not possible we call the phase hoist_disjunctive_patterns and separate the arm into one arm for each case of the disjunction.

maximebuyse avatar Sep 04 '24 13:09 maximebuyse

@W95Psp Not sure that you got notified but this now ready for review!

maximebuyse avatar Sep 18 '24 15:09 maximebuyse

I started to look at this a bit, there are issues with fresh vars I think https://hax-playground.cryspen.com/#fstar/7ec551634b/gist=898efdab8cc9c41469c5f28e72216b6e

W95Psp avatar Sep 19 '24 14:09 W95Psp

I started to look at this a bit, there are issues with fresh vars I think https://hax-playground.cryspen.com/#fstar/7ec551634b/gist=898efdab8cc9c41469c5f28e72216b6e

Good catch, I pushed a fix.

maximebuyse avatar Sep 23 '24 14:09 maximebuyse

Looks like there are conflicts that need to get resolved.

franziskuskiefer avatar Oct 08 '24 14:10 franziskuskiefer

@W95Psp @maximebuyse what's the state of this? Please update or close.

franziskuskiefer avatar Nov 11 '24 08:11 franziskuskiefer

This PR has been marked as stale due to a lack of activity for 60 days. If you believe this pull request is still relevant, please provide an update or comment to keep it open. Otherwise, it will be closed in 7 days.

github-actions[bot] avatar Jan 16 '25 01:01 github-actions[bot]

I have a branch I am working on to use refined integers for machine integers, which solves matching on size and u128. I would also like to redo the library so that arrays are lists, this would solve the issue here in a different way.

karthikbhargavan avatar Jan 16 '25 14:01 karthikbhargavan

We'll resolve this in other ways.

franziskuskiefer avatar Jan 23 '25 12:01 franziskuskiefer