Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8565503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:26:28+00:00 2026-06-11T17:26:28+00:00

I have an ocaml type : type t = A | B | …

  • 0

I have an ocaml type :

type t = A | B |  ...

and a function to print things about that type :

let pp_t fmt x = match x with 
  | A -> Format.fprintf fmt "some nice explanations about A"
  | B -> Format.fprintf fmt "some nice explanations about B"
  | ...

How could I write a function to print all the explanations ? Something equivalent to :

let pp_all_t fmt = 
   Format.fprintf fmt A;
   Format.fprintf fmt B;
   ...

but that would warn me if I forget to add a new constructor.
It would be even better to have something that automatically build that function,
because my problem is that t is quiet big and changes a lot.

I can’t imagine how I can “iterate” on the type constructors, but maybe there is a trick…

EDIT: What I finally did is :

type t = A | B |  ... | Z
let first_t = A
let next_t = function A -> B | B -> C | ... | Z -> raise Not_found
let pp_all_t fmt = 
  let rec pp x = pp_t fmt x ; try let x = next_t x in pp x with Not_found -> ()
  in pp first_t

so when I update t, the compiler warns me that I have to update pp_t and next_t, and pp_all_t doesn’t have to change.
Thanks to you all for the advices.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:26:29+00:00Added an answer on June 11, 2026 at 5:26 pm

    To solve your problem for a complicated and evolving type, in practice I would probably write an OCaml program that generates the code from a file containing a list of the values and the associated information.

    However, if you had a function incr_t : t -> t that incremented a value of type t, and if you let the first and last values of t stay fixed, you could write the following:

    let pp_all_t fmt =
        let rec loop v =
            pp_t fmt v;
            if v < Last_t then loop (incr_t v)
         in
             loop First_t
    

    You can’t have a general polymorphic incr_t in OCaml, because it only makes sense for types whose constructors are nullary (take no values). But you can write your own incr_t for any given type.

    This kind of thing is handled quite nicely in Haskell. Basically, the compiler will write some number of functions for you when the definitions are pretty obvious. There is a similar project for OCaml called deriving. I’ve never used it, but it does seem to handle the problem of enumerating values.

    Since you say you want a “trick”, if you don’t mind using the unsafe part of OCaml (which I personally do mind), you can write incr_t as follows:

    let incr_t (v: t) : t =
        (* Please don't use this trick in real code :-) !  See discussion below.
         *)
        if t < Last_t then
            Obj.magic (Obj.magic v + 1)
        else
            failwith "incr_t: argument out of range"
    

    I try to avoid this kind of code if at all possible, it’s too dangerous. For example, it will produce nonsense values if the type t gets constructors that take values. Really it’s “an accident waiting to happen”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that generates Fibonacci numbers: let rec fib n = match
For debugging purposes I'd like to have a function in OCaml that converts to
I have an Ocaml function that is giving me errors. What I am trying
I have a help function in my Ocaml project that helps to append a
I am writing a ocaml project, in which I have a function that replace
I have experience with OCaml. You had to write a stub for every function
I have a question about ocaml, i'm a beginner :-) Here is an example
As a beginner in Ocaml, I have this current working code: ... let ch_in
I just learned that OCAML have to have a . postfix for doing float
Suppose that I have the following record: type t = {a:int} In order to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.