caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Suggestion: Pervasives.identity
@ 2004-04-23 12:18 Martin Jambon
  2004-04-23 12:56 ` Xavier Leroy
  2004-04-23 15:48 ` Brian Hurt
  0 siblings, 2 replies; 6+ messages in thread
From: Martin Jambon @ 2004-04-23 12:18 UTC (permalink / raw)
  To: caml-list

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-list-request@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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Suggestion: Pervasives.identity
  2004-04-23 12:18 [Caml-list] Suggestion: Pervasives.identity Martin Jambon
@ 2004-04-23 12:56 ` Xavier Leroy
  2004-04-23 13:26   ` Martin Jambon
  2004-04-23 15:48 ` Brian Hurt
  1 sibling, 1 reply; 6+ messages in thread
From: Xavier Leroy @ 2004-04-23 12:56 UTC (permalink / raw)
  To: Martin Jambon; +Cc: caml-list

> 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

-------------------
To unsubscribe, mail caml-list-request@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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Suggestion: Pervasives.identity
  2004-04-23 12:56 ` Xavier Leroy
@ 2004-04-23 13:26   ` Martin Jambon
  0 siblings, 0 replies; 6+ messages in thread
From: Martin Jambon @ 2004-04-23 13:26 UTC (permalink / raw)
  To: caml-list

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

-------------------
To unsubscribe, mail caml-list-request@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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Suggestion: Pervasives.identity
  2004-04-23 12:18 [Caml-list] Suggestion: Pervasives.identity Martin Jambon
  2004-04-23 12:56 ` Xavier Leroy
@ 2004-04-23 15:48 ` Brian Hurt
  2004-04-23 16:26   ` Richard Jones
  2004-04-24  2:09   ` skaller
  1 sibling, 2 replies; 6+ messages in thread
From: Brian Hurt @ 2004-04-23 15:48 UTC (permalink / raw)
  To: Martin Jambon; +Cc: caml-list

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

-------------------
To unsubscribe, mail caml-list-request@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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Suggestion: Pervasives.identity
  2004-04-23 15:48 ` Brian Hurt
@ 2004-04-23 16:26   ` Richard Jones
  2004-04-24  2:09   ` skaller
  1 sibling, 0 replies; 6+ messages in thread
From: Richard Jones @ 2004-04-23 16:26 UTC (permalink / raw)
  Cc: caml-list

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

-------------------
To unsubscribe, mail caml-list-request@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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [Caml-list] Suggestion: Pervasives.identity
  2004-04-23 15:48 ` Brian Hurt
  2004-04-23 16:26   ` Richard Jones
@ 2004-04-24  2:09   ` skaller
  1 sibling, 0 replies; 6+ messages in thread
From: skaller @ 2004-04-24  2:09 UTC (permalink / raw)
  To: Brian Hurt; +Cc: 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:skaller@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



-------------------
To unsubscribe, mail caml-list-request@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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-04-24  2:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-23 12:18 [Caml-list] Suggestion: Pervasives.identity Martin Jambon
2004-04-23 12:56 ` Xavier Leroy
2004-04-23 13:26   ` Martin Jambon
2004-04-23 15:48 ` Brian Hurt
2004-04-23 16:26   ` Richard Jones
2004-04-24  2:09   ` skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).