Is there a cardinality pattern for DAML?
- Chris_RiversOP 2
Hi DAML Forum! @jamesljaworski85 and I are extending the ISDA CDM model to generate CalculationPeriods that include resets and novel events. The current function does so based on
CalculationPeriodDatesandResetDates:generateResetPeriods : (Fetch f) => CalculationPeriodDates -> Optional ResetDates -> f [CalculationPeriod] generateResetPeriods cpds None = generateCalculationPeriods cpds generateResetPeriods cpds (Some (checkResetDates cpds -> rds)) = do -- Roll out calculation periods and unadjusted reset dates cps <- generateCalculationPeriods cpdsOur current pattern for doing this:
generateSchedulePeriods : (Fetch f) => CalculationPeriodDates -> Optional ResetDates -> Optional NovelDates -> f [CalculationPeriod] generateSchedulePeriods cpds None None = generateCalculationPeriods cpds generateSchedulePeriods cpds (Some (checkResetDates cpds -> rds)) (Some (checkNovelDates cpds -> nds)) = do -- Roll out calculation periods and unadjusted reset dates cps <- generateCalculationPeriods cpdsThis function implies a pattern for handling None-None, Some-Some, None-Some and Some-None instances. In addition to the above function, we have two
doblocks for None-Some and Some-None cases.Is there a better pattern that handles resets and a second set of dates that correspond to novel event(s)?
The resets and novel events have the same number of parameters which load into the PaymentCalculationPeriods function:
generatePaymentCalculationPeriods : (Fetch f) => CalculationPeriodDates -> Optional ResetDates -> Optional NovelDates -> Optional PaymentDates -> f [PaymentCalculationPeriod] generatePaymentCalculationPeriods cpds rds nds (fmap (checkPaymentDates cpds) -> pds) = do cps <- generateSchedulePeriods cpds rds nds - bernhard 4
I’m not entirely sure what you are after. If you have four
doblocks, you can’t avoid writing some matching logic to switch between the four. I personally prefer case expressions to several function definitions:t : Int -> Optional Int -> Optional Int -> Int t i ij ik = case (fmap (+1) ij, fmap (+1) ik) of (Some j, Some k) -> i * j * k (Some j, _) -> i * j (_, Some k) -> i * k _ -> iIn your particular case, that would presumably look something like
generatePaymentCalculationPeriods : (Fetch f) => CalculationPeriodDates -> Optional ResetDates -> Optional NovelDates -> Optional PaymentDates -> f [PaymentCalculationPeriod] generatePaymentCalculationPeriods cpds ords onds opds = case (fmap (checkResetDates cpds) ords, fmap (checkNovelDates cpds) onds, fmap (checkPaymentDates cpds) opds) of (Some rds, Some nds, Some pds) -> do ... (Some rds, Some nds, _) -> do ... (Some rds, _, Some pds) -> do ... (_, Some nds, Some pds) -> do ... (Some rds, _, _) -> do ... (_, Some nds, _) -> do ... (_, _, Some pds) -> do ... _ -> do ... - Luciano 1
As Bernhard said, there’s no real way of getting around specifying the semantics for each independent case - if they are independent. However, if more than one of the cases overlap, there are several combinators that can make the code more concise.
For example, if you want to fail-fast with a
Noneif any of your inputs isNone, you could try something likeliftA2 (,) maybeLeft maybeRightThis returns an
Optional (a, a), which you can then do a single pattern match against. You can achieve something similar for higher arities with<*>, also in the Applicative typeclass.Haskell also has a handy
Alternativetypeclass which unfortunately we don’t include in the standard library. It has<|>, which given twoOptionals, returns a single one, the first non-empty going left-to-right. So this is different from the above, as it won’t fail if you have at least oneSomeas input. You can google for it to see how it’s implemented.Alternatively you can implement your own combinator function and then re-use it whenever you need! I do recommend googling around to see if you find your exact use-case covered in Haskell.
Hope that helps somewhat!
- bernhard 1
@Luciano that seems like overkill. Since
-matches on bothSomeandNone, you can just leave out any cases that should “fall through”:generatePaymentCalculationPeriods : (Fetch f) => CalculationPeriodDates -> Optional ResetDates -> Optional NovelDates -> Optional PaymentDates -> f [PaymentCalculationPeriod] generatePaymentCalculationPeriods cpds ords onds opds = case (fmap (checkResetDates cpds) ords, fmap (checkNovelDates cpds) onds, fmap (checkPaymentDates cpds) opds) of (Some rds, Some nds, Some pds) -> do ... _ -> do ... - Luciano 1
Sure, I take your point.
I think what I wanted to illustrate was the general approach of re-using functions to handle
Optionalbehaviour, as an alternative to doing a pattern match.In the case you describe it may not bring much, but for instance I do find the
<|>combinator really useful in cases where you would otherwise have to repeat yourself. - Chris_Rivers
bernhard:I’m not entirely sure what you are after.
We’re evaluating the
ResetEventsandNovelEvents(both contained in theSchedulePeriodclass, based onResetDatesclass) within our threedoblocks and then passing the output to thegeneratePaymentCalculationPeriodsfunction.The
generatePaymentCalculationPeriodsgenerates thePaymentCalculationPeriodfrom theCalculationPeriodDates(required) andResetDates,NovelDatesandPaymentDates, all of which are optional.We’d like to:
(1) set
floatingRateDefinitionthrough resets, which is partially based on generatingResetDatesclass, which in turn is based onCalculationPeriodDatesclass, as shown above ingenerateResetPeriodsfunction.(2) set the novel events, which have a similar DAML model and behavior as resets. (This is why you see
ResetDatesandNovelDatesin thegenerateSchedulePeriods.)(3) validate our approach with your suggestion: we have three do blocks in
generateSchedulePeriodswithNonecase handled outside of do block:generateSchedulePeriods : (Fetch f) => CalculationPeriodDates -> Optional ResetDates -> Optional NovelDates -> f [CalculationPeriod] generateSchedulePeriods cpds None None = generateCalculationPeriods cpds --should have own do block? generateSchedulePeriods cpds (Some (checkResetDates cpds -> rds)) (Some (checkNovelDates cpds -> nds)) = do -- Roll out calculation periods, unadjusted reset dates and unadjusted novel dates cps <- generateCalculationPeriods cpds ... generateSchedulePeriods cpds (Some (checkResetDates cpds -> rds)) None = do ... generateSchedulePeriods cpds None (Some (checkNovelDates cpds -> ads)) = do ... - Chris_Rivers
Chris_Rivers:The resets and novel events have the same number of parameters which load into the PaymentCalculationPeriods function:
generatePaymentCalculationPeriods : (Fetch f) => CalculationPeriodDates -> Optional ResetDates -> Optional NovelDates -> Optional PaymentDates -> f [PaymentCalculationPeriod] generatePaymentCalculationPeriods cpds rds nds (fmap (checkPaymentDates cpds) -> pds) = do cps <- generateSchedulePeriods cpds rds ndsRight now, we have the following error from the
cps <- generateSchedulePeriods...from inside thedoblock. This is because all of the args’ expected type isOptional, although its currently being represented as required:...daml/Org/Isda/Cdm/EventSpecificationModule/Impl/Contract/Payout/InterestRatePayout/Schedule/PaymentDates.daml:44:43: error: Couldn't match expected type `Optional ResetDates' with actual type `ResetDates' In the second argument of `generateSchedulePeriods', namely `rds' In a stmt of a 'do' block: cps <- generateSchedulePeriods cpds rds nds In the expression: do cps <- generateSchedulePeriods cpds rds nds let adj = optional noAdj (\ x -> ...) pds let payRelativeTo = optional PayRelativeToEnumCalculationPeriodEndDate (\ x -> ...) pds let offset = (\ x -> ...) =<< pds ....Is it because the
fmapexpects a non-empty list? There will be cases whererdsandndsare empty. - bernhard
@Chris_Rivers I think the first line(s) of your error message are missing. I’m not seeing the issue just from the code snippets you posted.
EDIT to answer your question " Is it because the
fmapexpects a non-empty list? There will be cases whererdsandndsare empty.":fmapdoesn’t expect a list at all. It expects a Functor, which is a container. If you are from the Java world, think of it as a collection. The construct(fmap (checkPaymentDates cpds) -> pds)is a view pattern. Since it comes in the fourth place in the definition ofgeneratePaymentCalculationPeriods, it means“take the fourth argument, apply the function
fmap (checkPaymentDates cpds)to it, and put the result in pds”.The fourth argument is of type
Optional PaymentDates, andcheckPaymentDates cpdsis of typePaymentDates -> PaymentDates. Thefmapjust maps that over theOptionalfunctor so thatfmap (checkPaymentDates cpds) : Optional PaymentDates -> Optional PaymentDates.Since
pdsisn’t used in the expression that is giving you the error, I don’t think this is what’s causing the issues. - Chris_Rivers
bernhard:I think the first line(s) of your error message are missing.
I’ve updated my original post’s error above. The compiler has the same error for
ResetDates,NovelDatesandPaymentDates.The
generateSchedulePeriodsfollow this pattern with the addition of NovelEvents. The result of which is applied to thegeneratePaymentCalculationPeriodssimilar to this. - cocreature
The error is telling you that you are trying to use the
optionalfunction on something that is not of typeOptional afor some typea. Usually you either pattern match or use theoptionalfunction to avoid a pattern match. If you’ve already pattern matched on theSomecase there is no need to useoptional. - Chris_Rivers
@bernhard and @cocreature we’ve removed the errors by adding
Someto correspond to eachcasevariable, as applicable in eachdoblock:cps <- generateSchedulePeriods cpds (Some rds) (Some nds)However, the
floatingRateDefinitionis still not set, when consolidatingResetDatesandNovelDatesinto one class, SchedulePeriod, which includes thegenerateSchedulePeriodsas shown above. (To quote Bernard, our current approach maybe overkill).Maybe a better way is to keep the ResetDates and NovelDates separate, but cast their result to the generatePaymentCalculationPeriods from the generateResetPeriods below:
generatePaymentCalculationPeriods : (Fetch f) => CalculationPeriodDates -> Optional ResetDates -> Optional PaymentDates -> f [PaymentCalculationPeriod] generatePaymentCalculationPeriods cpds rds (fmap (checkPaymentDates cpds) -> pds) = do -- Roll out calculation periods cps <- generateResetPeriods cpds rdsWhat would this look like? While the floatingRateDefinition sets using the generateResetPeriods, it won’t set when using the
generateSchedulePeriodsas above.