2 % (c) The AQUA Project, Glasgow University, 1993-1998
5 This is useful, general stuff for the Native Code Generator.
7 Provide trees (of instructions), so that lists of instructions
8 can be appended in linear time.
13 nilOL, isNilOL, unitOL, appOL, consOL, snocOL, concatOL,
14 fromOL, toOL, foldrOL, foldlOL
23 | Two (OrdList a) (OrdList a)
28 isNilOL :: OrdList a -> Bool
30 unitOL :: a -> OrdList a
31 snocOL :: OrdList a -> a -> OrdList a
32 consOL :: a -> OrdList a -> OrdList a
33 appOL :: OrdList a -> OrdList a -> OrdList a
34 concatOL :: [OrdList a] -> OrdList a
38 snocOL as b = Two as (One b)
39 consOL a bs = Two (One a) bs
40 concatOL aas = foldr Two None aas
43 isNilOL (One _) = False
44 isNilOL (Two as bs) = isNilOL as && isNilOL bs
45 isNilOL (Many xs) = null xs
49 appOL as bs = Two as bs
51 mapOL :: (a -> b) -> OrdList a -> OrdList b
53 mapOL f (One x) = One (f x)
54 mapOL f (Two x y) = Two (mapOL f x) (mapOL f y)
55 mapOL f (Many xs) = Many (map f xs)
57 instance Functor OrdList where
60 foldrOL :: (a->b->b) -> b -> OrdList a -> b
62 foldrOL k z (One x) = k x z
63 foldrOL k z (Two b1 b2) = foldrOL k (foldrOL k z b2) b1
64 foldrOL k z (Many xs) = foldr k z xs
66 foldlOL :: (b->a->b) -> b -> OrdList a -> b
68 foldlOL k z (One x) = k z x
69 foldlOL k z (Two b1 b2) = foldlOL k (foldlOL k z b1) b2
70 foldlOL k z (Many xs) = foldl k z xs
72 fromOL :: OrdList a -> [a]
77 flat (One x) rest = x:rest
78 flat (Two a b) rest = flat a (flat b rest)
79 flat (Many xs) rest = xs ++ rest
81 toOL :: [a] -> OrdList a