caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] polymorphism in general
@ 2011-12-23  8:30 Mihamina Rakotomandimby
  2011-12-23 10:07 ` Martin Koch
  0 siblings, 1 reply; 3+ messages in thread
From: Mihamina Rakotomandimby @ 2011-12-23  8:30 UTC (permalink / raw)
  To: caml-list

Hello,

I have a question about polymorphism in general.

Let's take this example:
  val polymorph1 : 'a * 'b -> string = <fun>

Is it polymorph?
I have a doubt because the result is always a "string", not something 
related to either "'a" or "'b".

So another way to ask the question: is polymorphism only qualified by 
the parameter?

Thank you for your help.

-- 
RMA.

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

* Re: [Caml-list] polymorphism in general
  2011-12-23  8:30 [Caml-list] polymorphism in general Mihamina Rakotomandimby
@ 2011-12-23 10:07 ` Martin Koch
  2011-12-27  7:44   ` David Baelde
  0 siblings, 1 reply; 3+ messages in thread
From: Martin Koch @ 2011-12-23 10:07 UTC (permalink / raw)
  To: Mihamina Rakotomandimby; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1449 bytes --]

A function is polymorphic if it accepts _arguments_ with different types
(the return value is not considered). Thus

# let f x = x + 1;;
val f : int -> int = <fun>

is not polymorphic; it only accepts an integer.

Your function, on the other hand, is polymorphic. It accepts a two-tuple,
where any type may appear on either position, e.g.

# let f (a,b) = "hi there";;
val f : 'a * 'b -> string = <fun>

So I can use f on both, say a string and a bool pair, or an int and an
empty list.

# f("Blue Stilton", false);;
- : string = "hi there"
# f(42, []);;
- : string = "hi there"

/Martin

On Fri, Dec 23, 2011 at 9:30 AM, Mihamina Rakotomandimby <mihamina@rktmb.org
> wrote:

> Hello,
>
> I have a question about polymorphism in general.
>
> Let's take this example:
>  val polymorph1 : 'a * 'b -> string = <fun>
>
> Is it polymorph?
> I have a doubt because the result is always a "string", not something
> related to either "'a" or "'b".
>
> So another way to ask the question: is polymorphism only qualified by the
> parameter?
>
> Thank you for your help.
>
> --
> RMA.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa-roc.inria.fr/**wws/info/caml-list<https://sympa-roc.inria.fr/wws/info/caml-list>
> Beginner's list: http://groups.yahoo.com/group/**ocaml_beginners<http://groups.yahoo.com/group/ocaml_beginners>
> Bug reports: http://caml.inria.fr/bin/caml-**bugs<http://caml.inria.fr/bin/caml-bugs>
>
>

[-- Attachment #2: Type: text/html, Size: 2908 bytes --]

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

* Re: [Caml-list] polymorphism in general
  2011-12-23 10:07 ` Martin Koch
@ 2011-12-27  7:44   ` David Baelde
  0 siblings, 0 replies; 3+ messages in thread
From: David Baelde @ 2011-12-27  7:44 UTC (permalink / raw)
  To: Martin Koch; +Cc: Mihamina Rakotomandimby, caml-list

Hi,

I would say that any value whose type contains a generalized type
variable ('a, 'b, etc.) is polymorphic.

The empty list (of type 'a list) is polymorphic:
  let empty = [] in (1::empty, "2"::empty)

And so is the function always returning the empty list, even though it
has no polymorphic argument:
  let empty () = []

My 2 cents,
-- 
David

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

end of thread, other threads:[~2011-12-27  7:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-23  8:30 [Caml-list] polymorphism in general Mihamina Rakotomandimby
2011-12-23 10:07 ` Martin Koch
2011-12-27  7:44   ` David Baelde

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).