caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Boris Yakobowski <boris@yakobowski.org>
To: brogoff <brogoff@speakeasy.net>
Cc: caml-list@yquem.inria.fr
Subject: Re: Petty complaint (Was Re: [Caml-list] Type from local module would escape its )scope?
Date: Tue, 4 Jul 2006 23:08:50 +0200	[thread overview]
Message-ID: <20060704210850.GA25504@elrond.dynalias.net> (raw)
In-Reply-To: <Pine.LNX.4.58.0607031009370.549@shell2.speakeasy.net>

On Mon, Jul 03, 2006 at 10:30:38AM -0700, brogoff wrote:
> It makes me wonder, if OCaml is to be a functional language, why
> functions are second class citizens of the language with regards
> to typing? By various encodings you can get this higher rank
> polymorphism, it's been there for years, but we can't write the function
> directly. Is it because we'd have to write it's type?

As Étienne Miret mentionned, what you're basically asking for is the
expressivity of System F. Unfortunately, type inference in that system is
undecidable, and its practicality as a programming langage is more than
doubtful. Below is the map function on lists in an hypothetical F-based
language. Notice that map must be fully annotated, incuding type
abstractions and type applications; this is why the recursive call is map A
B f q instead of map f q in ML.

let rec map : forall A B. (A -> B) -> A list -> B list =
 fun A B (l : list A) -> match l with
   | [] -> []
   | cons h q  -> cons B (f h) (map A B f q)

Numerous attempts at finding intermediary langages which would require less
annotations exist. However, few of them have found their ways in mainstream
programming languages. In fact, the only practical system I'm aware of is an
extension of Haskell which can be found in Ghc ; see
http://www.haskell.org/ghc/docs/latest/html/users_guide/type-extensions.html#universal-quantification
for some examples. Annotations are quite light : you only need to annotate
functions which have an F type (ML types are infered as usual).

The only problem with this approach is that the system used is predicative:
polymorphic instantiation is limited to simple types. Given a function with
type forall A. \sigma -> \sigma', it can only be applied with a type t which
is a monotype. For example, given the function app defined by app f x = f x,
it is not always the case that app f x is typable, even if f x is. Hence,
the system can sometimes lack compositionnality.
Also, it is not possible to write lists of polymorphic functions without
encapsulating the element inside polymorphic variants or records; this is
similar to the current situation in OCaml.


MLF is another attempt at taming the power of system F. The amount of type
annotations is similar to the one required for the GHC extension:
annotations on arguments which are used really polymorphically. On the plus
side, MLF is impredicative, hence more expressive; in particular, none of
the problems I mentionned above occur. Unfortunately, there are some
downsides. Types of MLF are less readable than those of System F, although
some solutions to mitigate this problem exist. Also, it was not obvious that
the algorithms for type inference in MLF would scale to large-scale
programs; this is on the verge of being resolved.

More problematic is the fact that MLF does not yet support recursive types.
Hence, adding it in OCaml would mean it would not be usable with objects, in
which higher-order polymorphism is often useful. More generally, adding a
new type feature to the OCaml compiler requires understanding its
interactions with the myriad of existing extensions (objects, polymorphic
variants, optional arguments, boxed polymorphism...), a non-trivial task.

-- 
Boris


      parent reply	other threads:[~2006-07-04 21:08 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-03 13:19 Type from local module would escape its scope? Bruno De Fraine
2006-07-03 13:23 ` [Caml-list] " Jonathan Roewen
2006-07-03 13:38 ` Jean-Marie Gaillourdet
2006-07-03 13:45 ` Alain Frisch
2006-07-03 13:51 ` Virgile Prevosto
2006-07-03 14:23   ` skaller
2006-07-03 14:50   ` Bruno De Fraine
2006-07-03 15:10     ` Jonathan Roewen
2006-07-03 17:30   ` Petty complaint (Was Re: [Caml-list] Type from local module would escape its )scope? brogoff
2006-07-03 18:50     ` Etienne Miret
2006-07-04 21:08     ` Boris Yakobowski [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=20060704210850.GA25504@elrond.dynalias.net \
    --to=boris@yakobowski.org \
    --cc=brogoff@speakeasy.net \
    --cc=caml-list@yquem.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).