Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumerator #72

Open
Ngoguey42 opened this issue Aug 26, 2021 · 1 comment
Open

Enumerator #72

Ngoguey42 opened this issue Aug 26, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@Ngoguey42
Copy link
Contributor

What would make my life easier is an enumerator of all the possible values (bit permutations) that the value of a typerepr can take.

This would look like deriving's enum and repr's random.

The enumerator could take the form of an OCaml sequence to deal with the fact that the user may be requesting an enumeration on something of almost infinite size (e.g. an int64).

type 'a enumerate = ('a -> 'a Seq.t) staged

val enumerate : 'a t -> 'a enumerate

My (simplified) use case, where such a feature would be greatly appreciated:

    module M = struct
      type tree =
        [ `Empty
        | `Remove ]
      [@@deriving repr]

      type tag =
        [ `Tree of tree
        | `Find_tree
        | `Add
        | `Init ]
      [@@deriving repr]
    end

    type t = [
      [ `M of M.tag
      | `Commit
      | `Block ]
    [@@deriving repr]

    let all = [
        `M (`Tree `Empty);
        `M (`Tree `Remove);
        `M `Find_tree;
        `M `Init;
        `Commit;
        `Block;
        (* This was cumbersome to write, I even missed one! *)
    ]
@Ngoguey42 Ngoguey42 added the enhancement New feature or request label Aug 26, 2021
@craigfe
Copy link
Member

craigfe commented Sep 6, 2021

Agreed. I've thought about writing one of these a few times, but haven't yet got around to it.

I think there's also a good use-case for having a deriver that goes straight to 'a list and works only for finite types, so that the generation work can be done at initialisation time. (I believe the JS convention for the finite enumeration is all, as you've written.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants