Skip to content

Instantly share code, notes, and snippets.

@prophile
Created February 19, 2014 16:30
Show Gist options
  • Select an option

  • Save prophile/9095693 to your computer and use it in GitHub Desktop.

Select an option

Save prophile/9095693 to your computer and use it in GitHub Desktop.
Conversion between Automaton and StateArrow
{-# LANGUAGE GADTs #-}
module StateAutoConversion(fromAutomaton, toAutomaton, StateResult(StateResult)) where
import Prelude hiding (id, (.))
import Control.Category
import Control.Arrow
import Control.Arrow.Transformer
import Control.Arrow.Operations
import Control.Arrow.Transformer.State
import Control.Arrow.Transformer.Automaton
data StateResult a b c where
StateResult :: StateArrow s a b c -> s -> StateResult a b c
fromAutomaton :: (ArrowApply a) => Automaton a b c -> StateResult a b c
fromAutomaton f = StateResult s f
where s = StateArrow $ rejig ^>> app
rejig ~(x, Automaton a) = (a, x)
toAutomaton :: (ArrowLoop a) => StateResult a b c -> Automaton a b c
toAutomaton (StateResult sa b) = loop (lift (runState sa) . second (delay b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment