annotate C-- calls that do not return
[ghc-hetmet.git] / utils / ext-core / Env.hs
1 {- Environments.
2    Uses lists for simplicity and to make the semantics clear.
3    A real implementation should use balanced trees or hash tables.
4 -}
5
6 module Env (Env,
7             eempty,
8             elookup,
9             eextend,
10             edomain,
11             efromlist,
12             efilter,
13             eremove)
14 where
15
16 import List
17
18 data Env a b = Env [(a,b)] 
19  deriving (Show)
20
21 eempty :: Env a b 
22 eempty = Env []
23
24 {- In case of duplicates, returns most recently added entry. -}
25 elookup :: (Eq a) => Env a b -> a -> Maybe b
26 elookup (Env l) k = lookup k l 
27
28 {- May hide existing entries. -}
29 eextend :: Env a b -> (a,b) -> Env a b
30 eextend (Env l) (k,d) = Env ((k,d):l)
31
32 edomain :: (Eq a) => Env a b -> [a]
33 edomain (Env l) = nub (map fst l)
34
35 {- In case of duplicates, first entry hides others. -}
36 efromlist :: [(a,b)] -> Env a b
37 efromlist l = Env l
38
39 eremove :: (Eq a)  => Env a b -> a -> Env a b
40 eremove (Env l) k = Env (filter ((/= k).fst) l)
41
42 efilter :: Env a b -> (a -> Bool) -> Env a b
43 efilter (Env l) p = Env (filter (p.fst) l)
44