fork download
  1. data FSM state input output = FSM -- Input and output alphabet are constricted at compile time by datatype
  2. { start :: state, -- Start state
  3. trans :: state -> input -> state, -- Transition function
  4. accept :: [state], -- Set of accept states
  5. out :: OutFunc state input output -- Output function, either Moore or Mealy
  6. }
  7.  
  8. data OutFunc state input output
  9. = Moore (state -> output)
  10. | Mealy (state -> input -> output)
  11.  
  12. runFSM :: Eq state => FSM state input output -> [input] -> (state, Bool)
  13. runFSM (FSM start trans accept out) inputs = (finalState, elem finalState accept)
  14. where
  15. -- finalState = foldl' trans start input
  16. finalState = go inputs start
  17.  
  18. -- Output only changes when state changes occur
  19. --go :: [input] -> state -> state
  20. go [] acc = acc
  21. go (x : xs) currentState = go xs (trans currentState x)
  22.  
  23.  
  24.  
  25.  
  26. data Binary = Zero | One deriving (Enum, Eq, Show)
  27.  
  28. data EvenState = E0 | E1 deriving (Eq, Show)
  29.  
  30. isEven :: FSM EvenState Binary ()
  31. isEven = FSM
  32. { start = E0
  33. , trans = trans
  34. , accept = [E0]
  35. , out = Moore $ const ()
  36. }
  37. where
  38. trans :: EvenState -> Binary -> EvenState
  39. trans E0 _ = E1
  40. trans E1 _ = E0
  41.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:1: error: unknown type name ‘data’
 data FSM state input output = FSM -- Input and output alphabet are constricted at compile time by datatype
 ^~~~
prog.c:1:10: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘state’
 data FSM state input output = FSM -- Input and output alphabet are constricted at compile time by datatype
          ^~~~~
prog.c:1:10: error: unknown type name ‘state’
prog.c:8:1: error: unknown type name ‘data’
 data OutFunc state input output
 ^~~~
prog.c:8:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘state’
 data OutFunc state input output
              ^~~~~
prog.c:8:14: error: unknown type name ‘state’
prog.c:15:26: warning: missing terminating ' character
     -- finalState = foldl' trans start input
                          ^
prog.c:15:26: error: missing terminating ' character
     -- finalState = foldl' trans start input
                          ^~~~~~~~~~~~~~~~~~~
prog.c:37:3: error: unknown type name ‘where’
   where
   ^~~~~
prog.c:38:11: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘:’ token
     trans :: EvenState -> Binary -> EvenState
           ^
stdout
Standard output is empty