caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Nils Becker <nils.becker@bioquant.uni-heidelberg.de>
Cc: OCaML List Mailing <caml-list@inria.fr>
Subject: Re: [Caml-list] GADT+polymorphic variants quirk
Date: Fri, 6 Jan 2017 10:39:23 +0900	[thread overview]
Message-ID: <85E36448-5530-4745-8A83-FD6D4CA4531B@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <40536bf7-5d7f-5528-80c2-45ed5d157d00@bioquant.uni-heidelberg.de>

On 2017/01/03 23:05, Nils Becker wrote:
> 
> hi,
> 
> i am the OP of the stackoverflow question referred to by anton.
> 
>> I would suggest avoid using polymorphic variants here, using rather
>> a simple encoding:
>> 
>>   type whole = Whole
>>   type general = General
>> 
>>   type _ num =
>>      | I : int -> _ num
>>      | F : float -> general num
> 
> i tried this simpler proposal and it does seem to work nicely. however,
> what i'm really interested in is encoding somewhat more elaborate
> subtyping relationships. for example, how would you handle the case
> where there is a 'rational' number type inbetween integers and reals? i
> don't see how your proposal can be generalized to that?
> 
> i tried to generalize the solution proposed by anton like this:
> 
> type integer = [ `Integer ]
> type rational = [ integer | `Rational ]
> type real = [ rational | `Real ]
> 
> type _ num =
>  | N : int -> [> integer ] num
>  | Q : int * int -> [> rational ] num
>  | R : float -> real num

You can encode any finite set type using core types.
The idea is just to use presence and absence.

type pre = Pre
type abs = Abs

type integer = pre * abs * abs  (* integer * rational * real *)
type rational = pre * pre * abs
type real = pre * pre * pre

However, in this case we are only taking about a linear inheritance hierarchy,
which can be expressed more compactly. For instance

type real = Real
type ’a rat = Rational of ‘a
type rational = int * int rat
type integer = int rat

In general, any linear hierarchy can be encoded using type level natural numbers

type zero = Zero
type ‘a succ = Succ of ‘a

which in this case would give

type real = zero
type rational = zero succ
type integer = zero succ succ

This scheme can be extended to any n-ary tree hierarchy, using a n-ary constructor in place of succ.

Jacques


  parent reply	other threads:[~2017-01-06  1:39 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-03 14:05 Nils Becker
2017-01-03 15:09 ` Anton Bachin
2017-01-03 15:22   ` Anton Bachin
2017-01-06  1:39 ` Jacques Garrigue [this message]
  -- strict thread matches above, loose matches on Subject: below --
2016-12-27 20:04 Anton Bachin
2017-01-02 13:51 ` Jacques Garrigue

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=85E36448-5530-4745-8A83-FD6D4CA4531B@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=nils.becker@bioquant.uni-heidelberg.de \
    /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).