-module Sim (simulate, Circuit, simulateIO) where
+module Sim (simulate, SCircuit, Circuit, simulateIO, stateless) where
import Data.Typeable
simulate f input s = do
output = run f input s
-- A circuit with input of type a, state of type s and output of type b
-type Circuit a s b = a -> s -> (s, b)
+type SCircuit i s o = i -> s -> (s, o)
+type Circuit i o = i -> o
-run :: Circuit a s b -> [a] -> s -> [(s, b)]
+run :: SCircuit i s o -> [i] -> s -> [(i, o, s)]
run f (i:input) s =
- (s', o): (run f input s')
+ (i, o, s'): (run f input s')
where
(s', o) = f i s
run _ [] _ = []
-simulateIO :: (Read a, Show b, Show s) => Sim.Circuit a s b -> s -> IO()
+simulateIO :: (Read i, Show i, Show o, Show s) => SCircuit i s o -> s -> IO()
simulateIO c s = do
putStr "Initial State: "
putStr $ show s
putStr "\n\n"
runIO c s
-runIO :: (Read a, Show b, Show s) => Sim.Circuit a s b -> s -> IO()
+runIO :: (Read i, Show i, Show o, Show s) => SCircuit i s o -> s -> IO()
runIO f s = do
- putStr "\nInput: "
+ putStr "\nInput? "
line <- getLine
if (line == "") then
return ()
else
let i = (read line) in do
let (s', o) = f i s in do
- printOutput (s', o)
+ printOutput (i, o, s')
simulateIO f s'
-printOutput :: (Show s, Show o) => (s, o) -> IO ()
-printOutput (s, o) = do
- putStr "Output: "
+printOutput :: (Show i, Show o, Show s) => (i, o, s) -> IO ()
+printOutput (i, o, s) = do
+ putStr "Input: "
+ putStr $ show i
+ putStr "\nOutput: "
putStr $ show o
putStr "\nNew State: "
putStr $ show s
putStr "\n\n"
+
+-- Takes a stateless circuit and turns it into a stateful circuit (with an
+-- empty state) so it can be used in simulation
+stateless :: Circuit i o -> SCircuit i () o
+stateless f = \i s -> (s, f i)
+
-- vim: set ts=8 sw=2 sts=2 expandtab: