{-# OPTIONS -XTemplateHaskell -XMultiParamTypeClasses #-} import UpdatableViewStub spec Q a = None | More a (Q a) deriving (Show, Eq) where isEmpty :: Q a -> Bool isEmpty None = True emptyQ :: Q a emptyQ = None enQ :: a -> Q a -> Q a enQ a None = More a None enQ a (More b x) = More b (enQ a x) deQ :: Q a -> Q a deQ (More a x) = x first :: Q a -> a first (More a x) = a impl Qi a = Qi [a] [a] deriving (Show, Eq) where enQ :: a -> Qi a -> Qi a enQ a (Qi x y) = Qi x (a:y) deQ :: Qi a -> Qi a deQ (Qi (a:x) y) = Qi x y deQ (Qi [] y) = deQ (Qi (reverse y) []) first :: Qi a -> a first (Qi (a:x) y) = a first (Qi [] y) = first (Qi (reverse y) []) abstractedBy view = $(foldT ''Qi) ( (idB <**> reverseB) >>>> appendB) >>>> l2l where l2l = $(foldT ''[]) ( $(toC 'None) <||> $(toC 'More) ) mapQ f None = None mapQ f (More a x) = More (f a) (mapQ f x) play1 0 q = first q play1 (n+1) q = do hd play1 n (enQ hd tl) where hd = first q tl = deQ q