Google Groups Home
Help | Sign in
Inferring Recursive Types
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Christopher Diggins  
View profile
 More options Dec 21 2006, 7:59 pm
Newsgroups: comp.lang.functional
From: "Christopher Diggins" <cdigg...@gmail.com>
Date: 21 Dec 2006 16:59:18 -0800
Local: Thurs, Dec 21 2006 7:59 pm
Subject: Inferring Recursive Types
In Combinator theory the combinator SII (a.k.a. M or Mockingbird)
applies a function to itself.

\a.aa

I notice that SML 98 and F# can't infer the type (e.g. let M a = a a ).
What languages can infer the type? What language features are required
to express the type?

Why don't the type systems to those languages simply output something
like:

M : 'a.('a -> 'b) -> 'b

Thanks in advance,
Christopher Diggins
http://www.cdiggins.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark T.B. Carroll  
View profile
 More options Dec 21 2006, 8:45 pm
Newsgroups: comp.lang.functional
From: Mark.Carr...@Aetion.com (Mark T.B. Carroll)
Date: Thu, 21 Dec 2006 20:45:07 -0500
Local: Thurs, Dec 21 2006 8:45 pm
Subject: Re: Inferring Recursive Types

"Christopher Diggins" <cdigg...@gmail.com> writes:
> let M a = a a
> Why don't the type systems to those languages simply output something
> like:

> M : 'a.('a -> 'b) -> 'b

Well, it's not that simple. The function a doesn't take an 'a as first
argument, it takes a function that takes, er, something with the same
signature as the function a, and outputs a b. The something is the
tricky bit - you get a sort of infinitely recursive type if you follow
the reasoning. Whether the languages' difficulty with this is a bug or a
feature, I don't know.

Mark.

--
Functional programming vacancy at http://www.aetion.com/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Benedikt Schmidt  
View profile
 More options Dec 21 2006, 10:12 pm
Newsgroups: comp.lang.functional
From: Benedikt Schmidt <besc...@cloaked.de>
Date: Fri, 22 Dec 2006 04:12:00 +0100
Subject: Re: Inferring Recursive Types

"Christopher Diggins" <cdigg...@gmail.com> writes:
> In Combinator theory the combinator SII (a.k.a. M or Mockingbird)
> applies a function to itself.

> \a.aa

> I notice that SML 98 and F# can't infer the type (e.g. let M a = a a ).
> What languages can infer the type?

Ocaml can with the rectypes option:
% ledit ocaml -rectypes
# fun a -> a a;;
- : ('a -> 'b as 'a) -> 'b = <fun>

> What language features are required to express the type?

Equi-recursive types where e.g. 'a list and 'a ('a list) are
considered equivalent types.
ML/Ocaml supports iso-recursive types where the user can define
type isomorphisms like

type intlist = Cons of int * intlist | Nil

and need explicit conversions between the types (fold/unfold) by
constructing (Cons 4 Nil) and deconstructing
(match l1 with Cons _ l2).

There is a chapter comparing the two in TaPL and some more
details are available in [1].

Benedikt

[1] http://cristal.inria.fr/~fpottier/publis/fpottier-appsem-2005.pdf


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andreas Rossberg  
View profile
(1 user)  More options Dec 22 2006, 5:21 am
Newsgroups: comp.lang.functional
From: Andreas Rossberg <rossb...@ps.uni-sb.de>
Date: Fri, 22 Dec 2006 11:21:58 +0100
Local: Fri, Dec 22 2006 5:21 am
Subject: Re: Inferring Recursive Types

Christopher Diggins wrote:
> In Combinator theory the combinator SII (a.k.a. M or Mockingbird)
> applies a function to itself.

> \a.aa

> I notice that SML 98 and F# can't infer the type (e.g. let M a = a a ).
> What languages can infer the type? What language features are required
> to express the type?

> Why don't the type systems to those languages simply output something
> like:

> M : 'a.('a -> 'b) -> 'b

I guess this is supposed to express the type

   forall b. (forall a. a -> b) -> b

This is indeed (one) possible solution, but with its nested quantifier,
it is a so-called higher-rank polymorphic type. Plain ML does not allow
such types, because type inference for them is undecidable in general.
Some extensions to ML - i.e. Le Botlan/Remy's MLF, or Haskell with GHC
extensions - allow it if you supply sufficient type annotations. But the
meta-theory of such extensions is pretty involved, and more or less an
open research area.

Plain ML only supports prenex polymorphism, where all quantifiers are in
front. This is said to hit a "sweet spot" in type system design, because
it is expressive enough for most practical problems but still allows
complete type inference.

To type self application of an argument x in that system, you have to
solve the type equation

   a = a->a'

To see why, assume x:t for some type t.
Because we apply something to x, we know t=t1->t2.
And because the argument also has type t, we need t1=t, i.e. t1=t1->t2.

The only solution is a recursive type, a fixed point of the above
equation. Usually this is written mu a.a->a'.

Such so-called equi-recursive types are not hard to add to ML, but the
thing is: You Do Not Want Them. Just recently, Andrew Bromage gave a
nice illustration why, on the Haskell-Cafe list:

http://www.haskell.org/pipermail/haskell-cafe/2006-December/020074.html

OCaml used to have recursive type inference in its very first versions,
but it was quickly banned to an explicit switch by popular demand.
Whenever you really need recursive types in ML, you can always express
them by explicit wrapping into an auxiliary algebraic datatype.

- Andreas


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christopher Diggins  
View profile
 More options Dec 22 2006, 5:57 pm
Newsgroups: comp.lang.functional
From: "Christopher Diggins" <cdigg...@gmail.com>
Date: 22 Dec 2006 14:57:22 -0800
Local: Fri, Dec 22 2006 5:57 pm
Subject: Re: Inferring Recursive Types
Thanks a lot for the in depth explanation Andreas, it was very
illuminating and educational!

I have a perhaps naive inquiry given my new understanding: would you
destabilize a type-system if you pulled the qualifier to the front and
expressed the type using an overly general type? e.g. instead of

  forall b. (forall a. a -> b) -> b

we wrote:

  forall b. forall a. (a -> b) -> b

?

Also is the first solution not Rank-2 polymorphism? Pierce says in the
TaPL that rank-2 polymorphism is in fact decidable and rank-3 and
higher polymorphism is undecidable. Is the type inference of rank-2
polymorphism particularly hard I wonder? It seems to me that detecting
cylcical constraints should be relatively easy for a type inference
algorithm.

Anyway, thanks again for your help.

Thanks,
Christopher


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christopher Diggins  
View profile
 More options Dec 22 2006, 5:58 pm
Newsgroups: comp.lang.functional
From: "Christopher Diggins" <cdigg...@gmail.com>
Date: 22 Dec 2006 14:58:29 -0800
Local: Fri, Dec 22 2006 5:58 pm
Subject: Re: Inferring Recursive Types
Thanks for the link and information Benedikt. It is very much
appreciated.

On Dec 21, 7:12 pm, Benedikt Schmidt <besc...@cloaked.de> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rossb...@ps.uni-sb.de  
View profile
 More options Dec 23 2006, 10:01 am
Newsgroups: comp.lang.functional
From: rossb...@ps.uni-sb.de
Date: 23 Dec 2006 07:01:09 -0800
Local: Sat, Dec 23 2006 10:01 am
Subject: Re: Inferring Recursive Types

Christopher Diggins wrote:

> I have a perhaps naive inquiry given my new understanding: would you
> destabilize a type-system if you pulled the qualifier to the front and
> expressed the type using an overly general type? e.g. instead of

>   forall b. (forall a. a -> b) -> b

> we wrote:

>   forall b. forall a. (a -> b) -> b

That is not a more general, it is a completely different type.

You can read forall in the same way as you read an arrow: it stands for
an argument, though a type, not a term argument. Considering that, the
difference between the two types above is analogous to the difference
between function types

  c -> (d -> a -> b) -> b

and

  c -> d -> (a -> b) -> b

You have inverted the roles of the function and its caller regarding
argument d, i.e. who is allowed to chose it. Same for type a above.

> Also is the first solution not Rank-2 polymorphism? Pierce says in the
> TaPL that rank-2 polymorphism is in fact decidable and rank-3 and
> higher polymorphism is undecidable. Is the type inference of rank-2
> polymorphism particularly hard I wonder?

Yes, your example is rank-2 (impredicative) polymorphism. Rank-2 is
decidable, but frankly I have no idea how the algorithm looks like.

- Andreas


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google