1 module Sim (simulate, SCircuit, Circuit, simulateIO, stateless) where
4 simulate f input s = do
7 putStr "\nInitial State: "
10 foldl1 (>>) (map (printOutput) output)
12 output = run f input s
14 -- A circuit with input of type a, state of type s and output of type b
15 type SCircuit i s o = i -> s -> (s, o)
16 type Circuit i o = i -> o
18 run :: SCircuit i s o -> [i] -> s -> [(i, o, s)]
20 (i, o, s'): (run f input s')
25 simulateIO :: (Read i, Show i, Show o, Show s) => SCircuit i s o -> s -> IO()
27 putStr "Initial State: "
32 runIO :: (Read i, Show i, Show o, Show s) => SCircuit i s o -> s -> IO()
39 let i = (read line) in do
40 let (s', o) = f i s in do
41 printOutput (i, o, s')
44 printOutput :: (Show i, Show o, Show s) => (i, o, s) -> IO ()
45 printOutput (i, o, s) = do
50 putStr "\nNew State: "
54 -- Takes a stateless circuit and turns it into a stateful circuit (with an
55 -- empty state) so it can be used in simulation
56 stateless :: Circuit i o -> SCircuit i () o
57 stateless f = \i s -> (s, f i)
59 -- vim: set ts=8 sw=2 sts=2 expandtab: