2 % (c) The University of Glasgow 2006
3 % (c) The AQUA Project, Glasgow University, 1993-1998
6 This is useful, general stuff for the Native Code Generator.
8 Provide trees (of instructions), so that lists of instructions
9 can be appended in linear time.
13 -- The above warning supression flag is a temporary kludge.
14 -- While working on this module you are encouraged to remove it and fix
15 -- any warnings in the module. See
16 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
21 nilOL, isNilOL, unitOL, appOL, consOL, snocOL, concatOL,
22 fromOL, toOL, foldrOL, foldlOL
30 = Many [a] -- Invariant: non-empty
31 | Two (OrdList a) -- Invariant: non-empty
32 (OrdList a) -- Invariant: non-empty
37 isNilOL :: OrdList a -> Bool
39 unitOL :: a -> OrdList a
40 snocOL :: OrdList a -> a -> OrdList a
41 consOL :: a -> OrdList a -> OrdList a
42 appOL :: OrdList a -> OrdList a -> OrdList a
43 concatOL :: [OrdList a] -> OrdList a
48 snocOL as b = Two as (One b)
50 consOL a bs = Two (One a) bs
51 concatOL aas = foldr appOL None aas
58 appOL as bs = Two as bs
60 mapOL :: (a -> b) -> OrdList a -> OrdList b
62 mapOL f (One x) = One (f x)
63 mapOL f (Two x y) = Two (mapOL f x) (mapOL f y)
64 mapOL f (Many xs) = Many (map f xs)
66 instance Functor OrdList where
69 foldrOL :: (a->b->b) -> b -> OrdList a -> b
71 foldrOL k z (One x) = k x z
72 foldrOL k z (Two b1 b2) = foldrOL k (foldrOL k z b2) b1
73 foldrOL k z (Many xs) = foldr k z xs
75 foldlOL :: (b->a->b) -> b -> OrdList a -> b
77 foldlOL k z (One x) = k z x
78 foldlOL k z (Two b1 b2) = foldlOL k (foldlOL k z b1) b2
79 foldlOL k z (Many xs) = foldl k z xs
81 fromOL :: OrdList a -> [a]
86 flat (One x) rest = x:rest
87 flat (Two a b) rest = flat a (flat b rest)
88 flat (Many xs) rest = xs ++ rest
90 toOL :: [a] -> OrdList a