caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Markus Mottl <markus.mottl@gmail.com>
To: Andreas Rossberg <rossberg@mpi-sws.org>
Cc: Gabriel Scherer <gabriel.scherer@gmail.com>,
	Julien Blond <julien.blond@gmail.com>,
	 David Allsopp <dra-news@metastack.com>,
	OCaml mailing-list <caml-list@inria.fr>
Subject: Re: [Caml-list] Empty polymorphic variant set
Date: Fri, 25 Nov 2016 10:42:27 -0500	[thread overview]
Message-ID: <CAP_800rMuXGVyVTtrgDtdP3jz13wQ+-VzCA7KnAb5pDYSb=_ag@mail.gmail.com> (raw)
In-Reply-To: <3AD797C1-47D8-4521-9CEF-03D12CDF6867@mpi-sws.org>

My preferred way of representing the empty type is as follows:

  type void = { void : 'a. 'a }

This allows you to do:

  type t = A | B of void

  let f = function
    | A -> "A"
    | B { void } -> void

This can come in handy for e.g. partially implementing type
definitions.  Instead of raising exceptions in branches you cannot
implement yet, you just simply return "void", which unifies with
anything.  Since there is provably no way to create a value that would
end up in that branch, this is safe.  Once you complete the argument
definition for tag B, the compiler will detect all code locations that
depend on that branch.

On Fri, Nov 25, 2016 at 8:52 AM, Andreas Rossberg <rossberg@mpi-sws.org> wrote:
>
>> On Nov 25, 2016, at 14:46 , Gabriel Scherer <gabriel.scherer@gmail.com> wrote:
>>
>> I would agree that OCaml lacks a convenient way to define the empty
>> type.
>
> Isn’t
>
>   type empty
>
> (as a definition) a sufficiently convenient way to define an empty type?
>
> /Andreas
>
>> (It used to be possible using the revised syntax, which uses
>> braces to delimit (non-polymorphic) variant definitions, but this was
>> ruled out by sanity checks introduced in OCaml 4.02).
>>
>> One way is to use GADTs to create an impossible type:
>>
>>  type 'a onlybool = Bool : bool onlybool
>>  type empty = int onlybool
>>
>>  let extract : ('a, empty) result -> 'a = function Ok x -> x
>>
>> Since 4.03 (April 2016), it is possible to explicitly write a
>> so-called "refutation case", of the form "<pattern> -> .", to say that
>> a given case cannot happen -- it is an error if the type-checker
>> cannot verify it:
>>
>>  https://caml.inria.fr/pub/docs/manual-ocaml/extn.html#sec241
>>
>>  let extract : ('a, empty) result -> 'a = function
>>    | Ok x -> x
>>    | Error _ -> .
>>
>> I would support the idea of having a built-in "empty" type to
>> represent a variant type with no constructor -- but then I am probably
>> biased in favor of the empty type.
>>
>>
>> On Fri, Nov 25, 2016 at 8:01 AM, Julien Blond <julien.blond@gmail.com> wrote:
>>> Yes, i knew the variant constructor but, somehow i didn't realize i was
>>> precisely using it for my mind was focused on the polymorphic variant list
>>> :)
>>>
>>> In fact, i wondered if a generic result type like this
>>>
>>> type ('a, 'b) result = Ok of 'a | Error of 'b
>>>
>>> that we can see in several library could be used to specify a "safe" result
>>> which could have type something like ('a, []) result. One could encode 'b as
>>> some error list at type level but it needs some complicated type management
>>> and i'm targeting OCaml beginners for which i just want them to be careful
>>> about non nominal results.
>>>
>>>
>>> 2016-11-25 12:22 GMT+01:00 David Allsopp <dra-news@metastack.com>:
>>>>
>>>> Julien Blond wrote:
>>>>> 2016-11-25 9:39 GMT+01:00 Julien Blond <mailto:julien.blond@gmail.com>:
>>>>> Hi,
>>>>> Let's try something :
>>>>> $ ocaml
>>>>>        OCaml version 4.03.0
>>>>>
>>>>> # let _ : [] list = [];;
>>>>> Characters 9-10:
>>>>> let _ : [] list = [];;
>>>>> Error: Syntax error
>>>>> # type empty = [];;
>>>>> type empty = []
>>>>> # let _ : empty list = [];;
>>>>> - : empty list = []
>>>>> #
>>>>>
>>>>> Does anyone know if there is a reason to forbid the empty polymorphic
>>>>> variant
>>>>> set in type expressions or if it's a bug ?
>>>>
>>>> As you've observed, [] is a variant constructor since 4.03.0 - see GPR#234
>>>> (https://github.com/ocaml/ocaml/pull/234). The GPR contains references and
>>>> comments as to the motivation for this.
>>>>
>>>> What's your desired use for the type of the non-extensible empty
>>>> polymorphic variant?
>>>>
>>>> Possibly related, you can define a general type for a list of polymorphic
>>>> variants:
>>>>
>>>> let (empty : [> ] list) = []
>>>>
>>>> or
>>>>
>>>> let (length : [> ] list -> int) = List.length;;
>>>> length [`Foo; `Bar];;
>>>> length [42];;
>>>>
>>>> if that's what you were after?
>>>>
>>>>
>>>> David
>>>
>>>
>>
>> --
>> 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
>
>
> --
> 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



-- 
Markus Mottl        http://www.ocaml.info        markus.mottl@gmail.com

  reply	other threads:[~2016-11-25 15:42 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-11-25  8:39 Julien Blond
2016-11-25  9:19 ` Ben Millwood
2016-11-25  9:20 ` Julien Blond
2016-11-25 11:22   ` David Allsopp
2016-11-25 13:01     ` Julien Blond
2016-11-25 13:46       ` Gabriel Scherer
2016-11-25 13:52         ` Andreas Rossberg
2016-11-25 15:42           ` Markus Mottl [this message]
2016-11-25 15:46           ` Gabriel Scherer
2016-11-25 15:59             ` Yaron Minsky
2016-11-25 16:42               ` Markus Mottl
2016-11-25 17:11                 ` Gabriel Scherer
2016-11-25 16:50           ` Stephen Dolan
2016-11-25 16:59             ` Jeremy Yallop

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='CAP_800rMuXGVyVTtrgDtdP3jz13wQ+-VzCA7KnAb5pDYSb=_ag@mail.gmail.com' \
    --to=markus.mottl@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=dra-news@metastack.com \
    --cc=gabriel.scherer@gmail.com \
    --cc=julien.blond@gmail.com \
    --cc=rossberg@mpi-sws.org \
    /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).