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