Really revert all of the recent rotating changes.
[matthijs/master-project/cλash.git] / Sim.hs
1 module Sim (simulate, SCircuit, Circuit, simulateIO, stateless) where
2 import Data.Typeable
3
4 simulate f input s = do
5   putStr "Input: "
6   putStr $ show input
7   putStr "\nInitial State: "
8   putStr $ show s
9   putStr "\n\n"
10   foldl1 (>>) (map (printOutput) output)
11   where
12     output = run f input s
13
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
17
18 run :: SCircuit i s o -> [i] -> s -> [(i, o, s)]
19 run f (i:input) s =
20   (i, o, s'): (run f input s')
21   where
22     (s', o) = f i s
23 run _ [] _ = []
24
25 simulateIO :: (Read i, Show i, Show o, Show s) => SCircuit i s o -> s -> IO()
26 simulateIO c s = do
27   putStr "Initial State: "
28   putStr $ show s
29   putStr "\n\n"
30   runIO c s
31
32 runIO :: (Read i, Show i, Show o, Show s) => SCircuit i s o -> s -> IO()
33 runIO f s = do
34   putStr "\nInput? "
35   line <- getLine
36   if (line == "") then
37       return ()
38     else
39       let i = (read line) in do
40       let (s', o) = f i s in do
41       printOutput (i, o, s')
42       simulateIO f s'
43
44 printOutput :: (Show i, Show o, Show s) => (i, o, s) -> IO ()
45 printOutput (i, o, s) = do
46   putStr "Input: "
47   putStr $ show i
48   putStr "\nOutput: "
49   putStr $ show o
50   putStr "\nNew State: "
51   putStr $ show s
52   putStr "\n\n"
53
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)
58
59 -- vim: set ts=8 sw=2 sts=2 expandtab: