Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques Carette <carette@mcmaster.ca>
To: OCaml <caml-list@inria.fr>
Subject: More fun with variants and constraints
Date: Wed, 20 Aug 2008 15:17:16 -0400	[thread overview]
Message-ID: <48AC6DBC.2040508@mcmaster.ca> (raw)

[One of my previous difficulties was alleviated by putting variance 
annotations on my variables, which was not at all obvious from the 
documentation, other than a vague hint that these are not automatically 
inferred; I put annotation everywhere in the code below, but +'d is the 
only one that matters].

In my actual code, I now have some routines that take a huge amount of 
time to compile (I gave up after 30 minutes).  Looking into why that 
might be, I have produced the following code which shows the issue:

type +'a a0 = [`A of 'a ]
type +'a a1 = [`B of 'a ]
type +'a a2 = [`C of 'a ]
type    d = [`D of int ]

type +'a f1 = ['a a0 | d]
type +'a f2 = ['a a1 | d]

type (+'a,+'b,+'c) e = ['b a0 | 'a a1 | 'c a2 | d]

type (+'a,+'b,+'c) c = ('a,'b,'c) e
    constraint 'a = [< 'b f1]
    constraint 'b = [< 'a f2]
    constraint 'c = [< 'c a2 ]

type +'d cc = 'd
    constraint 'd = [< ('a,'b,'c) c]

(* This craps out with 'cc' being an inexact variant
let is_cc = function
    | #cc -> true
    | _   -> false 
    *)

let id_cc (x: 'd cc) = x

let build (i:int) : 'd cc = `A (`B (`D i))

let a1 = build 1
;;

The type of a1 is reported as
val a1 :
  [< `A of [< ([< 'a f1 > `D ] as 'b) f2 > `B ] as 'a
   | `B of 'b
   | `C of
       [< 'c a2 ] as 'c &
       [< 'd a2 ] as 'd &
       [< 'e a2 ] as 'e &
       [< 'f a2 ] as 'f
   | `D of int
   > `A ]

What is puzzling is the type in `C.  Why are there 4 (identical!) 
possibilities?  I played around some, and the 4 is equal to the number 
of constraints in the program.

The issue is, in my 'real' code, there are 33 constraints.  Simple 
values now have types that take pages and pages of output.

How do I still get all the polymorphism that I want without getting this 
kind of blow-up?

Jacques Carette


                 reply	other threads:[~2008-08-21  7:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=48AC6DBC.2040508@mcmaster.ca \
    --to=carette@mcmaster.ca \
    --cc=caml-list@inria.fr \
    /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