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

  • Home
  • SEARCH
  • 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 6888547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:02:02+00:00 2026-05-27T06:02:02+00:00

I’m trying to write a function that optionally take a function as argument let

  • 0

I’m trying to write a function that optionally take a function as argument

let xxx ?(extractor = (fun a -> a)) yyy = ...

This ends up having type:

val xxx: ?extractor:('a -> 'a) -> 'c -> ...

My intention is to have the extractor to be a function that extract information from a structure, so the return type can be anything, but I want the default to be identity function

I tried to change the signature of it in the mli as

val xxx: ?extractor:('a -> 'b) -> 'c -> ...

But it does not compile, saying that (‘a -> ‘a) is not compatible with (a’ -> ‘b). I find it strange that (‘a -> ‘a) is not subset of (a’ -> ‘b). Is there a syntax that I can put in the mli file to say (‘a -> *) ?

  • 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-05-27T06:02:03+00:00Added an answer on May 27, 2026 at 6:02 am

    In a functional language, an unspecified (variable) type you get back from a function basically has to appear somewhere in the type that you pass in. Otherwise, where would the value come from? So there are no (useful) functions of type 'a -> 'b.

    It might help to think about the types that you’re expecting to pass to your function, and how the extractor function would relate to them. It should be possible to have a fairly straightforward relation. Fighting with the type system usually means you’re trying to do something that is likely to fail unexpectedly, which is what the type system is trying to prevent.

    Edit:

    Maybe what I’m trying to say is that 'a and 'b, the input and output types of your extractor function, aren’t going to be arbitrary types. The input type 'a is probably related somehow to the type 'c. Similarly the result type 'b is probably going to be related to the return type of the function xxx as a whole. Without a little more info about these relationships, it’s hard to make suggestions.

    If you try to think of them as independent, arbitrary types you run into the parametric impossibility that I was talking about above. (It also requires advanced typing, rank 2 polymorphism I think it is.)

    As a simple example, say that 'a and 'c are the same type and that 'b is the return type of the function. Then your function would pretty much have to look like this:

    let xxx ?extractor s =
        match extractor with
        | Some f -> f s
        | None -> s
    

    This has the behavior you wanted: the default extractor function is the identity function. But this also forces the return type of f to be the same as its input type. So the type of xxx is:

    val xxx : ?extractor:('a -> 'a) -> 'a -> 'a
    

    Unless you want to get extremely fancy, there’s really no way around this, and I suspect a fancy solution would introduce complexities that would outweigh the convenience of having the optional parameter.

    Maybe it would work to have two functions. To continue the simplified example they would look like this:

    # let xxx extractor s = extractor s;;
    val xxx : ('a -> 'b) -> 'a -> 'b = <fun>
    # let xxx_id s = xxx (fun x -> x) s;;
    val xxx_id : 'a -> 'a = <fun>
    

    I think these have the types you want, and the only inconvenience is that you have two different names.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.