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

Uniformize destruct and pattern matching logic #1907

Merged
merged 2 commits into from
May 6, 2024

Commits on May 6, 2024

  1. Uniformize destruct and pattern matching logic

    When patterns were first introduced, only let-destructuring existed, and
    match expressions weren't a thing. Rather than to manually generate code
    that would check that a pattern does match the destructured value, we
    piggy-backed on contracts instead, which was easier and provided better
    error messages. Up to now, this was still how destructuring worked: a
    contract is elaborated from the pattern and applied to the matched
    value, and should report any error if the value doesn't match. Then,
    destructuring is desugared to a series of let-bindings that assume that
    the destucture value has the right shape. The generated code is thus
    quite simple, as it doesn't have any error handling to do.
    
    When pattern matching landed, we kept the old destructuring
    infrastructure, because it was there and worked well. However, there is
    duplication: the compilation of match expressions does a work quite
    similar to destructuring.
    
    What's more, we're at risk of having a diverging semantics. Some
    questions aren't trivial to answer for destructuring, in particular with
    respect to lazyness: should `let _ = x in null` force `x`? What about
    `let 'Foo _ = x in null`, or `let 'Foo 5 = x in null`?
    
    After some discussion, we decided to give destructuring a simple
    semantics in term of pattern matching: `let <pat> = <matched> in <body>`
    should be strictly equivalent to `<matched> |> match { <pat> => <body>
    }`. Indeed, `match` has a clear semantics around forcing values in lazy
    languages. This also agrees with intuition, or at least gives a
    reasonable semantics for most cases.
    
    This commit implements this change in practice, by getting rid of the
    ad-hoc desugaring of destructuring and rewriting let-destructuring to
    actual pattern matching instead.
    yannham committed May 6, 2024
    Configuration menu
    Copy the full SHA
    922f237 View commit details
    Browse the repository at this point in the history
  2. Update core/src/term/pattern/compile.rs

    Co-authored-by: jneem <joeneeman@gmail.com>
    yannham and jneem authored May 6, 2024
    Configuration menu
    Copy the full SHA
    81ad1a6 View commit details
    Browse the repository at this point in the history