title: Haskell: Am I misunderstanding how Arrows may be used?
body: <p>I wrote some toy code to play with the concept of Arrows.  I wanted to see if I could write an Arrow which encoded the concept of a stateful function - giving a different value after different calls.</p>
  
  <pre><code>{-# LANGUAGE Arrows#-}
  module StatefulFunc where
  
  import Control.Category
  import Control.Arrow
  
  newtype StatefulFunc a b = SF { unSF :: a -&gt; (StatefulFunc a b, b) }
  
  idSF :: StatefulFunc a a
  idSF = SF $ \a -&gt; (idSF, a)
  
  dotSF :: StatefulFunc b c -&gt; StatefulFunc a b -&gt; StatefulFunc a c
  dotSF f g = SF $ \a -&gt; 
      let (g', b) = unSF g a
          (f', c) = unSF f b
      in (dotSF f' g', c)
  
  instance Category StatefulFunc where
    id = idSF
    (.) = dotSF
  
  arrSF :: (a -&gt; b) -&gt; StatefulFunc a b
  arrSF f = ret
    where ret = SF fun
          fun a = (ret, f a)
  
  bothSF :: StatefulFunc a b -&gt; StatefulFunc a' b' -&gt; StatefulFunc (a, a') (b, b')
  bothSF f g = SF $ \(a,a') -&gt;
      let (f', b) = unSF f a
          (g', b') = unSF g a'
      in (bothSF f' g', (b, b'))
  
  splitSF :: StatefulFunc a b -&gt; StatefulFunc a b' -&gt; StatefulFunc a (b, b')
  splitSF f g = SF $ \a -&gt;
      let (f', b) = unSF f a
          (g', b') = unSF g a
      in (splitSF f' g', (b, b'))
  
  instance Arrow StatefulFunc where
    arr  = arrSF
    first = flip bothSF idSF
    second = bothSF idSF
    (***) = bothSF
    (&amp;&amp;&amp;) = splitSF
  
  eitherSF :: StatefulFunc a b -&gt; StatefulFunc a' b' -&gt; StatefulFunc (Either a a') (Either b b')
  eitherSF f g = SF $ \e -&gt; case e of
        Left a -&gt; let (f', b) = unSF f a in (eitherSF f' g, Left b)
        Right a' -&gt; let (g', b') = unSF g a' in (eitherSF f g', Right b')
  
  mergeSF :: StatefulFunc a b -&gt; StatefulFunc a' b -&gt; StatefulFunc (Either a a') b
  mergeSF f g = SF $ \e -&gt; case e of 
        Left a -&gt; let (f', b) = unSF f a in (mergeSF f' g, b)
        Right a' -&gt; let (g', b) = unSF g a' in (mergeSF f g', b)
  
  instance ArrowChoice StatefulFunc where
    left = flip eitherSF idSF
    right = eitherSF idSF
    (+++) = eitherSF
    (|||) = mergeSF
  </code></pre>
  
  <p>So after I went through the various type class definitions (not sure whether or how ArrowZero would work for this, so I skipped it), I defined some helper functions</p>
  
  <pre><code>evalSF :: (StatefulFunc a b) -&gt; a -&gt; b
  evalSF f a = snd (unSF f a)
  
  givenState :: s -&gt; (s -&gt; a -&gt; (s, b)) -&gt; StatefulFunc a b
  givenState s f = SF $ \a -&gt; let (s', b) = f s a in (givenState s' f, b)
  </code></pre>
  
  <p>And worked out an example of use</p>
  
  <pre><code>count :: StatefulFunc a Integer
  count = givenState 1 $ \c _ -&gt; (c+1, c)
  
  countExample :: StatefulFunc a Integer
  countExample = proc _ -&gt; do
                    (count', one) &lt;- count -&lt; ()
                    (count'', two) &lt;- count' -&lt; ()
                    (count''', three) &lt;- count'' -&lt; ()
                    returnA -&lt; three
  </code></pre>
  
  <p>However, when I try to compile <code>countExample</code>, I get "Not in scope" errors for <code>count'</code> and <code>count''</code>, which I suppose means that I need to go back to the tutorial and read up on what can be used when.  I think what I'd really like anyway is something more like</p>
  
  <pre><code>countExample :: Integer
  countExample =
    let (count', one) = unSF count ()
        (count'', two) = unSF count' ()
        (count''', three) = unSF count'' ()
    in three
  </code></pre>
  
  <p>But that's kind of awkward, and I was hoping for something a bit more natural.</p>
  
  <p>Can anyone explain how I'm misunderstanding how Arrows work, and how they might be used?  Is there fundamental philosophy to Arrows that I'm missing?</p>