# Question about Pattern match(es) are non-exhaustive error

**URL:** <https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962>\
**Category:** App Development\
**Tags:** daml\
**Created:** [July 25, 2022, 7:45am UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962 "2022-07-25T07:45:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![cohen.avraham](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@cohen.avraham](https://forum.canton.network/u/cohen.avraham)\
**Post date:** [July 25, 2022, 7:45am UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/1 "2022-07-25T07:45:11Z")

</div>

G-d willing

Hello,  
Following to my previous post, I am using the `--ghc-option=-Wincomplete-uni-patterns` in my `build-options`  
This time I am getting an error on the following code:

```haskell
testCheckConstraintsAgainst : Script ()
testCheckConstraintsAgainst = do
  let record1 = Record 2 True (-1)
      record2 = Record (-1) False 2
      record3 = Record 1 True 0
      Right b = typecheckAgainst @Record $ var "record.foo" `mul` 1
      Right b' = typecheckAgainst @Record $ var "record.foo" `minus` 1

```

The error message is:

```haskell
Pattern match(es) are non-exhaustive
  In a pattern binding: Patterns not matched: (Left _)

```

Since I am new to Daml, I know to use `Right` and `Left` on a `case` switch, but I am not sure how it works when I just type it inside a `let` command.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 25, 2022, 9:34am UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/2 "2022-07-25T09:34:03Z")

</div>

I think your best option here is to do a `case` on the right

```haskell
let b = case typecheckAgainst @Record $ var "record.foo" `mul` 1 of
          Right b -> b
          Left e -> doSomething

```

---

<div class="post-metadata">

**Author:** ![cohen.avraham](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@cohen.avraham](https://forum.canton.network/u/cohen.avraham)\
**Post date:** [July 25, 2022, 11:07am UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/3 "2022-07-25T11:07:49Z")

</div>

Thanks for the reply but this is still not good enough.  
Keep in mind that I am using the `Wincomplete-uni-patterns` build option.  
Basically after the code I attached, there is usage for b & b’. So in that case b is not defined anymore as the right value, but the Either. And if I try to fix it by using Right whenever we use it, I am getting that error message again (`Pattern match(es) are non-exhaustive In a pattern binding: Patterns not matched: (Left _)`  
So, I am not sure how to get rid of that loop.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 25, 2022, 11:08am UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/4 "2022-07-25T11:08:36Z")

</div>

What do you want to happen in the `Left` case? Can you share the code afterwards that uses it?

---

<div class="post-metadata">

**Author:** ![cohen.avraham](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@cohen.avraham](https://forum.canton.network/u/cohen.avraham)\
**Post date:** [July 25, 2022, 11:13am UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/5 "2022-07-25T11:13:22Z")

</div>

Well the other 2 lines after that are:

```haskell
      dynamicConstraint = mkConstraintUnsafe @Record (var "record.b" `gt` 0) "b is positive" [mkBinding "b" b]
      updatedConstraint = updateConstraintBindingsUnsafe dynamicConstraint [mkBinding "b" b']

```

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 25, 2022, 11:52am UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/6 "2022-07-25T11:52:33Z")

</div>

Can you share what you expect to happen if you get a `Left`?

---

<div class="post-metadata">

**Author:** ![cohen.avraham](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@cohen.avraham](https://forum.canton.network/u/cohen.avraham)\
**Post date:** [July 25, 2022, 1:13pm UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/7 "2022-07-25T13:13:08Z")

</div>

Well, I was offered a solution which worked well for me at this point and it was adding the following code:

```haskell
testTypedExpr : Either TypecheckErr (TypedExprAgainst a) -> (TypedExprAgainst a)
testTypedExpr val = case val of
  Right result -> result
  Left _ -> error "Error"

```

and the previous 2 lines I changed to:

```haskell
b = testTypedExpr $ typecheckAgainst @Record $ var "record.foo" `mul` 1
      b' = testTypedExpr $ typecheckAgainst @Record $ var "record.foo" `minus` 1

```

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 25, 2022, 1:14pm UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/8 "2022-07-25T13:14:27Z")

</div>

Isn’t that exactly the `case` solution I suggested? I think I don’t understand why that did not work for you.

---

<div class="post-metadata">

**Author:** ![cohen.avraham](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@cohen.avraham](https://forum.canton.network/u/cohen.avraham)\
**Post date:** [July 25, 2022, 1:36pm UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/9 "2022-07-25T13:36:17Z")

</div>

G-d willing

No it is not the same since using the `case` will return `Either` value and not the `Right` value. And since I am using the returned value later on, it won’t work.  
That will force me again to use the Right and Left on the same returned value - so it is kind of an infinite loop.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 25, 2022, 2:17pm UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/10 "2022-07-25T14:17:12Z")

</div>

I don’t think that should be true. If you look at my example, note that I also extract the value from the Either

```haskell
 Right b -> b

```

What you say would apply if we had

```haskell
 Right b -> Right b

```

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [July 25, 2022, 2:18pm UTC](https://forum.canton.network/t/question-about-pattern-match-es-are-non-exhaustive-error/4962/11 "2022-07-25T14:18:02Z")

</div>

If you have a concrete example and an error message, I’m happy to take a look.
