Skip to content
CCPEDIAby Unity Nodes
App Development

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

11 posts381 viewsLast activity Jul 2022

  1. cohen.avrahamOP

    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.

  2. cocreature

    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
    
  3. cohen.avraham

    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.

  4. cocreature

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

  5. cohen.avraham

    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']
    
  6. cocreature

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

  7. cohen.avraham

    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
    
  8. cocreature

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

  9. cohen.avraham

    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.

  10. cocreature

    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
    
  11. cocreature

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