-----------------------------------------------------------------------------
-- |
-- 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 :: Reversed [a]
empty = [a] -> Reversed [a]
forall a. a -> Reversed a
Reversed []

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

snoc :: Reversed [a] -> a -> Reversed [a]
snoc :: Reversed [a] -> a -> Reversed [a]
snoc (Reversed xs :: [a]
xs) x :: 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 :: Reversed [a] -> [a] -> Reversed [a]
rappend (Reversed xs :: [a]
xs) ys :: [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 :: [a] -> Reversed [a] -> Reversed [a]
appendr xs :: [a]
xs (Reversed ys :: [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 :: Reversed [a] -> Reversed [a] -> Reversed [a]
rappendr (Reversed xs :: [a]
xs) (Reversed ys :: [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 :: (a -> b) -> Reversed [a] -> Reversed [b]
rmap f :: a -> b
f (Reversed xs :: [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 :: Reversed [a] -> [a]
reverse (Reversed xs :: [a]
xs) = [a] -> [a]
forall a. [a] -> [a]
List.reverse [a]
xs

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