summaryrefslogtreecommitdiff
path: root/assignment-12/cashModel.icl
diff options
context:
space:
mode:
authorCamil Staps2017-12-11 14:40:48 +0100
committerCamil Staps2017-12-11 14:42:07 +0100
commitf4784a9f4ce7b1a9cb6b0898a191c870f640243f (patch)
tree902e7971f50a63e395439377d51b0ef7a2f318b1 /assignment-12/cashModel.icl
parentStudent numbers (diff)
Template assignment 12
Diffstat (limited to 'assignment-12/cashModel.icl')
-rw-r--r--assignment-12/cashModel.icl48
1 files changed, 48 insertions, 0 deletions
diff --git a/assignment-12/cashModel.icl b/assignment-12/cashModel.icl
new file mode 100644
index 0000000..ed0174a
--- /dev/null
+++ b/assignment-12/cashModel.icl
@@ -0,0 +1,48 @@
+implementation module cashModel
+/*
+ Pieter Koopman, Radboud University, 2017
+ pieter@cs.ru.nl
+ Advanced programming
+
+ A simple state model for an automated cash register
+*/
+
+import StdEnv, GenEq
+
+class euro a :: a -> Euro
+instance euro Product
+where
+ euro Pizza = euro (4,99)
+ euro Beer = euro (0,65)
+ euro _ = euro 1
+
+instance euro Int where euro e = {euro = e, cent = 0}
+instance euro (Int, Int) where euro (e,c) = {euro = e, cent = c}
+instance euro [e] | euro e where euro l = sum (map euro l)
+instance euro Euro where euro e = e
+
+instance + Euro
+where
+ + x y = {euro = c / 100, cent = (abs c) rem 100}
+ where
+ c = (x.euro + y.euro) * 100 + sign x.euro * x.cent + sign y.euro * y.cent
+
+instance - Euro
+where
+ - x y = {euro = c / 100, cent = (abs c) rem 100}
+ where
+ c = (x.euro - y.euro) * 100 + sign x.euro * x.cent - sign y.euro * y.cent
+
+instance zero Euro where zero = {euro = 0, cent = 0}
+derive gEq Euro, Product
+instance == Product where (==) p q = p === q
+instance == Euro where (==) p q = p === q
+
+instance ~ Euro where ~ e = {e & euro = ~e.euro}
+
+model :: [Product] Action -> ([Product],[Euro])
+model list (Add p) = ([p:list],[euro p])
+model list (Rem p)
+| isMember p list = (removeMember p list,[~ (euro p)])
+| otherwise = (list,[])
+model list Pay = ([],[euro list])