{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE Safe #-}--------------------------------------------------------------------------------moduleFSM.FiniteStateMachine(State,Transition,switch)where--------------------------------------------------------------------------------classStateawhereclass(Statea,Stateb)=>Transitionabwhere--------------------------------------------------------------------------------switch::Transitionab=>a->b--------------------------------------------------------------------------------switch=undefined
#!/usr/bin/envstack{- stack
--resolver lts-12.0
--install-ghc
script
--ghc-options -Werror
--ghc-options -Wall
--
-}--------------------------------------------------------------------------------{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE Safe #-}--------------------------------------------------------------------------------moduleMain(main)where--------------------------------------------------------------------------------importFSM.FiniteStateMachine(State,Transition,switch)---------------------------------------------------------------------------------- We define states and transitions into the type systemdataReddataYellowdataGreeninstanceShowRedwhereshow_="Red"instanceShowYellowwhereshow_="Yellow"instanceShowGreenwhereshow_="Green"instanceStateRedinstanceStateYellowinstanceStateGreeninstanceTransitionRedGreeninstanceTransitionGreenYellowinstanceTransitionYellowRed---------------------------------------------------------------------------------- We define our finite-state-machine (FSM)dataTrafficLight=StopRed|CautionYellow|GoGreenderivingShow--------------------------------------------------------------------------------main::IO()main=doputStrLn.ps$shows0putStrLn.ps$shows1putStrLn.ps$shows2putStrLn.ps$shows3wheres0@(Stops0')=Stop$undefineds1@(Gos1')=Go$switchs0'{- We can't implement code that goes from `Green` to `Red`:
> si@(Stop si') = Stop $ switch s1'
• No instance for (Transition Green Red)
arising from a use of ‘switch’
-}s2@(Cautions2')=Caution$switchs1's3=Stop$switchs2'ps=("The traffic light is: "++)