haskell - Lenses with Sum Types (that have Product types) -
{-# language templatehaskell #-} import control.lens data fruit = fruit { _fruitcolor :: string } $(makeclassy ''fruit) data fruits = banana int fruit | strawberry string fruit printer :: hasfruit s => s -> io () printer f = putstrln $ f ^. fruitcolor sample :: io () sample = printer $ fruit "yellow" printer $ banana 5 $ fruit "yellow"
i have core product type, fruit. want have data types contain core product type, fruit, want able have them represented sum type, fruits. using sum type, fruits, i'd able access components of core product type, fruit. namely, i'd types banana , strawberry instance of hasfruit.
is there straightforward way have fruits instantiate hasfruit? there better pattern represent - core product type in sum type?
it make more sense this:
data fruittype = banana int | strawberry string data fruits = fruits { _fruitsfruittype :: fruittype, _fruitsfruit :: fruit } makefields ''fruits -- `fruits` instantiates `hasfruit`
you might want make prisms fruittype
. prisms traversals different parts of sum type, except can used construct it.
makeprisms ''fruittype -- double int if argument `banana` doublebanana = fruittype._banana *~ 2
Comments
Post a Comment