Module Value.Match

Runtime parallel match.

type 'a t

This module can be used to handle several cases in parallel instead of using a sequence of nested matches or if/then/else chains.

The combinators in the module are designed to be used as follows:

let lift v = Match.(begin
    switch v @@
    case memory_load   (fun x -> `Load x)  @@
    case memory_store  (fun x -> `Store x) @@
    case register_read (fun x -> `Read x)  @@
    default (fun () -> `Unknown)
  end)

Note: in the example, the whole expression will build and then match. In case when performance matter, and when there is more then one match, it is recommended to evaluate a matching object first, and return a function, that matches values. For this there is a select combinator:

let lift =
  Match.(begin
      select @@
      case memory_load   (fun x -> `Load x)  @@
      case memory_store  (fun x -> `Store x) @@
      case register_read (fun x -> `Read x)  @@
      default (fun () -> `Unknown)
    end)
val switch : value -> 's t -> 's

switch x matcher applies matcher to value x

val select : 's t -> value -> 's

select matcher x applies matcher to value x. select is the same as Fn.flip switch.

val case : 'a tag -> ('a -> 's) -> 's t -> 's t

case tag action matcher adds an action to matcher that will be invoked for values with a a given tag

val default : (unit -> 's) -> 's t

default def creates an empty matcher with default handler def.