Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type definitions with open unions

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.

like image 214
ZJanes Avatar asked Nov 02 '11 14:11

ZJanes


1 Answers

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.)

like image 72
Jeffrey Scofield Avatar answered Dec 05 '22 08:12

Jeffrey Scofield