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

Bool patterns #342

Merged
merged 4 commits into from
Apr 21, 2022
Merged

Bool patterns #342

merged 4 commits into from
Apr 21, 2022

Conversation

wezm
Copy link
Contributor

@wezm wezm commented Apr 20, 2022

This PR adds initial support for boolean patterns. As a demonstration of their use I extended opentype.fathom to implement some of the version and magic number checks.

There are several things that have been deemed out of scope for this PR that will be investigated/addressed in follow up work:

  • The check for sfnt version is commented out as it has a big impact on run time, although it does work.
  • There are two FIXMEs attributed to Check for duplicate patterns in match #340
  • When encountering a name in a pattern there is a special case to handle true and false to prevent all binding from resolving to their value. How to handle names in patterns #341 captures the alternatives to this for later resolution.

Other notes:

I'm open to better name suggestions for SplitConstBranches and its variants.

Fixes #340

@mikeday
Copy link
Contributor

mikeday commented Apr 20, 2022

I'm curious why the sfnt version check has an impact when the other tests do not?

@wezm
Copy link
Contributor Author

wezm commented Apr 20, 2022

I'm curious why the sfnt version check has an impact when the other tests do not?

Looks like I've found what to look at next :)

@wezm
Copy link
Contributor Author

wezm commented Apr 20, 2022

You know what I think it was the test harness that was going slow. Now that I've updated the snapshots for the other differences it's not slow. Perhaps generating the diffs was slow for some reason.

Edit: The formats/data/opentype/aots/cmap8_font1.snap snapshot is 8421 lines. Perhaps it was diffing that, that slowed it down.

fathom/src/surface/elaboration.rs Outdated Show resolved Hide resolved
fathom/src/surface/elaboration/unification.rs Outdated Show resolved Hide resolved
fathom/src/core/semantics.rs Outdated Show resolved Hide resolved
Comment on lines 184 to 189
#[derive(Clone, Debug)]
pub enum SplitConstBranches<'arena> {
Const(Const, ArcValue<'arena>, Split<'arena>),
Default(Closure<'arena>),
None,
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it would help reduce confusion to rename Split to ConstBranches? I was stuck in naming this originally, and it shows in some of the identifiers I picked.

It's confusing because insplit_telescope and split_const_branches I was trying to follow in an attempt to follow Rust's name for ‘uncons’, [T]::split_first. But then this collides with ‘split’ in the sense of a ‘case-split’.

Also wondering if we could make this type of split_const_branches follow [T]::split_first more closely with something like:

Option<((Const, ArcValue<'arena>), NextConstBranches<'arena>)>

using the following type:

#[derive(Clone, Debug)]
pub enum NextConstBranches<'arena> {
    Branches(ConstBranches<'arena>),
    Default(Closure<'arena>),
}

I dunno if that's better though - just an idea!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh interesting. That explanation does connect some dots for me. Perhaps Split could also be CaseSplit but ConstBranches is does seem clearer.

Also wondering if we could make this type of split_const_branches follow [T]::split_first more closely with something like:

Option<((Const, ArcValue<'arena>), NextConstBranches<'arena>)>

This also makes more sense now. I didn't understand in the original code why the tuples were nested versus using a 3-tuple. Perhaps it's worth giving (Const, ArcValue<'arena>) a name, even if only an alias like Branch or ConstBranch to clean up the nesting a bit?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah good idea about giving it a name!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had a go at refactoring this to use

Option<((Const, ArcValue<'arena>), NextConstBranches<'arena>)>

but it got a bit messy. Perhaps I misunderstood something but I decided to mostly leave it alone. I did change

SplitConstBranches::Const(Const, ArcValue<'arena>, Split<'arena>)

to

SplitConstBranches::Branch(Branch<'arena>, Split<'arena>)

though, where Branch<'arena> is type Branch<'arena> = (Const, ArcValue<'arena>);

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that's fair enough - I might have a go later if that's ok.

Eventually might be good to rename Split to Branches but it's probably good to take one thing at a time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's fair enough - I might have a go later if that's ok.

By all means go ahead 👍

fathom/src/core/semantics.rs Outdated Show resolved Hide resolved
@brendanzab
Copy link
Member

Looking good! Thanks for the various comment fixes by the way! 😄

Copy link
Member

@brendanzab brendanzab left a comment

Choose a reason for hiding this comment

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

Looks good!

┌─ tests/fail/elaboration/non-exhaustive-patterns/match-duplicate.fathom:3:7
3 │ ╭ match true {
│ ^^^^ patterns not covered
Copy link
Member

Choose a reason for hiding this comment

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

This is a bug in codespan. I'm pretty sure it was fixed but we might not have deployed it to crates.io

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's buggy about it?

Copy link
Member

Choose a reason for hiding this comment

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

It's missing the vertical line in the gutter

Copy link
Member

Choose a reason for hiding this comment

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

  ┌─ tests/fail/elaboration/non-exhaustive-patterns/match-duplicate.fathom:3:7
  │  
3 │ ╭ match true {
  │ │       ^^^^ patterns not covered

@wezm wezm merged commit 18c2263 into main Apr 21, 2022
@wezm wezm deleted the bool-patterns branch April 21, 2022 06:49
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.

Check for duplicate patterns in match
3 participants