Version française
Home     About     Download     Resources     Contact us    
Browse thread
Type from local module would escape its scope?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Boris Yakobowski <boris@y...>
Subject: Re: Petty complaint (Was Re: [Caml-list] Type from local module would escape its )scope?
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