Variant tags as parameters

For example, lets say we have a 4 state monad with various operations and main type as

type ('a, 'b, 'c) t -> Result of 'a | Minor_problem of 'b | Major_problem of 'c | Disaster

let (>>=) m f g h =
    match m with
    | Result a -> f a
    | Minor_problem b -> g b
    | Major_problem c -> h c
    | Disaster -> Disaster

It is useful as is, but I want more logical separation for various parts of my program, so it would be nice to have the exactly same monad with the same functions and signature, but only different would be a tag names. Is that even possible? To parametrize them? So to make a new monad like below, without copy-pasting all the code and substituting the tags:

type ('a, 'b, 'c) t -> NewResult of 'a | Not_a_problem of 'b | Catastrophe of 'c | Alien_invasion

let (>>=) m f g h =
    match m with
    | NewResult a -> f a
    | Not_a_problem b -> g b
    | Catastrophe c -> h c
    | Alien_invasion ->  Alien_invasion

This isn’t currently possible, but there’s some discussion of extending the language to make it possible (and of the technical challenges involved) here:

1 Like