This is a file from the Wikimedia Commons

File:Understanding arrows getWord data flow.svg

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Original file(SVG file, nominally 1,000 × 1,936 pixels, file size: 35 KB)

Summary

Description
English: Data flow in a fragment of the getWord example of b:en:Haskell/Arrow tutorial. Rectangles are arrows (rounded rectangles are arrows made with arr) and circles are other data flow split/merge points. Blue indicates a Left tag, red indicates Right and purple indicates lack of Either tags. Corresponding code:
proc () -> do
    firstTime <- oneShot -< ()
    mPicked <- if firstTime
        then do
            picked <- pickWord rng -< ()
            returnA -< Just picked
        else returnA -< Nothing
    accum' Nothing mplus -< mPicked
Date
Source Own work
Author Daniel Mlot

Haskell/diagrams code for generating the image:

{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE LambdaCase #-}

import Diagrams.Prelude
import Diagrams.Backend.Cairo.CmdLine

main = mainWith $ pad 1.05 . centerXY $ getWordDiagV

getWordDiagV =
    [ vsep 1
        [ anArrow Ne "accum' Nothing mplus" # named "accum"
        , aFork Ne "|||" # named "merge"
        ]
    , vsep 1
        [ aFunction Le "Just" # named "just"
        , anArrow Le "pickWord rng" # named "pickWord"
        , aFunction Le "const ()" # named "unit"
        ] # centerY
        |||
    strutX 1
        |||
    aFunction Ri "const Nothing" # named "nothing" # centerY
    , vsep 1
        [ aFork Ne "if" # named "if"
        , anArrow Ne "oneShot" # named "oneShot"
        ]
    ] # fmap centerX # vsep 0.5

    # connectNodes Ne "oneShot" "if" (1/4) (3/4)
    # connectNodes Le "if" "unit" (3/8) (7/8)
    # connectNodes Le "unit" "pickWord" (1/4) (3/4)
    # connectNodes Le "pickWord" "just" (1/4) (3/4)
    # connectNodes Le "just" "merge" (1/8) (5/8)
    # connectNodes Ri "if" "nothing" (1/8) (5/8)
    # connectNodes Ri "nothing" "merge" (3/8) (7/8)
    # connectNodes Le "merge" "accum" (1/4) (3/4)

data ForkFlag = Le | Ne | Ri

forkColour :: ForkFlag -> Colour Double
forkColour = \case
    Le -> blue
    Ne -> mediumorchid
    Ri -> red

connectNodes :: ForkFlag -> String -> String
                         -> Double -> Double -> Diagram B -> Diagram B
connectNodes ffl nam1 nam2 tur1 tur2 =
    connectPerim' (baseConnOpts ffl)
        nam1 nam2 (tur1 @@ turn) (tur2 @@ turn)

baseConnOpts ffl = with & lengths .~ large
    & shaftStyle %~ lc colo . lw veryThick
    & headStyle %~ fc (darken 0.2 colo)
    where
    colo = forkColour ffl

nodeSkeleton :: Diagram B -> ForkFlag -> String -> Diagram B
nodeSkeleton d ffl s = d # scaleX (max 1 $ fromIntegral (length s) / 7)
        # applyStyle initStyle # lc colo
    <> nudgedText s
        # fc colo
    where
    colo = forkColour ffl

aFunction :: ForkFlag -> String -> Diagram B
aFunction = nodeSkeleton $ roundedRect 1.6 1.2 0.4

anArrow :: ForkFlag -> String -> Diagram B
anArrow = nodeSkeleton $ rect 1.6 1.2

aFork :: ForkFlag -> String -> Diagram B
aFork = nodeSkeleton $ circle 0.5

initStyle :: Style V2 Double
initStyle = mempty # lwL 0.1

nudgedText :: String -> Diagram B
nudgedText s = alignedText 0.5 0.5 s
    # font "monospace" # fontSize (local 0.25) # bold

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

17 July 2015

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current04:26, 17 July 2015Thumbnail for version as of 04:26, 17 July 20151,000 × 1,936 (35 KB)DuplodeUser created page with UploadWizard

The following 2 pages use this file:

Metadata