[Haskell-cafe] An idea on extensible effects (anonymous record)
winter
drkoster at qq.com
Tue Nov 22 03:30:07 UTC 2016
Hi everyone!
First of all, i don't know if this idea is already being discussed so if there's a discussion on this i'd like to follow.
To illustrate, first i want to create a class like:
class Has a t where
get :: t -> a
then i define instance for simple product type such as tuple:
instance Has a (a, b) where
get (a, _) = a
instance Has b (a, b) where
get (_, b) = b
...
You can image i will use th to make lots of instance for difference tuple size. Now if i want an extensible reader, i use Has class like this:
someReader :: Has Int t => Reader t Int
someReader = do
x <- ask
return $ get x + 1
Then i can run it with any tuple with an Int field like:
runReader someReader (0 :: Int, "adad”) -- 1
This typeclass almost solved all problem of my network application: sometime’s i want ensure a logger, a sql backend and a http client pool in my monad’s environment, but i don’t want to fix my environment into a record.
We can add a set :: a -> t -> t, or use lens to define Has, so that we can have extensible states.
We can also use Tagged to achieve something like:
(Has (Tagged “SqlBackEndOne” SqlBackEnd) t, Has (Tagged “SqlBackEndTwo" SqlBackEnd) t) => Reader t ()
It there a library doing this, maybe in lens? or there’re some drawbacks i didn’t notice? All ideas are welcomed!
Cheers~
Winter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20161122/f80f2154/attachment.html>
More information about the Haskell-Cafe
mailing list