caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: alex@baretta.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] What about polymorphic union types in functors?
Date: Mon, 09 Feb 2004 10:40:13 +0900	[thread overview]
Message-ID: <20040209104013Y.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <4023E3BD.8040907@baretta.com>

From: Alex Baretta <alex@baretta.com>

> I need the ocaml compiler to typecheck the use of variant tags to ensure 
> that no tag is used in a context where it is not meaningful. The absence 
> of such static typechecking would force me to throw an exception at 
> runtime if a tag is used where it is not meaningful. Such typechecking 
> is possible, and I actually rely heavily on it. What I need is a _union_ 
> morphism between a pair of similar modules and a third module similar to 
> the first two. From an algebraic stadpoint, this operator is well 
> defined. However, ocaml is unable to compile such code because pattern 
> matching on polymorphic variants requires all variants to be known 
> statically. However, I suspect that this limitation is due to the 
> implementation rather than the underlying model. If the compiler knew 
> that F1.t and F2.t were polymorphic variant types, then it could 
> dispatch be executing sequentially the pattern matching code for F1.t 
> defined by the F1 module and the pattern matching code for F2.t defined 
> in the F2 module. This can be done because at the time when F1 and F2 
> are compiled all polymorphic variants are actually known. There might be 
> some corner cases to be worked out, but the general principle ought to work.

You are mostly right for the execution part, but this would also
require extending the type system: currently, when you write
  type t = [t1 | t2]
the type checker just extracts the tag definitions from t1 and t2 and
builds a new variant type. The fact the tags came from t1 and t2 is
completely forgotten.
This means that there is now way to represent [t1 | t2] without
knowing the tags of t1 and t2.

Supposing this refinement is possible, the next problem is that it
would require some form of runtime information passing.
When you put a type definition in a caml functor, no information at
all is passed at runtime. So if a variant type is abstract, there is
no way we can pattern-match on it.
We would need something like stream parsers, but typing specially:
again more typing problems.
By the way, the code for pattern matching t1 is not defined when you
define t1, but only when you pattern-match on it. This way we can
generate optimal code, considering all the cases in the
pattern-matching.

Even if we were to do all that, the performance of the resulting code
would not be as good as the one obtained through code generation.

Is this a serious limiting factor for your programming?
In particular, does it impact expressiveness, or just conciseness?

Cheers,

Jacques Garrigue

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


      reply	other threads:[~2004-02-09  1:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-02-06 15:51 Alex Baretta
2004-02-06 17:43 ` brogoff
2004-02-06 18:58   ` Alex Baretta
2004-02-09  1:40     ` Jacques Garrigue [this message]

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=20040209104013Y.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=alex@baretta.com \
    --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;
as well as URLs for NNTP newsgroup(s).