Case statement that not working as expected
- cohen.avrahamOP
G-d willing
Hello,
I have an issue with the followingcasethat is weird to me.
Here is the function with the problem:convertTypeByProduct : Optional SomeType -> ProductType -> Date -> Optional SomeOtherType convertTypeByProduct someType productType today = case productType of TypeOne -> mustHaveValue TypeTwo -> mustHaveValue TypeThree -> None where mustHaveValue = let valueType = doSomething someType in if isNone valueType then error $ "No Value Error | Product Type = " <> (show productType) else Some SomeOtherType with intValue = 1It is not important what is
SomeTypenorSomeOtherTypeand etc…
The problem I am having is when I call theconvertTypeByProductfunction with productType = TypeThree.
I am getting the error when I shouldn’t:message = "No Value Error | Product Type = TypeThree"I have no idea what the problem is here. What am I missing in here?
- cohen.avraham
G-d willing
I just changed the code to the following, and it is working.
convertTypeByProduct : Optional SomeType -> ProductType -> Date -> Optional SomeOtherType convertTypeByProduct someType productType today = case productType of TypeOne -> mustHaveValue 1 TypeTwo -> mustHaveValue 2 TypeThree -> trace "Reached TypeThree" None where mustHaveValue value = let valueType = doSomething someType in if isNone valueType then error $ "No Value Error | Product Type = " <> (show productType) <> " | value = " <> (show value) else Some SomeOtherType with intValue = 1I am not sure why without adding the value it didn’t work, and why adding the value resolved the problem.
- cocreature
Daml is a strict language. The bindings in your
whereclause are always evaluated even if they might not be used. So in your first example, even if you passTypeThree,mustHaveValueis evaluated and produces the error.In your second example,
mustHaveValueis still evaluated. However nowmustHaveValueis a function so evaluating it produces a lambda instead of producing an error because it is not yet applied to an argument. - cohen.avraham
G-d willing
Thanks for the explanation.
But isn’t everything in DAML a function? So this behavior was still not expected to behave as it should…
Anyway, how do you think is the correct way to solve it without forcing an unwanted argument, in order to make the where clause work as expected? - cocreature 1
No, only things of type
a -> bfor some type a and b are functions. Functional programming does not mean that everything is a function.I think making it a function is your best option here. In some cases, you could switch to a
letinstead but you’re sharing the code here so that doesn’t work nicely. I’d change the argument though to capture the one the behavior of the function depends on:convertTypeByProduct : Optional SomeType -> ProductType -> Date -> Optional SomeOtherType convertTypeByProduct someType productType today = case productType of TypeOne -> mustHaveValue someType TypeTwo -> mustHaveValue someType TypeThree -> trace "Reached TypeThree" None where mustHaveValue someType = let valueType = doSomething someType in if isNone valueType then error $ "No Value Error | Product Type = " <> (show productType) <> " | value = " <> (show value) else Some SomeOtherType with intValue = 1 - Gary_Verhaegen 2
Hi @cohen.avraham,
Not everything in Daml is a function: we also have values (
1,"hello",None, etc.).What @cocreature meant is that your code is essentially rewritten to (first snippet):
convertTypeByProduct : Optional SomeType -> ProductType -> Date -> Optional SomeOtherType convertTypeByProduct someType productType today = let mustHaveValue = let valueType = doSomething someType in if isNone valueType then error $ "No Value Error | Product Type = " <> (show productType) else Some SomeOtherType with intValue = 1 in case productType of TypeOne -> mustHaveValue TypeTwo -> mustHaveValue TypeThree -> Nonewhich may make the problem more apparent: when executing that function, the first thing that the Daml interpreter does is evaluating the value of the right hand side of the
mustHaveValue = ...assignment. If your argument happens to makedoSomethingreturnNone, this evaluation will trigger an error (due to theerrorfunction being called), before Daml even looks at thecase productType ofline.In the second case, we have essentially the same scenario, except that it roughly gets rewritten to:
convertTypeByProduct : Optional SomeType -> ProductType -> Date -> Optional SomeOtherType convertTypeByProduct someType productType today = let mustHaveValue = \value -> let valueType = doSomething someType in if isNone valueType then error $ "No Value Error | Product Type = " <> (show productType) <> " | value = " <> (show value) else Some SomeOtherType with intValue = 1 in case productType of TypeOne -> mustHaveValue 1 TypeTwo -> mustHaveValue 2 TypeThree -> trace "Reached TypeThree" Nonebecause the syntax
f x = bodyis a shorthand forf = \x -> body. The Daml interpreter is still going to look at themustHaveValue = ...assignment first, but in this case, because the right hand side is a function (specifically, an anonymous function, also known as a lambda), its evaluation is deferred until it is applied to an argument. Now, in this case, themustHaveValuefunction will not be called if theproductTypeargument matches theTypeThreeconstructor, and therefore, ifTypeThreewas the only value that madedoSomethingreturnNone, you no longer have an error.So hopefully that clarifies the problem a little bit.
Now, what to do about it? The fundamental issue here is that you are calling
error, which is hard to use because it produces a side-effect. It is generally best to keep your Daml code as pure as possible: as you can see here, it is sometimes a bit difficult to reason about the order of evaluation of a Daml expression, and pure code does not care about order of evaluation (barring non-termination).How to turn this code pure? One way to collect error conditions without calling
errorexplicitly is to use a “success/error” wrapping type. In your case, you’re almost already doing that:doSomethingreturns anOptional, which in some contexts can be interpreted as success =Someand error =None. Further,convertTypeByProductis already defined to return anOptionalitself, so one option would be to simply carry on theOptionalvalue and haveNonemean “there was an error”:convertTypeByProduct : Optional SomeType -> ProductType -> Date -> Optional SomeOtherType convertTypeByProduct someType productType today = let mustHaveValue = \value -> let valueType = doSomething someType in if isNone valueType then None else Some SomeOtherType with intValue = 1 in case productType of TypeOne -> mustHaveValue 1 TypeTwo -> mustHaveValue 2 TypeThree -> trace "Reached TypeThree" NoneThis assumes the code that calls
convertTypeByProductconsidersNoneto be an error and handles it appropriately.Note that if
doSomethingalready returnsNoneexclusively forTypeThreevalues, using some syntactic sugar, I believe this can be rewritten asconvertTypeByProduct : Optional SomeType -> ProductType -> Date -> Optional SomeOtherType convertTypeByProduct someType productType today = do valueType <- doSomething someType return $ SomeOtherType 1but that may be a topic for another time.
One thing we’ve lost with this approach is that we no longer have an error message, whereas we had one in the original approach. This is because
Optionaldoes not have any argument to its second (None) constructor. There’s another type we can use, though, which can have a similar interpretation:Either. Specifically,Either Text SomeTypein this case, where you could have the calling code interpretRight SomeTypeas a success andLeft Textas an error with the error message attached.This would look something like:
convertTypeByProduct : Optional SomeType -> ProductType -> Date -> Either Text SomeOtherType convertTypeByProduct someType productType today = let mustHaveValue = let valueType = doSomething someType in if isNone valueType then Left $ "No Value Error | Product Type = " <> (show productType) else Right SomeOtherType with intValue = 1 in case productType of TypeOne -> mustHaveValue TypeTwo -> mustHaveValue TypeThree -> Left "given TypeThree"Note the changed return type for
convertTypeByProduct.Finally, if you also rewrote
doSomethingitself to return anEither, you could do something like:convertTypeByProduct : Optional SomeType -> ProductType -> Date -> Either Text SomeOtherType convertTypeByProduct someType productType today = let mustHaveValue = do valueType <- doSomething someType return $ SomeOtherType 1 in case productType of TypeOne -> mustHaveValue TypeTwo -> mustHaveValue TypeThree -> Left "given TypeThree"assuming that
doSomethingreturns aLeft Textwith the appropriate error message itself. - cohen.avraham
@Gary_Verhaegen, you always surprise me with your full detailed answers. I have no way of expressing my appreciation for that.
Regarding your answer, well, the error was placed on purpose since the business logic is asking me to fail that process in casevalueTypeis None whenever theproductType == TypeThree.