<div dir="ltr">Hi all,<div><br></div><div>Take a look at this disaster area, or just scroll down to where I come to the point...</div><div><br></div><div>=======================</div><div><font face="courier new, monospace"><br>
</font></div>
<div><div><font face="courier new, monospace">type SankeyBrain = (P2,CircleFrac,Double) -- like a turtle plus width</font></div><div><font face="courier new, monospace">data SankeyWorld tb = SankeyWorld ((Trail R2,Trail R2),tb) --outgoing and returning trails, plus brain</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">
emptySankey :: SankeyWorld SankeyBrain</font></div><div><font face="courier new, monospace">emptySankey = SankeyWorld ((mempty,mempty),(origin,0,0))</font></div><div><font face="courier new, monospace"><br></font></div><div>
<font face="courier new, monospace">sankeyFrom:: CircleFrac -> Double -> SankeyWorld SankeyBrain</font></div><div><font face="courier new, monospace">sankeyFrom a w = SankeyWorld ((mempty,mempty),(p2 (0,0),a,w)) -- kick off with an angle and width</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">instance Monad SankeyWorld where</font></div><div><font face="courier new, monospace"> return a = SankeyWorld ((mempty,mempty), a) --never use this</font></div>
<div><font face="courier new, monospace"> (SankeyWorld l) >>= f = let (SankeyWorld r) = f (snd l) in -- out = left then right, return = right then left</font></div>
<div><font face="courier new, monospace"><span style="white-space:pre-wrap">                </span>SankeyWorld ( (((fst.fst) l <> (fst.fst) r),((snd.fst) r <> (snd.fst) l)),(snd r) )</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">sankeyVia :: Double -> SankeyBrain -> SankeyWorld SankeyBrain</font></div>
<div><font face="courier new, monospace">sankeyVia d (p,a,w) = </font></div><div><font face="courier new, monospace"> let -- draw parallel lines and move them into place</font></div><div><font face="courier new, monospace"> l1 = hrule 1 # scaleX d # translateX (d/2) # translateY (w/2) # rotate a # translate (origin .-. p) </font></div>
<div><font face="courier new, monospace">
l2 = hrule 1 # scaleX (-d) # translateX (d/2) # translateY (-w/2) # rotate a # translate (origin .-. p) </font></div><div><font face="courier new, monospace"> in SankeyWorld ( ( l1 , l2 ) , ( p .+^ (unitX # scale d # rotate a), a, w) )</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">
sankeyTo :: SankeyBrain -> SankeyWorld SankeyBrain</font></div><div><font face="courier new, monospace">sankeyTo (p,a,w) = SankeyWorld ( --arrow at the end of the flow</font></div><div><font face="courier new, monospace"> ( hrule w # translateX (w/2) # translateY (w/2) # rotate (-1/8::CircleFrac) # scale (0.7071) # rotate a # translate (origin .-. p)</font></div>
<div><font face="courier new, monospace"> , hrule (-w) # translateX (w/2) # translateY (-w/2) # rotate (1/8::CircleFrac) # scale (0.7071) # rotate a # translate (origin .-. p)</font></div><div><font face="courier new, monospace"> ), (p,a,w)) </font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">sankeyTurn r a' (p,a,w) = let (outr, inr, qu) = if a'>=0 then (r, -w-r, -0.25::CircleFrac) else (-w-r, r, 0.25::CircleFrac) in</font></div>
<div><font face="courier new, monospace"> SankeyWorld ( -- turn a corner with nice round edges</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">        </span>( arc' outr (a+qu) (a+a'+qu) # translate (unitY # rotate (a+a' )# scale w)</font></div>
<div><font face="courier new, monospace"> , arc' inr (a+a'+qu) (a+qu) # translate (unitY # rotate (a+a' )# scale w)</font></div>
<div><font face="courier new, monospace"> ),(p,a+a',w))</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">-- bump...</font></div><div><font face="courier new, monospace">sankeySplit :: [(Double, SankeyBrain -> SankeyWorld SankeyBrain)] -> SankeyBrain -> SankeyWorld SankeyBrain</font></div>
<div><font face="courier new, monospace">sankeySplit fs (p,a,w) = let (placed,_) = ( foldl ( \(l,t) -> \(i,c) -> ( l++[( ( p .+^ (unitY # rotate a # scale (((t+i/2)-0.5)*w)), a, w*i) ,c )],t+i) ) ([],0) fs ) in </font></div>
<div><font face="courier new, monospace"><span style="white-space:pre-wrap">        </span>foldl (\(SankeyWorld ((lo,lr),lb)) -> \(SankeyWorld ((ro,rr),rb)) -> SankeyWorld ( ( lo <> ro , rr <> lr ), rb ) ) emptySankey $ map (\(b,f)-> f b) placed</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">SankeyWorld ((turtb,turta),_) = </font></div><div style><font face="courier new, monospace">{- This is the bit that fails:</font></div>
<div><font face="courier new, monospace"><span style="white-space:pre-wrap">        </span>sankeyFrom 0 5 >>= sankeyVia 5 >>= </font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">        </span>sankeySplit </font></div>
<div><font face="courier new, monospace"><span style="white-space:pre-wrap">                </span>[ (0.3, sankeyVia 10 )</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">                </span>, (0.7, sankeyVia 15 )</font></div>
<div><font face="courier new, monospace"><span style="white-space:pre-wrap">                </span>]</font></div>
<div><font face="courier new, monospace"><span style="white-space:pre-wrap">                </span>-}</font></div><div><font face="courier new, monospace"><span style="white-space:pre-wrap">        </span>sankeyFrom 0 5 >>= sankeyVia 5 >>= sankeyTurn 1 (-0.125) >>= sankeyVia 10 >>= sankeyTurn 1 (0.25) >>= sankeyTo -- >>= turn 0.25 >>= forward 10 >>= turn 0.25 >>= forward 20 >>= turn 0.25 >>= forward 10</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">pic3 = (strokeT (close ( turtb<>turta) )) # fc red</font></div><div><br></div><div>======================</div><div>
<br></div><div style>The idea is that <font face="courier new, monospace">SankeyWorld</font> is a monad containing two trails (outbound and inbound) and a turtle-like state. I bind it onto functions like <font face="courier new, monospace">SankeyBrain -> SankeyWorld</font>, whereby <font face="courier new, monospace">>>=</font> passes the state across. <font face="courier new, monospace">>>=</font> draws the left hand outward trail, then the right hand outward trail, then the right hand inward trail, then the left hand inward trail, so it all makes a nice polygon and I can colour it in. </div>
<div style><font face="courier new, monospace"><br></font></div><div style><font face="courier new, monospace">sankeyFrom angle width</font> is already a monad, <font face="courier new, monospace">sankeyVia length</font> is such a function and I could have <font face="courier new, monospace">sankeyTo</font> contain <font face="courier new, monospace">()</font> in place of the brain (i.e. state) cos you're not supposed to continue from it.</div>
<div style><br></div><div style>The tricky bit is splitting the flow. I want a function that takes the brain, splits the width according to named shares and shoves each share into a function <font face="courier new, monospace">SankeyBrain -> SankeyWorld</font> that might have lots more stages and splits downwind.</div>
<div style><br></div><div>It was all going fine until I discovered that if I can say <font face="courier new, monospace">m >>= f</font>, then I can't say <font face="courier new, monospace">f >>= f.</font> So I don't know how to write the bits after the split. Silly me. But what should I do instead to model Sankey diagrams splitting? Is MonadPlus the trick? If so, am I gonna have to make <font face="courier new, monospace">[SankeyWorld]</font> a monad as well?</div>
</div><div style><br></div><div style>TIA,</div><div style>Adrian.</div><div style><br></div><div style>PS: I rarely have any use for the polymorphism of the parameter to Monad. In this case, it's a SankeyBrain, end of story. Is there a simpler kind of monad that doesn't throw this complication at me?</div>
<div style><br></div><div style><br></div><div><br></div><div><br></div></div>