Is it possible to create a function in Haskell that operates on a set of types? -
i have been searching google while, not able find answer:
say have tree adt polymorphic, base payload sum type , 2 extention sum types:
--base.hs data tree = node [tree a] | empty data basepayload = basea string | baseb int --extention1.hs data extention1 = ext1a string string | ext1b int --extention2.hs data extention2 = b |
i cannot modify base type, , not know if , how many extention types used @ compile time. possible create functions work this:
--base.hs type generaltree = tree someboxtype transform :: tree (someboxtype (any | basepayload)) -> tree (someboxtype any) --extention1.hs transform :: tree (someboxtype (any | extention1)) -> tree (someboxtype any) --extention2.hs transform :: tree (someboxtype (any | extention2)) -> tree (someboxtype any)
is possible? when searched found gadt, not need , datakinds , typefamilies, did not understand 100%, dont think here. row polymorphism? help.
i suspect want functor
instance. thus:
instance functor tree fmap f (node x children) = node (f x) (fmap (fmap f) children) fmap f empty = empty
now fmap
can given of these types:
fmap :: (either basepayload -> any) -> tree (either basepayload) -> tree fmap :: (either extention1 -> any) -> tree (either extention1 ) -> tree fmap :: (either extention2 -> any) -> tree (either extention2 ) -> tree
as aside, find empty
constructor suspicious. what's difference between node 0 []
, node 0 [empty]
example? node 0 [empty, empty]
sensible value have @ all? consider using data.tree
, comes compiler, doesn't have issue, , has functor
instance.
Comments
Post a Comment