Domanda

type 'a lazy_node = 
  | Empty
  | Node of 'a * 'a lazy_list
and 'a lazy_list = 'a lazy_node lazy_t

let con x zl = lazy (Node (x,zl))

So there should be two types defined here: 'a lazy_node and 'a lazy_list.

I thought that con's type would be 'a -> 'a lazy_list -> 'a lazy_list

However, actually when I tried it in utop, it gave con such a type:

val con : 'a -> 'a lazy_list -> 'a lazy_node lazy_t = <fun>


Why the return type is not 'a lazy_list? How does OCaml infer in this case?

È stato utile?

Soluzione

In your code lazy_list is not a new type but just an alias for lazy_node lazy_t. And the Ocaml type inferer only consider real types, not aliases. So it infers the real type and doesn't check if there is an alias for this type.

The alias is used for the argument because Node (x,zl) implies that zl has type 'a lazy_list.

You can add type annotation to enforce the use of the alias:

let con x zl: 'a lazy_list = lazy (Node (x,zl))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top