summaryrefslogtreecommitdiff
path: root/assignment-2
diff options
context:
space:
mode:
authorCamil Staps2017-09-19 17:54:01 +0200
committerCamil Staps2017-09-19 17:54:01 +0200
commitfd3eca3371b0284553e8e0a855f2bb1c09165be9 (patch)
tree875225bccdb6af2bc93f43bab59806f666bd2672 /assignment-2
parentfoldr instead of seq+map (diff)
Start file assignment 2
Diffstat (limited to 'assignment-2')
-rw-r--r--assignment-2/serialize2start.icl87
1 files changed, 87 insertions, 0 deletions
diff --git a/assignment-2/serialize2start.icl b/assignment-2/serialize2start.icl
new file mode 100644
index 0000000..14e0037
--- /dev/null
+++ b/assignment-2/serialize2start.icl
@@ -0,0 +1,87 @@
+module serialize2start
+
+/*
+ Definition for assignment 2 in AFP 2017
+ Pieter Koopman pieter@cs.ru.nl
+ September 2017
+*/
+
+import StdEnv, StdMaybe
+
+class serialize a where
+ write :: a [String] -> [String]
+ read :: [String] -> Maybe (a,[String])
+
+instance serialize Bool where
+ write b c = [toString b:c]
+ read ["True":r] = Just (True,r)
+ read ["False":r] = Just (False,r)
+ read _ = Nothing
+
+instance serialize Int where
+ write i c = [toString i:c]
+ read [s:r]
+ # i = toInt s
+ | s == toString i
+ = Just (i,r)
+ = Nothing
+ read _ = Nothing
+
+// ---
+
+:: UNIT = UNIT
+:: EITHER a b = LEFT a | RIGHT b
+:: PAIR a b = PAIR a b
+:: CONS a = CONS String a
+
+// ---
+
+:: ListG a :== EITHER (CONS UNIT) (CONS (PAIR a [a]))
+
+instance serialize [a] | serialize a where // to be imporved
+ write l c = c
+ read l = Nothing
+
+:: Bin a = Leaf | Bin (Bin a) a (Bin a)
+:: BinG a :== EITHER (CONS UNIT) (CONS (PAIR (Bin a) (PAIR a (Bin a))))
+
+instance serialize (Bin a) | serialize a where // to be imporved
+ write a c = c
+ read l = Nothing
+
+instance == (Bin a) | == a where // better use the generic approach
+ (==) Leaf Leaf = True
+ (==) (Bin l a r) (Bin k b s) = l == k && a == b && r == s
+ (==) _ _ = False
+
+// ---
+
+Start =
+ [test True
+ ,test False
+ ,test 0
+ ,test 123
+ ,test -36
+ ,test [42]
+ ,test [0..4]
+ ,test [[True],[]]
+ ,test (Bin Leaf True Leaf)
+ ,test [Bin (Bin Leaf [1] Leaf) [2] (Bin Leaf [3] (Bin Leaf [4,5] Leaf))]
+ ,test [Bin (Bin Leaf [1] Leaf) [2] (Bin Leaf [3] (Bin (Bin Leaf [4,5] Leaf) [6,7] (Bin Leaf [8,9] Leaf)))]
+ ]
+
+test :: a -> ([String],[String]) | serialize, == a
+test a =
+ (if (isJust r)
+ (if (fst jr == a)
+ (if (isEmpty (tl (snd jr)))
+ ["Oke "]
+ ["Fail: not all input is consumed! ":snd jr])
+ ["Fail: Wrong result ":write (fst jr) []])
+ ["Fail: read result is Nothing "]
+ , ["write produces ": s]
+ )
+ where
+ s = write a ["\n"]
+ r = read s
+ jr = fromJust r