caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] some beautiful OCaml code
@ 2013-01-09  3:48 Francois Berenger
  2013-01-09  8:38 ` David MENTRE
  0 siblings, 1 reply; 6+ messages in thread
From: Francois Berenger @ 2013-01-09  3:48 UTC (permalink / raw)
  To: caml-list

The code is here:

https://raw.github.com/orbitz/blog_post_src/master/intro_return_t/ex5.ml

There is a full blog post about it there:

http://functional-orbitz.blogspot.se/2013/01/introduction-to-resultt-vs-exceptions.html

Regards,
F.

PS: I'm not the author of this beauty

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

* Re: [Caml-list] some beautiful OCaml code
  2013-01-09  3:48 [Caml-list] some beautiful OCaml code Francois Berenger
@ 2013-01-09  8:38 ` David MENTRE
  2013-01-09 10:06   ` Malcolm Matalka
                     ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: David MENTRE @ 2013-01-09  8:38 UTC (permalink / raw)
  To: Francois Berenger; +Cc: caml-list

Hello OCaml experts,

2013/1/9 Francois Berenger <berenger@riken.jp>:
> There is a full blog post about it there:
>
> http://functional-orbitz.blogspot.se/2013/01/introduction-to-resultt-vs-exceptions.html

Regarding this blog post, the final code is using Polymorphic Variants
(http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html#toc36).
E.g.
"""
    | _ ->
      Error (`Bad_line s)
