arybczak / haskellx18

0 stars 0 forks source link

Cartesian examples? #1

Open coodoo opened 5 years ago

coodoo commented 5 years ago

https://github.com/arybczak/haskellx18/blob/0e12125f5d49b5cc42ff65a218fe189446a74720/code.hs#L100-L106

First of all, thanks for the great talk at HaskellX 18', I was wondering could you please provide more details on the Cartesian, for example how could it be used to solve real world task? (i.e. how could Star be used with Cartesian)? Thanks.

arybczak commented 5 years ago

You instantiate p to Star if you want to use an optic as a traversal, e.g. in traverseOf.

arybczak commented 5 years ago

or in view1 or preview.

coodoo commented 5 years ago

Here's some exercises I did following the Profunctor example, but I failed to do the same for Cartesian for that I don't really know how first and second operates and how it works with Profunctor, it would be great if you could provide some concrete example as the attached image?

screen shot 2018-12-13 at 5 02 47 pm
arybczak commented 5 years ago

I'm not sure if it's satisfactory, but you can take a look at https://github.com/arybczak/lens-profunctor/blob/master/src/Data/Lens/Profunctor/Lens.hs#L38 - this uses Cartesian (it's Strong there) to convert from van Laarhoven lens to a profunctor one. This example is unfortunately a bit dense.

BTW, https://github.com/arybczak/lens-profunctor is a proof-of-concept profunctor optics base implementation with majority of optics hierarchy (see https://github.com/arybczak/lens-profunctor/blob/master/src/Data/Lens/Profunctor/Types.hs) which I later used to convert https://github.com/well-typed/optics to profunctor encoding (see https://github.com/well-typed/optics/issues/18 and associated PR that was recently merged). There is a lot of info in these tickets/PR if you want to go deeper towards something more practical than proof-of-concept.

coodoo commented 5 years ago

Thanks, those were very helpful, and I eventually come up with following sample code, with two questions:

  1. Is this the correct usage of first and Star?

  2. What's the use case for passing in an extra aa, could you provide some real world examples? For now I'm thinking aa might be some kind of global variable that one may want to pass around, but I can't figure out how aa could be used within the computation inside the Profunctor.


ss :: Star Maybe Int Int
ss = Star (\x -> pure $ x+1)
s1 = runStar ss 5 -- Just 6

ss2 = first ss
s2 = runStar ss2
s2' = runStar ss2 (5, "aa") -- Just (6,"aa")