diff --git a/src/Graph.hs b/src/Graph.hs index 313530d..219005c 100644 --- a/src/Graph.hs +++ b/src/Graph.hs @@ -2,6 +2,7 @@ {-# LANGUAGE DeriveFunctor #-} module Graph ( Graph(..) + , Vertex(..) , editLabel , follow , open diff --git a/src/Transducer.hs b/src/Transducer.hs index e3296e4..284c371 100644 --- a/src/Transducer.hs +++ b/src/Transducer.hs @@ -1,11 +1,13 @@ +{-# LANGUAGE NamedFieldPuns #-} module Transducer ( Transducer , fromList , run ) where -import Stream (Stream) -import Graph (Graph, editLabel, open, rewind, singleton, weave) +import Stream (Stream(..), merge) +import qualified Stream (empty) +import Graph (Graph(..), Vertex(..), editLabel, follow, open, rewind, singleton, weave) type Transducer input output = Graph input [output] @@ -19,5 +21,12 @@ add transducer (path, output) = fromList :: Ord input => [([input], output)] -> Transducer input output fromList = foldl add empty -run :: Ord input => Transducer input output -> Stream input -> Stream output -run = undefined +run :: (Ord input, Eq output) => Transducer input output -> Stream input -> Stream output +run transducer (Stream inputs) = foldl (\(Stream outputs) (input, stream) -> + case follow transducer input of + Nothing -> Stream.empty + Just newState@(Graph {focus}) -> + Stream ((emit stream <$> label focus) ++ outputs) `merge` run newState stream + ) Stream.empty inputs + where + emit stream output = (output, run (rewind transducer) stream)