Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. count :: Eq a => a -> [a] -> Int
  2. count x = length . filter (==x)
  3.  
  4. cost :: Operation a -> Integer
  5. cost (Removed Arm) = 5000
  6. cost (Attached Arm) = 15000
  7. cost (Removed Hand) = 2000
  8. cost (Attached Hand) = 8000
  9. cost (Removed Leg) = 12000
  10. cost (Attached Leg) = 20000
  11.  
  12. type Cost = Integer
  13. type SSN = Text
  14. data Limb = Arm | Hand | Leg deriving (Show,Eq)
  15. data PatientStatus = NewPatient |
  16. PatientAdmitted Integer [Operation Limb] |
  17. PatientReleased |
  18. PatientDeceased
  19. deriving (Show,Eq)
  20. data Operation a = Removed a | Attached a deriving (Show,Eq)
  21. data Event a = Operation a | Release | Deceased deriving (Show,Eq)
  22. data Action = SendBill | SendCondolences
  23.  
  24. instance MealyInstance SSN PatientStatus (Event Limb) Action
  25.  
  26. transition (NewPatient, op@(Operation a)) = PatientAdmitted (cost op) [op]
  27.  
  28. transition (PatientAdmitted bill oes, Removed l)
  29. | count (Removed l) oes > 1 = error "Cannot remove limb that's not there anymore!"
  30. | otherwise = let newbill = bill + cost (Removed l) in
  31. (PatientAdmitted newbill $ Removed l : oes, [SendCondolences])
  32.  
  33. transition (PatientAdmitted bill oes, Attached l)
  34. | isNothing (find (Removed l) oes) = error "Cannot attach limb, there is no space!"
  35. | otherwise = let newbill = bill + cost (Removed l) in
  36. (PatientAdmitted newbill (Attached l) : oes, [])
  37.  
  38. transition (PatientAdmitted bill _oes, Release) = (PatientReleased, [SendBill bill])
  39. transition (PatientAdmitted bill _oes, Deceased) = (PatientDeceased, [SendCondolences, SendBill bill])
  40.  
  41. transition (NewPatient, _) = error "Patients are neither allowed to leave nor to die here before an operation"
  42. transition (PatientDeceased, _) = error "Operations on dead patients are not billable"
  43.  
  44.  
  45. effects :: Msg a -> IO Bool
  46. effects SendCondolences = putStrLn "not implemented" >> return True
  47. effects SendBill bill = charge bill :: IO Bool
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement