caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: caml-list <caml-list@inria.fr>
Subject: AW: [Caml-list] some beautiful OCaml code
Date: Wed, 09 Jan 2013 14:15:38 +0100	[thread overview]
Message-ID: <1357737338.28639.11@samsung> (raw)
In-Reply-To: <CAC3Lx=bkQMo50rs=zjUpz==y_RzUffLCCDX5wcNK0Q=Mn_j5xQ@mail.gmail.com>

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

  parent reply	other threads:[~2013-01-09 13:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-09  3:48 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   ` Gerd Stolpmann [this message]
2013-01-09 14:15   ` Kristopher Micinski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1357737338.28639.11@samsung \
    --to=info@gerd-stolpmann.de \
    --cc=caml-list@inria.fr \
    --cc=gerd@edgespring.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).