Hi all!
I need some guidance with Monoids. Help will be highly appreciated!
I had some code that I decided to refactor in Haskell fashion. I have a data structure:
data TwoFungiblesAndWitness = TwoFungiblesAndWitness with
f1 : FungibleImplementation.Fungible
f2 : FungibleImplementation.Fungible
witness: Party
And I have a list of functions that return Bool for this type:
isWitnessOwner: TwoFungiblesAndWitness -> Bool
isWitnessOwner TwoFungiblesAndWitness{ f1, f2, witness } = f1.account.owner == witness && f2.account.owner == witness
isSameAccount: TwoFungiblesAndWitness -> Bool
isSameAccount TwoFungiblesAndWitness{ f1, f2 } = f1.account == f2.account
isSameInstrument: TwoFungiblesAndWitness -> Bool
isSameInstrument TwoFungiblesAndWitness{ f1, f2 } = f1.instrument == f2.instrument
Now I want to pass my data through this list and return True only if all functions return True.
At the current moment, I do it like this:
isMergeable twoFungiblesAndWitness = all ((==True) . ($ twoFungiblesAndWitness)) [isWitnessOwner, isSameAccount, isSameInstrument]
And this is fine. But I want to add foldMap to my arsenal. So after learning I came up with this:
isMergeable = getAll . foldMap (All .) predicates
where predicates = [isWitnessOwner, isSameAccount, isSameInstrument]
I get an error here:
No instance for (Monoid (TwoFungiblesAndWitness -> All))
arising from a use of ‘foldMap’
How can I implement such an instance? As I understand I need to tell the compiler how to act with a list of functions, how concat results, etc…
I tried
instance Monoid (TwoFungiblesAndWitness -> All) where
mempty _ = All True
mappend
But I do not feel confident about what I should do here.
Thanks!