Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-regular recursive type leads to loss of generality #5242

Closed
vicuna opened this issue Mar 27, 2011 · 1 comment
Closed

Non-regular recursive type leads to loss of generality #5242

vicuna opened this issue Mar 27, 2011 · 1 comment
Assignees
Labels

Comments

@vicuna
Copy link

vicuna commented Mar 27, 2011

Original bug ID: 5242
Reporter: SpiceGuid
Assigned to: @garrigue
Status: closed (set by @xavierleroy on 2013-08-31T10:43:54Z)
Resolution: not a bug
Priority: normal
Severity: minor
Version: 3.11.0
Fixed in version: 3.12.0
Category: ~DO NOT USE (was: OCaml general)

Bug description

It seems to me the compiler assumes a recursive type to be regular even when told otherwise. The consequence is restricted generality, even when full generality is explicited.

type ('a, 'b) bijective_map =
| Nil
| Cons of 'a * 'b * ('b, 'a) bijective_map

let rec connected : ('a -> 'b -> bool) -> ('a, 'b) bijective_map -> bool =
fun r -> function
| Nil -> false
| Cons(a,b,l) -> r a b or connected (fun x y -> r y x) l;;
val connected : ('a -> 'a -> bool) -> ('a, 'a) bijective_map -> bool =

Additional information

Untested with 3.12.0

@vicuna
Copy link
Author

vicuna commented Mar 28, 2011

Comment author: @garrigue

There is a misunderstanding here.
The problem is not with the type definition, which does not have to be
regular for datatypes (only type abbreviations are restricted).
The problem is just that your expected type requires polymorphic
recursion, which is known to be undecidable.
Moreover your type annotation is misleading, since in ocaml type variables
appearing in annotations may be freely instantiated by the typechecker.
Since 3.12, polymorphic recursion is allowed, using a new form of type
annotation, which makes polymorphism requirements explicit.
The following code will give you the expected type.

let rec connected : 'a 'b. ('a -> 'b -> bool) -> ('a, 'b) bijective_map -> bool

fun r -> function
| Nil -> false
| Cons(a,b,l) -> r a b or connected (fun x y -> r y x) l;;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants