The laws I get but not sure how I would implement ...
# arrow
d
The laws I get but not sure how I would implement it in arrow if I have an
Boolean expression AND Boolean expression AND Boolean expression
😅 Do I implement it on boolean expression or is it a different type/class all together?
j
so you have something like
class BoolExp
? Do you use kapt with arrow-meta? Then your typeclass instance becomes:
Copy code
@extension
interface BoolExpMonoid : Monoid<BoolExp> {}
That will generate some code. Using it will become:
BoolExp.monoid().run { exp1 + exp2 + exp3 }
etc
Btw the code that it will generate is usually only:
fun BoolExp.Companion.monoid(): Monoid<BoolExp>
and some quality of life methods.
r
@Dennis Tel model your algebra of ops as an ADT
Your algebra then will show the Monoid structure through it's empty identity node and Semigroup combine through plus or combine
We can help you if you describe an simple example of an operation your program performs
For example Add(Add(1), Add(1))
For that you should have an interpreter that through fold or pattern matching turns that value into a terminal value of 2.
Modeling with ADTs will help you understand what typeclasses your tree data structure can support. You'll quickly find that all ADTs are Foldable or Traverse Functors
Sometimes Applicative and Monads
d
Thanks for the response! I'll attempt this over the weekend and come up with a proper example. @raulraja is it OK if I @ you once I have an example?
r
Take the code of Option as an example
It models two operations
Some and None
It's a foldable tree, Monad etc
And it's a GADT because it's generic but you can have the same over non generic ADTs
Also if it does not click for you I can model it for you in a few mins over hang outs to explain how ADTs work.
TLDR it's modeling operations as classes and objects that are children of a sealed class hierarchy, enum or in the upcoming arrow 1.0, unions.
And by doing that you have a value you can interpret by using when to pattern match on it
This is the cornerstone of functional design as it decouples pure programs from impure interpreters
All data types in Arrow are ADTs and follow the same design once you understand this you can understand any DataType in arrow
p
Didn’t we have MultiplicativeMonoid, Ring, and the rest of the family?
yes!
d
@pakoito thanks!