Module Stdlib320.Env

Basic functions

val empty : 'a env

Empty map

val is_empty : 'a env -> bool

Equivalent to (=) empty.

val mem : string -> 'a env -> bool

mem s e is

  • true if s is a key in e
  • false otherwise
val add : string -> 'a -> 'a env -> 'a env

add s x e is the same as e except that s maps to x.

val remove : string -> 'a env -> 'a env

remove s e is the same as e except that s does not map to anything.

val union : (string -> 'a -> 'a -> 'a option) -> 'a env -> 'a env -> 'a env

union f e1 e2 is the combination of e1 and e2, using f as the combiner. More formally, if mem s e1 and mem s e2 then

  • find s (union f e1 e2) = x if f (find s e1) (find s e2) = Some x
  • not (mem s (union f e1 e2) otherwise

Higher-order functions

val map : ('a -> 'b) -> 'a env -> 'b env

map f e is e with f applied to each value. More formally, find_opt s (map f e) = Option.map (find s e).

val filter : (string -> 'a -> bool) -> 'a env -> 'a env

filter f e is e with the only the mappings (k, v) such that f k v holds.

Finding

val find : string -> 'a env -> 'a

find s e is v if mem s e and s maps to v in e

Raises a Not_found exception otherwise.

val find_opt : string -> 'a env -> 'a option

Same as the previous function, but is None in the case of failure.

Converting

val to_list : 'a env -> (string * 'a) list

to_list e is a list of the key-value pairs in e. More formally, find_opt s e = assoc_opt s (to_list e).

val of_list : (string * 'a) list -> 'a env

of_list l is an map in which each key value pair in l is added to the empty map, in order from left to right. In particular, if a key is mapped to multiple values in l, then only the last appears in the of_list l.