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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:10:52+00:00 2026-05-27T11:10:52+00:00

I’m wondering why do we need function like %identity, which is the same as

  • 0

I’m wondering why do we need function like “%identity”, which is the same as let a = a. Is it going to improve performance by using it ?

I’m introducing phantom typing in my program, calling identity functions a ton times to convert types, curious if “%identity” can reduce a bit of overhead.

  • 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-27T11:10:52+00:00Added an answer on May 27, 2026 at 11:10 am

    The %identity function is part of the implementation, not part of the OCaml language. It tells the compiler (in essence) that there’s nothing to do to change the function’s parameter to its return value. In other words, it tells the compiler to keep using the same value but to change its idea of the type. If used incorrectly, it basically voids all the excellent safety guarantees of the OCaml type system. Also, of course, it’s not guaranteed to work in any other implementations of the language (including future releases of the INRIA compiler).

    The inlining capability of the OCaml compiler should already guarantee that no code is generated for identity functions. So I would recommend that you just keep using them.

    Update

    To answer an unrelated question in the comments…. Assume you have function composition and the identity function:

    let (<<) f g x = f (g x)
    let id x = x
    

    Then here are functions to add the elements of a list, to multiply the elements of a list, and to compose all the functions in a list:

    # let sum l = List.fold_right (+) l 0;;
    val sum : int list -> int = <fun>
    # let product l = List.fold_right ( * ) l 1;;
    val product : int list -> int = <fun>
    # let composition l = List.fold_right (<<) l id;;
    val composition : ('a -> 'a) list -> 'a -> 'a = <fun>
    

    Examples:

    # sum [2; 3; 5; 7];;
    - : int = 17
    # product [2; 4; 17];;
    - : int = 136
    # let mx = composition [(+) 1; ( * ) 10];;
    val mx : int -> int = <fun>
    # mx 2;;
    - : int = 21
    

    The point is that 0 is the identity for addition, 1 for multiplication, and id for function composition. id is useful all the time, just as 0 and 1 are.

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

Sidebar

Related Questions

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 have thousands of HTML files to process using Groovy/Java and I need to
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
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace

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.