Type definitions with open unions
Asked Answered
F

1

5

1) I have an open union defined as follows:

type 'a choice = [> `One | `Other ] as 'a

I then try to define a type choice_list:

type choice_list = choice list

which does not work. How does one define types where one or more of the components are open unions?

2) If instead I forgo creating the choice_list type, and just use a choice list, when I try writing an interface/signature statement using a choice list,

val choice_handler : choice list -> int

the compiler complains that type 'a choice = 'a constraint 'a = [> `One | `Other ] is not included in type infection_state. They have different arities.

My question is, how does one write the type declaration of choice list in the interface/signature.

Fortenberry answered 2/11, 2011 at 14:37 Comment(0)
B
9

The compiler is trying to tell you that choice is a parameterized type. At the type level, it has an arity of 1. In other words, you need to supply one type parameter. You have constrained the parameter to be a subtype of [`One|`Other], but other than that it can be any type:

# ([`One; `Third] : 'a choice list);;
- : [> `One | `Other | `Third ] choice list = [`One; `Third]

If you want to define a list of choices, the extra type has to come from somewhere. I.e., it has to be a parameter to the new type:

# type 'a choice_list = 'a choice list;;
type 'a choice_list = 'a choice list constraint 'a = [> `One | `Other ]

(These sorts of constructions get tricky pretty fast, in my experience.)

Biologist answered 2/11, 2011 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.