4
type ide = string;;
type exp = http://pastebin.com/EhD9QdYj;;
let emptyEnv = fun x -> raise EmptyEnvException;;
let emptyFunEnv = fun x -> raise EmptyEnvException;;
let raddoppia = Function("mul2", "x", Mul(Ide "x", Int 2));;
(**)let funenv0 = funDeclr raddoppia emptyEnv emptyFunEnv;;

This code, compiled with ocamlc, returns this error:

File "progetto.ml", line (**), characters 14-53:
Error: The type of this expression, ide -> ide * exp * ('_a -> 'b),
contains type variables that cannot be generalized

What is the problem?

alessandro308
  • 1,912
  • 2
  • 15
  • 27
  • 1
    check this out : http://caml.inria.fr/pub/old_caml_site/FAQ/FAQ_EXPERT-eng.html#variables_de_types_faibles – didierc Aug 28 '15 at 14:10

1 Answers1

8

First of all you should follow the link, provided by @didierc and read about weak type variables, so that you understand why they exist and when they come into play.

Usually, there is nothing bad in weak type variables, especially, when you're scripting. You can strengthen them by using eta-expansion (i.e., by enumerating all arguments of a partially applied function, e.g., substituting List.hd with fun x -> List.hd x).

Weak variables are not allowed everywhere, in particular, they cannot escape a module, i.e., they can't occur in a module signature. So that a value, that has a weak type can be only accessed from inside the module where it is defined. That lexically guarantees, that a variable will have only one type, that will be assigned as soon as it is accessed.

When you write an ml file, you create a part of a compilation unit. The compilation unit is composed from the implementation (ml file) and interface (mli file). If you omit the latter, then compiler will infer the interface automatically, exporting all fields of a module. In that case it will also export weak variables.

With all that said, you can just create an empty progetto.mli file, that will close the Progetto module, and no weak variables will leak it. So that the compiler will be happy, as nobody can break the type system.

ivg
  • 34,431
  • 2
  • 35
  • 63