Skip to content
Snippets Groups Projects
Commit 24c20cd7 authored by Sebastian Graf's avatar Sebastian Graf
Browse files

Pattern match complex expressions by GVN

By referential transparency, multiple syntactic occurrences of the same
expression evaluate to the same value. Global value numbering (GVN)
assigns each such expression the same unique number (a `Name` in our
case). Two expressions trivially have the same value if they are
assigned the same value number.

The term oracle `TmOracle` of the pattern match checker couldn't handle
any complex expression before this patch. It would just give up on
anything involving a function application whose head was not a
constructor, by falling back to `PmExprOther`. This means it could not
determine completeness of the following example:

```haskell
foo
  | True <- id True
  = 1
  | False <- id True
  = 2
```

This is simply because `TmOracle` couldn't figure out that `id True`
always evaluates to the same `Bool`.

In this patch, we desugar such `PmExprOther`s in pattern guards to
`CoreExpr`.  We do so in order to utilise `CoreMap Name` for a
light-weight GVN pass without concern for subexpressions.  `TmOracle`
only sees the representing variables, like so:

```haskell
x = id True

foo
  | True <- x
  = 1
  | False <- x
  = 2
```

So `TmOracle` still doesn't need to decide equality of complex
expressions, which allows it to stay dead simple.
parent 37a4fd97
No related branches found
No related tags found
1 merge request!870WIP: Pattern match complex expressions by GVN
Pipeline #5415 failed
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment