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 9246917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:36:21+00:00 2026-06-18T09:36:21+00:00

In OCaml, are there polymorphic function on generic maps? I.e: You can have: f

  • 0

In OCaml, are there polymorphic function on generic maps? I.e:

You can have: f : 'a list -> bool as the equivalent to bool f<T> ([T] x)

Can you also have something like: f : ('k,'v) map -> bool as the equivalent to bool f<K,V> (Map<K,V> x)?

If so, what is the correct syntax of the type signature, and how do you implement such a function (I ask since map is an abstract type and the only way to manipulate it is to use functions that you get by calling Map.Make on a concrete type)?

Thanks

  • 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-18T09:36:22+00:00Added an answer on June 18, 2026 at 9:36 am

    It depends on which type of generic map you use, and you have to make a distinction between the two parameters (the type of keys, and the type of values). If you use the Map.Make functor of the default library, you can be generic on the type of values, but the type of keys will be fixed (by the functor instantiation): for a given key module Key, the maps generated by module MyMap = Map.Make(Key) have only one parameter, 'v MyMap.t, so you can have f : 'v MyMap.t -> bool.

    There are other libraries that provide so-called “polymorphic” maps with a type of the form ('k, 'v) pmap that allow to be polymorphic over both the key and the value type. However, this generally comes at the cost of lesser static guarantees: if you were to change 'k in a way that does not preserve the ordering on keys, you would get inconsistent results in a way that cannot be detected at compilation time.

    Of course, you can implement additional function that work for any key type by enhancing the Map.Make functor itself (code untested):

    module MyMake(K : Map.OrderedType) = struct
      include Map.Make(K)
      (* after the "include", all the functions provided by `Map.Make`
         are available in the module *)
      let is_not_empty m = not (is_empty m)
    end
    
    module MyMap = MyMake(String)
    let foo = MyMap.is_not_empty (...)
    

    (Remark: If you don’t need a persistent collection (no copy/sharing/backtracking of values) and you don’t care about the worst-case complexity (no use exposed to deny of service attacks by an user), you may also use hashtables, with the Hashtbl module that provides a ('k, 'v) Hashtbl.t type. But the polymorphism on 'k is not really present: you don’t get any function that can make this parameter vary).

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

Sidebar

Related Questions

For debugging purposes I'd like to have a function in OCaml that converts to
There is series of functions like print_int, print_endline and Printf in OCaml. I can't
What OCaml libraries are out there that provide lazy list handling? I am looking
In Ocaml, is there easier ways to write a graphics-based toy programs like deminer
I have experience with OCaml. You had to write a stub for every function
I'm trying to write a polymorphic function, which needs to do something slightly different
If I have an object, how can I determine its type? (Is there an
I would like to write an OCaml module having a compile function which will
I have function 'my_a' in OCaml, which could have a very complicated return type:
Is there an OCaml equivalent to Haskell's pattern matching on an arbitrary number of

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.