Skip to content
Discussions/App Development/Question about Pattern match(es) are non-exhaustive errorForum ↗

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

App Development11 posts329 viewsLast activity Jul 2022
CO
cohen.avrahamOP
Jul 2022

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:

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:

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.

CO
cocreature
Jul 2022

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

let b = case typecheckAgainst @Record $ var "record.foo" `mul` 1 of
          Right b -> b
          Left e -> doSomething
CO
cohen.avraham
Jul 2022

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.

CO
cocreature
Jul 2022

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

CO
cohen.avraham
Jul 2022

Well the other 2 lines after that are:

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

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

CO
cohen.avraham
Jul 2022

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

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:

b = testTypedExpr $ typecheckAgainst @Record $ var "record.foo" `mul` 1
      b' = testTypedExpr $ typecheckAgainst @Record $ var "record.foo" `minus` 1
CO
cocreature
Jul 2022

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

CO
cohen.avraham
Jul 2022

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.

CO
cocreature
Jul 2022

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

 Right b -> b

What you say would apply if we had

 Right b -> Right b
CO
cocreature
Jul 2022

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

← Back to Discussions