working on w3
authorCamil Staps <info@camilstaps.nl>
Sun, 22 Feb 2015 19:09:19 +0000 (20:09 +0100)
committerCamil Staps <info@camilstaps.nl>
Sun, 22 Feb 2015 19:09:19 +0000 (20:09 +0100)
week3/camil/.gitignore [new file with mode: 0644]
week3/camil/StdStack.dcl [new file with mode: 0644]
week3/camil/StdStack.icl [new file with mode: 0644]
week3/camil/StdStackTest.icl [new file with mode: 0644]

diff --git a/week3/camil/.gitignore b/week3/camil/.gitignore
new file mode 100644 (file)
index 0000000..3d25840
--- /dev/null
@@ -0,0 +1 @@
+/Clean System Files/
diff --git a/week3/camil/StdStack.dcl b/week3/camil/StdStack.dcl
new file mode 100644 (file)
index 0000000..8c861a1
--- /dev/null
@@ -0,0 +1,13 @@
+definition module StdStack\r
+\r
+:: Stack a\r
+\r
+newStack :: Stack a                        // lege stack\r
+push     ::  a  (Stack a) -> Stack a       // plaats nieuw element bovenop de stack\r
+pushes   :: [a] (Stack a) -> Stack a       // plaats elementen achtereenvolgens bovenop stack\r
+pop      ::     (Stack a) -> Stack a       // haal top element van stack\r
+popn     :: Int (Stack a) -> Stack a       // haal bovenste $n$ top elementen van stack\r
+top      ::     (Stack a) -> a             // geef top element van stack\r
+topn     :: Int (Stack a) -> [a]           // geef bovenste $n$ top elementen van stack\r
+elements ::     (Stack a) -> [a]           // geef alle elementen van stack\r
+count    ::     (Stack a) -> Int           // tel aantal elementen in stack\r
diff --git a/week3/camil/StdStack.icl b/week3/camil/StdStack.icl
new file mode 100644 (file)
index 0000000..25ff265
--- /dev/null
@@ -0,0 +1,32 @@
+implementation module StdStack\r
+\r
+import StdEnv\r
+\r
+:: Stack a :== [a]\r
+\r
+newStack :: (Stack a)\r
+newStack = []\r
+\r
+push :: a (Stack a) -> (Stack a)\r
+push a s = s ++ [a]\r
+\r
+pop\r
+\r
+Start  = ( "s0 = newStack = ",        s0,'\n'\r
+         , "s1 = push 1 s0 = ",       s1,'\n'\r
+         , "s2 = pushes [2..5] s1 = ",s2,'\n'\r
+         , "s3 = pop s2 = ",          s3,'\n'\r
+         , "s4 = popn 3 s3 = ",       s4,'\n'\r
+         , "s5 = top s4 = ",          s5,'\n'\r
+         , "s6 = topn 3 s2 = ",       s6,'\n'\r
+         , "s7 = elements s2 = ",     s7,'\n'\r
+         )\r
+where\r
+       s0 = newStack\r
+       s1 = push 1 s0\r
+       s2 = pushes [2..5] s1\r
+       s3 = pop s2\r
+       s4 = popn 3 s3\r
+       s5 = top s4\r
+       s6 = topn 3 s2\r
+       s7 = elements s2\r
diff --git a/week3/camil/StdStackTest.icl b/week3/camil/StdStackTest.icl
new file mode 100644 (file)
index 0000000..8127f53
--- /dev/null
@@ -0,0 +1,60 @@
+module StdStackTest\r
+\r
+/*     Test module StdStack\r
+       Voor werken met Gast: \r
+               (*) gebruik Environment 'Gast'\r
+               (*) zet Project Options op 'Basic Values Only' en '2M' Maximum Heap Size\r
+*/\r
+\r
+import gast\r
+import StdStack\r
+\r
+Start\r
+                                                       = testn 1000\r
+                                                               (\x n ->\r
+                                                                  newStack_is_empty               /\\r
+                                                                  stack_is_reverse        n       /\\r
+                                                                  pop_empty_is_ok                 /\\r
+                                                                  top_na_push             n x     /\\r
+                                                                  pop_na_push               x     /\\r
+                                                                  count_counts            n x     /\\r
+                                                                  pop_maakt_stack_korter  n       /\\r
+                                                                  True\r
+                                                               )\r
+\r
+newStack_is_empty                      :: Property\r
+newStack_is_empty                      = name "newStack_is_empty" (isEmpty (elements empty))\r
+\r
+stack_is_reverse                       :: Int -> Property\r
+stack_is_reverse n                     = name "stack_is_reverse"\r
+                                                         (elements (pushes [1..n`] newStack) == reverse [1..n`])\r
+where            n`                    = min (abs n) 100\r
+\r
+pop_empty_is_ok                                :: Property\r
+pop_empty_is_ok                                = name "pop_empty_is_ok" (count (pop empty) == 0)\r
+\r
+top_na_push                                    :: Int Int -> Property\r
+top_na_push x n                                = name "top_na_push"\r
+                                                         (top (push x (pushes [1..n`] newStack)) == x)\r
+where         n`                       = min (abs n) 100\r
+\r
+pop_na_push                                    :: Int -> Property\r
+pop_na_push a                          = name "pop_na_push"\r
+                              (top (pop (pop (pushes [a,b,c] newStack))) == a)\r
+where b                                                = a + a + one\r
+      c                                                = b + a + one\r
+\r
+count_counts                           :: Int Int -> Property\r
+count_counts n x                       = name "count_counts"\r
+                                                         (length (elements stack) == count stack)\r
+where stack                                    = pushes [1..n`] newStack\r
+      n`                                       = min (abs n) 100\r
+\r
+pop_maakt_stack_korter         :: Int -> Property\r
+pop_maakt_stack_korter n       = name "pop_maakt_stack_korter"\r
+                                                         (count stack == 0 || count (pop stack) == count stack - 1)\r
+where stack                                    = pushes [1..n`] newStack\r
+         n`                                    = min (abs n) 100\r
+\r
+empty                                          :: Stack Int\r
+empty                                          = newStack\r