Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml functor taking polymorphic variant type

Tags:

ocaml

Trying to compile

module F (M : sig
  type t = [> `Foo ]
end) = struct
  type t = [ M.t | `Bar ]
end

gets me

Error: A type variable is unbound in this type declaration.
In type [> `Foo ] as 'a the variable 'a is unbound

What am I doing wrong?

like image 613
int3 Avatar asked Oct 18 '14 07:10

int3


People also ask

Does OCaml allow polymorphic functions?

In OCaml, higher-order and polymorphic functions are powerful tools for code reuse. Higher-order functions are those functions that take other functions as arguments or return functions as results, while polymorphic functions are functions that act over values with many different types.

What are polymorphic variants?

Polymorphism, as related to genomics, refers to the presence of two or more variant forms of a specific DNA sequence that can occur among different individuals or populations. The most common type of polymorphism involves variation at a single nucleotide (also called a single-nucleotide polymorphism, or SNP).

What is a functor in OCaml?

What are functors and why do we need them? A functor is a module that is parametrized by another module, just like a function is a value which is parametrized by other values, the arguments. It allows one to parametrize a type by a value, which is not possible directly in OCaml without functors.

What is :: In OCaml?

:: means 2 camel humps, ' means 1 hump! – Nick Craver. Feb 27, 2010 at 12:09. Ok a decent comment: merd.sourceforge.net/pixel/language-study/… I don't use oCaml, but there's a full syntax list you my find useful, even if there's no real context to it.


1 Answers

type t = [> `Foo] is invalid since [> `Foo] is an open type and contains a type variable implicitly. The definition is rejected just as the following type definition is rejected since the RHS has a type variable which is not quantified in LHS:

type t = 'a list

You have to make it closed:

type t = [ `Foo ]

or quantify the type variable:

type 'a t = [> `Foo] as 'a

which is equivalent to

type 'a t = 'a constraint 'a = [> `Foo]
like image 134
camlspotter Avatar answered Sep 21 '22 18:09

camlspotter