Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Suggestion: Pervasives.identity

64 views
Skip to first unread message

Martin Jambon

unread,
Apr 23, 2004, 8:20:45 AM4/23/04
to caml...@inria.fr
Hello,

Is there a good why there is no predefined identity function?
(fun x -> x) is sometimes less readable, and seems to be not
compiled (yet?) as this black magic:
external identity : 'a -> 'a : "%identity"

In the same style, we already have Pervasives.ignore, so why not
Pervasives.identity?


Martin

-------------------
To unsubscribe, mail caml-lis...@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

Xavier Leroy

unread,
Apr 23, 2004, 8:57:37 AM4/23/04
to Martin Jambon, caml...@inria.fr
> Is there a good why there is no predefined identity function?

A good reason, yes: it's generally useless. There are no predefined S
and K combinators either :-)

> (fun x -> x) is sometimes less readable, and seems to be not
> compiled (yet?) as this black magic:
> external identity : 'a -> 'a : "%identity"

Even if you define
external identity: 'a -> 'a = "%identity"
references to "identity" will be compiled like "fun x -> x".
It's only direct applications of "identity", e.g. "identity 3", that
would be more efficient than with the definition
let identity x = x
But I hope your programs don't contain calls like "identity x"...

> In the same style, we already have Pervasives.ignore, so why not
> Pervasives.identity?

Because Pervasives.ignore is very useful and commonly used to deal
with the "should have type unit" warning.

- Xavier Leroy

Martin Jambon

unread,
Apr 23, 2004, 9:28:44 AM4/23/04
to caml...@inria.fr
On Fri, 23 Apr 2004, Xavier Leroy wrote:

> Even if you define
> external identity: 'a -> 'a = "%identity"
> references to "identity" will be compiled like "fun x -> x".
> It's only direct applications of "identity", e.g. "identity 3", that
> would be more efficient than with the definition
> let identity x = x
> But I hope your programs don't contain calls like "identity x"...

My problem was:

(* something.mli *)
type key

(* something.ml *)
type key = int
let key_of_int = identity (** what should I write here? **)

(* other.ml *)
let key = Something.key_of_int 1


Sure, there are more important problems in the world...


Martin

Brian Hurt

unread,
Apr 23, 2004, 11:45:48 AM4/23/04
to Martin Jambon, caml...@inria.fr
On Fri, 23 Apr 2004, Martin Jambon wrote:

> Hello,
>
> Is there a good why there is no predefined identity function?
> (fun x -> x) is sometimes less readable, and seems to be not
> compiled (yet?) as this black magic:
> external identity : 'a -> 'a : "%identity"
>
> In the same style, we already have Pervasives.ignore, so why not
> Pervasives.identity?
>
>

Possibly stupid question: what use would this function be?

--
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
- Gene Spafford
Brian

skaller

unread,
Apr 24, 2004, 3:06:09 PM4/24/04
to Brian Hurt, Martin Jambon, caml-list
On Sat, 2004-04-24 at 01:48, Brian Hurt wrote:
> On Fri, 23 Apr 2004, Martin Jambon wrote:
> >
> > In the same style, we already have Pervasives.ignore, so why not
> > Pervasives.identity?

> Possibly stupid question: what use would this function be?

I use it with higher order functions. For example
I have a function:

type typed_expr = expr_t * type_t
map_typed_expr:
(expr_t -> expr_t) ->
(type_t -> type_t) ->
typed_expr_t -> typed_expr_t

Sometimes I wish to map only the typing in an expression,
and sometimes only the expression terms not the typing.
So I pass 'identity' as one of the arguments.

An identity function would also be useful in machine
generated code in a situation similar to the above
where the HOF was implemented in the code generator,
and you don't want to specialise the generator
but would rather the target compiler optimise
the application of 'identity' away.

So it is 'useful' -- but this is no argument
it should be in Pervasives.

--
John Skaller, mailto:ska...@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net

Richard Jones

unread,
Apr 24, 2004, 3:12:12 PM4/24/04
to caml...@inria.fr
On Fri, Apr 23, 2004 at 10:48:56AM -0500, Brian Hurt wrote:
> Possibly stupid question: what use would this function be?

I've had to define an identity function from time to time. Most
recently I wanted to define a function for printing labels on charts:

let plot ?(labels = identity) data =
(* ... *)

Called as:

plot ~labels:string_of_int data

if the label was an int instead of the default string. There was a
thread on this on ocaml-beginners I think.

Rich.

--
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
http://www.winwinsales.co.uk/ - CRM improvement consultancy

0 new messages