-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue.hs
38 lines (30 loc) · 969 Bytes
/
Queue.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{-# LANGUAGE TemplateHaskell #-}
import Test.Rufous
import Prelude hiding (head, tail)
class Queue q where
empty :: q a
snoc :: a -> q a -> q a
tail :: q a -> q a
head :: q a -> a
-- define Shadow to guard against partial head/tail applications
newtype ShadowQueue a = ShadowQueue Int
deriving (Show)
instance Queue ShadowQueue where
empty = ShadowQueue 0
snoc _ (ShadowQueue i) = ShadowQueue (i + 1)
tail (ShadowQueue 0) = guardFailed
tail (ShadowQueue i) = ShadowQueue (i - 1)
head (ShadowQueue 0) = guardFailed
head _ = shadowUndefined
-- the actual implementation we want to benchmark
newtype ListQueue a = ListQueue [a]
deriving (Show)
instance Queue ListQueue where
empty = ListQueue []
snoc x (ListQueue xs) = ListQueue (xs ++ [x])
tail (ListQueue (_:xs)) = ListQueue xs
head (ListQueue (x:_)) = x
-- Generate Queue spec
makeADTSignature ''Queue
main :: IO ()
main = mainWith args{signature=_Queue}