"""

I never fully grasped polymorphic variants compared to regular ones
but I always had the feeling the polymorphic variants where less safe
that variants because they would allow more possibility to mix
unrelated things[1].

Are the use of polymorphic variant mandatory to write code
Return-Value-style code or can regular variants be used?

Best regards,
david

[1] Of course this ability is the very thing that is of interest to
people using polymorphic variants.

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

* Re: [Caml-list] some beautiful OCaml code
  2013-01-09  8:38 ` David MENTRE
@ 2013-01-09 10:06   ` Malcolm Matalka
  2013-01-10  8:13     ` David MENTRE
  2013-01-09 13:15   ` AW: " Gerd Stolpmann
  2013-01-09 14:15   ` Kristopher Micinski
  2 siblings, 1 reply; 6+ messages in thread
From: Malcolm Matalka @ 2013-01-09 10:06 UTC (permalink / raw)
  To: David MENTRE; +Cc: Francois Berenger, caml-list

Hey, author here,

The problem polymorphic variants are solving here is that if you want to
sequence unrelated functions but return their errors, you have to join
their return types with the return types of the function that is calling
them.  Polymorphic variants basically do this for you without every
function defining its own error return variant.

This, so far, is the only time I have found polymorphic variants the
best solution for a problem in Ocaml.

/Malcolm

David MENTRE <dmentre@linux-france.org> writes:

> Hello OCaml experts,
>
> 2013/1/9 Francois Berenger <berenger@riken.jp>:
>> There is a full blog post about it there:
>>
>> http://functional-orbitz.blogspot.se/2013/01/introduction-to-resultt-vs-exceptions.html
>
> Regarding this blog post, the final code is using Polymorphic Variants
> (http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html#toc36).
> E.g.
> """
>     | _ ->
>       Error (`Bad_line s)
> """
>
> I never fully grasped polymorphic variants compared to regular ones
> but I always had the feeling the polymorphic variants where less safe
> that variants because they would allow more possibility to mix
> unrelated things[1].
>
> Are the use of polymorphic variant mandatory to write code
> Return-Value-style code or can regular variants be used?
>
> Best regards,
> david
>
> [1] Of course this ability is the very thing that is of interest to
> people using polymorphic variants.

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

* AW: [Caml-list] some beautiful OCaml code
  2013-01-09  8:38 ` David MENTRE
  2013-01-09 10:06   ` Malcolm Matalka
@ 2013-01-09 13:15   ` Gerd Stolpmann
  2013-01-09 14:15   ` Kristopher Micinski
  2 siblings, 0 replies; 6+ messages in thread
From: Gerd Stolpmann @ 2013-01-09 13:15 UTC (permalink / raw)
  To: caml-list

Am 09.01.2013 09:38:51 schrieb(en) David MENTRE:
> Hello OCaml experts,
> 
> 2013/1/9 Francois Berenger <berenger@riken.jp>:
> > There is a full blog post about it there:
> >
> >  
> http://functional-orbitz.blogspot.se/2013/01/introduction-to-resultt-vs-exceptions.html
> 
> Regarding this blog post, the final code is using Polymorphic Variants
> (http://caml.inria.fr/pub/docs/manual-ocaml/manual006.html#toc36).
> E.g.
> """
>     | _ ->
>       Error (`Bad_line s)
> """
> 
> I never fully grasped polymorphic variants compared to regular ones
> but I always had the feeling the polymorphic variants where less safe
> that variants because they would allow more possibility to mix
> unrelated things[1].

That's exactly the point: Polyvariants allow you to mix unrelated  
things. I don't think, though, that they are unsafer, because you get  
help from the compiler to keep these things nevertheless separated. You  
need to be aware what can happen, and here and there it is helpful to  
add a type hint or a coercion to control typing.

> Are the use of polymorphic variant mandatory to write code
> Return-Value-style code or can regular variants be used?

No. You can also use normal variants, but of course you need then a  
declaration like

type error = Bad_line of string | Bad_name of string | ...

before using it. Also, function composition can be very painful.  
Imagine you wrote two modules M1 and M2 in this style, and each module  
defines an error type. Now you want to call functions from both modules  
at one place, and run into the problem that you get [Error e1] values  
with [e1:M1.error_type] and [Error e2] values with [e2:M2.error_type].  
There is no simple way to mix that. Probably you need to do

type error = M1_error of M1.error | M2_error of M2.error

which is kind of inelegant. Polyvariants, in contrast, allow you to  
directly unite the error types as long as the tags are not  
contradictory. This means function composition as again as easy as if  
you used exceptions for error reporting.

Gerd

> Best regards,
> david
> 
> [1] Of course this ability is the very thing that is of interest to
> people using polymorphic variants.
> 
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 
> 

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
Creator of GODI and camlcity.org.
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------

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

* Re: [Caml-list] some beautiful OCaml code
  2013-01-09  8:38 ` David MENTRE
  2013-01-09 10:06   ` Malcolm Matalka
  2013-01-09 13:15   ` AW: " Gerd Stolpmann
@ 2013-01-09 14:15   ` Kristopher Micinski
  2 siblings, 0 replies; 6+ messages in thread
From: Kristopher Micinski @ 2013-01-09 14:15 UTC (permalink / raw)
  To: David MENTRE; +Cc: Francois Berenger, caml-list

On Wed, Jan 9, 2013 at 3:38 AM, David MENTRE <dmentre@linux-france.org> wrote:
> Hello OCaml experts,
>
> 2013/1/9 Francois Berenger <berenger@riken.jp>:
> I never fully grasped polymorphic variants compared to regular ones
> but I always had the feeling the polymorphic variants where less safe
> that variants because they would allow more possibility to mix
> unrelated things[1].
>


I would say that it's not that polymorphic variants are less safe, but
they can occasionally be more painful, because of the explicit type
signatures, and because of type inference you can end up with some
ugly errors.  It's typically recommended that when you use polymorphic
variants you use explicit annotations.

I suppose they could be unsafe if, say, you had two similarly named
constructors with the same signature that had different semantics
between different modules.  But that would probably be bad style to
begin with.

There's a StackOverflow guide on when to use polymorphic variants, I
agree with the answer:

http://stackoverflow.com/questions/9367181/variants-or-polymorphic-variants

Kris

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

* Re: [Caml-list] some beautiful OCaml code
  2013-01-09 10:06   ` Malcolm Matalka
@ 2013-01-10  8:13     ` David MENTRE
  0 siblings, 0 replies; 6+ messages in thread
From: David MENTRE @ 2013-01-10  8:13 UTC (permalink / raw)
  To: Malcolm Matalka; +Cc: Francois Berenger, caml-list

Hello,

2013/1/9 Malcolm Matalka <mmatalka@gmail.com>:
> The problem polymorphic variants are solving here is that if you want to
> sequence unrelated functions but return their errors, you have to join
> their return types with the return types of the function that is calling
> them.  Polymorphic variants basically do this for you without every
> function defining its own error return variant.

Thank you Malcolm, Gerd and Kristopher for the helpful comments.

Best regards,
david

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

end of thread, other threads:[~2013-01-10  8:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-09  3:48 [Caml-list] some beautiful OCaml code Francois Berenger
2013-01-09  8:38 ` David MENTRE
2013-01-09 10:06   ` Malcolm Matalka
2013-01-10  8:13     ` David MENTRE
2013-01-09 13:15   ` AW: " Gerd Stolpmann
2013-01-09 14:15   ` Kristopher Micinski

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