-----------------------------------------------------------------------------
-- |
-- Module      :  Language.C.Data.RList
-- Copyright   :  (c) [2007..2008] Duncan Coutts, Benedikt Huber
-- License     :  BSD-style
-- Maintainer  : benedikt.huber@gmail.com
-- Stability   : experimental
-- Portability : ghc
--
-- Due to the way the grammar is constructed we very often have to build lists
-- in reverse. To make sure we do this consistently and correctly we have a
-- newtype to wrap the reversed style of list:
-----------------------------------------------------------------------------
module Language.C.Data.RList (
    RList,Reversed(..),
    empty,singleton,snoc,rappend,appendr,rappendr,rmap,reverse,
    viewr,
)
where
import Prelude hiding (reverse)
import qualified Data.List as List

newtype Reversed a = Reversed a
type RList a = Reversed [a]
empty :: Reversed [a]
empty :: forall a. Reversed [a]
empty = [a] -> Reversed [a]
forall a. a -> Reversed a
Reversed []

singleton :: a -> Reversed [a]
singleton :: forall a. a -> Reversed [a]
singleton a
x = [a] -> Reversed [a]
forall a. a -> Reversed a
Reversed [a
x]

snoc :: Reversed [a] -> a -> Reversed [a]
snoc :: forall a. Reversed [a] -> a -> Reversed [a]
snoc (Reversed [a]
xs) a
x = [a] -> Reversed [a]
forall a. a -> Reversed a
Reversed (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs)
infixl 5 `snoc`

rappend :: Reversed [a] -> [a] -> Reversed [a]
rappend :: forall a. Reversed [a] -> [a] -> Reversed [a]
rappend (Reversed [a]
xs) [a]
ys = [a] -> Reversed [a]
forall a. a -> Reversed a
Reversed ([a] -> [a]
forall a. [a] -> [a]
List.reverse [a]
ys [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
xs)

appendr :: [a] -> Reversed [a] -> Reversed [a]
appendr :: forall a. [a] -> Reversed [a] -> Reversed [a]
appendr [a]
xs (Reversed [a]
ys) = [a] -> Reversed [a]
forall a. a -> Reversed a
Reversed ([a]
ys [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a] -> [a]
forall a. [a] -> [a]
List.reverse [a]
xs)

rappendr :: Reversed [a] -> Reversed [a] -> Reversed [a]
rappendr :: forall a. Reversed [a] -> Reversed [a] -> Reversed [a]
rappendr (Reversed [a]
xs) (Reversed [a]
ys) = [a] -> Reversed [a]
forall a. a -> Reversed a
Reversed ([a]
ys [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a]
xs)

rmap :: (a -> b) -> Reversed [a] -> Reversed [b]
rmap :: forall a b. (a -> b) -> Reversed [a] -> Reversed [b]
rmap a -> b
f (Reversed [a]
xs) = [b] -> Reversed [b]
forall a. a -> Reversed a
Reversed ((a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
f [a]
xs)

reverse :: Reversed [a] -> [a]
reverse :: forall a. Reversed [a] -> [a]
reverse (Reversed [a]
xs) = [a] -> [a]
forall a. [a] -> [a]
List.reverse [a]
xs

viewr :: Reversed [a] -> (Reversed [a] , a)
viewr :: forall a. Reversed [a] -> (Reversed [a], a)
viewr (Reversed []) = [Char] -> (Reversed [a], a)
forall a. HasCallStack => [Char] -> a
error [Char]
"viewr: empty RList"
viewr (Reversed (a
x:[a]
xs)) = ([a] -> Reversed [a]
forall a. a -> Reversed a
Reversed [a]
xs, a
x)